-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
235 lines (201 loc) · 8.86 KB
/
main.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
import argparse
import os
import json
import numpy as np
from tqdm import tqdm
import torch
from torch.optim import SGD, Adam
import torch.utils.data
import torchvision.transforms as T
import torchvision.datasets as datasets
from torch.utils import data
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torchnet as tnt
from torchnet.engine import Engine
from utils import cast, data_parallel, print_tensor_dict
from torch.backends import cudnn
from resnet import resnet
cudnn.benchmark = True
parser = argparse.ArgumentParser(description='Wide Residual Networks')
# Model options
parser.add_argument('--model', default='wrn', type=str)
parser.add_argument('--depth', default=28, type=int)
parser.add_argument('--width', default=2, type=float)
parser.add_argument('--dataset', default='CIFAR10', type=str)
parser.add_argument('--dataroot', default='.', type=str)
parser.add_argument('--dtype', default='float', type=str)
parser.add_argument('--groups', default=1, type=int)
parser.add_argument('--nthread', default=4, type=int)
parser.add_argument('--seed', default=1, type=int)
# Training options
parser.add_argument('--batch_size', default=128, type=int)
parser.add_argument('--nl', default=4000, type=int)
parser.add_argument('--nc', default=10, type=int)
parser.add_argument('--lr', default=0.1, type=float)
parser.add_argument('--epochs', default=200, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--weight_decay', default=0.0005, type=float)
parser.add_argument('--epoch_step', default='[60,120,160]', type=str,
help='json list with epochs to drop lr on')
parser.add_argument('--lr_decay_ratio', default=0.2, type=float)
parser.add_argument('--resume', default='', type=str)
parser.add_argument('--opt', default='sgd', type=str)
parser.add_argument('--note', default='', type=str)
# Device options
parser.add_argument('--cuda', action='store_true')
parser.add_argument('--save', default='', type=str,
help='save parameters and logs in this folder')
parser.add_argument('--ngpu', default=1, type=int,
help='number of GPUs to use for training')
parser.add_argument('--gpu_id', default='0', type=str,
help='id(s) for CUDA_VISIBLE_DEVICES')
def create_dataset(opt, train):
# tensor_x = torch.Tensor(my_x) # transform to torch tensor
# tensor_y = torch.Tensor(my_y)
#
# my_dataset = TensorDataset(tensor_x, tensor_y) # create your datset
# my_dataloader = DataLoader(my_dataset) # create your dataloader
transform = T.Compose([
T.ToTensor(),
T.Normalize(np.array([125.3, 123.0, 113.9]) / 255.0,
np.array([63.0, 62.1, 66.7]) / 255.0),
])
if train:
transform = T.Compose([
T.Pad(4, padding_mode='reflect'),
T.RandomHorizontalFlip(),
T.RandomCrop(32),
transform
])
if opt.dataset == 'SVHN':
# transform = None
dataset = getattr(datasets, opt.dataset)(opt.dataroot, split='train', download=True, transform=transform)
elif opt.dataset == 'plant':
import data_loader1 as dl
dso = dl.read_data_sets('cifar10', 4000)
from custom_utils import create_my_datasets
return create_my_datasets(dso.train.labeled_ds.images,dso.train.labeled_ds.labels)
else:
dataset = getattr(datasets, opt.dataset)(opt.dataroot, train=train, download=True, transform=transform)
samples_per_class = opt.nl // opt.nc
if opt.nl != -1:
labels = torch.tensor([y for x, y in dataset])
indices = torch.arange(len(labels))
indices = torch.cat([indices[labels == x][:samples_per_class] for x in torch.unique(labels)])
dataset = data.Subset(dataset, indices)
return dataset
else:
if opt.dataset == 'SVHN':
# transform = None
return getattr(datasets, opt.dataset)(opt.dataroot, split='train', download=True, transform=transform)
elif opt.dataset == 'plant':
import data_loader1 as dl
dso = dl.read_data_sets('cifar10', 4000,scale=False)
from custom_utils import create_my_datasets
print('here')
return create_my_datasets(dso.test.images, dso.test.labels, is_test=True)
return getattr(datasets, opt.dataset)(opt.dataroot, train=train, download=True, transform=transform)
def main():
opt = parser.parse_args()
print('parsed options:', vars(opt))
epoch_step = json.loads(opt.epoch_step)
num_classes = opt.nc # 10 if opt.dataset == 'CIFAR10' else 100
torch.manual_seed(opt.seed)
os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu_id
def create_iterator(mode):
return DataLoader(create_dataset(opt, mode), opt.batch_size, shuffle=mode,
num_workers=opt.nthread, pin_memory=torch.cuda.is_available())
# train_loader = create_iterator(True)
# test_loader = create_iterator(False)
from custom_utils import create_data_loaders
train_loader, test_loader = create_data_loaders(opt)
f, params = resnet(opt.depth, opt.width, num_classes)
def create_optimizer(opt, lr):
print('creating optimizer with lr = ', lr)
if opt.opt.lower() == 'adam':
return Adam([v for v in params.values() if v.requires_grad], lr)
return SGD([v for v in params.values() if v.requires_grad], lr, momentum=0.9, weight_decay=opt.weight_decay)
optimizer = create_optimizer(opt, opt.lr)
epoch = 0
if opt.resume != '':
state_dict = torch.load(opt.resume)
epoch = state_dict['epoch']
params_tensors = state_dict['params']
for k, v in params.items():
v.data.copy_(params_tensors[k])
optimizer.load_state_dict(state_dict['optimizer'])
print('\nParameters:')
print_tensor_dict(params)
n_parameters = sum(p.numel() for p in params.values() if p.requires_grad)
print('\nTotal number of parameters:', n_parameters)
meter_loss = tnt.meter.AverageValueMeter()
classacc = tnt.meter.ClassErrorMeter(accuracy=True)
timer_train = tnt.meter.TimeMeter('s')
timer_test = tnt.meter.TimeMeter('s')
# if not os.path.exists(opt.save):
# os.mkdir(opt.save)
def h(sample):
inputs = cast(sample[0], opt.dtype)
targets = cast(sample[1], 'long')
y = data_parallel(f, inputs, params, sample[2], list(range(opt.ngpu))).float()
return F.cross_entropy(y, targets), y
def log(t, state):
path = './logs/' + opt.dataset + '/' +opt.model + '-' + str(opt.depth) + '-' + str(int(opt.width)) + '/' #+ opt.save
torch.save(dict(params=params, epoch=t['epoch'], optimizer=state['optimizer'].state_dict()),
os.path.join(path, 'model.pt7'))
z = {**vars(opt), **t}
with open(os.path.join(path, 'log.txt'), 'a') as flog:
flog.write('json_stats: ' + json.dumps(z) + '\n')
# print(z)
def on_sample(state):
state['sample'].append(state['train'])
def on_forward(state):
loss = float(state['loss'])
classacc.add(state['output'].data, state['sample'][1])
meter_loss.add(loss)
if state['train']:
state['iterator'].set_postfix(loss=loss)
def on_start(state):
state['epoch'] = epoch
def on_start_epoch(state):
classacc.reset()
meter_loss.reset()
timer_train.reset()
state['iterator'] = tqdm(train_loader, dynamic_ncols=True)
epoch = state['epoch'] + 1
if epoch in epoch_step:
lr = state['optimizer'].param_groups[0]['lr']
state['optimizer'] = create_optimizer(opt, lr * opt.lr_decay_ratio)
def on_end_epoch(state):
train_loss = meter_loss.value()
train_acc = classacc.value()
train_time = timer_train.value()
meter_loss.reset()
classacc.reset()
timer_test.reset()
with torch.no_grad():
engine.test(h, test_loader)
test_acc = classacc.value()[0]
print(log({
"train_loss": train_loss[0],
"train_acc": train_acc[0],
"test_loss": meter_loss.value()[0],
"test_acc": test_acc,
"epoch": state['epoch'],
"num_classes": num_classes,
"n_parameters": n_parameters,
"train_time": train_time,
"test_time": timer_test.value(),
}, state))
print('==> id: %s (%d/%d), test_acc: \33[91m%.2f\033[0m' %
(opt.save, state['epoch'], opt.epochs, test_acc))
engine = Engine()
engine.hooks['on_sample'] = on_sample
engine.hooks['on_forward'] = on_forward
engine.hooks['on_start_epoch'] = on_start_epoch
engine.hooks['on_end_epoch'] = on_end_epoch
engine.hooks['on_start'] = on_start
engine.train(h, train_loader, opt.epochs, optimizer)
if __name__ == '__main__':
main()