-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
187 lines (166 loc) · 7.12 KB
/
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import argparse
import glob
import os
import PIL.Image as pil
from PIL import Image
import matplotlib.pyplot as plt
import cv2
from racklay import model
import numpy as np
import torch
from torchvision import transforms
def get_args():
parser = argparse.ArgumentParser(
description="Testing arguments for Racklay")
parser.add_argument("--image_path", type=str,
help="path to folder of images", required=True)
parser.add_argument("--model_path", type=str,
help="path to Racklay model", required=True)
parser.add_argument(
"--ext",
type=str,
default="png",
help="extension of images in the folder")
parser.add_argument("--out_dir", type=str,
default="output directory to save topviews")
parser.add_argument("--type", type=str,
default="both/topview/frontview")
parser.add_argument("--num_racks", type=int, default=1,
help="Max number of racks")
parser.add_argument("--occ_map_size", type=int, default=128,
help="size of topview occupancy map")
return parser.parse_args()
def save_topview(idx, tv_temp, name_dest_im):
# print("PRINTING THE TEST OUTPUT SHAPE")
# print(tv_temp.shape)
for i in range(args.num_racks):
tv = tv_temp[:,3*i:3*i+3,:,:]
tv_np = tv.squeeze()
tv = torch.argmax(tv_np, 0)
#for i in range(len(tv)):
# for j in range(len(tv[i])):
# if(tv[i][j]==1):
# print("Here",end=" ")
tv[tv==1] = 115
tv[tv==2] = 255
dir_name = os.path.dirname(name_dest_im)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
cv2.imwrite(name_dest_im + "rackno_" +str(i) + ".png", tv.cpu().numpy())
# print("Saved prediction to {}".format(name_dest_im))
def npy_loader(path):
return np.load(path,allow_pickle=True)
def readlines(filename):
"""Read all the lines in a text file and return as a list
"""
with open(filename, 'r') as f:
lines = f.read().splitlines()
return lines
def test(args):
models = {}
device = torch.device("cuda")
encoder_path = os.path.join(args.model_path, "encoder.pth")
encoder_dict = torch.load(encoder_path, map_location=device)
feed_height = encoder_dict["height"]
feed_width = encoder_dict["width"]
models["encoder"] = model.Encoder(18, feed_width, feed_height, False)
filtered_dict_enc = {
k: v for k,
v in encoder_dict.items() if k in models["encoder"].state_dict()}
models["encoder"].load_state_dict(filtered_dict_enc)
if args.type == "both":
top_decoder_path = os.path.join(
args.model_path, "top_decoder.pth")
front_decoder_path = os.path.join(
args.model_path, "front_decoder.pth")
models["top_decoder"] = model.Decoder(
models["encoder"].resnet_encoder.num_ch_enc, 3*args.num_racks,args.occ_map_size)
models["top_decoder"].load_state_dict(
torch.load(top_decoder_path, map_location=device))
models["front_decoder"] = model.Decoder(
models["encoder"].resnet_encoder.num_ch_enc, 3*args.num_racks,args.occ_map_size)
models["front_decoder"].load_state_dict(
torch.load(front_decoder_path, map_location=device))
elif args.type == "topview":
decoder_path = os.path.join(args.model_path, "top_decoder.pth")
models["top_decoder"] = model.Decoder(
models["encoder"].resnet_encoder.num_ch_enc, 3*args.num_racks,args.occ_map_size)
models["top_decoder"].load_state_dict(
torch.load(decoder_path, map_location=device))
elif args.type == "frontview":
decoder_path = os.path.join(args.model_path, "front_decoder.pth")
models["front_decoder"] = model.Decoder(
models["encoder"].resnet_encoder.num_ch_enc, 3*args.num_racks,args.occ_map_size)
models["front_decoder"].load_state_dict(
torch.load(decoder_path, map_location=device))
for key in models.keys():
models[key].to(device)
models[key].eval()
# if os.path.isfile(args.image_path):
# # Only testing on a single image
# paths = [args.image_path]
# output_directory = os.path.dirname(args.image_path)
if os.path.isfile(args.image_path):
# Searching folder for images
paths = readlines(args.image_path)
else:
raise Exception(
"Can not find args.image_path: {}".format(
args.image_path))
print("-> Predicting on {:d} test images".format(len(paths)))
# print(paths)
# PREDICTING ON EACH IMAGE IN TURN
with torch.no_grad():
for idx, image_path in enumerate(paths):
# print("Current path is:", image_path)
# Load image and preprocess
input_image = pil.open(image_path).convert('RGB')
#img = npy_loader(image_path)
#input_image = Image.fromarray(img.astype('uint8'), 'RGB')
original_width, original_height = input_image.size
input_image = input_image.resize(
(feed_width, feed_height), pil.LANCZOS)
# print("INPUT IMAGE SHAPE")
# print(input_image.size)
input_image = transforms.ToTensor()(input_image).unsqueeze(0)
# PREDICTION
input_image = input_image.to(device)
features = models["encoder"](input_image)
output_name = image_path
# print("output_name", output_name)
print(
"Processing {:d} of {:d} images- ".format(idx + 1, len(paths)))
if args.type == "both":
top_tv = models["top_decoder"](
features, is_training=False)
front_tv = models["front_decoder"](
features, is_training=False)
output_name_top = output_name.replace("img/", "Results/topview/")
# print("output_name_top: ", output_name_top)
save_topview(
idx,
top_tv,
os.path.join("{}".format(output_name_top)))
output_name_front = output_name.replace("img/", "Results/frontview/")
save_topview(
idx,
front_tv,
os.path.join("{}".format(output_name_front)))
elif args.type == "topview":
tv = models["top_decoder"](features, is_training=False)
output_name_top = output_name.replace("img/", "Results/topview/")
save_topview(
idx,
tv,
os.path.join("{}".format(output_name_top)))
elif args.type == "frontview":
tv = models["front_decoder"](features, is_training=False)
output_name_front = output_name.replace("img/", "Results/frontview/")
save_topview(
idx,
tv,
os.path.join("{}".format(output_name_front)))
print('-> Done!')
if __name__ == "__main__":
args = get_args()
test(args)