def test_net( model_name, net, dataset, num_images=100, batch=False, max_per_target=5, thresh=0.05, vis=False, output_dir=None, ): """Test a network on an image database.""" _t = {'im_detect': Timer(), 'misc': Timer()} correct = 0 correct_ish = 0 total = 0 for il in range(num_images): #gets a random batch batch = dataset[0] #get first and second images im_data = match_and_concat_images(batch[0], batch[3]) - 127 #get target images target_data = match_and_concat_images(batch[2][0], batch[2][1]) - 127 #get gt box, add 1 for fg label gt_box = batch[1] gt_box.append(1) gt_box = np.asarray(gt_box, dtype=np.float32) #1 gt_box for each image, the second is a dummy box since there is no fg gt_boxes = np.asarray([gt_box, [0, 0, 1, 1, 0]]) _t['im_detect'].tic() scores, boxes = im_detect(net, target_data, im_data) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() #get scores for foreground, non maximum supression inds = np.where(scores[:] > thresh)[0] #fg_scores = scores[inds, 1] fg_scores = scores[inds, :] fg_boxes = boxes[inds, 1 * 4:(1 + 1) * 4] #fg_dets = np.hstack((fg_boxes, fg_scores[:, np.newaxis])) \ # .astype(np.float32, copy=False) fg_dets = np.hstack((fg_boxes, fg_scores)) \ .astype(np.float32, copy=False) keep = nms(fg_dets, cfg.TEST.NMS) fg_dets = fg_dets[keep, :] # Limit to max_per_target detections *over all classes* if max_per_target > 0: image_scores = np.hstack([fg_dets[:, -1]]) if len(image_scores) > max_per_target: image_thresh = np.sort(image_scores)[-max_per_target] keep = np.where(fg_dets[:, -1] >= image_thresh)[0] fg_dets = fg_dets[keep, :] if len(fg_dets) > max_per_target: breakp = 1 nms_time = _t['misc'].toc(average=False) print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(il + 1, num_images, detect_time, nms_time) not_found = True for box in fg_dets: iou = get_boxes_iou(gt_box, box) if not_found and iou > .5 and box[-1] > .3: not_found = False correct += 1 if iou > .4: #not_found = False correct_ish += 1 total += 1 #see if box is correct or not print correct print correct_ish print total acc = 0 acc_ish = 0 if total != 0: acc = float(correct) / float(total) acc_ish = float(correct_ish) / float(total) return [acc, acc_ish, correct, correct_ish]
im_data = np.transpose(im_data, (1, 2, 0)) resize = 1 if max(im_data.shape) > 1000: resize = .5 im_data = cv2.resize(im_data, (int( im_data.shape[1] * resize), int(im_data.shape[0] * resize))) im_data2 = batch[0][1].numpy() im_data2 = np.transpose(im_data2, (1, 2, 0)) resize2 = 1 if max(im_data2.shape) > 1000: resize2 = .5 im_data = cv2.resize(im_data, (int( im_data.shape[1] * resize2), int(im_data.shape[0] * resize2))) im_data = match_and_concat_images(im_data, im_data2) #im_data = ((im_data/255.0) - [0.485, 0.456, 0.406])/[0.229, 0.224, 0.225] gt_boxes = np.asarray(batch[1][0][0], dtype=np.float32) gt_boxes2 = np.asarray(batch[1][1][0], dtype=np.float32) #if there are no boxes for this image, add a dummy background box if gt_boxes.shape[0] == 0: gt_boxes = np.asarray([[0, 0, 1, 1, 0]]) if gt_boxes2.shape[0] == 0: gt_boxes2 = np.asarray([[0, 0, 1, 1, 0]]) #get the gt inds that are in this image, not counting 0(background) objects_present = gt_boxes[:, 4] objects_present2 = gt_boxes2[:, 4] objects_present = objects_present[np.where(objects_present != 0)[0]] objects_present2 = objects_present2[np.where(objects_present2 != 0)[0]]
train_loss = 0 step_cnt = 0 t = Timer() t.tic() window_loss = 0 window_steps = 0 for step in range(1, max_steps): #gets a random batch batch = train_set[0] #get first and second images #im_data = match_and_concat_images(batch[0],batch[3]) -127 im_data = match_and_concat_images(batch[0], batch[3]) im_data = ( (im_data / 255.0) - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225] #get target images #target_data = match_and_concat_images(batch[2][0],batch[2][1], min_size=16) -127 target_data = match_and_concat_images(batch[2][0], batch[2][1], min_size=16) target_data = ( (target_data / 255.0) - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225] #get gt box, add 1 for fg label gt_box = batch[1] gt_box.append(1) gt_box = np.asarray(gt_box, dtype=np.float32) #1 gt_box for each image, the second is a dummy box since there is no fg
def test_net(model_name, net, dataloader, name_to_id, target_images, chosen_ids, max_per_target=5, thresh=0, vis=False, output_dir=None,): """Test a Fast R-CNN network on an image database.""" #get map from target id to name id_to_name = {} for name in name_to_id.keys(): id_to_name[name_to_id[name]] =name #num images in test set num_images = len(dataloader) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(dataloader.dataset.get_num_classes())] #array of result dicts all_results = {} # timers _t = {'im_detect': Timer(), 'misc': Timer()} if output_dir is not None: det_file = os.path.join(output_dir, model_name+'.json') print det_file #pre compute features for all targets target_features_dict = {} for id_ind,t_id in enumerate(chosen_ids): t_name = id_to_name[t_id] target_data = target_images[t_name] target_data = match_and_concat_images(target_data[0][0,:,:,:], target_data[1][0,:,:,:]) target_data = network.np_to_variable(target_data, is_cuda=True) target_data = target_data.permute(0, 3, 1, 2) target_features_dict[t_name] = net.features(target_data) #for i in range(num_images): for i,batch in enumerate(dataloader): im_data=batch[0].unsqueeze(0).numpy() im_data=np.transpose(im_data,(0,2,3,1)) im_info = im_data.shape[1:] #im_info = np.zeros((1,3)) #im_info[0,:] = [im_data.shape[1],im_data.shape[2],1] dontcare_areas = np.zeros((0,4)) #get image features im_data = network.np_to_variable(im_data, is_cuda=True) im_data = im_data.permute(0, 3, 1, 2) img_features = net.features(im_data) all_image_dets = np.zeros((0,6)) for id_ind,t_id in enumerate(chosen_ids): target_name = id_to_name[t_id] if target_name == 'background': continue target_features = target_features_dict[target_name] if (target_data is None) or len(target_data) < 1: print 'Empty target data: {}'.format(target_name) continue #target_data = match_and_concat_images(target_data[0][0,:,:,:], target_data[1][0,:,:,:]) _t['im_detect'].tic() scores, boxes = im_detect(net, target_features, img_features, im_info) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() #get scores for foreground, non maximum supression inds = np.where(scores[:, 1] > thresh)[0] fg_scores = scores[inds, 1] fg_boxes = boxes[inds, 1 * 4:(1 + 1) * 4] fg_dets = np.hstack((fg_boxes, fg_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(fg_dets, cfg.TEST.NMS) fg_dets = fg_dets[keep, :] # Limit to max_per_target detections *over all classes* if max_per_target > 0: image_scores = np.hstack([fg_dets[:, -1]]) if len(image_scores) > max_per_target: image_thresh = np.sort(image_scores)[-max_per_target] keep = np.where(fg_dets[:, -1] >= image_thresh)[0] fg_dets = fg_dets[keep, :] nms_time = _t['misc'].toc(average=False) print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, detect_time, nms_time) #put class id in the box fg_dets = np.insert(fg_dets,4,t_id,axis=1) all_image_dets = np.vstack((all_image_dets,fg_dets)) #record results by image name all_results[batch[1][1]] = all_image_dets.tolist() if output_dir is not None: with open(det_file, 'w') as f: json.dump(all_results,f) return all_results