-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtask_2.py
More file actions
243 lines (206 loc) · 6.32 KB
/
task_2.py
File metadata and controls
243 lines (206 loc) · 6.32 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
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import torch
import argparse
import torch.utils.model_zoo as model_zoo
from torch.nn.parameter import Parameter
import numpy as np
from datetime import datetime
import pickle as pkl
# imports
from wsddn import WSDDN
from voc_dataset import *
import wandb
from utils import nms, tensor_to_PIL
from PIL import Image, ImageDraw
# hyper-parameters
# ------------
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
parser.add_argument(
'--lr',
default=0.0001,
type=float,
description='Learning rate'
)
parser.add_argument(
'--lr-decay-steps',
default=150000,
type=int,
description='Interval at which the lr is decayed'
)
parser.add_argument(
'--lr-decay',
default=0.1,
type=float,
description='Decay rate of lr'
)
parser.add_argument(
'--momentum',
default=0.9,
type=float,
description='Momentum of optimizer'
)
parser.add_argument(
'--weight-decay',
default=0.0005,
type=float,
description='Weight decay'
)
parser.add_argument(
'--epochs',
default=5,
type=int,
description='Number of epochs'
)
parser.add_argument(
'--val-interval',
default=5000,
type=int,
description='Interval at which to perform validation'
)
parser.add_argument(
'--disp-interval',
default=10,
type=int,
description='Interval at which to perform visualization'
)
parser.add_argument(
'--use-wandb',
default=False,
type=bool,
description='Flag to enable visualization'
)
# ------------
# Set random seed
rand_seed = 1024
if rand_seed is not None:
np.random.seed(rand_seed)
torch.manual_seed(rand_seed)
# Set output directory
output_dir = "./"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
def calculate_map():
"""
Calculate the mAP for classification.
"""
# TODO (Q2.3): Calculate mAP on test set.
# Feel free to write necessary function parameters.
pass
def test_model(model, val_loader=None, thresh=0.05):
"""
Tests the networks and visualizes the detections
:param thresh: Confidence threshold
"""
with torch.no_grad():
for iter, data in enumerate(val_loader):
# one batch = data for one image
image = data['image']
target = data['label']
wgt = data['wgt']
rois = data['rois']
gt_boxes = data['gt_boxes']
gt_class_list = data['gt_classes']
# TODO (Q2.3): perform forward pass, compute cls_probs
# TODO (Q2.3): Iterate over each class (follow comments)
for class_num in range(20):
# get valid rois and cls_scores based on thresh
# use NMS to get boxes and scores
pass
# TODO (Q2.3): visualize bounding box predictions when required
calculate_map()
def train_model(model, train_loader=None, val_loader=None, optimizer=None, args=None):
"""
Trains the network, runs evaluation and visualizes the detections
"""
# Initialize training variables
train_loss = 0
step_cnt = 0
for epoch in range(args.epochs):
for iter, data in enumerate(train_loader):
# TODO (Q2.2): get one batch and perform forward pass
# one batch = data for one image
image = data['image']
target = data['label']
wgt = data['wgt']
rois = data['rois']
gt_boxes = data['gt_boxes']
gt_class_list = data['gt_classes']
# TODO (Q2.2): perform forward pass
# take care that proposal values should be in pixels
# Convert inputs to cuda if training on GPU
# backward pass and update
loss = model.loss
train_loss += loss.item()
step_cnt += 1
optimizer.zero_grad()
loss.backward()
optimizer.step()
# TODO (Q2.2): evaluate the model every N iterations (N defined in handout)
# Add wandb logging wherever necessary
if iter % args.val_interval == 0 and iter != 0:
model.eval()
ap = test_model(model, val_loader)
print("AP ", ap)
model.train()
# TODO (Q2.4): Perform all visualizations here
# The intervals for different things are defined in the handout
# TODO (Q2.4): Plot class-wise APs
def main():
"""
Creates dataloaders, network, and calls the trainer
"""
args = parser.parse_args()
# TODO (Q2.2): Load datasets and create dataloaders
# Initialize wandb logger
train_dataset = None
val_dataset = None
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=1, # batchsize is one for this implementation
shuffle=True,
num_workers=4,
pin_memory=True,
sampler=None,
drop_last=True)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=1,
shuffle=False,
num_workers=4,
pin_memory=True,
drop_last=True)
# Create network and initialize
net = WSDDN(classes=train_dataset.CLASS_NAMES)
print(net)
if os.path.exists('pretrained_alexnet.pkl'):
pret_net = pkl.load(open('pretrained_alexnet.pkl', 'rb'))
else:
pret_net = model_zoo.load_url(
'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth')
pkl.dump(pret_net,
open('pretrained_alexnet.pkl', 'wb'), pkl.HIGHEST_PROTOCOL)
own_state = net.state_dict()
for name, param in pret_net.items():
print(name)
if name not in own_state:
continue
if isinstance(param, Parameter):
param = param.data
try:
own_state[name].copy_(param)
print('Copied {}'.format(name))
except:
print('Did not find {}'.format(name))
continue
# Move model to GPU and set train mode
net.load_state_dict(own_state)
net.cuda()
net.train()
# TODO (Q2.2): Freeze AlexNet layers since we are loading a pretrained model
# TODO (Q2.2): Create optimizer only for network parameters that are trainable
optimizer = None
# Training
train_model(net, train_loader, optimizer, args)