-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·131 lines (104 loc) · 4.79 KB
/
train.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
import torch
from torch.autograd import Variable
from utils.misc import AverageMeter
from env import Env
from utils.dataloader import ReadSingleImage
import numpy as np
def train(args, model, optimizer=None, video_train=None):
reward_avg = AverageMeter()
loss_avg = AverageMeter()
value_loss_avg = AverageMeter()
policy_loss_avg = AverageMeter()
root_dir = '/home/piaozx/VOT16'
data_type = 'VOT'
model.train()
# save_path='dataset/Result/VOT'
env = Env(seqs_path=root_dir, data_set_type=data_type, save_path='save')
for video_name in video_train:
actions = []
rewards = []
values = []
entropies = []
logprobs = []
# reset for new video
observation1, observation2 = env.reset(video_name)
img1 = ReadSingleImage(observation2)
img1 = Variable(img1).cuda()
hidden_prev = model.init_hidden_state(batch_size=1) # variable cuda tensor
_, _, _, _, hidden_pres = model(imgs=img1, hidden_prev=hidden_prev)
# for loop init parameter
hidden_prev = hidden_pres
observation = observation2
FLAG = 1
loss_dd = 0
i = 2
while FLAG:
img = ReadSingleImage(observation)
img = Variable(img).cuda()
action_prob, action_logprob, action_sample, value, hidden_pres = model(imgs=img, hidden_prev=hidden_prev)
entropy = -(action_logprob * action_prob).sum(1, keepdim=True)
entropies.append(entropy)
actions.append(action_sample.long()) # list, Variable cuda inner
action_np = action_sample.data.cpu().numpy()
# print('train:', action_np)
# import pdb; pdb.set_trace()
# print(action_prob[0, 1])
loss_dd += torch.abs(0.5 - action_prob[0, 1]).pow(2)
hidden_prev = hidden_pres
sample = Variable(torch.LongTensor(action_np).cuda()).unsqueeze(0)
logprob = action_logprob.gather(1, sample)
logprobs.append(logprob)
reward, new_observation, done = env.step(action=action_np)
env.show_all()
print('train:', 'frame{%d}' % (i), 'Action:{%1d}' % action_np[0], 'rewards:{%.6f}' % reward,
'probability:{%.6f}, {%.6f}' % (action_prob.data.cpu().numpy()[0, 0],
action_prob.data.cpu().numpy()[0, 1]))
i += 1
rewards.append(reward) # just list
values.append(value) # list, Variable cuda inner
observation = new_observation
if done:
FLAG = 0
num_seqs = len(rewards)
running_add = Variable(torch.FloatTensor([0])).cuda()
value_loss = 0
policy_loss = 0
gae = torch.FloatTensor([0]).cuda()
values.append(running_add)
for i in reversed(range(len(rewards))):
running_add = args.gamma * running_add + rewards[i]
advantage = running_add - values[i]
value_loss = value_loss + 0.5 * advantage.pow(2)
delta_t = rewards[i] + args.gamma * values[i + 1].data - values[i].data
gae = gae * args.gamma * args.tau + delta_t
# gae = delta_t
policy_loss = policy_loss - logprobs[i] * Variable(gae) - args.entropy_coef * entropies[i]
value_loss = value_loss / num_seqs
policy_loss = policy_loss / num_seqs
# values.append(running_add)
# for i in reversed(range(len(rewards))):
# running_add = args.gamma * running_add + rewards[i]
# advantage = running_add - values[i]
# value_loss = value_loss + 0.5 * advantage.pow(2)
# policy_loss = policy_loss - logprobs[i] * advantage - args.entropy_coef * entropies[i]
#
# value_loss = value_loss / num_seqs
# policy_loss = policy_loss/num_seqs
optimizer.zero_grad()
loss = args.value_loss_coef * value_loss + policy_loss
loss += 0.005 * loss_dd[0]
# print model.actor.fc1.weight
loss.backward()
# viz_ = viz.get_viz('main')
# # viz_.update_plot()
torch.nn.utils.clip_grad_norm(model.critic.parameters(), args.max_grad_norm)
torch.nn.utils.clip_grad_norm(model.actor.parameters(), args.max_grad_norm)
optimizer.step()
print(video_name, 'rewards:{%.6f}' % np.mean(rewards), 'loss:{%.6f}' % loss.data[0], 'value_loss:{%6f}' %
value_loss.data[0], 'policy_loss:{%.6f}' % policy_loss.data[0])
# update the loss
loss_avg.update(loss.data.cpu().numpy())
value_loss_avg.update(value_loss.data.cpu().numpy())
policy_loss_avg.update(policy_loss.data.cpu().numpy())
reward_avg.update(np.mean(rewards))
return reward_avg.avg, loss_avg.avg, value_loss_avg.avg, policy_loss_avg.avg