def TestOnlyTemporalNet(): tem_model = resnet152_TemporalNet().cuda() if tem_model_save_file is not None: tem_model.load_state_dict(torch.load(tem_model_save_file)) print('load tem_model success!') tem_model.eval() dsl = test_UCF0101_Temporal() def gen(): return GenVariables_Temporal(dsl, batchsize=16) loops = 10 correct_1 = 0 correct_5 = 0 correct_10 = 0 for l in range(loops): images, labels = gen() pred = tem_model(images) # pred = F.softmax(pred) labels = labels.cpu() pred = pred.cpu() acc = accuracy(pred, labels, topk=(1, 5, 10)) correct_1 += acc[0] correct_5 += acc[1] correct_10 += acc[2] print(l, ':', acc) print('acc@1: ', correct_1 / loops) print('acc@5: ', correct_5 / loops) print('acc@10: ', correct_10 / loops) '''
# for i in range(worker): # self.ts.append(threading.Thread(target=self.pr,name='Producter_{}'.format(i))) # for i in range(worker): # self.ts[i].start() # # def pr(self): # while True: # self.q.put(self.Gen(self.dsl,self.batchsize)) # # def Get(self): # imgs,labels = self.q.get() # return imgs.cuda(),labels.cuda() # # def Close(self): # ''' # May Be Need To Stop Threads # :return: # ''' # pass if __name__ == '__main__': dsl = test_UCF101_C3D() dsl = test_UCF0101_Spatial() dsl = test_UCF0101_Temporal() itemss = random.choices(dsl, k=3) pq = PictureQueue(dsl, GenVariables_C3D, 3) pa, pb = pq.Get()
from VideoClassification.utils.Others.toolkits import accuracy, try_to_load_state_dict from VideoClassification.utils.DataSetLoader.UCF101_DataSetLoader_FromFileName.PictureQueue import PictureQueue, \ GenVariables_Spatial, GenVariables_Temporal, GenVariables_C3D batchsize = 86 epochs = 80 loops = 2000 TEST = 'S' if TEST == 'T': pq_train = PictureQueue(dsl=train_UCF0101_Temporal(), Gen=GenVariables_Temporal, batchsize=batchsize) pq_test = PictureQueue(dsl=test_UCF0101_Temporal(), Gen=GenVariables_Temporal, batchsize=batchsize) elif TEST == 'S': pq_train = PictureQueue(dsl=train_UCF0101_Spatial(), Gen=GenVariables_Spatial, batchsize=batchsize) pq_test = PictureQueue(dsl=test_UCF0101_Spatial(), Gen=GenVariables_Spatial, batchsize=batchsize) elif TEST == 'C3D': pq_train = PictureQueue(dsl=train_UCF101_C3D(), Gen=GenVariables_C3D, batchsize=batchsize) pq_test = PictureQueue(dsl=test_UCF101_C3D(), Gen=GenVariables_C3D,
def VGG_Temporal_Net_Run(): epochs = 81 loops = 2001 learningrate = 0.0001 attenuation = 0.1 model = VGG_Temporal_Net(pretrained=False, dropout1=0.9, dropout2=0.8).cuda() if Config.LOAD_SAVED_MODE_PATH is not None: import types model.try_to_load_state_dict = types.MethodType(try_to_load_state_dict, model) model.try_to_load_state_dict(torch.load(Config.LOAD_SAVED_MODE_PATH)) print('LOAD {} done!'.format(Config.LOAD_SAVED_MODE_PATH)) lossfunc = nn.CrossEntropyLoss() optim = torch.optim.Adam(model.parameters(), lr=learningrate) pq_train = PictureQueue(dsl=train_UCF0101_Temporal(), Gen=GenVariables_Temporal, batchsize=batchsize) pq_test = PictureQueue(dsl=test_UCF0101_Temporal(), Gen=GenVariables_Temporal, batchsize=batchsize) cnt = 0 for epoch in range(epochs): for l in range(loops): cnt += 1 imgs, labels = pq_train.Get() model.zero_grad() pred = model(imgs) loss = lossfunc(pred, labels) logger.scalar_summary('Temporal/train_loss', loss.data[0], cnt) loss.backward() optim.step() print('Temporal epoch: {} cnt: {} loss: {}'.format(epoch, cnt, loss.data[0])) if cnt % 25 == 0: model.eval() imgs, labels = pq_test.Get() pred = model.inference(imgs) loss = lossfunc(pred, labels) logger.scalar_summary('Temporal/test_loss', loss.data[0], cnt) # acc acc = accuracy(pred, labels, topk=(1, 5, 10)) logger.scalar_summary('Temporal/test_acc@1', acc[0], cnt) logger.scalar_summary('Temporal/test_acc@5', acc[1], cnt) logger.scalar_summary('Temporal/test_acc@10', acc[2], cnt) imgs, labels = pq_train.Get() pred = model.inference(imgs) acc = accuracy(pred, labels, topk=(1, 5, 10)) logger.scalar_summary('Temporal/train_acc@1', acc[0], cnt) logger.scalar_summary('Temporal/train_acc@5', acc[1], cnt) logger.scalar_summary('Temporal/train_acc@10', acc[2], cnt) model.train() if cnt % 2000 == 0: savefile = savepath + 'VGG_Temporal_EX1_{:02d}.pt'.format(epoch % 50) print('Temporal save model to {}'.format(savefile)) torch.save(model.state_dict(), savefile) if epoch in [20, 40, 60]: learningrate = learningrate * attenuation optim = torch.optim.Adam(model.parameters(), lr=learningrate)