99from layers .modules import MultiBoxLoss
1010from layers .functions .prior_box import PriorBox
1111import time
12+ import datetime
1213import math
1314from models .faceboxes import FaceBoxes
1415
3031if not os .path .exists (args .save_folder ):
3132 os .mkdir (args .save_folder )
3233
33- img_dim = 1024
34- rgb_means = (104 , 117 , 123 ) #bgr order
34+ img_dim = 1024 # only 1024 is supported
35+ rgb_mean = (104 , 117 , 123 ) # bgr order
3536num_classes = 2
37+ num_gpu = args .ngpu
38+ num_workers = args .num_workers
3639batch_size = args .batch_size
40+ momentum = args .momentum
3741weight_decay = args .weight_decay
42+ initial_lr = args .lr
3843gamma = args .gamma
39- momentum = args .momentum
44+ max_epoch = args .max_epoch
45+ training_dataset = args .training_dataset
46+ save_folder = args .save_folder
4047gpu_train = cfg ['gpu_train' ]
4148
4249net = FaceBoxes ('train' , img_dim , num_classes )
5865 new_state_dict [name ] = v
5966 net .load_state_dict (new_state_dict )
6067
61- if args . ngpu > 1 and gpu_train :
62- net = torch .nn .DataParallel (net , device_ids = list (range (args . ngpu )))
68+ if num_gpu > 1 and gpu_train :
69+ net = torch .nn .DataParallel (net , device_ids = list (range (num_gpu )))
6370
6471device = torch .device ('cuda:0' if gpu_train else 'cpu' )
6572cudnn .benchmark = True
6673net = net .to (device )
6774
68- optimizer = optim .SGD (net .parameters (), lr = args . lr , momentum = args . momentum , weight_decay = args . weight_decay )
75+ optimizer = optim .SGD (net .parameters (), lr = initial_lr , momentum = momentum , weight_decay = weight_decay )
6976criterion = MultiBoxLoss (num_classes , 0.35 , True , 0 , True , 7 , 0.35 , False )
7077
71- priorbox = PriorBox (cfg )
78+ priorbox = PriorBox (cfg , image_size = ( img_dim , img_dim ) )
7279with torch .no_grad ():
7380 priors = priorbox .forward ()
7481 priors = priors .to (device )
@@ -79,10 +86,10 @@ def train():
7986 epoch = 0 + args .resume_epoch
8087 print ('Loading Dataset...' )
8188
82- dataset = VOCDetection (args . training_dataset , preproc (img_dim , rgb_means ), AnnotationTransform ())
89+ dataset = VOCDetection (training_dataset , preproc (img_dim , rgb_mean ), AnnotationTransform ())
8390
84- epoch_size = math .ceil (len (dataset ) / args . batch_size )
85- max_iter = args . max_epoch * epoch_size
91+ epoch_size = math .ceil (len (dataset ) / batch_size )
92+ max_iter = max_epoch * epoch_size
8693
8794 stepvalues = (200 * epoch_size , 250 * epoch_size )
8895 step_index = 0
@@ -95,15 +102,15 @@ def train():
95102 for iteration in range (start_iter , max_iter ):
96103 if iteration % epoch_size == 0 :
97104 # create batch iterator
98- batch_iterator = iter (data .DataLoader (dataset , batch_size , shuffle = True , num_workers = args . num_workers , collate_fn = detection_collate ))
105+ batch_iterator = iter (data .DataLoader (dataset , batch_size , shuffle = True , num_workers = num_workers , collate_fn = detection_collate ))
99106 if (epoch % 10 == 0 and epoch > 0 ) or (epoch % 5 == 0 and epoch > 200 ):
100- torch .save (net .state_dict (), args . save_folder + 'FaceBoxes_epoch_' + repr (epoch ) + '.pth' )
107+ torch .save (net .state_dict (), save_folder + 'FaceBoxes_epoch_' + str (epoch ) + '.pth' )
101108 epoch += 1
102109
103110 load_t0 = time .time ()
104111 if iteration in stepvalues :
105112 step_index += 1
106- lr = adjust_learning_rate (optimizer , args . gamma , epoch , step_index , iteration , epoch_size )
113+ lr = adjust_learning_rate (optimizer , gamma , epoch , step_index , iteration , epoch_size )
107114
108115 # load train data
109116 images , targets = next (batch_iterator )
@@ -112,33 +119,35 @@ def train():
112119
113120 # forward
114121 out = net (images )
115-
122+
116123 # backprop
117124 optimizer .zero_grad ()
118125 loss_l , loss_c = criterion (out , priors , targets )
119126 loss = cfg ['loc_weight' ] * loss_l + loss_c
120127 loss .backward ()
121128 optimizer .step ()
122129 load_t1 = time .time ()
123- print ( 'Epoch:' + repr ( epoch ) + ' || epochiter: ' + repr ( iteration % epoch_size ) + '/' + repr ( epoch_size ) +
124- '|| Totel iter ' + repr ( iteration ) + ' || L: %.4f C: %.4f||' % ( cfg [ 'loc_weight' ] * loss_l . item (), loss_c . item ()) +
125- 'Batch time: % .4f sec. ||' % ( load_t1 - load_t0 ) + 'LR: %.8f' % ( lr ))
130+ batch_time = load_t1 - load_t0
131+ eta = int ( batch_time * ( max_iter - iteration ))
132+ print ( 'Epoch:{}/{} || Epochiter: {}/{} || Iter: {}/{} || L: {: .4f} C: {:.4f} || LR: {:.8f} || Batchtime: {:.4f} s || ETA: {}' . format ( epoch , max_epoch , ( iteration % epoch_size ) + 1 , epoch_size , iteration + 1 , max_iter , loss_l . item (), loss_c . item (), lr , batch_time , str ( datetime . timedelta ( seconds = eta )) ))
126133
127- torch .save (net .state_dict (), args .save_folder + 'Final_FaceBoxes.pth' )
134+
135+ torch .save (net .state_dict (), save_folder + 'Final_FaceBoxes.pth' )
128136
129137
130138def adjust_learning_rate (optimizer , gamma , epoch , step_index , iteration , epoch_size ):
131- """Sets the learning rate
139+ """Sets the learning rate
132140 # Adapted from PyTorch Imagenet example:
133141 # https://github.com/pytorch/examples/blob/master/imagenet/main.py
134142 """
135- if epoch < 0 :
136- lr = 1e-6 + (args .lr - 1e-6 ) * iteration / (epoch_size * 5 )
143+ warmup_epoch = - 1
144+ if epoch <= warmup_epoch :
145+ lr = 1e-6 + (initial_lr - 1e-6 ) * iteration / (epoch_size * warmup_epoch )
137146 else :
138- lr = args . lr * (gamma ** (step_index ))
147+ lr = initial_lr * (gamma ** (step_index ))
139148 for param_group in optimizer .param_groups :
140149 param_group ['lr' ] = lr
141150 return lr
142-
151+
143152if __name__ == '__main__' :
144153 train ()
0 commit comments