-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_posenet.py
59 lines (50 loc) · 2.31 KB
/
train_posenet.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
# -*- coding:utf-8 -*-
import os
from tqdm import tqdm
from progress.bar import Bar
from pycocotools.coco import COCO
import torch
#import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader
from posenetopt import Options
from posenet import poseNet
# from utils.eval import Evaluation
# from utils.utils import save_options
from utils.utils import save_model, adjust_lr
from dataloader import COCOkeypointloader
import matplotlib.pyplot as plt
def main(optin):
if not os.path.exists('checkpoint/'+optin.exp):
os.makedirs('checkpoint/'+optin.exp)
model = poseNet(101).cuda()
model.train()
#model = torch.nn.DataParallel(model).cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=optin.lr)
criterion = torch.nn.MSELoss().cuda()
# print(os.path.join('./annotations/person_keypoints_train2017.json'))
coco_train = COCO(os.path.join('./annotations/person_keypoints_train2017.json'))
trainloader = DataLoader(dataset=COCOkeypointloader(coco_train),batch_size=optin.batch_size, num_workers=optin.num_workers, shuffle=True)
bar = Bar('-->', fill='>', max=len(trainloader))
for epoch in range(optin.number_of_epoch):
print ('-------------Training Epoch {}-------------'.format(epoch))
print ('Total Step:', len(trainloader), '| Total Epoch:', optin.number_of_epoch)
lr = adjust_lr(optimizer, epoch, optin.lr_gamma)
print('\nEpoch: %d | LR: %.8f' % (epoch + 1, lr))
for idx, (input, label) in tqdm(enumerate(trainloader)):
input = input.cuda().float()
label = label.cuda().float()
outputs = model(input)
optimizer.zero_grad()
loss = criterion(outputs, label)
loss.backward()
optimizer.step()
print('Epoch {} : loss {}'.format(epoch,loss.data))
#if idx % 200 == 0:
# bar.suffix = 'Epoch: {epoch} Total: {ttl} | ETA: {eta:} | loss:{loss}' \
# .format(ttl=bar.elapsed_td, eta=bar.eta_td, loss=loss.data, epoch=epoch)
# bar.next()
if epoch % 5 == 0:
torch.save(model,os.path.join('checkpoint/'+optin.exp, 'model_{}.pth'.format(epoch)))
if __name__ == "__main__":
option = Options().parse()
main(option)