def prepare(): if request.method == 'POST': print('Preparing...') loadObjects() downloadModel.main(objectList) print('Downloaded all models') objectID = request.form.get('id') GlobalModel.graph = object for obj in objectList: print("Object ID: " + obj.id) print("Object Name: " + obj.name) if obj.id == objectID: objName = obj.name modelName = objectID + "_" + objName + '_model.h5' modelPath = os.path.join('object_detector_retinanet', 'weights', modelName) # start tensorflow backend tf.disable_resource_variables() get_session() # set the modified tf session as backend in keras keras.backend.tensorflow_backend.set_session(get_session()) GlobalModel.model = models.load_model(modelPath, backbone_name='resnet50') GlobalModel.graph = tf.get_default_graph() print("Loaded model " + modelName) return jsonify(success=True, message="Prepared model") return jsonify(success=True, message="Prepared model") return jsonify(success=False, message="This is GET method")
def main(args=None): # parse arguments # if args is None: # args = sys.argv[1:] # args = parse_args(args) # load and convert model model = models.load_model(args.model_in, convert=True, backbone_name=args.backbone, nms=args.nms) # save model model.save(args.model_out)
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) if args.hard_score_rate: hard_score_rate = float(args.hard_score_rate.lower()) else: hard_score_rate = 0.5 print("hard_score_rate={}".format(hard_score_rate)) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU use_cpu = False if args.gpu: gpu_num = args.gpu else: gpu_num = str(0) if use_cpu: os.environ["CUDA_VISIBLE_DEVICES"] = str(666) else: os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num ##newly added line os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num = str(0) keras.backend.tensorflow_backend.set_session(get_session()) # make save path if it doesn't exist if args.save_path is not None and not os.path.exists(args.save_path): os.makedirs(args.save_path) # create the generator generator = create_generator(args) # load the model print('Loading model, this may take a second...') model = models.load_model(os.path.join(root_dir(), args.model), backbone_name=args.backbone, convert=args.convert_model, nms=False) # start prediction while True: predict(generator, model, score_threshold=args.score_threshold, save_path=os.path.join(root_dir(), 'res_images_iou'), hard_score_rate=hard_score_rate) value = input("Please enter y to continue\n") if value != 'y': break
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) if DEBUG_MODE: args.steps = 10 # create object that stores backbone information backbone = models.backbone(args.backbone) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU use_cpu = False if args.gpu: gpu_num = args.gpu else: gpu_num = str(0) if use_cpu: os.environ["CUDA_VISIBLE_DEVICES"] = str(666) else: os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num keras.backend.tensorflow_backend.set_session(get_session()) # Weights and logs saves in a new locations stmp = time.strftime("%c").replace(" ", "_") args.snapshot_path = os.path.join(args.snapshot_path, stmp) args.tensorboard_dir = os.path.join(args.tensorboard_dir, stmp) print("Weights will be saved in {}".format(args.snapshot_path)) print("Logs will be saved in {}".format(args.tensorboard_dir)) create_folder(args.snapshot_path) create_folder(args.tensorboard_dir) # create the generators train_generator, validation_generator = create_generators(args) print('train_size:{},val_size:{}'.format(train_generator.size(), validation_generator.size())) # create the model if args.snapshot is not None: print('Loading model, this may take a second...') model = models.load_model(args.snapshot, backbone_name=args.backbone) training_model = model prediction_model = retinanet_bbox(model=model) else: weights = os.path.join(os.path.join(root_dir(), args.weights)) # default to imagenet if nothing else is specified if weights is None and args.imagenet_weights: weights = backbone.download_imagenet() print('Creating model, this may take a second...') model, training_model, prediction_model = create_models( backbone_retinanet=backbone.retinanet, num_classes=train_generator.num_classes(), weights=weights, multi_gpu=args.multi_gpu, freeze_backbone=args.freeze_backbone) # print model summary # print(model.summary()) # this lets the generator compute backbone layer shapes using the actual backbone model if 'vgg' in args.backbone or 'densenet' in args.backbone: compute_anchor_targets = functools.partial( anchor_targets_bbox, shapes_callback=make_shapes_callback(model)) train_generator.compute_anchor_targets = compute_anchor_targets if validation_generator is not None: validation_generator.compute_anchor_targets = compute_anchor_targets # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, args, ) # start training training_model.fit_generator(generator=train_generator, steps_per_epoch=args.steps, epochs=args.epochs, verbose=1, callbacks=callbacks, validation_data=validation_generator, validation_steps=validation_generator.size())
def main(image_folder_path: str, detection_save_folder: str, model_wights_path: str, classes=None, hard_score_rate = 0.5, backbone = 'resnet50', gpu = None, score_threshold = 0.1, iou_threshold = 0.5, image_min_side = 800, image_max_side = 1333, args = None): # parse arguments, to fill args with parameters initialized from defaults if args is None: args = ["--model", model_wights_path, 'csv'] args = parse_args(args) if args.hard_score_rate is None: args.hard_score_rate = hard_score_rate # hard_score_rate = float(args.hard_score_rate.lower()) if args.model is None: args.model = model_wights_path if args.base_dir is None: args.base_dir = image_folder_path if args.backbone is None: args.backbone = backbone if args.gpu is None: args.gpu = gpu if args.score_threshold is None: args.score_threshold = float(score_threshold) if args.iou_threshold is None: args.iou_threshold = float(iou_threshold) if args.save_path is None: args.save_path = detection_save_folder if args.image_max_side is None: args.image_max_side = int(image_max_side) if args.image_min_side is None: args.image_min_side = int(image_min_side) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU use_cpu = False if args.gpu: gpu_num = args.gpu else: gpu_num = str(0) if use_cpu: os.environ["CUDA_VISIBLE_DEVICES"] = str(666) else: os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num keras.backend.tensorflow_backend.set_session(get_session()) # make save path if it doesn't exist if args.save_path is not None and not os.path.exists(args.save_path): os.makedirs(args.save_path) # create the generator generator = create_generator(args, args.base_dir) # load the model print('Loading model, this may take a second...') model = models.load_model(os.path.join(root_dir(), args.model), backbone_name=args.backbone, convert=args.convert_model, nms=False) # start prediction detection_csv_results_file_path = predict( generator, model, score_threshold=args.score_threshold, # save_path=os.path.join(root_dir(), 'res_images_iou'), image_save_path = os.path.join(args.save_path, "detection_results_images"), results_save_path = args.save_path, hard_score_rate=hard_score_rate) return detection_csv_results_file_path
def infer(weights, image_dir, labels_to_name, output_file, threshold, hard_score_rate): model = models.load_model(weights, backbone_name='resnet50', convert=1, nms=False) labels_to_names = labels_to_name csv_data_lst = [] csv_data_lst.append( ['image_id', 'x1', 'y1', 'x2', 'y2', 'confidence', 'hard_score']) threshold = threshold hard_score_rate = hard_score_rate max_detections = 9999 image_dir = image_dir images = os.listdir(image_dir) if os.path.exists(str(output_file) + '/' + 'images'): shutil.rmtree(str(output_file) + '/' + 'images') os.makedirs(str(output_file) + '/' + 'images') # Run inference t0 = time.time() for img in images: image_path = image_dir + '/' + img t = time.time() image = read_image_bgr(image_path) # patches = image.unfold(0, 128, 128).unfold(1, 128, 128).unfold(2, 3, 3) draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) image = preprocess_image(image) image, scale = resize_image(image) boxes, hard_scores, labels, soft_scores = model.predict_on_batch( np.expand_dims(image, axis=0)) soft_scores = np.squeeze(soft_scores, axis=-1) soft_scores = hard_score_rate * hard_scores + ( 1 - hard_score_rate) * soft_scores boxes /= scale indices = np.where(hard_scores[0, :] > threshold)[0] scores = soft_scores[0][indices] hard_scores = hard_scores[0][indices] scores_sort = np.argsort(-scores)[:max_detections] image_boxes = boxes[0, indices[scores_sort], :] image_scores = scores[scores_sort] image_hard_scores = hard_scores[scores_sort] image_labels = labels[0, indices[scores_sort]] image_detections = np.concatenate([ image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_labels, axis=1) ], axis=1) results = np.concatenate([ image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_hard_scores, axis=1), np.expand_dims(image_labels, axis=1) ], axis=1) filtered_data = EmMerger.merge_detections(image_path, results) filtered_boxes = [] filtered_scores = [] filtered_labels = [] for ind, detection in filtered_data.iterrows(): box = np.asarray([ detection['x1'], detection['y1'], detection['x2'], detection['y2'] ]) filtered_boxes.append(box) filtered_scores.append(detection['confidence']) filtered_labels.append('{0:.2f}'.format(detection['hard_score'])) row = [ image_path, detection['x1'], detection['y1'], detection['x2'], detection['y2'], detection['confidence'], detection['hard_score'] ] csv_data_lst.append(row) for box, score, label in zip(filtered_boxes, filtered_scores, filtered_labels): # scores are sorted so we can break if score < threshold: break color = [ 31, 0, 255 ] #change the length of color array here based on the object classes, here I have hardcoded! b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format( labels_to_names[0], score) #hardcoded the index in the dictionary draw_caption(draw, b, caption) plt.figure(figsize=(20, 20)) plt.axis('off') plt.imshow(draw) plt.savefig( str(output_file) + '/' + 'images' + '/' + str(img.split('.')[0]) + '.png')