-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreTrainCNN.py
executable file
·246 lines (204 loc) · 9.17 KB
/
PreTrainCNN.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
Train and Test CNN
Store model in disk
Pre-trained models are then used for GAN evaluation (i.e., InceptionV3) or DRA in feature space
"""
wd = '/home/xin/OneDrive/Working_directory/DDRE_Sampling_GANs/CIFAR10'
import argparse
import shutil
import os
os.chdir(wd)
import timeit
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
import torch.nn as nn
import torch.backends.cudnn as cudnn
import random
import matplotlib.pyplot as plt
import matplotlib as mpl
from torch import autograd
from torchvision.utils import save_image
import csv
from models import *
from tqdm import tqdm
import gc
#############################
# Settings
#############################
parser = argparse.ArgumentParser(description='Pre-train CNNs')
parser.add_argument('--CNN', type=str, default='ResNet34',
help='CNN for training (default: "ResNet34"); Candidates: VGGs(11,13,16,19), ResNet(18,34,50,101), InceptionV3')
parser.add_argument('--isometric_map', action='store_true', default=False,
help='isometric mapping? True for GAN evaluation; False for DRA in feature space')
parser.add_argument('--epochs', type=int, default=200, metavar='N',
help='number of epochs to train CNNs (default: 200)')
parser.add_argument('--batch_size_train', type=int, default=256, metavar='N',
help='input batch size for training')
parser.add_argument('--batch_size_test', type=int, default=256, metavar='N',
help='input batch size for testing')
parser.add_argument('--base_lr', type=float, default=0.1,
help='learning rate, default=0.1')
parser.add_argument('--weight_dacay', type=float, default=1e-4,
help='Weigth decay, default=1e-4')
parser.add_argument('--seed', type=int, default=2019, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--transform', action='store_true', default=False,
help='flip or crop images for CNN training')
parser.add_argument('--num_classes', type=int, default=10, metavar='N',
help='number of classes')
args = parser.parse_args()
# cuda
device = torch.device("cuda")
ngpu = torch.cuda.device_count() # number of gpus
args.base_lr = args.base_lr * ngpu
args.batch_size_train = args.batch_size_train*ngpu
args.batch_size_test = args.batch_size_test*ngpu
# random seed
random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = True
# directories for checkpoint, images and log files
save_models_folder = wd + '/Output/saved_models/'
os.makedirs(save_models_folder,exist_ok=True)
save_logs_folder = wd + '/Output/saved_logs/'
os.makedirs(save_logs_folder,exist_ok=True)
###########################################################################################################
# Necessary functions
###########################################################################################################
#initialize CNNs
def net_initialization(Pretrained_CNN_Name, isometric_map = False, num_classes=10, ngpu = 1):
if Pretrained_CNN_Name == "ResNet18":
net = ResNet18(isometric_map = isometric_map, num_classes=num_classes, ngpu = ngpu)
elif Pretrained_CNN_Name == "ResNet34":
net = ResNet34(isometric_map = isometric_map, num_classes=num_classes, ngpu = ngpu)
elif Pretrained_CNN_Name == "ResNet50":
net = ResNet50(isometric_map = isometric_map, num_classes=num_classes, ngpu = ngpu)
elif Pretrained_CNN_Name == "ResNet101":
net = ResNet101(isometric_map = isometric_map, num_classes=num_classes, ngpu = ngpu)
# elif Pretrained_CNN_Name == "VGG11":
# net = VGG("VGG11", isometric_map = isometric_map)
# elif Pretrained_CNN_Name == "VGG13":
# net = VGG("VGG13", isometric_map = isometric_map)
# elif Pretrained_CNN_Name == "VGG16":
# net = VGG("VGG16", isometric_map = isometric_map)
# elif Pretrained_CNN_Name == "VGG19":
# net = VGG("VGG19", isometric_map = isometric_map)
elif Pretrained_CNN_Name == "InceptionV3":
net = Inception3(num_classes=num_classes, aux_logits=True, transform_input=False)
if isometric_map:
net_name = 'PreCNNForDRE_' + Pretrained_CNN_Name #get the net's name
else:
net_name = 'PreCNNForEvalGANs_' + Pretrained_CNN_Name #get the net's name
if Pretrained_CNN_Name == "InceptionV3" and ngpu>1:
net = nn.DataParallel(net).cuda()
else:
net = net.to(device)
return net, net_name
#adjust CNN learning rate
def adjust_learning_rate(optimizer, epoch, BASE_LR_CNN):
"""decrease the learning rate at 100 and 150 epoch"""
lr = BASE_LR_CNN
if epoch <= 9 and lr > 0.1:
# warm-up training for large minibatch
lr = 0.1 + (BASE_LR_CNN - 0.1) * epoch / 10.
if epoch >= 100:
lr /= 10
if epoch >= 150:
lr /= 10
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def train_CNN():
for epoch in range(args.epochs):
net.train()
train_loss = 0
adjust_learning_rate(optimizer, epoch, args.base_lr)
for batch_idx, (batch_train_images, batch_train_labels) in enumerate(trainloader):
if args.CNN == "InceptionV3":
batch_train_images = nn.functional.interpolate(batch_train_images, size = (299,299), scale_factor=None, mode='bilinear', align_corners=False)
batch_train_images = batch_train_images.type(torch.float).cuda()
batch_train_labels = batch_train_labels.type(torch.long).cuda()
#Forward pass
outputs,_ = net(batch_train_images)
loss = criterion(outputs, batch_train_labels)
#backward pass
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.cpu().item()
#end for batch_idx
test_acc = test_CNN(False)
print('CNN: [epoch %d/%d] train_loss:%.3f, test_acc:%.3f' % (epoch+1, args.epochs, train_loss/(batch_idx+1), test_acc))
#end for epoch
return net, optimizer
def test_CNN(verbose=True):
net.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
correct = 0
total = 0
for images, labels in testloader:
if args.CNN == "InceptionV3":
images = nn.functional.interpolate(images, size = (299,299), scale_factor=None, mode='bilinear', align_corners=False)
images = images.type(torch.float).cuda()
labels = labels.type(torch.long).cuda()
outputs,_ = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
if verbose:
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100.0 * correct / total))
return 100.0 * correct / total
###########################################################################################################
# Training and Testing
###########################################################################################################
# data loader
means = (0.5, 0.5, 0.5) #Inceptionv3 acc 95.36?? 83.17
stds = (0.5, 0.5, 0.5)
if args.transform:
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
# transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize(means, stds),
])
else:
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(means, stds),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(means, stds),
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size_train, shuffle=True, num_workers=8)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=args.batch_size_test, shuffle=False, num_workers=8)
# model initialization
net, net_name = net_initialization(args.CNN, isometric_map = args.isometric_map, num_classes=args.num_classes, ngpu = ngpu)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr = args.base_lr, momentum= 0.9, weight_decay=args.weight_dacay)
filename_ckpt = save_models_folder + '/ckpt_' + net_name + '_epoch_' + str(args.epochs) + '_SEED_' + str(args.seed) + '_Transformation_' + str(args.transform)
# training
if not os.path.isfile(filename_ckpt):
# TRAIN CNN
print("\n Begin training CNN: ")
start = timeit.default_timer()
net, optimizer = train_CNN()
stop = timeit.default_timer()
print("Time elapses: {}s".format(stop - start))
# save model
torch.save({
'net_state_dict': net.state_dict(),
}, filename_ckpt)
else:
print("\n Ckpt already exists")
print("\n Loading...")
torch.cuda.empty_cache()#release GPU mem which is not references
#testing
checkpoint = torch.load(filename_ckpt)
net.load_state_dict(checkpoint['net_state_dict'])
_ = test_CNN(True)
torch.cuda.empty_cache()