def create_tiny_model( input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2, weights_path="keras_yolo3/model_data/tiny_yolo_weights.h5", ): """create the training model, for Tiny YOLOv3""" K.clear_session() # get a new session image_input = Input(shape=(None, None, 3)) h, w = input_shape num_anchors = len(anchors) y_true = [ Input( shape=( h // {0: 32, 1: 16}[l], w // {0: 32, 1: 16}[l], num_anchors // 2, num_classes + 5, ) ) for l in range(2) ] model_body = tiny_yolo_body(image_input, num_anchors // 2, num_classes) print( "Create Tiny YOLOv3 model with {} anchors and {} classes.".format( num_anchors, num_classes ) ) if load_pretrained: model_body.load_weights(weights_path, by_name=True, skip_mismatch=True) print("Load weights {}.".format(weights_path)) if freeze_body in [1, 2]: # Freeze the darknet body or freeze all but 2 output layers. num = (20, len(model_body.layers) - 2)[freeze_body - 1] for i in range(num): model_body.layers[i].trainable = False print( "Freeze the first {} layers of total {} layers.".format( num, len(model_body.layers) ) ) model_loss = Lambda( yolo_loss, output_shape=(1,), name="yolo_loss", arguments={ "anchors": anchors, "num_classes": num_classes, "ignore_thresh": 0.7, }, )([*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model
def create_model(is_tiny, input_shape, anchors, n_classes, load_pretrained=True, freeze_body=2, weights_path='model/keras_yolo3/model_data/yolo_weights.h5'): K.clear_session() # get a new session img = Input(shape=(None, None, 3)) h, w = input_shape n_anchors = len(anchors) if not is_tiny: y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \ n_anchors//3, n_classes+5)) for l in range(3)] model_body = yolo_body(img, n_anchors // 3, n_classes) ignore_thresh = 0.5 else: # 'tiny' y_true = [Input(shape=(h//{0:32, 1:16}[l], w//{0:32, 1:16}[l],\ n_anchors//2, n_classes+5)) for l in range(2)] model_body = tiny_yolo_body(img, n_anchors // 2, n_classes) ignore_thresh = 0.7 print( f'Create YOLOv3 model with {n_anchors} anchors and {n_classes} classes' ) if load_pretrained: model_body.load_weights(weights_path, by_name=True, skip_mismatch=True) print(f'Load weights from {weights_path}') if freeze_body in [1, 2]: # Freeze darknet53 body or freeze all but 3 output layers. if not is_tiny: num = (185, len(model_body.layers) - 3)[freeze_body - 1] else: num = (20, len(model_body.layers) - 2)[freeze_body - 1] for i in range(num): model_body.layers[i].trainable = False print( f'Freeze the first {num} layers of total {model_body.layers} layers.' ) model_loss = Lambda(yolo_loss, output_shape=(1, ), name='yolo_loss', arguments={ 'anchors': anchors, 'num_classes': n_classes, 'ignore_thresh': ignore_thresh })([*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model
def generate(self): model_path = self.model_path assert model_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) is_tiny_version = num_anchors == 6 # default setting try: self.yolo_model = load_model(model_path, compile=False) except: self.yolo_model = tiny_yolo_body(Input(shape=(None, None, 3)), num_anchors // 2, num_classes) \ if is_tiny_version else yolo_body(Input(shape=(None, None, 3)), num_anchors // 3, num_classes) self.yolo_model.load_weights( self.model_path) # make sure model, anchors and classes match else: assert self.yolo_model.layers[-1].output_shape[-1] == \ num_anchors / len(self.yolo_model.output) * (num_classes + 5), \ 'Mismatch between model and given anchor and class sizes' print('{} model, anchors, and classes loaded.'.format(model_path)) # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) np.random.seed(10101) # Fixed seed for consistent colors across runs. np.random.shuffle( self.colors) # Shuffle colors to decorrelate adjacent classes. np.random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. self.input_image_shape = K.placeholder(shape=(2, )) if self.gpu_num >= 2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num) boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes