-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
84 lines (60 loc) · 2.5 KB
/
train.py
File metadata and controls
84 lines (60 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import torch
import numpy as np
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader, WeightedRandomSampler
from torch.optim import AdamW
import os, random, argparse
# custom
from models.CNNClassifier import Model
from models.PANClassifier import PANClassifier
from Trainer import Trainer
from datahandlers.MakeDataset import MakeDataset
from Evaluater import Evaluater
from datahandlers.CNNDataLoader import CNNDataLoader
# reproducibility
def initialization(seed = 0):
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # multi-GPU
cudnn.deterministic = True
cudnn.benchmark = False
os.environ['PYTHONHASHSEED'] = str(seed)
parser = argparse.ArgumentParser(description='Settings for PrefixModel training')
parser.add_argument('-n', '--exp_name', type=str, default='no_name', help='name of the experiment')
parser.add_argument('-device', '--device', type = int, default=0, help='device number to use (if available)')
parser.add_argument('-train_bs', '--train_bs', type = int, default=20, help='train batch size')
parser.add_argument('-test_bs', '--test_bs', type = int, default=20, help='test batch size')
args = parser.parse_args()
total_epochs = 60
LR = 5e-5
#------------------Settings--------------------
#reproducibility
random_seed=42
initialization(seed=random_seed)
print("random_seed :", random_seed)
total_epochs = 55
LR = 7e-5
TEST_BATCH_SIZE = 30
TRAIN_BATCH_SIZE = 30
base_dir = '/data/valerii/heartbeats_classification/data/physionet'
train_dataloader = CNNDataLoader('train', TRAIN_BATCH_SIZE, base_dir, raw_audio=True)
test_dataloader = CNNDataLoader('test', TEST_BATCH_SIZE, base_dir, raw_audio=True,
weighted_sampler=False)
#============Experiment================
torch.cuda.empty_cache()
MODEL_NAME = args.exp_name
USE_CUDA = torch.cuda.is_available()
device = torch.device('cuda:'+ str(args.device) if USE_CUDA else 'cpu')
print("Using device: ", device)
model = PANClassifier(num_classes=2, device=args.device)
# params = torch.load('',\
# map_location='cuda:'+str(args.device))
# model.load_state_dict(params)
# print(args.device, type(args.device))
optimizer = AdamW(model.parameters(), lr=LR, weight_decay = 0.01)
trainer = Trainer(model, MODEL_NAME, train_dataloader, test_dataloader, optimizer, args.device, is_distributed=False)
trainer.train(total_epochs)
torch.cuda.empty_cache()
#============Experiment================