Beispiel #1
0
    def get_eval_model(self):
        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        #YOLOv3 model has 9 anchors and 3 feature layers but
        #Tiny YOLOv3 model has 6 anchors and 2 feature layers,
        #so we can calculate feature layers number to get model type
        num_feature_layers = num_anchors // 3

        if num_anchors == 5:
            # YOLOv2 use 5 anchors
            eval_model, _ = get_yolo2_model(self.model_type,
                                            num_anchors,
                                            num_classes,
                                            input_shape=self.model_image_size +
                                            (3, ),
                                            model_pruning=self.model_pruning)
        else:
            eval_model, _ = get_yolo3_model(self.model_type,
                                            num_feature_layers,
                                            num_anchors,
                                            num_classes,
                                            input_shape=self.model_image_size +
                                            (3, ),
                                            model_pruning=self.model_pruning)

        return eval_model
Beispiel #2
0
    def _generate_model(self):
        '''to generate the bounding boxes'''
        weights_path = os.path.expanduser(self.weights_path)
        assert weights_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        #YOLOv3 model has 9 anchors and 3 feature layers but
        #Tiny YOLOv3 model has 6 anchors and 2 feature layers,
        #so we can calculate feature layers number to get model type
        num_feature_layers = num_anchors//3

        try:
            if num_anchors == 5:
                # YOLOv2 use 5 anchors
                yolo_model, _ = get_yolo2_model(self.model_type, num_anchors, num_classes, input_shape=self.model_image_size + (3,), model_pruning=self.pruning_model)
            else:
                yolo_model, _ = get_yolo3_model(self.model_type, num_feature_layers, num_anchors, num_classes, input_shape=self.model_image_size + (3,), model_pruning=self.pruning_model)
            yolo_model.load_weights(weights_path) # make sure model, anchors and classes match
            if self.pruning_model:
                yolo_model = sparsity.strip_pruning(yolo_model)
            yolo_model.summary()
        except Exception as e:
            print(repr(e))
            assert yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'
        print('{} model, anchors, and classes loaded.'.format(weights_path))
        if self.gpu_num>=2:
            yolo_model = multi_gpu_model(yolo_model, gpus=self.gpu_num)

        return yolo_model
Beispiel #3
0
    def _generate_model(self):
        '''to generate the bounding boxes'''
        weights_path = os.path.expanduser(self.weights_path)
        assert weights_path.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        #YOLOv3 model has 9 anchors and 3 feature layers but
        #Tiny YOLOv3 model has 6 anchors and 2 feature layers,
        #so we can calculate feature layers number to get model type
        num_feature_layers = num_anchors // 3

        yolo_model, _ = get_yolo3_model(self.model_type,
                                        num_feature_layers,
                                        num_anchors,
                                        num_classes,
                                        input_shape=self.model_image_size +
                                        (3, ),
                                        model_pruning=self.pruning_model)

        yolo_model.load_weights(
            weights_path)  # make sure model, anchors and classes match
        #if self.pruning_model:
        #    yolo_model = sparsity.strip_pruning(yolo_model)
        yolo_model.summary()

        print('{} model, anchors, and classes loaded.'.format(weights_path))

        return yolo_model
    def get_eval_model(self):
        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        #YOLOv3 model has 9 anchors and 3 feature layers but
        #Tiny YOLOv3 model has 6 anchors and 2 feature layers,
        #so we can calculate feature layers number to get model type
        num_feature_layers = num_anchors // 3

        if self.model_type.startswith(
                'scaled_yolo4_') or self.model_type.startswith('yolo5_'):
            # Scaled-YOLOv4 & YOLOv5 entrance
            eval_model, _ = get_yolo5_model(self.model_type,
                                            num_feature_layers,
                                            num_anchors,
                                            num_classes,
                                            input_shape=self.model_image_size +
                                            (3, ),
                                            model_pruning=self.model_pruning)
            self.v5_decode = True
        elif self.model_type.startswith('yolo3_') or self.model_type.startswith('yolo4_') or \
             self.model_type.startswith('tiny_yolo3_') or self.model_type.startswith('tiny_yolo4_'):
            # YOLOv3 & v4 entrance
            eval_model, _ = get_yolo3_model(self.model_type,
                                            num_feature_layers,
                                            num_anchors,
                                            num_classes,
                                            input_shape=self.model_image_size +
                                            (3, ),
                                            model_pruning=self.model_pruning)
            self.v5_decode = False
        elif self.model_type.startswith(
                'yolo2_') or self.model_type.startswith('tiny_yolo2_'):
            # YOLOv2 entrance
            eval_model, _ = get_yolo2_model(self.model_type,
                                            num_anchors,
                                            num_classes,
                                            input_shape=self.model_image_size +
                                            (3, ),
                                            model_pruning=self.model_pruning)
            self.v5_decode = False
        else:
            raise ValueError('Unsupported model type')

        return eval_model
Beispiel #5
0
anchors_path = 'configs/tiny_yolo_anchors.txt'
classes_path = 'yolo_format_training_classes.txt'
model_image_size = (416, 416)
gpu_num = 1

# Load Classes and get anchors for comparision
class_names = get_classes(classes_path)
anchors = get_anchors(anchors_path)
colors = get_colors(class_names)

num_anchors = len(anchors)
num_classes = len(class_names)
num_feature_layers = num_anchors//3

# Initialise the model with same architecture and load the trained model provided in the config
yolo_model, _ = get_yolo3_model(model_type, num_feature_layers, num_anchors, num_classes, input_shape=model_image_size + (3,))
yolo_model.load_weights(model_path) # make sure model, anchors and classes match
print('{} model, anchors, and classes loaded.'.format(model_path))

# Define the Prediction with config parameters
def predict(image_data, image_shape):
    
    out_boxes, out_classes, out_scores = yolo3_postprocess_np(yolo_model.predict(image_data), 
                                                              image_shape, 
                                                              anchors,
                                                              len(class_names), 
                                                              model_image_size, 
                                                              max_boxes=100, confidence = 0.3, iou_threshold=0.4)
    
    return out_boxes, out_classes, out_scores
Beispiel #6
0
    def _generate_model(self):
        '''to generate the bounding boxes'''
        weights_path = None
        #assert weights_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        #YOLOv3 model has 9 anchors and 3 feature layers but
        #Tiny YOLOv3 model has 6 anchors and 2 feature layers,
        #so we can calculate feature layers number to get model type
        num_feature_layers = num_anchors // 3

        try:
            if self.model_type.startswith(
                    'scaled_yolo4_') or self.model_type.startswith('yolo5_'):
                # Scaled-YOLOv4 & YOLOv5 entrance
                yolo_model, _ = get_yolo5_model(
                    self.model_type,
                    num_feature_layers,
                    num_anchors,
                    num_classes,
                    input_shape=self.model_image_size + (3, ),
                    model_pruning=self.pruning_model)
            elif self.model_type.startswith('yolo3_') or self.model_type.startswith('yolo4_') or \
                 self.model_type.startswith('tiny_yolo3_') or self.model_type.startswith('tiny_yolo4_'):
                # YOLOv3 & v4 entrance
                yolo_model, _ = get_yolo3_model(
                    self.model_type,
                    num_feature_layers,
                    num_anchors,
                    num_classes,
                    input_shape=self.model_image_size + (3, ),
                    model_pruning=self.pruning_model)
            elif self.model_type.startswith(
                    'yolo2_') or self.model_type.startswith('tiny_yolo2_'):
                # YOLOv2 entrance
                yolo_model, _ = get_yolo2_model(
                    self.model_type,
                    num_anchors,
                    num_classes,
                    input_shape=self.model_image_size + (3, ),
                    model_pruning=self.pruning_model)
            else:
                raise ValueError('Unsupported model type')

            yolo_model.load_weights(
                weights_path)  # make sure model, anchors and classes match
            if self.pruning_model:
                yolo_model = sparsity.strip_pruning(yolo_model)
            yolo_model.summary()
        except Exception as e:
            print(repr(e))
            assert yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'
        print('{} model, anchors, and classes loaded.'.format(weights_path))
        # tf2.4와의 호환을 위해 아래를 삭제.
        '''
        if self.gpu_num>=2:
            yolo_model = multi_gpu_model(yolo_model, gpus=self.gpu_num)
        '''
        return yolo_model