-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_script.py
96 lines (62 loc) · 3.09 KB
/
train_script.py
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
85
86
87
88
89
90
91
92
93
94
95
96
'''Script to train the Attention-CNN model.
'''
from tqdm import tqdm
import torch
from torch.utils.data import DataLoader
from torch.optim import Adam
from torchsummary import summary
from torchvision import transforms
from runtime_args import args
from attention_cnn import AttentionCNN
from load_dataset import LoadDataset
device = torch.device("cuda:0" if torch.cuda.is_available() and args.device == 'gpu' else 'cpu')
train_dataset = LoadDataset(dataset_folder_path=args.data_folder, image_size=args.img_size, image_depth=args.img_depth, train=True,
transform=transforms.ToTensor())
test_dataset = LoadDataset(dataset_folder_path=args.data_folder,image_size=args.img_size, image_depth=args.img_depth, train=False,
transform=transforms.ToTensor())
train_generator = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers)
test_generator = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers)
model = AttentionCNN(image_size=args.img_size, image_depth=args.img_depth, num_classes=args.num_classes, drop_prob=args.dropout_rate, device=device)
optimizer = Adam(model.parameters(), lr=args.learning_rate)
criterion = torch.nn.CrossEntropyLoss()
model = model.to(device)
summary(model, (args.img_depth, args.img_size, args.img_size))
best_accuracy = 0
for epoch_idx in range(args.epoch):
model.train()
epoch_loss = []
epoch_accuracy = []
i = 0
for i, sample in tqdm(enumerate(train_generator)):
batch_x, batch_y = sample['image'].to(device), sample['label'].to(device)
optimizer.zero_grad()
_,_,net_output = model(batch_x)
total_loss = criterion(input=net_output, target=batch_y)
total_loss.backward()
optimizer.step()
batch_acc = model.calculate_accuracy(predicted=net_output, target=batch_y)
epoch_loss.append(total_loss.item())
epoch_accuracy.append(batch_acc)
curr_accuracy = sum(epoch_accuracy)/(i+1)
curr_loss = sum(epoch_loss)/(i+1)
print(f"Epoch {epoch_idx}")
print(f"Training Loss : {curr_loss}, Training accuracy : {curr_accuracy}")
model.eval()
epoch_loss = []
epoch_accuracy = []
i = 0
for i, sample in tqdm(enumerate(test_generator)):
batch_x, batch_y = sample['image'].to(device), sample['label'].to(device)
_,_,net_output = model(batch_x)
total_loss = criterion(input=net_output, target=batch_y)
batch_acc = model.calculate_accuracy(predicted=net_output, target=batch_y)
epoch_loss.append(total_loss.item())
epoch_accuracy.append(batch_acc)
curr_accuracy = sum(epoch_accuracy)/(i+1)
curr_loss = sum(epoch_loss)/(i+1)
print(f"Testing Loss : {curr_loss}, Testing accuracy : {curr_accuracy}")
if curr_accuracy > best_accuracy:
torch.save(model.state_dict(), args.model_save_path.rstrip('/')+'/attention_cnn.pth')
best_accuracy = curr_accuracy
print("Model is saved!")
print('------------------------------------------------------------------------------')