本文整理汇总了Python中layers.functions.Detect方法的典型用法代码示例。如果您正苦于以下问题:Python functions.Detect方法的具体用法?Python functions.Detect怎么用?Python functions.Detect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类layers.functions
的用法示例。
在下文中一共展示了functions.Detect方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_net
# 需要导入模块: from layers import functions [as 别名]
# 或者: from layers.functions import Detect [as 别名]
def test_net(save_folder, net, detector, cuda, testset, transform, max_per_image=300, thresh=0.005):
if not os.path.exists(save_folder):
os.mkdir(save_folder)
num_images = len(testset)
print_info('=> Total {} images to test.'.format(num_images),['yellow','bold'])
num_classes = cfg.model.m2det_config.num_classes
all_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)]
_t = {'im_detect': Timer(), 'misc': Timer()}
det_file = os.path.join(save_folder, 'detections.pkl')
tot_detect_time, tot_nms_time = 0, 0
print_info('Begin to evaluate',['yellow','bold'])
for i in tqdm(range(num_images)):
img = testset.pull_image(i)
# step1: CNN detection
_t['im_detect'].tic()
boxes, scores = image_forward(img, net, cuda, priors, detector, transform)
detect_time = _t['im_detect'].toc()
# step2: Post-process: NMS
_t['misc'].tic()
nms_process(num_classes, i, scores, boxes, cfg, thresh, all_boxes, max_per_image)
nms_time = _t['misc'].toc()
tot_detect_time += detect_time if i > 0 else 0
tot_nms_time += nms_time if i > 0 else 0
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print_info('===> Evaluating detections',['yellow','bold'])
testset.evaluate_detections(all_boxes, save_folder)
print_info('Detect time per image: {:.3f}s'.format(tot_detect_time / (num_images-1)))
print_info('Nms time per image: {:.3f}s'.format(tot_nms_time / (num_images - 1)))
print_info('Total time per image: {:.3f}s'.format((tot_detect_time + tot_nms_time) / (num_images - 1)))
print_info('FPS: {:.3f} fps'.format((num_images - 1) / (tot_detect_time + tot_nms_time)))
示例2: main
# 需要导入模块: from layers import functions [as 别名]
# 或者: from layers.functions import Detect [as 别名]
def main():
global args
args = arg_parse()
cfg_from_file(args.cfg_file)
bgr_means = cfg.TRAIN.BGR_MEAN
dataset_name = cfg.DATASETS.DATA_TYPE
batch_size = cfg.TEST.BATCH_SIZE
num_workers = args.num_workers
if cfg.DATASETS.DATA_TYPE == 'VOC':
trainvalDataset = VOCDetection
classes = VOC_CLASSES
top_k = 200
else:
trainvalDataset = COCODetection
classes = COCO_CLASSES
top_k = 300
valSet = cfg.DATASETS.VAL_TYPE
num_classes = cfg.MODEL.NUM_CLASSES
save_folder = args.save_folder
if not os.path.exists(save_folder):
os.mkdir(save_folder)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
cfg.TRAIN.TRAIN_ON = False
net = SSD(cfg)
checkpoint = torch.load(args.weights)
state_dict = checkpoint['model']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
name = k[7:] # remove `module.`
else:
name = k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
detector = Detect(cfg)
img_wh = cfg.TEST.INPUT_WH
ValTransform = BaseTransform(img_wh, bgr_means, (2, 0, 1))
input_folder = args.images
thresh = cfg.TEST.CONFIDENCE_THRESH
for item in os.listdir(input_folder):
img_path = os.path.join(input_folder, item)
print(img_path)
img = cv2.imread(img_path)
dets = im_detect(img, net, detector, ValTransform, thresh)
draw_img = draw_rects(img, dets, classes)
out_img_name = "output_" + item[:-4] + '_hsd'+item[-4:]
save_path = os.path.join(save_folder, out_img_name)
cv2.imwrite(save_path, img)
示例3: main
# 需要导入模块: from layers import functions [as 别名]
# 或者: from layers.functions import Detect [as 别名]
def main():
global args
args = arg_parse()
cfg_from_file(args.cfg_file)
bgr_means = cfg.TRAIN.BGR_MEAN
dataset_name = cfg.DATASETS.DATA_TYPE
batch_size = cfg.TEST.BATCH_SIZE
num_workers = args.num_workers
if cfg.DATASETS.DATA_TYPE == 'VOC':
trainvalDataset = VOCDetection
top_k = 200
else:
trainvalDataset = COCODetection
top_k = 300
dataroot = cfg.DATASETS.DATAROOT
if cfg.MODEL.SIZE == '300':
size_cfg = cfg.SMALL
else:
size_cfg = cfg.BIG
valSet = cfg.DATASETS.VAL_TYPE
num_classes = cfg.MODEL.NUM_CLASSES
save_folder = args.save_folder
if not os.path.exists(save_folder):
os.mkdir(save_folder)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
cfg.TRAIN.TRAIN_ON = False
net = SSD(cfg)
checkpoint = torch.load(args.weights)
state_dict = checkpoint['model']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
name = k[7:] # remove `module.`
else:
name = k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
detector = Detect(cfg)
ValTransform = BaseTransform(size_cfg.IMG_WH, bgr_means, (2, 0, 1))
val_dataset = trainvalDataset(dataroot, valSet, ValTransform, "val")
val_loader = data.DataLoader(
val_dataset,
batch_size,
shuffle=False,
num_workers=num_workers,
collate_fn=detection_collate)
top_k = 300
thresh = cfg.TEST.CONFIDENCE_THRESH
eval_net(
val_dataset,
val_loader,
net,
detector,
cfg,
ValTransform,
top_k,
thresh=thresh,
batch_size=batch_size)
示例4: main
# 需要导入模块: from layers import functions [as 别名]
# 或者: from layers.functions import Detect [as 别名]
def main():
global args
args = arg_parse()
cfg_from_file(args.cfg_file)
bgr_means = cfg.TRAIN.BGR_MEAN
dataset_name = cfg.DATASETS.DATA_TYPE
batch_size = cfg.TEST.BATCH_SIZE
num_workers = args.num_workers
if cfg.DATASETS.DATA_TYPE == 'VOC':
trainvalDataset = VOCDetection
classes = VOC_CLASSES
top_k = 200
else:
trainvalDataset = COCODetection
classes = COCO_CLASSES
top_k = 300
valSet = cfg.DATASETS.VAL_TYPE
num_classes = cfg.MODEL.NUM_CLASSES
save_folder = args.save_folder
if not os.path.exists(save_folder):
os.mkdir(save_folder)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
cfg.TRAIN.TRAIN_ON = False
net = SSD(cfg)
checkpoint = torch.load(args.weights)
state_dict = checkpoint['model']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
name = k[7:] # remove `module.`
else:
name = k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
detector = Detect(cfg)
img_wh = cfg.TEST.INPUT_WH
ValTransform = BaseTransform(img_wh, bgr_means, (2, 0, 1))
input_folder = args.images
thresh = cfg.TEST.CONFIDENCE_THRESH
for item in os.listdir(input_folder)[2:3]:
img_path = os.path.join(input_folder, item)
print(img_path)
img = cv2.imread(img_path)
dets = im_detect(img, net, detector, ValTransform, thresh)
draw_img = draw_rects(img, dets, classes)
out_img_name = "output_" + item
save_path = os.path.join(save_folder, out_img_name)
cv2.imwrite(save_path, img)