-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
223 lines (195 loc) · 7.9 KB
/
test.py
File metadata and controls
223 lines (195 loc) · 7.9 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from __future__ import print_function
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader, WeightedRandomSampler
import torch.optim as optim
import torchvision.models as models
import json
import os
import cv2
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from PIL import Image
import numpy as np
from sklearn.metrics import accuracy_score
from VRUDataset import VRUDataset
import argparse
from models.cnn import CNN
from models.autoencoder import Autoencoder
from process_img_from_json import crop_and_resize_image, num_objects, find_region
# from obj_det.darknet import Darknet
# from obj_det.util import load_classes
import copy
import pickle
import argparse
import glob
JSON_F = "obj_det/detected.json"
POTENTIAL_LIST = ["chair", "bench", "bicycle", "motorbike"]
NEW_PERSON_THRESH = 0.9
OVERLAP_PERC = 0.5
CLASSIFIER_PATH = os.path.join(".", "Saved Models", "model")
def classify_img(img, epoch_to_load=-1):
# Given an input image, classify the image with the classifier
# Input- img: H x W x Z Numpy Tensor (channel first)
# epoch_to_load: which epoch's model/params to load (idx
# starting from 0), default loading the
# last epoch's model/params
#
# Output- : label associated with the feeded image
map_location = None
if torch.cuda.is_available():
map_location=lambda storage, loc: storage.cuda()
else:
map_location='cpu'
saved_models = glob.glob(os.path.join(CLASSIFIER_PATH, "*.pt"))
# Assumed model saved in a pattern with larger ones corresponding to
# later epochs
saved_models.sort()
model_to_load = saved_models[epoch_to_load]
model = torch.load(model_to_load, map_location=map_location)
######################################################
######################################################
# TODO: interprete tensor as whether the img has
# wheelchair user
# example of the current output:
# tensor([[ 0.9059, -0.4302]], grad_fn=<AddmmBackward>)
######################################################
######################################################
return model(img)
def crop_image_with_processed_bbox(img_name, j):
# Given an input image, crop image by the information specified in json file
# Input- img_name: name of the image
# j: json file specifies the information for the bounding box
#
# Output- : matrix represent the bounding box
pass
# Determines if CNN output is wheelchair or not
# Input - torch tensor
# Example input -
# tensor([[ 0.9059, -0.4302]], grad_fn=<AddmmBackward>)
# output - false
def is_wheelchair(output):
if output[0][0]>output[0][1]:
return False
else:
return True
# if a bbox labelled person overlaps by OVERLAP_PERC
# with at least one labelled bbox in the following:
# motorbike, bicycle, chair, bench ...
# then append a large bbox including both to json
# return: list of processed bbox
def process_bbox(img, j):
processed = []
persons = []
person_obj = []
person_range = set()
for obj in j:
if obj["label"] == "person":
px = set(range(obj["left"], obj["right"]))
py = set(range(obj["up"], obj["down"]))
this_person = set((x, y) for x in px for y in py)
# if (float(len(this_person & person_range)) / float(len(this_person)) < NEW_PERSON_THRESH):
person_range |= this_person
persons.append(this_person)
person_obj.append(obj)
for obj in j:
if obj["label"] in POTENTIAL_LIST:
def overlap(persons, obj):
ox = set(range(obj["left"], obj["right"]))
oy = set(range(obj["up"], obj["down"]))
this_obj = set((x, y) for x in ox for y in oy)
if (float(len(this_obj & person_range)) / float(len(this_obj)) > OVERLAP_PERC):
for i, p in enumerate(persons):
if (float(len(this_obj & p)) / float(len(this_obj)) > OVERLAP_PERC):
processed.append({
"label_debug": obj["label"],
"up": min(obj["up"], person_obj[i]["up"]),
"down": max(obj["down"], person_obj[i]["down"]),
"left": min(obj["left"], person_obj[i]["left"]),
"right": max(obj["right"], person_obj[i]["right"])
})
persons.pop(i)
person_obj.pop(i)
break;
overlap(persons, obj)
# print(processed)
# print(len(processed))
return processed
pass
def run_obj_det():
os.system("\
cd obj_det && \
rm det/* || true && \
python det.py --images imgs --det det && \
cd ..");
def save_img_with_new_bbox(new_js):
for img_name in new_js:
img = cv2.imread(os.path.join(os.getcwd(), "obj_det/imgs/", img_name))
cv2.imshow(img_name, img)
for obj in new_js[img_name]:
print("an object!")
c1 = tuple((obj["left"], obj["up"]))
c2 = tuple((obj["right"], obj["down"]))
import pickle as pkl
import random
color = random.choice(pkl.load(open("obj_det/pallete", "rb")))
cv2.rectangle(img, c1, c2, color, 2)
cv2.imwrite("new_bbox/new_" + img_name, img)
# interate list of image
def run():
# run_obj_det()
js = {}
with open(JSON_F, 'r') as f:
js = json.load(f)
new_js = {}
for j in js:
new_js[j] = process_bbox(j, js[j])
with open("new_bbox.json", 'w') as f:
json.dump(new_js, f)
return new_js
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-b', action='store_true', help="save img w/ new bbox")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = get_args()
new_js = run()
if args.b:
save_img_with_new_bbox(new_js)
################################################
################################################
######### Testing for second layer #############
################################################
################################################
for img_name in new_js.keys():
for idx in range(num_objects(img_name, new_js)):
region = find_region(img_name, new_js, idx)
cropped_img = crop_and_resize_image(img_name, region, idx, train=False, save=False)
# resize the cropped image
new_size = (100, 100)
# resize the cropped image
import PIL
assert type(cropped_img) is PIL.Image.Image
cropped_img = cropped_img.resize(new_size)
# Convert from PIL image to numpy array
cropped_img = np.array(cropped_img)
######################################################
######################################################
# TODO: deal with the 4 dimensions in PNG
# right now the second layer model can only
# handle 3 dimension JPG pics
######################################################
######################################################
# Convert from channel last to channel first np array
cropped_img = np.moveaxis(cropped_img, -1, 0)
# Expand the dimension for it to fit the model weight
cropped_img = np.expand_dims(cropped_img, axis=0)
label = classify_img(torch.Tensor(cropped_img))
output = is_wheelchair(label)
print(label)
print(output)
################################################