示例#1
0
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
示例#2
0
    def use_release(self, gpus=1):
        '''Use the latest DeepForest model release from github and load model. Optionally download if release doesn't exist
        
        Returns:
            model (object): A trained keras model
            gpus: number of gpus to parallelize, default to 1
        '''
        #Download latest model from github release
        release_tag, self.weights = utilities.use_release()

        #load saved model and tag release
        self.__release_version__ = release_tag
        print("Loading pre-built model: {}".format(release_tag))

        if gpus == 1:
            with warnings.catch_warnings():
                #Suppress compilte warning, not relevant here
                warnings.filterwarnings("ignore", category=UserWarning)
                self.model = utilities.read_model(self.weights, self.config)

            #Convert model
            self.prediction_model = convert_model(self.model)
        elif gpus > 1:
            backbone = models.backbone(self.config["backbone"])
            n_classes = len(self.labels.keys())
            self.model, self.training_model, self.prediction_model = create_models(
                backbone.retinanet,
                num_classes=n_classes,
                weights=self.weights,
                multi_gpu=gpus)

        #add to config
        self.config["weights"] = self.weights
示例#3
0
	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)
示例#4
0
    def predict_image(self, image_path, return_plot=True, show=False):
        '''Predict tree crowns based on loaded (or trained) model
        
        Args:
            image_path (str): Path to image on disk
            show (bool): Plot the predicted image with bounding boxes. Ignored if return_plot=False
            return_plot: Whether to return image with annotations overlaid, or just a numpy array of boxes
        Returns:
            predictions (array): if return_plot, an image. Otherwise a numpy array of predicted bounding boxes
        '''
        #Check for model weights

        if (self.weights is None):
            raise ValueError(
                "Model currently has no weights, either train a new model using deepforest.train, loading existing model, or use prebuilt model (see deepforest.use_release()"
            )

        #convert model to prediction
        self.prediction_model = convert_model(self.model)

        if return_plot:
            image = predict.predict_image(self.prediction_model,
                                          image_path,
                                          return_plot=return_plot)
            #cv2 channel order
            if show:
                plt.imshow(image[:, :, ::-1])
                plt.show()
            return image
        else:
            boxes = predict.predict_image(self.prediction_model,
                                          image_path,
                                          return_plot=return_plot)
            return boxes
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 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 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)
示例#8
0
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)
示例#9
0
def init():

    global detection_model
    global recognition_model
    global label_to_names

    # set the modified tf session as backend in keras
    tf.compat.v1.keras.backend.set_session(get_session())

    # load retinanet model
    print("[INFO] loading detection model...")
    detection_model = load_detection_model(detection_model_path,
                                           backbone_name='resnet50')
    detection_model = models.convert_model(detection_model)

    # load label to names mapping for visualization purposes
    labels_to_names = pd.read_csv(CLASSES_FILE, header=None).T.loc[0].to_dict()

    # load the traffic sign recognizer model
    print("[INFO] loading recognition model...")
    recognition_model = load_recognition_model(recognition_model_path)

    # load the label names
    label_to_names = open(CLASSES_FILE).read().strip().split("\n")
    label_to_names = [l.split(",")[0] for l in label_to_names]
示例#10
0
    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("")
示例#11
0
def test_model(model, generator, score_threshold):
    result = evaluate(generator,
                      models.convert_model(model, None),
                      score_threshold=score_threshold,
                      iou_threshold=0.5,
                      max_detections=100,
                      save_path=None)
    return result
示例#12
0
def retinanet_test():
    test_images = [
        'fe1b92a1-faaaa1eb.jpg', 'fe1cc363-a3f36598.jpg',
        'fe1d74f0-5cdc4057.jpg', 'fe1d74f0-6969bdb5.jpg',
        'fe1d9184-cd999efe.jpg', 'fe1d9184-d144106a.jpg',
        'fe1d9184-dec09b65.jpg', 'fe1f2409-5b415eb7.jpg',
        'fe1f2409-c16ea1ed.jpg', 'fe1f55fa-19ba3600.jpg'
    ]

    model = kr_models.load_model(retinanet_h5_path +
                                 'densenet121_bdd10k_18.h5',
                                 backbone_name='densenet121')
    kr_models.check_training_model(model)
    inference_model = kr_models.convert_model(model)
    labels_to_names = bu.get_label_names(bdd100k_labels_path +
                                         'class_mapping.csv')
    # load image
    image = read_image_bgr(bdd100k_val_path + test_images[2])

    # 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)

    # process image
    start = time.time()
    boxes, scores, labels = inference_model.predict_on_batch(
        np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)

    # correct for image scale
    boxes /= scale

    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        if score < 0.5:
            break

        color = label_color(label)

        b = box.astype(int)
        draw_box(draw, b, color=color)

        caption = "{} {:.3f}".format(labels_to_names[label], score)
        draw_caption(draw, b, caption)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
示例#13
0
 def freeze_model(self):
     K.clear_session()
     model1 = models.load_model(self.weight_path)
     model = models.convert_model(model1, nms=True, class_specific_filter=False,
                                  anchor_params=None)
     #model.load_weights(self.weight_path, by_name=True)
     model.summary()
     print([node.op.name for node in model.outputs])
     graph = self.freeze_session(K.get_session(), output_names=[model.output[i].op.name for i in range(len(model.outputs))])
     tf.train.write_graph(graph,self.save_folder, self.save_name,as_text=False)
     print("Done !")
def load_retina(weights: str, num_classes: int,
                anchors_wrap: AnchorParametersWrap,
                backbone_name: str) -> Model:
    """ Loads retinanet with weights. """
    ret = models.backbone(backbone_name=backbone_name).retinanet(
        num_classes=num_classes, num_anchors=anchors_wrap.num_anchors)
    ret.load_weights(weights)
    ret = models.convert_model(model=ret,
                               nms=True,
                               class_specific_filter=True,
                               anchor_params=anchors_wrap.anchors)
    return ret
def get_joint_detection_model(model_path, model_type):
    """
    Input -> Model path for the object detection model
            Model type-> Foot or Hand
    Output -> Inference model for getting the predictions on test images
    
    """
    # config_file_path = '/usr/local/bin/config'
    if model_type == 'Foot_detection':
        # with open('/usr/local/bin/src/config.ini','w') as f:
        #     f.write('[anchor_parameters]\nsizes   = 32 64 128 256 512 1024\nstrides = 8 16 32 64 128 256\nratios  = 1.2 1.5 2 2.5 3\nscales  =1 1.5 2\n')

        model, training_model, prediction_model = create_models(
        backbone_retinanet=backbone('resnet50').retinanet,
        num_classes=5,
        weights=None,
        multi_gpu=False,
        freeze_backbone=True,
        lr=1e-3,
        config=read_config_file('/usr/local/bin/Config files/config_foot.ini'))

        training_model.load_weights(model_path)
        infer_model = convert_model(training_model, anchor_params = parse_anchor_parameters(read_config_file('/usr/local/bin/Config files/config_foot.ini')))

    elif model_type == 'Hand_detection':
        # with open('/usr/local/bin/src/config.ini','w') as f:
        #     f.write('[anchor_parameters]\nsizes   = 32 64 128 256 512 1024\nstrides = 8 16 32 64 128 256\nratios  = 1 1.5 2 2.5 3\nscales  = 1 1.2 1.6\n')

        model, training_model, prediction_model = create_models(
            backbone_retinanet=backbone('resnet50').retinanet,
            num_classes=6,
            weights=None,
            multi_gpu=False,
            freeze_backbone=True,
            lr=1e-3,
            config=read_config_file('/usr/local/bin/Config files/config_hand.ini'))
        training_model.load_weights(model_path)
        infer_model = convert_model(training_model, anchor_params = parse_anchor_parameters(read_config_file('/usr/local/bin/Config files/config_hand.ini')))
    
    return infer_model
示例#16
0
def choose_inference_model(model_dir, unconverted_model):
    """
    Parameters:
       unconverted_model: h5 file to be converted to inference model (from ./snapshots/)
       model_dir: the dir containing the custom or default models
    Returns:
       converted retinanet model
    """
    keras.backend.tensorflow_backend.set_session(get_session())
    model_path = os.path.join(model_dir, unconverted_model)
    model = models.load_model(model_path, backbone_name='resnet50')
    model = models.convert_model(model)
    return model
def upload_file():
    if request.method == 'POST':
        model = models.load_model(PATH + 'resnet50_csv_06.h5')
        model = models.convert_model(model)
        f = request.files['file']
        f.save(PATH + 'imgs/' + secure_filename(f.filename))

        x = os.listdir(PATH + 'imgs')
        output_path, bbox = img_inference(PATH + '/imgs/' + x[0], model)
        pci = calculate_pci(bbox)
        op = {'FilePath': output_path, 'PCI': pci}
        os.remove(PATH + 'imgs/' + x[0])
        return jsonify(op)
示例#18
0
    def __init__(self):
        # Reading the classes and respective index from classes.json file
        self.classes = {
            value["id"] - 1: value["name"]
            for value in json.load(open("classes.json", "r")).values()
        }
        self.num_classes = 2
        self.colors_classes = [
            np.random.randint(0, 256, 3).tolist()
            for _ in range(self.num_classes)
        ]
        #Threshold on score to filter detections with (defaults to 0.05).
        self.score_threshold = 0.5
        # IoU Threshold to count for a positive detection (defaults to 0.5).
        self.iou_threshold = 0.05
        # Max Detections per image (defaults to 100).
        self.max_detections = 100
        # Setup GPU device
        self.gpu = 0
        setup_gpu(self.gpu)
        # Rescale the image so the smallest side is min_side.
        self.image_min_side = 800
        # Rescale the image if the largest side is larger than max_side.
        self.image_max_side = 1333
        # make save path if it doesn't exist
        self.save_path = "/eveluation"
        if self.save_path is not None and not os.path.exists(self.save_path):
            os.makedirs(self.save_path)

        # optionally load anchor parameters when the inference model has been generated along with training
        # Provide the path of config of file such as (self.config = "path_to_config_file")
        self.config = None
        self.anchor_params = None
        if self.config and "anchor_parameters" in self.config:
            self.anchor_params = parse_anchor_parameters(self.config)

        # Backbone Network
        self.backbone_network = "resnet50"
        self.weight_dir = "snapshots"
        # Model to be evaluated
        self.model_to_load = os.path.join(self.weight_dir,
                                          "resnet50_csv_17.h5")
        # Convert the trained model to ind=ference model
        self.convert_model = True

        # load the model
        print("Loading model, this may take a second...")
        self.model = models.load_model(self.model_to_load,
                                       backbone_name=self.backbone_network)
        self.model = models.convert_model(self.model,
                                          anchor_params=self.anchor_params)
示例#19
0
def init():
    session = get_session()
    keras.backend.tensorflow_backend.set_session(session)
    CLASSES_FILE = './files/classes.csv'
    model_path = './files/R50_20_02_04_15_40.h5'

    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')
    model = models.convert_model(model)

    # load label to names mapping for visualization purposes
    labels_to_names = pandas.read_csv(CLASSES_FILE,
                                      header=None).T.loc[0].to_dict()
    return model, session
def load_retinanet():
    # caching.clear_cache()
    model_path = 'output/models/inference/plate_inference_tf2.h5'

    # load retinanet model
    print("Loading Model: {}".format(model_path))
    with st.spinner("Loading retinanet weights!"):
        model = models.load_model(model_path, backbone_name='resnet50')

    #Check that it's been converted to an inference model
    try:
        model = models.convert_model(model)
    except:
        print("Model is likely already an inference model")

    return model
示例#21
0
    def __init__(self, weights=None, saved_model=None):
        self.weights = weights
        self.saved_model = saved_model

        # Read config file - if a config file exists in local dir use it,
        # if not use installed.
        if os.path.exists("deepforest_config.yml"):
            config_path = "deepforest_config.yml"
        else:
            try:
                config_path = get_data("deepforest_config.yml")
            except Exception as e:
                raise ValueError(
                    "No deepforest_config.yml found either in local "
                    "directory or in installed package location. {}".format(e))

        print("Reading config file: {}".format(config_path))
        self.config = utilities.read_config(config_path)

        # Create a label dict, defaults to "Tree"
        self.read_classes()

        # release version id to flag if release is being used
        self.__release_version__ = None

        # Load saved model if needed
        if self.saved_model:
            print("Loading saved model")
            # Capture user warning, not relevant here
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                self.model = models.load_model(saved_model)
                self.prediction_model = convert_model(self.model)

        elif self.weights:
            print("Creating model from weights")
            backbone = models.backbone(self.config["backbone"])
            self.model, self.training_model, self.prediction_model = create_models(
                backbone.retinanet, num_classes=1, weights=self.weights)
        else:
            print(
                "A blank deepforest object created. "
                "To perform prediction, either train or load an existing model."
            )
            self.model = None
示例#22
0
    def train(self, annotations, comet_experiment=None):
        '''Train a deep learning tree detection model using keras-retinanet
        This is the main entry point for training a new model based on either existing weights or scratch
        
        Args:
            annotations (str): Path to csv label file, labels are in the format -> path/to/image.jpg,x1,y1,x2,y2,class_name
            comet_experiment: A comet ml object to log images. Optional.
        Returns:
            model (object): A trained keras model
        '''
        arg_list = utilities.format_args(annotations, self.config)

        print("Training retinanet with the following args {}".format(arg_list))

        #Train model
        self.training_model = retinanet_train(arg_list, comet_experiment)

        #Create prediction model
        self.prediction_model = convert_model(self.training_model)
示例#23
0
def evaluate_class_thresholds(model, generator):
    '''
    Evaluates the model using testing data
    printing out an F1 score as well as optimal confidence thresholds for each concept
    '''

    # Initializing model for eval
    model = convert_model(model)

    best_f1, best_thresh = f1_evaluation(
        generator, model, save_path=config.TEST_EXAMPLES)

    total_f1 = 0
    for concept, f1 in best_f1.items():
        # TODO: put this into the DB
        # print("Concept: " + concept)
        # print("F1 Score: " + str(f1))
        # print("Confidence Threshold: " + str(best_thresh[concept]))
        # print("")
        total_f1 += f1
示例#24
0
def predict():
    if request.method == 'POST':
        model = models.load_model('{}/resnet50_csv_06.h5'.format(PATH))
        model = models.convert_model(model)
        payload = request.get_json()
        print(payload)
        imgUrl = payload['imageUrl']
        response = requests.get(imgUrl, stream=True)
        if response.status_code == 200:
            file = open("{}/imgs/sample_png.png".format(PATH), "wb")
            file.write(response.content)
            file.close()
            # f = request.files['file']
            # f.save('{}/imgs/'.format(PATH)+(f.filename))
            path_img, bbox = img_inference(
                '{}/imgs/sample_png.png'.format(PATH), model)
            pipeshelf = Cloud.uploader.unsigned_upload(path_img, upload_preset)
            print(pipeshelf['url'])
            pci = calculate_pci(bbox)
            return jsonify({'damageImageUrl': pipeshelf['url'], 'pci': pci})
示例#25
0
def get_model_dicts():
    """ define a dictionary of trained models with their names and weight file names """

    models_gcv = collections.defaultdict(dict)
    models_gcv['SSD']['model'] = 'ssd_512_resnet50_v1_voc'
    models_gcv['SSD'][
        'weights'] = '../models_for_ensemble/epoch_22_ssd_512_resnet50_v1_voc_mio_tcd.params'
    models_gcv['FASTER-RCNN']['model'] = 'faster_rcnn_resnet50_v1b_voc'
    models_gcv['FASTER-RCNN'][
        'weights'] = '../models_for_ensemble/faster_rcnn_resnet50_v1b_voc_0061_0.7285.params'
    models_gcv['SSD-EXPERT']['model'] = 'ssd_512_resnet50_v1_voc'
    models_gcv['SSD-EXPERT'][
        'weights'] = '../models_for_ensemble/epoch_29_ssd_512_resnet50_v1_voc_mio_tcd.params'
    models_gcv = dict(models_gcv)
    model_retina_weights = '../models_for_ensemble/retina_weights.70-0.76.hdf5'
    model_retina = models.load_model(model_retina_weights,
                                     backbone_name='resnet50')
    model_retina = models.convert_model(model_retina)

    return models_gcv, model_retina
示例#26
0
def evaluate_model(concepts, model_path,  min_examples, download_data=False):

    classmap = get_classmap(concepts)

    if download_data:
        folders = []
        folders.append(test_examples)
        for dir in folders:
            if os.path.exists(dir):
                shutil.rmtree(dir)
            os.makedirs(dir)
        download_annotations(min_examples, concepts, classmap, good_users, img_folder, train_annot_file, valid_annot_file, split=0)

    '''
    Initializing model for eval
    '''
    model = load_model(model_path, backbone_name='resnet50')
    model = convert_model(model)

    temp = pd.DataFrame(list(zip(classmap.values(), classmap.keys())))
    temp.to_csv('classmap.csv',index=False, header=False)
    test_generator = CSVGenerator(
        valid_annot_file,
        'classmap.csv',
        shuffle_groups=False,
        batch_size=batch_size
    )

    best_f1, best_thresh = f1_evaluation(test_generator, model, save_path=test_examples)

    total_f1 = 0
    for concept, f1 in best_f1.items():
        print("Concept: " + classmap[concept])
        print("F1 Score: " + str(f1))
        print("Confidence Threshold: " + str(best_thresh[concept]))
        print("")
        total_f1 += f1

    print("Average F1: " + str(total_f1/len(best_f1)))
    print("Find evaluation examples in: " + test_examples)
    '''
示例#27
0
def build_model(model_file, add_filtering=True, max_detections=2000, nms_threshold=0.6, score_threshold=0.05, training_model=False, filtering_layer_included=False):
    model_path = os.path.join('..', 'snapshots', model_file)
    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')

    if training_model:
        model = models.convert_model(model)
    #print(model.summary())

    if filtering_layer_included:
        print("model already contains filtered layer")
        boxes, classification = model.layers[-3].output, model.layers[-2].output
    else:
        boxes, classification = model.layers[-2].output, model.layers[-1].output

    if add_filtering:
        #boxes, classification = model.layers[-3].output, model.layers[-2].output
        #other = model.layers[-2].output
        print("Adding modified filtering layer...")
        detections = layers.FilterDetections(
                nms                   = True,
                class_specific_filter = True,
                name                  = 'filtered_detections',
                nms_threshold         = nms_threshold,
                score_threshold       = score_threshold,
                max_detections        = max_detections,
                parallel_iterations   = 32
            ) ([boxes, classification])
       

        # define model with newly defined filtering layer
        model1 = Model(model.input, detections)
    else:
        # get classification and regression layers and build new model 
        model1 = Model(model.input, outputs=[boxes, classification])

    #print(model1.summary())

    return model1
def main(args=None):
    from keras_retinanet.utils.config import parse_anchor_parameters
    from keras.utils import custom_object_scope

    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    anchor_params = None
    if 0:
        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 = parse_anchor_parameters(config)

    # load and convert model
    with custom_object_scope({
            'AdamAccumulate': AdamAccumulate,
            'AccumOptimizer': Adam
    }):
        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,
            max_detections=500,
            nms_threshold=0.3,
            score_threshold=0.01,
            anchor_params=anchor_params)

    # save model
    model.save(args.model_out)
示例#29
0
def main():
    args = get_arguments()
    if args.predictions is None:
        args.predictions = 'predictions.csv'
    # print(args.model)
    # print(args.directory)
    # print(args.predictions)
    # print(args.classes)

    #loading model
    model = models.load_model(args.model, backbone_name='resnet50')
    #ADD THIS LINE TO CONVERT TO INFERENCE MODEL
    model = models.convert_model(model)

    #loading class names
    labels_to_names = load_classes(args.classes)
    #loading filenames
    filenames = os.listdir(args.directory)

    create_score_list(model=model,
                      filenames=filenames,
                      image_dir=args.directory,
                      save=args.predictions,
                      labels_to_names=labels_to_names)
示例#30
0
def solar_detection(images_path=''):
    DATASET_DIR = 'dataset'
    ANNOTATIONS_FILE = 'annotations.csv'
    CLASSES_FILE = 'classes.csv'

    keras.backend.tensorflow_backend.set_session(get_session())
    #load model
    model_path = './resnet50_csv_10.h5'
    print(model_path)
    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')
    model = models.convert_model(model)

    # load label to names mapping for visualization purposes
    labels_to_names = pandas.read_csv(CLASSES_FILE,
                                      header=None).T.loc[0].to_dict()

    list_dir = os.listdir('./')
    if 'anh' in list_dir:
        shutil.rmtree('anh')
    sliding_window(images_path)
    os.mkdir('anh')

    path_file = './anh/'
    list_bb = {}
    for img in os.listdir(path_file):
        A = img.split('.')
        pos_window = (int(A[0]), int(A[1]))
        path_img = os.path.join(path_file, img)
        id = A[0] + '.' + A[1]
        print(id)
        bb = img_inference(path_img, pos_window, model, labels_to_names)
        # print(bb)
        list_bb[id] = bb
    with open('./list_bb.json', 'w') as f_w:
        json.dump(list_bb, f_w)