-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget_datasets.py
112 lines (96 loc) · 4.29 KB
/
get_datasets.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
import os
import argparse
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from data import *
import torch.utils.data as data
import numpy as np
import cv2
import tools
import time
import os.path as osp
parser = argparse.ArgumentParser(description='YOLO Detection')
parser.add_argument('-v', '--version', default='yolo_v2',
help='yolo_v2, yolo_v3, yolo_v3_spp, slim_yolo_v2, tiny_yolo_v3')
parser.add_argument('-d', '--dataset', default='voc',
help='voc, coco-val.')
parser.add_argument('-size', '--input_size', default=416, type=int,
help='input_size')
parser.add_argument('--trained_model', default='weight/voc/',
type=str, help='Trained state_dict file path to open')
parser.add_argument('--conf_thresh', default=0.1, type=float,
help='Confidence threshold')
parser.add_argument('--nms_thresh', default=0.50, type=float,
help='NMS threshold')
parser.add_argument('--visual_threshold', default=0.3, type=float,
help='Final confidence threshold')
parser.add_argument('--cuda', action='store_true', default=False,
help='use cuda.')
args = parser.parse_args()
def vis(img, bboxes, scores, cls_inds, thresh, class_colors, class_names, class_indexs=None, dataset='voc'):
if dataset == 'voc' or dataset == "widerface":
for i, box in enumerate(bboxes):
cls_indx = cls_inds[i]
xmin, ymin, xmax, ymax = box
if scores[i] > thresh:
cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), class_colors[int(cls_indx)], 1)
cv2.rectangle(img, (int(xmin), int(abs(ymin)-20)), (int(xmax), int(ymin)), class_colors[int(cls_indx)], -1)
mess = '%s' % (class_names[int(cls_indx)])
cv2.putText(img, mess, (int(xmin), int(ymin-5)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
elif dataset == 'coco-val' and class_indexs is not None:
for i, box in enumerate(bboxes):
cls_indx = cls_inds[i]
xmin, ymin, xmax, ymax = box
if scores[i] > thresh:
cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), class_colors[int(cls_indx)], 1)
cv2.rectangle(img, (int(xmin), int(abs(ymin)-20)), (int(xmax), int(ymin)), class_colors[int(cls_indx)], -1)
cls_id = class_indexs[int(cls_indx)]
cls_name = class_names[cls_id]
# mess = '%s: %.3f' % (cls_name, scores[i])
mess = '%s' % (cls_name)
cv2.putText(img, mess, (int(xmin), int(ymin-5)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
return img
if __name__ == '__main__':
# get device
if args.cuda:
print('use cuda')
cudnn.benchmark = True
device = torch.device("cuda")
else:
device = torch.device("cpu")
input_size = [args.input_size, args.input_size]
# dataset
if args.dataset == 'voc':
print('test on voc ...')
class_names = VOC_CLASSES
class_indexs = None
num_classes = 20
dataset = VOCDetection(root=VOC_ROOT, image_sets=[('2007', 'test')], transform=None)
elif args.dataset == 'widerface':
print('test on voc ...')
class_names = WIDERFACE_CLASSES
class_indexs = None
num_classes = 1
dataset = WiderfaceDetection(root=WIDERFACE_ROOT, image_sets=['test'], transform=None)
# dataset = TestDatasets(root=WIDERFACE_ROOT)
elif args.dataset == 'coco-val':
print('test on coco-val ...')
class_names = coco_class_labels
class_indexs = coco_class_index
num_classes = 80
dataset = COCODataset(
data_dir=coco_root,
json_file='instances_val2017.json',
name='val2017',
img_size=input_size[0])
from thumb import thumb_contain
target_w = 416
target_h = 416
num_images = len(dataset)
for index in range(num_images):
print('Testing image {:d}/{:d}....'.format(index+1, num_images))
img, name = dataset.pull_image(index)
h, w, _ = img.shape
print(h, w)
thumb_contain(img, target_w, target_h, os.path.join("out", "images", f"{index}.jpg"), force_save = True)