Python keras_retinanet.models.load_model() Examples

The following are 6 code examples of keras_retinanet.models.load_model(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras_retinanet.models , or try the search function .
Example #1
Source File: detector.py    From NudeNet with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self):
        '''
            model = Detector()
        '''
        url = 'https://github.com/bedapudi6788/NudeNet/releases/download/v0/detector_model'
        home = os.path.expanduser("~")
        model_folder = os.path.join(home, '.NudeNet/')
        if not os.path.exists(model_folder):
            os.mkdir(model_folder)
        
        model_path = os.path.join(model_folder, 'detector')

        if not os.path.exists(model_path):
            print('Downloading the checkpoint to', model_path)
            pydload.dload(url, save_to_path=model_path, max_time=None)

        Detector.detection_model = models.load_model(model_path, backbone_name='resnet101') 
Example #2
Source File: testScoreWithAdapaRetinaNet.py    From nyoka with Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        print("******* Unit Test for RetinaNet *******")
        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)

        classes = json.load(open("nyoka/tests/categories_coco.json",'r'))
        cls.classes = list(classes.values())
        cls.adapa_utility = AdapaUtility()
        cls.model = load_model('resnet50_coco_best_v2.1.0.h5', backbone_name='resnet50') 
Example #3
Source File: test_retinanet_to_pmml_UnitTest.py    From nyoka with Apache License 2.0 5 votes vote down vote up
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') 
Example #4
Source File: aae_retina_pose_estimator.py    From AugmentedAutoencoder with MIT License 4 votes vote down vote up
def _load_model_with_nms(self, test_args):
        """ This is mostly copied fomr retinanet.py """

        backbone_name = test_args.get('DETECTOR','backbone')
        print backbone_name
        print test_args.get('DETECTOR','detector_model_path')
        model = keras.models.load_model(
                str(test_args.get('DETECTOR','detector_model_path')),
                custom_objects=backbone(backbone_name).custom_objects
                )

        # compute the anchors
        features = [model.get_layer(name).output
                for name in ['P3', 'P4', 'P5', 'P6', 'P7']]
        anchors  = build_anchors(AnchorParameters.default, features)

        # we expect the anchors, regression and classification values as first
        # output
        print len(model.outputs)
        regression     = model.outputs[0]
        classification = model.outputs[1]
        print classification.shape[1]
        print regression.shape

        # "other" can be any additional output from custom submodels,
        # by default this will be []
        other = model.outputs[2:]

        # apply predicted regression to anchors
        boxes = layers.RegressBoxes(name='boxes')([anchors, regression])
        boxes = layers.ClipBoxes(name='clipped_boxes')([model.inputs[0], boxes])

        # filter detections (apply NMS / score threshold / select top-k)
        #detections = layers.FilterDetections(
        #        nms=True,
        #        name='filtered_detections',
        #        nms_threshold = test_args.getfloat('DETECTOR','nms_threshold'),
        #        score_threshold = test_args.getfloat('DETECTOR','det_threshold'),
        #        max_detections = test_args.getint('DETECTOR', 'max_detections')
        #        )([boxes, classification] + other)        
        detections = layers.filter_detections.filter_detections(
                boxes=boxes,
                classification=classification,
                other=other,
                nms=True,
                nms_threshold = test_args.getfloat('DETECTOR','nms_threshold'),
                score_threshold = test_args.getfloat('DETECTOR','det_threshold'),
                max_detections = test_args.getint('DETECTOR', 'max_detections')
                )

        outputs = detections

        # construct the model
        return keras.models.Model(
                inputs=model.inputs, outputs=outputs, name='retinanet-bbox') 
Example #5
Source File: train_keras_retinanet.py    From 3d-dl with MIT License 4 votes vote down vote up
def on_epoch_end(self, epoch):
        # load this epoch's saved snapshot
        model_path = '{backbone}_{dataset_type}_{epoch:02d}.h5'.format(
            backbone=self.snapshot_data['backbone'], dataset_type=self.snapshot_data['dataset_type'], epoch=(epoch+1))
        model_path = os.path.join(self.snapshot_data['path'], model_path)
        print('loading model {}, this may take a while ... '.format(model_path))
        self.model = models.load_model(model_path, convert=True, backbone_name=self.snapshot_data['backbone'], nms=False)
        
        # run a detection as classification on the model and our test dataset
        TP, FP = detection_as_classification(self.model, self.test_generator)
        precision = float(TP)/(len(self.test_generator)*self.batch_size)
        if TP+FP == 0:
            recall = -1
        else:
            recall = float(TP)/(TP+FP)

        my_file = Path(self.log_filename)

        # write header if this is the first run
        if not my_file.is_file():
            print("writing head")
            with open(self.log_filename, "w") as log:
                log.write("datetime,epoch,precision,recall\n")

        # append parameters
        with open(self.log_filename, "a") as log:
            log.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
            log.write(',')
            log.write(str(epoch))
            log.write(',')
            log.write(str(precision))
            log.write(',')
            log.write(str(recall))
            log.write('\n')

        print('\nValidation set at {}:'.format(self.test_data_dir))
        print('Precision: {}% , Recall: {}% \n'.format(precision*100, recall*100))

        # remove snapshots, but save the last one
        if (not epoch >= self.num_epochs-1) and self.delete_model:
            os.remove(model_path)
        # make sure we don't run out of memory!
        del self.model
        gc.collect() 
Example #6
Source File: train_keras_retinanet.py    From 3d-dl with MIT License 4 votes vote down vote up
def on_epoch_end(self, epoch):
        # load this epoch's saved snapshot
        model_path = '{backbone}_{dataset_type}_{epoch:02d}.h5'.format(
            backbone=self.snapshot_data['backbone'], dataset_type=self.snapshot_data['dataset_type'], epoch=(epoch+1))
        model_path = os.path.join(self.snapshot_data['path'], model_path)
        print('loading model {}, this may take a while ... '.format(model_path))
        self.model = models.load_model(model_path, convert=True, backbone_name=self.snapshot_data['backbone'], nms=False)
        
        # run a detection as classification on the model and our test dataset
        TP, FP = detection_as_classification(self.model, self.test_generator)
        precision = float(TP)/(len(self.test_generator)*self.batch_size)
        if TP+FP == 0:
            recall = -1
        else:
            recall = float(TP)/(TP+FP)

        my_file = Path(self.log_filename)

        # write header if this is the first run
        if not my_file.is_file():
            print("writing head")
            with open(self.log_filename, "w") as log:
                log.write("datetime,epoch,precision,recall\n")

        # append parameters
        with open(self.log_filename, "a") as log:
            log.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
            log.write(',')
            log.write(str(epoch))
            log.write(',')
            log.write(str(precision))
            log.write(',')
            log.write(str(recall))
            log.write('\n')

        print('\nValidation set at {}:'.format(self.test_data_dir))
        print('Precision: {}% , Recall: {}% \n'.format(precision*100, recall*100))

        # remove snapshots, but save the last one
        if (not epoch >= self.num_epochs-1) and self.delete_model:
            os.remove(model_path)
        # make sure we don't run out of memory!
        del self.model
        gc.collect()