-
Notifications
You must be signed in to change notification settings - Fork 54
/
test.py
executable file
·129 lines (100 loc) · 3.99 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
import argparse
import tensorflow as tf
import os
import sys
import numpy as np
import yaml
from tqdm import tqdm
from anchor import generate_default_boxes
from box_utils import decode, compute_nms
from voc_data import create_batch_generator
from image_utils import ImageVisualizer
from losses import create_losses
from network import create_ssd
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument('--data-dir', default='../dataset')
parser.add_argument('--data-year', default='2007')
parser.add_argument('--arch', default='ssd300')
parser.add_argument('--num-examples', default=-1, type=int)
parser.add_argument('--pretrained-type', default='specified')
parser.add_argument('--checkpoint-dir', default='')
parser.add_argument('--checkpoint-path', default='')
parser.add_argument('--gpu-id', default='0')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
NUM_CLASSES = 21
BATCH_SIZE = 1
def predict(imgs, default_boxes):
confs, locs = ssd(imgs)
confs = tf.squeeze(confs, 0)
locs = tf.squeeze(locs, 0)
confs = tf.math.softmax(confs, axis=-1)
classes = tf.math.argmax(confs, axis=-1)
scores = tf.math.reduce_max(confs, axis=-1)
boxes = decode(default_boxes, locs)
out_boxes = []
out_labels = []
out_scores = []
for c in range(1, NUM_CLASSES):
cls_scores = confs[:, c]
score_idx = cls_scores > 0.6
# cls_boxes = tf.boolean_mask(boxes, score_idx)
# cls_scores = tf.boolean_mask(cls_scores, score_idx)
cls_boxes = boxes[score_idx]
cls_scores = cls_scores[score_idx]
nms_idx = compute_nms(cls_boxes, cls_scores, 0.45, 200)
cls_boxes = tf.gather(cls_boxes, nms_idx)
cls_scores = tf.gather(cls_scores, nms_idx)
cls_labels = [c] * cls_boxes.shape[0]
out_boxes.append(cls_boxes)
out_labels.extend(cls_labels)
out_scores.append(cls_scores)
out_boxes = tf.concat(out_boxes, axis=0)
out_scores = tf.concat(out_scores, axis=0)
boxes = tf.clip_by_value(out_boxes, 0.0, 1.0).numpy()
classes = np.array(out_labels)
scores = out_scores.numpy()
return boxes, classes, scores
if __name__ == '__main__':
with open('./config.yml') as f:
cfg = yaml.load(f)
try:
config = cfg[args.arch.upper()]
except AttributeError:
raise ValueError('Unknown architecture: {}'.format(args.arch))
default_boxes = generate_default_boxes(config)
batch_generator, info = create_batch_generator(
args.data_dir, args.data_year, default_boxes,
config['image_size'],
BATCH_SIZE, args.num_examples, mode='test')
try:
ssd = create_ssd(NUM_CLASSES, args.arch,
args.pretrained_type,
args.checkpoint_dir,
args.checkpoint_path)
except Exception as e:
print(e)
print('The program is exiting...')
sys.exit()
os.makedirs('outputs/images', exist_ok=True)
os.makedirs('outputs/detects', exist_ok=True)
visualizer = ImageVisualizer(info['idx_to_name'], save_dir='outputs/images')
for i, (filename, imgs, gt_confs, gt_locs) in enumerate(
tqdm(batch_generator, total=info['length'],
desc='Testing...', unit='images')):
boxes, classes, scores = predict(imgs, default_boxes)
filename = filename.numpy()[0].decode()
original_image = Image.open(
os.path.join(info['image_dir'], '{}.jpg'.format(filename)))
boxes *= original_image.size * 2
visualizer.save_image(
original_image, boxes, classes, '{}.jpg'.format(filename))
log_file = os.path.join('outputs/detects', '{}.txt')
for cls, box, score in zip(classes, boxes, scores):
cls_name = info['idx_to_name'][cls - 1]
with open(log_file.format(cls_name), 'a') as f:
f.write('{} {} {} {} {} {}\n'.format(
filename,
score,
*[coord for coord in box]))