def load_models(): sign_model = torch.load('./weights/weights_101.pt', map_location=torch.device('cpu')) sign_model.eval() sign_model.cpu() #----------------------------------------------------------------------------------------price price_model_path = './weights/priceresnet50_csv_1all.h5' price_model = models.load_model(price_model_path, backbone_name='resnet50') price_model = models.convert_model(price_model) # load label to names mapping for visualization purposes price_labels_to_names = pd.read_csv('./weights/price_mapping.csv', header=None).T.loc[0].to_dict() #-----------------------------------------------------------------------------------------digits digit_model_path = './weights/resnet101_DIGITSFINAL.h5' digit_model = models.load_model(digit_model_path, backbone_name='resnet101') digit_model = models.convert_model(digit_model) digit_labels_to_names = pd.read_csv('./weights/digits_mapping.csv', header=None).T.loc[0].to_dict() #-----------------------------------------------------------------------------------------labels label_model_path = './weights/resnet101_LABELSG1.h5' label_model = models.load_model(label_model_path, backbone_name='resnet101') label_model = models.convert_model(label_model) label_labels_to_names = pd.read_csv('./weights/labels_mapping.csv', header=None).T.loc[0].to_dict() return sign_model, price_model, digit_model, label_model
def __init__(self, MODELPATH, *args, **kwargs): self.start_keras() # load retinanet model self.model = models.load_model(MODELPATH, backbone_name='resnet50') if ('model2' in kwargs): MODEL2PATH = kwargs.get('model2', None) self.model2 = models.load_model(MODEL2PATH, backbone_name='resnet50') else: self.model2 = None
def load_trained_model(fpath, backbone_name='resnet50'): model = models.load_model(fpath, backbone_name='resnet50') # if the model is not converted to an inference model, use the line below # see: https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model model = models.convert_model(model) return model
def __init__(self): global model, graph MODEL_PATH = os.path.join("model", "Retinanet.h5") model = models.load_model(MODEL_PATH, backbone_name='resnet50') model._make_predict_function() self.graph = tf.get_default_graph() model = models.convert_model(model)
def main(): global MODEL parser = argparse.ArgumentParser( description= 'This program receives a video as an input in order to look through it' ' and process every Nth frame to detect objects.') parser.add_argument( '--model', '-m', help='The path of the RetinaNet model to use for inference.', required=True) parser.add_argument('--video', '-v', help='The path to the video to process.', required=True) parser.add_argument('--output', '-o', help='Output directory', required=True) parser.add_argument('--n-frames', '-n', help='Process every N\'th frame.', default=1, type=int) args = parser.parse_args() MODEL = models.load_model(args.model, backbone_name='resnet50') process_video(args.video, args.n_frames, args.output)
def main(args=None): args = parse_args(args) weights_name = args.input backbone = args.backbone dirname = os.path.dirname(weights_name) basename = os.path.basename(weights_name) fn, ext = os.path.splitext(basename) model = models.load_model(weights_name, backbone_name=backbone) # Convert Keras model to ConcreteFunction full_model = tf.function(lambda input_1: model(input_1)) full_model = full_model.get_concrete_function( tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype)) # Get frozen ConcreteFunction frozen_func = convert_variables_to_constants_v2(full_model) frozen_func.graph.as_graph_def() layers = [op.name for op in frozen_func.graph.get_operations()] print("Frozen model inputs: ") print(frozen_func.inputs) print("Frozen model outputs: ") print(frozen_func.outputs) # Save frozen graph to disk tf.io.write_graph(graph_or_graph_def=frozen_func.graph, logdir=dirname, name=f"{fn}.pb", as_text=False) print(f'weights saved: {dirname}')
def evaluateSet(setpath,weightnumber=13): gpu = 0 setup_gpu(gpu) model_path = os.path.join('..', 'keras_retinanet', 'resnet50_csv_' + str(weightnumber) + '_final.h5') # load retinanet model model = models.load_model(model_path, backbone_name='resnet50') iset = readSet(setpath) #read set imagelist = iset[:,0] # load imagelist resultlist = [["image","bounds","score", "label"]] for i in imagelist: image = read_image_bgr(str("../" + i)) #originally bgr(i) if draw: image_copy = image.copy() image = preprocess_image(image) image, scale = resize_image(image) boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0)) boxes /= scale coords = [] cats = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < 0.4: break else: resultlist.append([i,box,label,score]) return resultlist
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu 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(args.model, backbone_name=args.backbone, convert=args.convert_model) # start evaluation all_detections = get_detections( generator, model, score_threshold=args.score_threshold, max_detections=args.max_detections, save_path=args.save_path, do_draw_annotations=False, window_leveling=True )
def main(): img = cv2.imread("tools/avery_test_Color.png", 1) # set the modified tf session as backend in keras keras.backend.tensorflow_backend.set_session(get_session()) # adjust this to point to your downloaded/trained model model_path = '/home/nvidia/Documents/SalamiSense/snapshots_1/resnet50_csv_inference.h5' #model_path = '/home/nvidia/Documents/SalamiSense/resnet50_csv_inference.h5' # load retinanet model model = models.load_model(model_path, backbone_name='resnet50') # load label to names mapping for visualization purposes labels_to_names = {0: 'person'} for i in range(16): img_copy = img.copy() img_copy = preprocess_image(img_copy) t0 = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(img_copy, axis=0)) t1 = time.time() elapsed = (t1 - t0) if i > 0: print(elapsed)
def __init__(self): self.class_list = CLASSES self.score_thresh = SCORE_THRESHOLD self.graph = tf.get_default_graph() #self.model = models.load_model(self.get_model(), backbone_name='resnet50') self.model = models.load_model('retinanet_inference_weiquan', backbone_name='resnet50')
def __init__(self): """ Initialization of Tensorflow session and Retinanet model. """ session = self.get_session() # session = tf.compat.v1.keras.backend.get_session() tf.compat.v1.keras.backend.set_session(session) self.model = models.load_model(MODEL_PATH, backbone_name='resnet50') # load label to names mapping for visualization purposes self.labels_to_names = { 0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush' }
def generate(self): # Generates detections for each image, saving each detection array to self.before_editing. # Quick lambda for use later dist = lambda a, b: abs(a - b) if self.detection: self.model = models.load_model( '../classifiers/type_one_detection_classifier_resnet50.h5', backbone_name='resnet50') self.model = models.convert_model(self.model) # Generate for each image def generate_callback(index): img_i = index img = self.imgs[img_i] # Progress indicator sys.stdout.write( "\rGenerating Type One Detections on Image {}/{}...".format( img_i, len(self.imgs) - 1)) # final detections for img, will be archived after detection and suppression detections = [] if self.detection: imscan = cv2.resize(img, (0, 0), fx=0.025, fy=0.025) imscan = cv2.cvtColor(imscan, cv2.COLOR_RGB2BGR) imscan = preprocess_image(imscan.copy()) imscan, scale = resize_image(imscan) boxes, scores, labels = self.model.predict( np.expand_dims(imscan, axis=0)) boxes /= scale boxes /= 0.025 for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < 0.4: continue box = get_outline_rectangle_coordinates( box[0], box[1], box[2], box[3], self.rect_h * 10, self.rect_w * 10) print("box:", box) detections.append(box) # Save these detections to both the before and after editing datasets, since we initialize them to be the # same. self.before_editing[img_i] = detections self.after_editing[img_i] = detections # img_i, img in enumerate(self.imgs): root = ProgressRoot(len(self.imgs), "Generating Type Ones", generate_callback) root.mainloop() # Now that we've finished generating, we've started editing, so we update user progress. self.dataset.progress["type_ones_started_editing"] = True if self.dataset.progress["model"] == "balbc": self.dataset.progress["type_ones_finished_editing"] = True sys.stdout.flush() print("")
def main(): # config_file = '/home/segmind/Desktop/PRATIK/TF-SERVING/snapshots/config.ini' model_in = 'resnet50_csv_50.h5' model_out_base = '/tmp/keras_retinanet' backbone = 'resnet50' # optionally load config parameters #anchor_parameters = None #if args.config: # args_config = read_config_file(config_file) # anchor_parameters = parse_anchor_parameters(args_config) # load the model model = models.load_model(model_in, backbone_name=backbone) # check if this is indeed a training model models.check_training_model(model) # convert the model print('Building inference model ..') model = models.convert_model(model, nms=True, class_specific_filter=True) print('exporting with tf-serving ..') exporter(model, export_path_base=model_out_base)
def get_model(cls, model_path='../model/resnet50_csv_06.h5.frozen'): """ Method to load a model by specified file path. Returns False if model cannot be loaded properly.""" print("get_model called") try: cls.min_no_of_frames = 2 # 2 seems more reasonable than 4 !!! cls.left_crop = False cls.right_crop = False cls.flip_lr = True cls.bright_frame = False cls.dark_frame = False cls.pedestrian_nms_thr = 0.4 cls.car_nms_thr = 0.35 cls.conf_score_bias = 0.1 cls.reassign_id_pedestrian = False cls.dummy_id_range = range(5000, 5555) cls.threshold_pedestrian = 0.5 # DO NOT CHANGE cls.threshold_car = 0.5 # DO NOT CHANGE cls.expansion = 0 # DO NOT USE cls.scales = [0.2] # DO NOT CHANGE cls.apply_heuristic_post_processing = True # ALWAYS USE THIS HEURISTIC !!! cls.apply_adaptive_pedestrian_nms = False cls.model = models.load_model('../model/resnet101_csv_15.5classes.all_bboxes.h5.frozen', backbone_name='resnet101') # 0.6358 # batch_size, 1216, 1936, 3 # _, _, _ = cls.model.predict_on_batch(np.zeros((2, 1216, 1936, 3))) cls.car_classification_model = load_model('../model/car_model_074.h5', compile=False) cls.pedestrian_classification_model = load_model('../model/pedestrian_model_epoch_3_068.h5', compile=False) return True except Exception as e: print("Failed to load model {}".format(e)) return False
def predict_images(model_path: str, image_dir: str, backbone_name: str = 'resnet50'): """ predict_images using a model trained with keras-retinanet Predicts all images within a directory, using a detection model trained with keras-retinanet. :param model_path: path to the file where the model is stored (.h5 file) :type model_path: str :param image_dir: path to the directory containing all the images :type image_dir: str :param backbone_name: model backbone used in the retinanet, defaults to 'resnet50' :type backbone_name: str, optional """ # load retinanet model model = models.load_model(model_path, backbone_name=backbone_name) imagnames = os.listdir(image_dir) for i in imagnames: predict_and_plot_single_image(os.path.join(image_dir, i), model)
def run(): args = parse_args() print(args) if not test_args(args): return keras.backend.tensorflow_backend.set_session(get_session()) label_map = get_labels(args.label) model = models.load_model(args.model, backbone_name=args.backbone) for img_file in args.image: img = read_image_bgr(img_file) canvas = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2RGB) img, scale = resize_image(preprocess_image(img)) start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(img, axis=0)) print("Processing time: ", time.time() - start) boxes /= scale for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < args.threshold: # Labels are sorted break color = label_color(label) b = box.astype(int) draw_box(canvas, b, color=color) caption = "{} {:.3f}".format(label_map[label], score) draw_caption(canvas, b, caption) canvas = cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR) out_file = args.out_prefix + os.path.split(img_file)[1] out_full = os.path.join(args.out_dir, out_file) cv2.imwrite(out_full, canvas) print("Done with writing to file {:s}.".format(out_full))
def main(): models_stats_list = list() model_attributes = parse_models(models_path) images = get_images(images_path) for model_name in model_attributes.keys(): print('\n Evaluating Retinanet with backbone {}'.format( model_attributes[model_name]['backbone'])) model = models.load_model( os.path.join(models_path, model_name), backbone_name=model_attributes[model_name]['backbone']) number_params = model.count_params() model = models.convert_model(model, custom_nms_theshold=0.5) for resolution in RESOLUTIONS: inference_time = predict(images, model, resolution=resolution) models_stats_list.append([ 'RetinaNet', model_attributes[model_name]['backbone'], number_params, str(resolution) + 'x' + str(int(resolution * 320 / 240)), resolution, inference_time ]) print( 'Average inference time for backbone {} and image resolution {} is {} s' .format(model_attributes[model_name]['backbone'], resolution, inference_time)) models_stats = pd.DataFrame(models_stats_list, columns=[ 'model', 'backbone', 'number_parameters', 'image_resolution', 'image_min_side', 'inference_time' ]) models_stats.to_csv('runtime_analysis.csv', index=None)
def find_los(): print("FINDING THE LINE OF SCRIMMAGE") global maxBox, los_boxes, los_scores, los_labels keras.backend.tensorflow_backend.set_session(get_session()) model_path = 'saved_good_files/los_detection_inference_model.h5' # load retinanet model model = models.load_model(model_path, backbone_name='resnet50') image = read_image_bgr('snap.jpg') # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) los_boxes, los_scores, los_labels = model.predict_on_batch( np.expand_dims(image, axis=0)) # correct for image scale los_boxes /= scale maxScore = 0 maxBox = None # Find the box with the greatest confidence for box, score, label in zip(los_boxes[0], los_scores[0], los_labels[0]): # scores are sorted so we can break if score > maxScore: maxScore = score maxBox = box
def main(args=None): from keras_retinanet.utils.config import parse_anchor_parameters # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) config = dict() config['anchor_parameters'] = dict() config['anchor_parameters']['sizes'] = '16 32 64 128 256 512' config['anchor_parameters']['strides'] = '8 16 32 64 128' config['anchor_parameters']['ratios'] = '0.1 0.5 1 2 4 8' config['anchor_parameters']['scales'] = '1 1.25 1.5 1.75' anchor_params = None anchor_params = parse_anchor_parameters(config) # load and convert model model = models.load_model(args.model_in, backbone_name=args.backbone) model = models.convert_model( model, nms=args.nms, class_specific_filter=args.class_specific_filter, anchor_params=anchor_params) # save model model.save(args.model_out)
def __init__(self, model_name="default"): """ model = Detector() """ checkpoint_url = FILE_URLS[model_name]["checkpoint"] classes_url = FILE_URLS[model_name]["classes"] home = os.path.expanduser("~") model_folder = os.path.join(home, f".NudeNet/{model_name}") if not os.path.exists(model_folder): os.makedirs(model_folder) checkpoint_path = os.path.join(model_folder, "checkpoint") classes_path = os.path.join(model_folder, "classes") if not os.path.exists(checkpoint_path): print("Downloading the checkpoint to", checkpoint_path) pydload.dload(checkpoint_url, save_to_path=checkpoint_path, max_time=None) if not os.path.exists(classes_path): print("Downloading the classes list to", classes_path) pydload.dload(classes_url, save_to_path=classes_path, max_time=None) self.detection_model = models.load_model(checkpoint_path, backbone_name="resnet50") self.classes = [ c.strip() for c in open(classes_path).readlines() if c.strip() ]
def predictImage(self, imagePath): model = models.load_model(self.modelWeights, backbone_name="resnet50") xmlPath = imagePath[0:imagePath.rfind(".")] + ".xml" # load the input image (in BGR order), clone it, and preprocess it image = read_image_bgr(imagePath) wI, hI, d = image.shape output = image.copy() image = preprocess_image(image) (image, scale) = resize_image(image) image = np.expand_dims(image, axis=0) # detect objects in the input image and correct for the image scale (boxes, scores, labels) = model.predict_on_batch(image) boxes /= scale boxes1 = [] for (box, score, label) in zip(boxes[0], scores[0], labels[0]): if score < self.CONFIDENCE: continue boxes1.append(([label, box], score)) # parse the filename from the input image path, construct the # path to the output image, and write the image to disk filename = imagePath.split(os.path.sep)[-1] # outputPath = os.path.sep.join([args["output"], filename]) file = open(xmlPath, "w") file.write(self.generateXML(filename, imagePath[0:imagePath.rfind("/")], wI, hI, d, boxes1)) file.close() self.combineImageAndPrediction(imagePath, xmlPath)
def load_debris_model(): global model if model is None: # model =inception_v3.InceptionV3() # model.compile(optimizer='adam', loss='categorical_crossentropy') model_path = "/tmp/debris_model_v3_10_6.h5" model = models.load_model(model_path, backbone_name='resnet50') return model
def setUpClass(cls): url = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5' r = requests.get(url) with open('resnet50_coco_best_v2.1.0.h5', 'wb') as f: f.write(r.content) cls.model = load_model('resnet50_coco_best_v2.1.0.h5', backbone_name='resnet50')
def getModel(): config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config) keras.backend.tensorflow_backend.set_session(session) model_path = './retinaNet/resnet50_pins_n_solder_14_inference.h5' return models.load_model(model_path, backbone_name='resnet50')
def load_models( global_model_path='Models/multi_class_v1-1_epoch52.h5', local_model_path='Models/interm_v1-8.h5', sc_model_path='Models/sc_augmented_v1.7.h5', # Single cell model class_model_path='Models/2019-07-22onTG3_chkpt_model.22-acc0.94_V1.hdf5'): global_model = krmodels.load_model(global_model_path, backbone_name='resnet50') local_model = krmodels.load_model(local_model_path, backbone_name='resnet50') single_cell_model = krmodels.load_model(sc_model_path, backbone_name='resnet50') class_model = models.load_model(class_model_path) pmodels = [global_model, local_model, single_cell_model, class_model] return pmodels
def load_model(self): print("Loading model: {}".format(self.weight_path)) try: self.model = models.load_model(self.weight_path, backbone_name='resnet50') except Exception as e: print("Unable to load weight file: {}".format(e)) print(" ...Done!")
def load_model(args): global model global labels_to_names setup_gpu(args.gpu) model = models.load_model(args.model, backbone_name='resnet50') labels_to_names = {0: 'Pedestrian'} return model, labels_to_names
def read_model(model_path, config): """Read keras retinanet model from keras.model.save()""" # Suppress user warning, module does not need to be compiled for prediction with warnings.catch_warnings(): # warnings.simplefilter('ignore', UserWarning) model = models.load_model(model_path, backbone_name='resnet50') return model
def get_model(model_name): print("") print(" model_map : ", ModelPool.model) if ModelPool.model is None: model_path = SparkFiles.get(model_name) model = models.load_model(model_path, backbone_name='resnet50') ModelPool.model = model return ModelPool.model
def safe_load_model(model_path): """ Wrapper function for keras_retinanet.load_model to account for different required arguments in different versions. Args: model_path: path to the .hd5 model file Returns: keras.models.model """ from keras_retinanet import models try: # For older versions of keras_retinanet model = models.load_model(model_path, backbone_name='resnet50', convert=True) except: model = models.load_model(model_path, backbone_name='resnet50') return model
def main(): global MODEL parser = argparse.ArgumentParser(description='This program receives a video as an input in order to look through it' ' and process every Nth frame to detect objects.') parser.add_argument('--model', '-m', help='The path of the RetinaNet model to use for inference.', required=True) parser.add_argument('--video', '-v', help='The path to the video to process.', required=True) parser.add_argument('--output', '-o', help='Output directory', required=True) parser.add_argument('--n-frames', '-n', help='Process every N\'th frame.', default=1, type=int) args = parser.parse_args() MODEL = models.load_model(args.model, backbone_name='resnet50') process_video(args.video, args.n_frames, args.output)