-
Notifications
You must be signed in to change notification settings - Fork 37
/
a3c_test.py
132 lines (107 loc) · 4.69 KB
/
a3c_test.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
132
import numpy as np
import torch
import torch.nn.functional as F
import time
import logging
import env as grounding_env
from models import A3C_LSTM_GA
from torch.autograd import Variable
from constants import *
def test(rank, args, shared_model):
torch.manual_seed(args.seed + rank)
env = grounding_env.GroundingEnv(args)
env.game_init()
model = A3C_LSTM_GA(args)
if (args.load != "0"):
print("Loading model ... "+args.load)
model.load_state_dict(
torch.load(args.load, map_location=lambda storage, loc: storage))
model.eval()
(image, instruction), _, _, _ = env.reset()
# Print instruction while evaluating and visualizing
if args.evaluate != 0 and args.visualize == 1:
print("Instruction: {} ".format(instruction))
# Getting indices of the words in the instruction
instruction_idx = []
for word in instruction.split(" "):
instruction_idx.append(env.word_to_idx[word])
instruction_idx = np.array(instruction_idx)
image = torch.from_numpy(image).float()/255.0
instruction_idx = torch.from_numpy(instruction_idx).view(1, -1)
reward_sum = 0
done = True
start_time = time.time()
episode_length = 0
rewards_list = []
accuracy_list = []
episode_length_list = []
num_episode = 0
best_reward = 0.0
test_freq = 50
while True:
episode_length += 1
if done:
if (args.evaluate == 0):
model.load_state_dict(shared_model.state_dict())
cx = Variable(torch.zeros(1, 256), volatile=True)
hx = Variable(torch.zeros(1, 256), volatile=True)
else:
cx = Variable(cx.data, volatile=True)
hx = Variable(hx.data, volatile=True)
tx = Variable(torch.from_numpy(np.array([episode_length])).long(),
volatile=True)
value, logit, (hx, cx) = model(
(Variable(image.unsqueeze(0), volatile=True),
Variable(instruction_idx, volatile=True), (tx, hx, cx)))
prob = F.softmax(logit)
action = prob.max(1)[1].data.numpy()
(image, _), reward, done, _ = env.step(action[0])
done = done or episode_length >= args.max_episode_length
reward_sum += reward
if done:
num_episode += 1
rewards_list.append(reward_sum)
# Print reward while evaluating and visualizing
if args.evaluate != 0 and args.visualize == 1:
print("Total reward: {}".format(reward_sum))
episode_length_list.append(episode_length)
if reward == CORRECT_OBJECT_REWARD:
accuracy = 1
else:
accuracy = 0
accuracy_list.append(accuracy)
if(len(rewards_list) >= test_freq):
print(" ".join([
"Time {},".format(time.strftime("%Hh %Mm %Ss",
time.gmtime(time.time() - start_time))),
"Avg Reward {},".format(np.mean(rewards_list)),
"Avg Accuracy {},".format(np.mean(accuracy_list)),
"Avg Ep length {},".format(np.mean(episode_length_list)),
"Best Reward {}".format(best_reward)]))
logging.info(" ".join([
"Time {},".format(time.strftime("%Hh %Mm %Ss",
time.gmtime(time.time() - start_time))),
"Avg Reward {},".format(np.mean(rewards_list)),
"Avg Accuracy {},".format(np.mean(accuracy_list)),
"Avg Ep length {},".format(np.mean(episode_length_list)),
"Best Reward {}".format(best_reward)]))
if np.mean(rewards_list) >= best_reward and args.evaluate == 0:
torch.save(model.state_dict(),
args.dump_location+"model_best")
best_reward = np.mean(rewards_list)
rewards_list = []
accuracy_list = []
episode_length_list = []
reward_sum = 0
episode_length = 0
(image, instruction), _, _, _ = env.reset()
# Print instruction while evaluating and visualizing
if args.evaluate != 0 and args.visualize == 1:
print("Instruction: {} ".format(instruction))
# Getting indices of the words in the instruction
instruction_idx = []
for word in instruction.split(" "):
instruction_idx.append(env.word_to_idx[word])
instruction_idx = np.array(instruction_idx)
instruction_idx = torch.from_numpy(instruction_idx).view(1, -1)
image = torch.from_numpy(image).float()/255.0