def main(): parser = argparse.ArgumentParser( description= 'Creates the dataset to train the matching classifier from triplet annotations' ) parser.add_argument('--downsample_rate', help='number of times that the input is downsampled', default=16, type=int) parser.add_argument('--image_size', help='input image size', default=512, type=int) parser.add_argument('--no_random', help='true too apply data augmentation', action='store_false') parser.add_argument('--add_appearance', help='true add appearance features', action='store_true') parser.add_argument('--path_dataset', help='path of the Imagenet VID dataset', type=str) args = parser.parse_args() downsample_rate = args.downsample_rate image_size = (args.image_size, args.image_size) rndm = args.no_random add_appearace_similarity = args.add_appearance path_dataset = args.path_dataset print(downsample_rate, image_size, rndm, add_appearace_similarity, path_dataset) if add_appearace_similarity: import roi_nn import train_utils base_path_model = './demos/YOLOv3/pretrained_models/ILSVRC/1203_1758_model_8/' path_roi_model = base_path_model + 'embedding_model/' path_weights = train_utils.get_best_weights(base_path_model) backbone = roi_nn.get_backbone(path_weights, downsample_rate=downsample_rate) branch_model = roi_nn.load_branch_body(path_roi_model) else: backbone, branch_model = None, None np.random.seed(0) path_annotations = './data_annotations/triplet_annotations/triplet_annotations_train.txt' get_and_store_dataset_aug(path_annotations, path_dataset, image_size, backbone, branch_model, downsample_rate, rndm, 'train') path_annotations = './data_annotations/triplet_annotations/triplet_annotations_val.txt' get_and_store_dataset_aug(path_annotations, path_dataset, image_size, backbone, branch_model, downsample_rate, rndm, 'val')
def get_best_weights(model_folder, train_params, score=0, iou=0.5): annotations_file = train_params['path_annotations'][1] eval_t = '{}stats_{}_score{}_iou{}.json'.format(model_folder, annotations_file.split('/')[-1][:-4], score, iou) eval_f = '{}stats_stage2_{}_score{}_iou{}.json'.format(model_folder, annotations_file.split('/')[-1][:-4], score, iou) eval_t = json.load(open(eval_t, 'r')) eval_f = json.load(open(eval_f, 'r')) if eval_t['total'][1] > eval_f['total'][1]: # best_weights return train_utils.get_best_weights(model_folder) else: # Final weights return model_folder + 'weights/trained_weights_final.h5'
def predict_and_store_from_annotations(model_folder, train_params, annotations_file, preds_filename, score, iou, best_weights=True): if os.path.isfile(preds_filename): return None, -1 if best_weights: model_path = train_utils.get_best_weights(model_folder) else: model_path = model_folder + 'weights/trained_weights_final.h5' model = EYOLO( model_image_size=tuple(train_params['input_shape']), model_path=model_path, classes_path=train_params['path_classes'], anchors_path=train_params['path_anchors'], score=score, # 0.3 iou=iou, # 0.5 td_len=train_params.get('td_len', None), mode=train_params.get('mode', None), spp=train_params.get('spp', False)) # Create pred file with open(annotations_file, 'r') as f: annotations = f.read().splitlines() preds = [] t = time.time() total = len(annotations) for ann in tqdm(annotations[:total], total=total, file=sys.stdout): img = ann.split()[0] # image = cv2.imread(train_params['path_dataset'] + img) # image = Image.fromarray(image) images = [ Image.open(train_params['path_dataset'] + img) for img in img.split(',') ] img_size = images[0].size # images = images[0] if len(images) == 1 else np.stack(images) boxes, scores, classes = model.get_prediction(images) for i in range(len(boxes)): left, bottom, right, top = [int(b) for b in boxes[i].tolist()] left, bottom = max(0, left), max(0, bottom) right, top = min(img_size[1], right), min(img_size[0], top) width = top - bottom height = right - left preds.append({ 'image_id': img[:-4], 'category_id': int(classes[i]), # 'bbox': [ x_min, y_min, width, height ], 'bbox': [bottom, left, width, height], 'score': float(scores[i]), }) fps = len(annotations) / (time.time() - t) json.dump(preds, open(preds_filename, 'w')) return model, fps
logging, checkpoint, reduce_lr_2, # reduce_lr early_stopping ]) model.save_weights(path_model + 'weights/trained_weights_final.h5') print('=' * print_line) ## %% train_utils.remove_worst_weights(path_model) ## %% best_weights = train_utils.get_best_weights(path_model) model.load_weights(best_weights) # %% log('EVALUATING') model_num = int(path_model.split('/')[-2].split('_')[-1]) #dataset_name, path_results = 'kitchen', '/mnt/hdd/egocentric_results/' #evaluate_model.main(path_results, dataset_name, model_num, score, iou, num_annotation_file, plot=True, full=True) iou = 0.5 if dataset_name == 'kitchen': annotation_files = [(0.005, 1), (0.005, 0)] else: annotation_files = [(0, 1), (evaluate_model.MIN_SCORE, 0)]
def predict_and_store_from_annotations(model_folder, train_params, annotations_file, output_dir, image_size, score, nms_iou, best_weights=True, raw_eval='def', load=False, model_path=None): if raw_eval == 'raw_scores': preds_filename = '{}preds_{}_{}_is{}_score{}_raw_score.json'.format( output_dir, 'bw' if best_weights else 'stage2', annotations_file.split('/')[-1][:-4], image_size[0], score, nms_iou) elif raw_eval == 'raw': preds_filename = '{}preds_{}_{}_is{}_score{}_raw.json'.format( output_dir, 'bw' if best_weights else 'stage2', annotations_file.split('/')[-1][:-4], image_size[0], score) elif 'def': preds_filename = '{}preds_{}_{}_is{}_score{}_iou{}.json'.format( output_dir, 'bw' if best_weights else 'stage2', annotations_file.split('/')[-1][:-4], image_size[0], score, nms_iou) else: raise ValueError('Unrecognized eval error') if os.path.isfile(preds_filename): print(' * Loading:', preds_filename) if load: return json.load(open(preds_filename, 'r')), preds_filename else: return None, preds_filename print(' * Predicting:', preds_filename) if model_path is None: if best_weights: model_path = train_utils.get_best_weights(model_folder) else: model_path = model_folder + 'weights/trained_weights_final.h5' model = EYOLO( model_image_size=image_size, model_path=model_path, classes_path=train_params['path_classes'], anchors_path=train_params['path_anchors'], score=score, iou=nms_iou, # 0.5 td_len=train_params.get('td_len', None), mode=train_params.get('mode', None), spp=train_params.get('spp', False), raw_eval=raw_eval) # Create pred file with open(annotations_file, 'r') as f: annotations = f.read().splitlines() preds = [] prediction_time = 0 total = len(annotations) total_prediction_time = time.time() for ann in tqdm(annotations[:total], total=total, file=sys.stdout): img = ann.split()[0] # image = cv2.imread(train_params['path_dataset'] + img) # image = Image.fromarray(image) images = [ Image.open(train_params['path_dataset'] + img) for img in img.split(',') ] img_size = images[0].size max_size = max(img_size[0], img_size[1]) # images = images[0] if len(images) == 1 else np.stack(images) t = time.time() boxes, scores, classes = model.get_prediction(images) prediction_time += time.time() - t for i in range(len(boxes)): left, bottom, right, top = [ min(int(b), max_size) for b in boxes[i].tolist() ] left, bottom = max(0, left), max(0, bottom) right, top = min(img_size[1], right), min(img_size[0], top) width = top - bottom height = right - left preds.append({ 'image_id': '.'.join(img.split('.')[:-1]), 'category_id': int(classes[i]), # 'bbox': [ x_min, y_min, width, height ], 'bbox': [bottom, left, width, height], 'score': float(scores[i]), }) print('Total prediction time: ', time.time() - total_prediction_time) print('Prediction time: {} secs || {:.2f}'.format( prediction_time, prediction_time / len(annotations))) # keras.backend.clear_session() model.yolo_model = None del model.yolo_model model = None del model # fps = len(annotations)/(time.time()-t) json.dump(preds, open(preds_filename, 'w')) if load: return preds, preds_filename else: return None, preds_filename
model_num = 1 model_folder = train_utils.get_model_path('/mnt/hdd/egocentric_results/', 'kitchen', model_num) train_params = json.load(open(model_folder + 'train_params.json', 'r')) # classes_path = './dataset_scripts/kitchen/kitchen_classes{}.txt'.format(version) classes_path = train_params['path_classes'] path_base = '' # anchors_path = 'base_models/yolo_anchors.txt' anchors_path = train_params['path_anchors'] model_image_size = [416, 416] else: raise ValueError('Model not recognized') if model_path is None: model_path = train_utils.get_best_weights(model_folder) print('Loading:', model_path) print('=' * 80) model = EYOLO( model_image_size=(img_size, img_size), model_path=model_path, anchors_path=anchors_path, classes_path=classes_path, score=0.1, iou=0.5, # gpu_num = 2 td_len=train_params['td_len'], mode=train_params['mode'], spp=train_params.get('spp', False))