-
Notifications
You must be signed in to change notification settings - Fork 4
/
boom_transfer.py
170 lines (128 loc) · 5.43 KB
/
boom_transfer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from __future__ import print_function, division
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
from torchvision.datasets import ImageFolder
import matplotlib.pyplot as plt
import time
import copy
import os
from shutil import copyfile
import argparse
from tqdm import tqdm, trange
data_transforms = {
'train': transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
def train_model(model, loss_function, optimizer, num_epochs, save_path, file_name):
start_time = time.time()
best_accuracy = 0.0
for epoch in trange(num_epochs, desc="Epoch"):
# training phase
model.train(True)
running_loss = 0.0
running_accuracy = 0
for idx, data in enumerate(tqdm(dataset_loaders['train'], desc="Iter")):
inputs, labels = data
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
optimizer.zero_grad()
output = model(inputs)
_, predictions = torch.max(output.data, 1)
loss = loss_function(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.data[0]
this_batch_num_acc = torch.sum(predictions == labels.data)
running_accuracy += this_batch_num_acc
if idx % 200 == 0:
tqdm.write("[{}, {}], {}".format(epoch, idx, this_batch_num_acc))
epoch_loss = running_loss / dataset_sizes['train']
epoch_accuracy = running_accuracy / dataset_sizes['train']
f = open(save_path+file_name+'_acc.txt', 'a+')
f.write(str(epoch_accuracy) + "\n")
f.close()
f = open(save_path+file_name+'_loss.txt', 'a+')
f.write(str(epoch_loss) + "\n")
f.close()
print('Training Loss: {:.4f} Acc: {:.4f}'.format(epoch_loss, epoch_accuracy))
# validation phase
model.train(False)
running_loss = 0.0
running_accuracy = 0
for idx, data in enumerate(tqdm(dataset_loaders['val'], desc="Val")):
inputs, labels = data
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
optimizer.zero_grad()
output = model(inputs)
_, predictions = torch.max(output.data, 1)
loss = loss_function(output, labels)
running_loss += loss.data[0]
this_batch_num_acc = torch.sum(predictions == labels.data)
running_accuracy += this_batch_num_acc
epoch_loss = running_loss / dataset_sizes['val']
epoch_accuracy = running_accuracy / dataset_sizes['val']
f = open(save_path + file_name + '_val_loss.txt', 'a+')
f.write(str(epoch_loss) + "\n")
f.close()
f = open(save_path + file_name + '_val_acc.txt', 'a+')
f.write(str(epoch_accuracy) + "\n")
f.close()
print('Validation Loss: {:.4f} Acc: {:.4f}'.format(epoch_loss, epoch_accuracy))
if epoch_accuracy > best_accuracy:
best_accuracy = epoch_accuracy
torch.save(model.state_dict(), save_path + file_name + 'model')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, default="resnet34")
parser.add_argument("--opt", type=str, default="sgd")
parser.add_argument("--epochs", type=int, default=5)
args = parser.parse_args()
data_dir = "./new_data"
torch.cuda.manual_seed(100)
datasets = {x: ImageFolder(os.path.join(data_dir, x), data_transforms[x])
for x in ['train', 'val']}
dataset_loaders = {x: torch.utils.data.DataLoader(datasets[x], batch_size=80,
shuffle=True, num_workers=4) for x in ['train', 'val']}
dataset_sizes = {x: len(datasets[x]) for x in ['train', 'val']}
model = models.resnet34(pretrained=True)
for param in model.parameters():
param.requires_grad = False
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 2)
criterion = nn.CrossEntropyLoss()
if args.opt is "sgd":
opt = optim.SGD(model.fc.parameters(), lr=0.001, momentum=0.9)
elif args.opt is "adam":
opt = optim.Adam(model.fc.parameters(), lr=3e-4)
file_name = os.path.basename(__file__)
start_time = time.ctime().replace(" ", "")
save_path = './models/' + start_time + '/'
os.makedirs(save_path)
full_file_path = os.path.realpath(__file__)
copyfile(full_file_path, save_path + '/' + file_name)
model = model.cuda()
tqdm.write("Training - Base Layers Frozen:")
train_model(model=model, loss_function=criterion, optimizer=opt,
num_epochs=1, save_path=save_path, file_name=file_name)
for param in model.parameters():
param.requires_grad = True
if args.opt is "sgd":
opt = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
elif args.opt is "adam":
opt = optim.Adam(model.parameters(), lr=3e-4)
tqdm.write("Training:")
train_model(model=model, loss_function=criterion,
optimizer=opt, num_epochs=args.epochs,
save_path=save_path, file_name=file_name)