def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2, weights_path='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 generate(self): model_path = os.path.expanduser(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)) print('OK!') # 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) print('Ok!') return boxes, scores, classes
def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2, weights_path='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_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, weights_path='model_data/tiny_yolo_weights.h5'): '''create the training model, for Tiny YOLOv3''' K.clear_session() # get a new session h, w = input_shape image_input = Input(shape=(h, w, 3)) 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: yolo_weight = load_model(weights_path).get_weights() for i, w in enumerate(yolo_weight): if w.shape == (1, 1, 1024, 255): yolo_weight[i] = w[..., :(num_anchors // 2) * (num_classes + 5)] if w.shape == (1, 1, 512, 255): yolo_weight[i] = w[..., :(num_anchors // 2) * (num_classes + 5)] if w.shape == (1, 1, 256, 255): yolo_weight[i] = w[..., :(num_anchors // 2) * (num_classes + 5)] if w.shape == (255,): yolo_weight[i] = w[:(num_anchors // 2) * (num_classes + 5)] model_body.set_weights(yolo_weight) print('Load weights {}.'.format(weights_path)) # freeze_body = 2 # 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, 'print_loss': True})( [*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=True, weights_path='model_data/yolo_weights.h5'): 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, 2: 8}[l], w // {0: 32, 1: 16, 2: 8}[l], \ num_anchors // 3, num_classes + 5)) for l in range(3)] model_body = tiny_yolo_body(image_input, num_anchors // 3, num_classes) print('Create 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: # Do not freeze 3 output layers. #20181002 0100 修改 laster three layer are conv2D 因此从-7 改为-3 num = len(model_body.layers) - 3 print(num) 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.5 })([*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model
def generate(self): model_path = os.path.expanduser(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 gpu_num>=2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=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
def generate(self): # 准备已经训练好的模型 model_path = os.path.expanduser(self.model_path) assert model_path.endswith('.h5'), 'Keras model or weights must b a .h5 file.' # 加载已经训练好的模型,如果加载失败,则创建模型,并加载训练好的weights num_anchors = len(self.anchors) num_classes = len(self.class_names) is_tiny_version = num_anchors==6 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) 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)) # 为描绘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) np.random.shuffle(self.colors) np.random.seed(None) # 输入值的占位 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) # 调用yolo_eval,计算出boxes,scores,classes 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
def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=True): '''create the training model, for Tiny YOLOv3''' 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) if load_pretrained: weights_path = os.path.join('model_data/', 'tiny_yolo_weights.h5') if not os.path.exists(weights_path): print("CREATING WEIGHTS FILE" + weights_path) yolo_path = os.path.join('model_data', 'tiny_yolo.h5') orig_model = load_model(yolo_path, compile=False) orig_model.save_weights(weights_path) model_body.load_weights(weights_path, by_name=True, skip_mismatch=True) if freeze_body: # Do not freeze 2 output layers. for i in range(len(model_body.layers) - 2): model_body.layers[i].trainable = False 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 generate(self): model_path = os.path.expanduser(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))
def generate(self): model_path = os.path.expanduser(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: print(self.yolo_model.layers[-1].output_shape[-1]) print(num_anchors) print(len(self.yolo_model.output)) print(num_classes) 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 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
def load_model(self): model_path = self._get_data_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' input_image_shape = keras.Input(shape=(2, ), name='image_shape') image_input = keras.Input((None, None, 3), dtype='float32') y1, y2, y3 = self.yolo_model(image_input) boxes, box_scores = \ YOLOEvaluationLayer(anchors=self.anchors, num_classes=len(self.class_names))( inputs=[y1, y2, y3, input_image_shape]) out_boxes, out_scores, out_indices = \ YOLONMSLayer(anchors=self.anchors, num_classes=len(self.class_names))( inputs=[boxes, box_scores]) self.final_model = keras.Model( inputs=[image_input, input_image_shape], outputs=[out_boxes, out_scores, out_indices]) self.final_model.save('model_data/final_model.h5') print('{} model, anchors, and classes loaded.'.format(model_path))
def modify_backprop(model, name, image_input, num_anchors, num_classes): g = tf.get_default_graph() # override the gradient function with g.gradient_override_map({'Relu': name}): # get layers that have an activation layer_dict = [ layer for layer in model.layers[1:] if hasattr(layer, 'activation') ] # replace relu activation for layer in layer_dict: layer.activation = tf.nn.relu # if layer.activation == keras.activations.relu: # layer.activation = tf.nn.relu # re-instanciate a new model new_model = tiny_yolo_body(image_input, num_anchors // 2, num_classes) try: new_model.load_weights(weights_load_path) except: print("Impossible to find weight path. Returning untrained model") return new_model
def load(model_path, anchors_path, class_path): """Load the trained model. Args: model_path: absolute path to the model. anchors_path: the absolute path of the file which contains all the anchors used for the network. class_path: the absolute path of the file which contains the name of categories. Returns: The loaded yolo/tiny-yolo model. """ class_names = get_classes(class_path) anchors = get_anchors(anchors_path) model_path = os.path.expanduser(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(anchors) num_classes = len(class_names) is_tiny_version = num_anchors==6 # default setting try: yolo_model = load_model(model_path, compile=False) except: 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) yolo_model.load_weights(model_path) # make sure model, anchors and classes match else: 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' input_image_shape = tf.constant([416, 416], shape=(2,)) boxes, scores, classes = yolo_eval(yolo_model.output, anchors, len(class_names), input_image_shape, score_threshold=0.3, iou_threshold=0.45) print('{} model, anchors, and classes loaded.'.format(model_path)) return yolo_model, is_tiny_version
def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2, weights_path='model_data/tiny_yolo_weights.h5', model_name=None, num_yolo_heads=None): '''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) if model_name in ['tiny_yolo_infusion', 'tiny_yolo_infusion_hydra']: y_true_input = [ 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) ] #old style if model_name == 'tiny_yolo_infusion_hydra': #old style # y_true_input += [Input(shape=(None, None, 2)), Input(shape=(None, None, 2))] # model_body = tiny_yolo_infusion_hydra_body(image_input, num_anchors//2, num_classes) #new style #keep commented. model_body, connection_layer = tiny_yolo_infusion_hydra_body( image_input, num_anchors // 2, num_classes) elif model_name == 'tiny_yolo_infusion': #old style # y_true_input.append(Input(shape=(None, None, 2)))#add segmentation y input. #model_body = tiny_yolo_infusion_body(image_input, num_anchors//2, num_classes) #new style model_body, connection_layer = tiny_yolo_infusion_body( image_input, num_anchors // 2, num_classes) #new style: keep commented. print( 'Create Tiny YOLOv3 INFUSION model with {} anchors and {} classes.' .format(num_anchors, num_classes)) if load_pretrained: # raise Exception('freezing requires review.') 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))) ''' def yolo_loss(args, anchors, num_classes, ignore_thresh=.5, print_loss=False): ... return loss ''' print('*model_body.output', *model_body.output) print('*model_body.input', model_body.input) print('*y_true_input', *y_true_input) ''' Checking Lambda [ <tf.Tensor 'yolo_head_a_output/BiasAdd:0' shape=(?, ?, ?, 18) dtype=float32>, <tf.Tensor 'yolo_head_b_output/BiasAdd:0' shape=(?, ?, ?, 18) dtype=float32>, <tf.Tensor 'seg_output/LeakyRelu/Maximum:0' shape=(?, ?, ?, 2) dtype=float32>, <tf.Tensor 'input_2:0' shape=(?, 13, 13, 3, 6) dtype=float32>, <tf.Tensor 'input_3:0' shape=(?, 26, 26, 3, 6) dtype=float32>, <tf.Tensor 'input_4:0' shape=(?, ?, ?, 2) dtype=float32>] ''' default_output = Lambda( yolo_loss, output_shape=(1, ), name='yolo_loss', arguments={ 'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.7, 'model_name': model_name, 'num_yolo_heads': num_yolo_heads, 'print_loss': False })([*model_body.output, *y_true_input ]) #this is calling yolo_loss and these are the args. # model_body.output is the last layer output tensor. #old style # model = Model([model_body.input, *y_true_input], default_output) ''' model_body.input = image_input = Input(shape=(None, None, 3)) *y_true_input = input_y_true_layer1, input_y_true_layer2, ... default_output = yolo_loss ''' #new style seg_output = infusion_layer(connection_layer) #[model_body.input, *y_true_input] -> [images, y_layer_1, y_layer_2] model = Model([model_body.input, *y_true_input], outputs=[default_output, seg_output]) return model elif model_name in ['tiny_yolo', 'tiny_yolo_small_objs']: if model_name == 'tiny_yolo_small_objs': y_true = [Input(shape=(h//{0:32, 1:8}[l], w//{0:32, 1:8}[l], \ num_anchors//2, num_classes+5)) for l in range(2)] model_body = tiny_yolo_small_objs_body(image_input, num_anchors // 2, num_classes) else: 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, 'num_yolo_heads': num_yolo_heads, 'model_name': model_name })([*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model else: raise Exception('unknown model.')
def generate(self): if self.model_path: model_path = os.path.expanduser(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 = True if self.model_name in [ 'tiny_yolo', 'tiny_yolo_infusion' ] else False if self.model_name == 'tiny_yolo_infusion': print('Loading model weights', self.model_path) #old style # self.yolo_model = tiny_yolo_infusion_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) ## self.yolo_model.load_weights(self.model_path, by_name=True) #new style yolo_model, connection_layer = tiny_yolo_infusion_body( Input(shape=(None, None, 3)), num_anchors // self.num_yolo_heads, num_classes) seg_output = infusion_layer(connection_layer) self.yolo_model = Model(inputs=yolo_model.input, outputs=[*yolo_model.output, seg_output]) # self.yolo_model.load_weights(self.model_path, by_name=True) elif self.model_name == 'tiny_yolo_infusion_hydra': #old style self.yolo_model = tiny_yolo_infusion_hydra_body( Input(shape=(None, None, 3)), num_anchors // self.num_yolo_heads, num_classes) # self.yolo_model.load_weights(self.model_path, by_name=True) #new style #not implemented yet elif self.model_name == 'yolo_infusion': print('Loading model weights', self.model_path) yolo_model, seg_output = yolo_infusion_body( Input(shape=(None, None, 3)), num_anchors // self.num_yolo_heads, num_classes) self.yolo_model = Model(inputs=yolo_model.input, outputs=[*yolo_model.output, seg_output]) # self.yolo_model.load_weights(self.model_path, by_name=True) else: if self.model_name == 'yolo_small_objs': self.yolo_model = yolo_body_for_small_objs( Input(shape=(None, None, 3)), num_anchors // self.num_yolo_heads, num_classes) elif self.model_name == 'tiny_yolo_small_objs': self.yolo_model = tiny_yolo_small_objs_body( Input(shape=(None, None, 3)), num_anchors // self.num_yolo_heads, num_classes) else: try: self.yolo_model = load_model(model_path, compile=False) except: self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//self.num_yolo_heads, num_classes) \ if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//self.num_yolo_heads, 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' if self.model_path: 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 gpu_num >= 2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=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, model_name=self.model_name) return boxes, scores, classes
def __init__(self, model_path, classes_path, anchors_path, score_threshold, iou_threshold, size): """ :param model_path: :param classes_path: :param anchors_path: :param score_threshold: :param iou_threshold: :param size """ self.model_path = model_path self.classes_path = classes_path self.anchors_path = anchors_path self.iou = iou_threshold self.score = score_threshold self.model_image_size = size self.font = ImageFont.truetype(font=font_path, size=np.floor(3e-2 * size[1] + 0.5).astype('int32')) self.thickness = (size[0] + size[1]) // 300 self.class_names = self._get_class() self.anchors = self._get_anchors() self.sess = K.get_session() # 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(self.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(self.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, )) self.boxes, self.scores, self.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)
name='y1', kernel_initializer=glorot_uniform(seed=0), kernel_regularizer=l2(0.0001))(y1) y2 = Dense(4, activation='softmax', name='y2', kernel_initializer=glorot_uniform(seed=0), kernel_regularizer=l2(0.0001))(y2) model = Model(inputs, [y1, y2], name='tinymodel') return model model = tiny_yolo_body(input_shape=(128, 1024, 1), num_anchors=3, num_classes=4) model.compile(optimizer='adam', loss={ 'y1': 'categorical_crossentropy', 'y2': 'categorical_crossentropy' }, metrics=['accuracy']) Trainingset = scio.loadmat('pretraindata.mat') X = Trainingset['Tx'] Y = Trainingset['Ty'] X_train = X[0:151] Y_train = Y[0:151]
def generate(self): model_path = os.path.expanduser(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: if is_tiny_version: self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) else: self.yolo_model = yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes, self.spp) 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)) # Add feature map outptus if specified if self.raw_eval.startswith('raw_scores_'): downsample_rates = list(map(int, self.raw_eval.split('_')[2:])) print(downsample_rates) extra_outputs = [] for downsample_rate in downsample_rates: if downsample_rate == 32: extra_outputs.append(self.yolo_model.layers[184].output) # 13x1054 elif downsample_rate == 16: extra_outputs.append(self.yolo_model.layers[152].output) # 26x512 elif downsample_rate == 8: extra_outputs.append(self.yolo_model.layers[92].output) # 52x256 elif downsample_rate == 4: extra_outputs.append(self.yolo_model.layers[32].output) # 104x128 elif downsample_rate == 2: extra_outputs.append(self.yolo_model.layers[14].output) # 208x64 elif downsample_rate == 1: extra_outputs.append(self.yolo_model.layers[3].output) # 416x32 elif downsample_rate == 0: extra_outputs.append(self.yolo_model.layers[0].output) # 413x3 else: raise ValueError('downsample_rate not valid: {}'.format(downsample_rate)) print('Extra outputs:') for eo in extra_outputs: print(' -', eo) self.yolo_model = Model(self.yolo_model.inputs, self.yolo_model.outputs + extra_outputs) # 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) if not hasattr(self, 'raw_eval') or self.raw_eval == 'def': # Each detection has a single score and category return yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) elif self.raw_eval == 'raw': return yolo_eval_raw(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score) # elif self.raw_eval == 'raw_scores': # return yolo_eval_raw_scores(self.yolo_model.output, self.anchors, # len(self.class_names), self.input_image_shape, # score_threshold=self.score) # elif 'raw_scores_' in self.raw_eval: # ej. raw_scores_16_32 elif 'raw_scores' in self.raw_eval: # ej. raw_scores_16_32 # Detection scores as given as a vector of class confidences return yolo_eval_raw_scores_feats(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, list(map(int, self.raw_eval.split('_')[2:])), max_boxes = self.max_boxes, score_threshold=self.score, iou_threshold=self.iou) else: raise ValueError('Unrecognized eval error')
def __init__(self): # Using OpenCV to capture from device 0. If you have trouble capturing # from a webcam, comment the line below out and use a video file # instead. # self.video = cv2.VideoCapture(0) os.environ['CUDA_VISIBLE_DEVICES'] = '0' gpu_num = 1 # model_path = '../logs/human_pose_dataset_1400_416_yolo/trained_weights_final.h5' model_path = '../logs/human_pose_dataset_2388_size_416_batch_size_4/human_pose_dataset_2388_size_416_batch_size_4.h5' anchors_path = '../model_data/yolo_anchors.txt' classes_path = '../model_data/human_pose.txt' score = 0.2 iou = 0.2 model_image_size = (416, 416) self.sess = K.get_session() # Get class classes_path = os.path.expanduser(classes_path) with open(classes_path) as f: class_names = f.readlines() self.class_names = [c.strip() for c in class_names] # Anchors anchors_path = os.path.expanduser(anchors_path) with open(anchors_path) as f: anchors = f.readline() anchors = [float(x) for x in anchors.split(',')] anchors = np.array(anchors).reshape(-1, 2) # Load model model_path = os.path.expanduser(model_path) assert model_path.endswith('.h5'), 'Keras model end with file .h5' num_anchors = len(anchors) num_classes = len(self.class_names) is_tiny_version = num_anchors == 6 try: yolo_model = load_model(model_path, compile=False) except: if is_tiny_version: yolo_model = tiny_yolo_body(Input(shape=(None, None, 3)), num_anchors // 2, num_classes) else: self.yolo_model = yolo_body(Input(shape=(None, None, 3)), num_anchors // 3, num_classes) self.yolo_model.load_weights(model_path) else: self.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(model_path)) face_encodings_in_room = [] face_names_in_room = [] known_face_encodings_array = np.load( "../data/numpy/known_face_encoding.npy") known_face_names = np.load("../data/numpy/known_face_names.npy") # Convert nparray -> List to face_encoding len_of_array_known_face_names = len(known_face_names) known_face_encodings_array = known_face_encodings_array.reshape( len_of_array_known_face_names, 128) known_face_encodings = [] for i in range(len_of_array_known_face_names): known_face_encodings.append(known_face_encodings_array[i]) self.input_image_shape = K.placeholder(shape=(2, )) self.boxes, self.scores, self.classes = yolo_eval( self.yolo_model.output, anchors, len(self.class_names), self.input_image_shape, score_threshold=score, iou_threshold=iou) num_frame = 0 self.font = cv2.FONT_HERSHEY_DUPLEX self.video = WebcamVideoStream(src=0).start()
def main(args): # If output_model path is relative and in cwd, make it absolute from root output_model = FLAGS.output_model if str(Path(output_model).parent) == '.': output_model = str((Path.cwd() / output_model)) output_fld = Path(output_model).parent output_model_name = Path(output_model).name output_model_stem = Path(output_model).stem output_model_pbtxt_name = output_model_stem + '.pbtxt' # Create output directory if it does not exist Path(output_model).parent.mkdir(parents=True, exist_ok=True) if FLAGS.channels_first: K.set_image_data_format('channels_first') else: K.set_image_data_format('channels_last') # model = load_model(FLAGS.input_model, FLAGS.input_model_json, FLAGS.input_model_yaml) model = None if FLAGS.is_tiny: model = tiny_yolo_body(Input(shape=(None, None, 3)), 3, FLAGS.num_class) else: model = yolo_body(Input(shape=(None, None, 3)), 3, FLAGS.num_class) model.load_weights(FLAGS.input_model) # TODO(amirabdi): Support networks with multiple inputs orig_output_node_names = [node.op.name for node in model.outputs] if FLAGS.output_nodes_prefix: num_output = len(orig_output_node_names) pred = [None] * num_output converted_output_node_names = [None] * num_output # Create dummy tf nodes to rename output for i in range(num_output): converted_output_node_names[i] = '{}{}'.format( FLAGS.output_nodes_prefix, i) pred[i] = tf.identity(model.outputs[i], name=converted_output_node_names[i]) else: converted_output_node_names = orig_output_node_names logging.info('Converted output node names are: %s', str(converted_output_node_names)) sess = K.get_session() if FLAGS.output_meta_ckpt: saver = tf.train.Saver() saver.save(sess, str(output_fld / output_model_stem)) if FLAGS.save_graph_def: tf.train.write_graph(sess.graph.as_graph_def(), str(output_fld), output_model_pbtxt_name, as_text=True) logging.info('Saved the graph definition in ascii format at %s', str(Path(output_fld) / output_model_pbtxt_name)) if FLAGS.quantize: from tensorflow.tools.graph_transforms import TransformGraph transforms = ["quantize_weights", "quantize_nodes"] transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [], converted_output_node_names, transforms) constant_graph = graph_util.convert_variables_to_constants( sess, transformed_graph_def, converted_output_node_names) else: constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), converted_output_node_names) graph_io.write_graph(constant_graph, str(output_fld), output_model_name, as_text=False) logging.info('Saved the freezed graph at %s', str(Path(output_fld) / output_model_name))
classes_path = 'model_data/voc_classes.txt' anchors_path = 'model_data/yolo_anchors.txt' weights_path = 'logs/000/backup.h5' model_path = 'tmp/keras_savedmodel' input_shape = (416, 416) # multiple of 32, hw class_names = get_classes(classes_path) num_classes = len(class_names) anchors = get_anchors(anchors_path) num_anchors = len(anchors) image_input = Input(shape=(input_shape[0], input_shape[0], 3), name='input0') if num_anchors == 9: model_body = yolo_body(image_input, num_anchors // 3, num_classes) elif num_anchors == 6: model_body = tiny_yolo_body(image_input, num_anchors // 3, num_classes) model_body.load_weights(weights_path, by_name=True) print('Load weights {}.'.format(weights_path)) print('model output names', model_body.output_names) print('model input names', model_body.input_names) tf.keras.experimental.export_saved_model(model_body, model_path, custom_objects={ 'hard_swish': hard_swish, 'relu6': relu6 }) print('export savedmodel to ', model_path)
def generate(self): model_path = os.path.expanduser(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) if self.print_summary: self.yolo_model.summary(line_length=150) except FileNotFoundError: 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 if self.print_summary: self.yolo_model.summary(line_length=150) else: from_model_layers = self.yolo_model.layers[-1].output_shape[-1] from_computed_number = num_anchors / len( self.yolo_model.output) * (num_classes + 5) assert from_model_layers == from_computed_number, \ 'Mismatch between model and given anchor and class sizes' from_model_layers = len(self.yolo_model.output) print( '{} model with {} output layers, {} anchors, and {} classes loaded.' .format(model_path, from_model_layers, num_anchors, num_classes)) # 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_outputs, features = yolo_eval( self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) if self.save_pb: # Freeze and save model to tf_model .pb file frozen_graph = freeze_session( K.get_session(), output_names=[out.op.name for out in self.yolo_model.outputs]) tf.train.write_graph(frozen_graph, "pb_models", "{}.pb".format(self.model_name), as_text=False) return boxes, scores, classes, yolo_outputs, features
def generate(self): model_path = os.path.expanduser(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: from yolo3.model import yolo_head print("Loading json model") with open('logs/002/model_in_json.json', 'r') as f: model_json = json.load(f) model = model_from_json(model_json, custom_objects={"yolo_head": yolo_head}) model.load_weights('logs/002/trained_weights_final.h5') except: # This is not working: # self.yolo_model = load_model(model_path, compile=False) # We create a new body: print("Is tiny model: {} ".format(is_tiny_version)) 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 {} -> {}'.format( self.yolo_model.layers[-1].output_shape[-1], num_anchors, num_classes, num_anchors/len(self.yolo_model.output) * (num_classes + 5)) 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
import onnxmltools from keras.layers import Input from yolo3.model import tiny_yolo_body import argparse def arg_parse(): """Parse arguements to the detect module""" parser = argparse.ArgumentParser( description='YOLO v3 Video Detection Module') parser.add_argument("--model", dest='model', help="Keras model to convert", type=str) return parser.parse_args() args = arg_parse() yolo_model = tiny_yolo_body(Input(shape=(416, 416, 3)), 6, 2) yolo_model.load_weights( args.model) # make sure model, anchors and classes match # # Convert it! The target_opset parameter is optional. # onnx_model = onnxmltools.convert_keras(keras_model, target_opset=7)
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2, weights_path='model_data/yolo_weights.h5'): '''create the training model''' 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, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \ num_anchors//3, num_classes+5)) for l in range(3)] #model_body = yolo_body(image_input, num_anchors//3, num_classes) #print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes)) model_body = tiny_yolo_body(image_input, num_anchors // 3, num_classes) print('Create YOLOv3-tiny 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 darknet53 body or freeze all but 3 output layers. num = (185, len(model_body.layers) - 3)[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))) # get output of second last layers and create bottleneck model of it out1 = model_body.layers[246].output out2 = model_body.layers[247].output out3 = model_body.layers[248].output bottleneck_model = Model([model_body.input, *y_true], [out1, out2, out3]) # create last layer model of last layers from yolo model in0 = Input(shape=bottleneck_model.output[0].shape[1:].as_list()) in1 = Input(shape=bottleneck_model.output[1].shape[1:].as_list()) in2 = Input(shape=bottleneck_model.output[2].shape[1:].as_list()) last_out0 = model_body.layers[249](in0) last_out1 = model_body.layers[250](in1) last_out2 = model_body.layers[251](in2) model_last = Model(inputs=[in0, in1, in2], outputs=[last_out0, last_out1, last_out2]) model_loss_last = Lambda(yolo_loss, output_shape=(1, ), name='yolo_loss', arguments={ 'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5 })([*model_last.output, *y_true]) last_layer_model = Model([in0, in1, in2, *y_true], model_loss_last) model_loss = Lambda(yolo_loss, output_shape=(1, ), name='yolo_loss', arguments={ 'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5 })([*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model, bottleneck_model, last_layer_model
# -*- coding: utf-8 -*-
def load_model(self, yolo_weights=None): model_path = self._get_data_path(self.model_path, self.yolo3_dir) assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.' if yolo_weights is None: # 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' else: self.yolo_model = yolo_weights input_image_shape = keras.Input(shape=(2,), name='image_shape') image_input = keras.Input((None, None, 3), dtype='float32', name='input_1') y = list(self.yolo_model(image_input)) y.append(input_image_shape) if len(y) == 3: evaluation_input = [keras.Input((None, None, 255), dtype='float32', name='conv2d_10'), keras.Input((None, None, 255), dtype='float32', name='conv2d_13'), keras.Input(shape=(2,), name='image_shape') ] elif len(y) == 4: evaluation_input = [keras.Input((None, None, 255), dtype='float32', name='conv2d_59'), keras.Input((None, None, 255), dtype='float32', name='conv2d_67'), keras.Input((None, None, 255), dtype='float32', name='conv2d_75'), keras.Input(shape=(2,), name='image_shape') ] boxes, box_scores = \ YOLOEvaluationLayer(anchors=self.anchors, num_classes=len(self.class_names))(inputs=evaluation_input) self.evaluation_model = keras.Model(inputs=evaluation_input, outputs=[boxes, box_scores]) nms_input = [keras.Input((4,), dtype='float32', name='concat_9'), keras.Input((80,), dtype='float32', name='concat_10'),] out_boxes, out_scores, out_indices = \ YOLONMSLayer(anchors=self.anchors, num_classes=len(self.class_names))( inputs=nms_input) self.nms_model = keras.Model(inputs=nms_input, outputs=[out_boxes, out_scores, out_indices]) boxes, box_scores = \ YOLOEvaluationLayer(anchors=self.anchors, num_classes=len(self.class_names))(inputs=y) out_boxes, out_scores, out_indices = \ YOLONMSLayer(anchors=self.anchors, num_classes=len(self.class_names))( inputs = [boxes, box_scores]) self.final_model = keras.Model(inputs=[image_input, input_image_shape], outputs = [out_boxes, out_scores, out_indices]) self.final_model.save('final_model.h5') print('{} model, anchors, and classes loaded.'.format(model_path))
def generate(self): model_path = os.path.expanduser(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) #added by Fiona to generate TF pb model #keras_sess = tf.keras.backend.get_session() #frozen_graph = freeze_session(K.get_session(), output_names=[out.op.name for out in self.yolo_model.outputs]) '''frozen_graph = freeze_session(keras_sess, output_names=[out.op.name for out in self.yolo_model.outputs]) tf.train.write_graph(frozen_graph, "model", "tf_yolov3_DBL.pb", as_text=False) from tensorflow.python.platform import gfile graph = tf.Graph() f = gfile.FastGFile("./model/tf_yolov3_DBL.pb", 'rb') graph_def = tf.GraphDef.FromString(f.read()) f.close() with graph.as_default(): tf.import_graph_def(graph_def) train_writer = tf.summary.FileWriter("./") train_writer.add_graph(graph) sess = tf.Session(graph=graph)''' 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) #graph_def = self.sess.graph.as_graph_def() #keras_sess = tf.keras.backend.get_session() frozen_graph = freeze_session(self.sess, output_names=[out.op.name for out in [boxes,scores,classes]]) tf.train.write_graph(frozen_graph, "model", "tf_yolov3_fullx.pb", as_text=False) #train_writer = tf.summary.FileWriter("./") #train_writer.add_graph(graph_def) return boxes, scores, classes
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2, weights_path='model_data/yolo_weights.h5', td_len=None, mode=None, spp=False, loss_percs={}): '''create the training model''' K.clear_session() # get a new session h, w = input_shape num_anchors = len(anchors) is_tiny_version = num_anchors == 6 # default setting # y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \ # num_anchors//3, num_classes+5)) for l in range(3)] # print([ (h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \ # num_anchors//3, num_classes+5) for l in range(3) ]) if is_tiny_version: y_true = [ Input(shape=(None, None, num_anchors // 2, num_classes + 5)) for l in range(2) ] model_body = tiny_yolo_body(Input(shape=(None, None, 3)), num_anchors // 2, num_classes) elif td_len is not None and mode is not None: y_true = [ Input(shape=(None, None, num_anchors // 3, num_classes + 5)) for l in range(3) ] image_input = Input(shape=(td_len, None, None, 3)) model_body = r_yolo_body(image_input, num_anchors // 3, num_classes, td_len, mode) else: # y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \ # num_anchors//3, num_classes+5)) for l in range(3)] y_true = [ Input(shape=(None, None, num_anchors // 3, num_classes + 5)) for l in range(3) ] image_input = Input(shape=(None, None, 3)) model_body = yolo_body(image_input, num_anchors // 3, num_classes, spp) print('Create YOLOv3 model with {} anchors and {} classes.'.format( num_anchors, num_classes)) if load_pretrained and weights_path != '': 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 darknet53 body or freeze all but 3 output layers. num = 2 if td_len is not None and mode is not None else \ (185, len(model_body.layers)-3)[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))) else: print('No freezing, no pretraining') # from keras.utils import multi_gpu_model # model_body = multi_gpu_model(model_body, gpus=2) model_loss = Lambda(yolo_loss, output_shape=(1, ), name='yolo_loss', arguments={ 'anchors': anchors, 'num_classes': num_classes, 'loss_percs': loss_percs, 'ignore_thresh': 0.5 })([*model_body.output, *y_true]) model = Model([model_body.input, *y_true], model_loss) return model
def generate(self): '''构造测试模型 内置输入参数 model.input: image_data,修正尺寸后的图像数据 input_image_shape: [image.size[1],image.size[0]], 原始图像尺寸;由yolo_val输入张量(2,),经session.run(feed_dict={self.input_image_shape:...})填充输入值 K.learning_phase():0,测试非训练标记 anchors:锚点数组,[9 x 2],第一列为宽度,第二列为高度 class_names:检测类别list[80 x 1] score_threshold:Bounding Box预测得分阈值 iou_threshold:IOU阈值 @return boxes, scores, classes boxes.shape =>(?,4) ,修正后的真实原始图像边框值,(ymin,xmin,ymax,xmax) scores.shape =>(?, ) classes.shape=>(?, ) ''' print('============generate=============') model_path = os.path.expanduser(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) #加载模型 #self.yolo_model.summary(line_length=250) #打印模型参数 self.yolo_model.summary() #打印模型参数 #输出日志 summary_writer = tf.summary.FileWriter(self.log_path, tf.get_default_graph()) summary_writer.close() 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: #模型参数与anchor,class不匹配 print('self.yolo_model.layers[-1].output_shape[-1]:',self.yolo_model.layers[-1].output_shape[-1]) print('num_anchors/len(self.yolo_model.output) * (num_classes + 5):',num_anchors/len(self.yolo_model.output) * (num_classes + 5)) print('num_anchors:',num_anchors) print('len(self.yolo_model.output):',len(self.yolo_model.output)) print('num_classes:',num_classes) 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) print('self.yolo.model.output.shape:') #此范例:anchors=9,num_classes=80,yolo_model.output为[y1,y2,y3],平均每个分配3个anchor => 9/3*(80+5)=255) print([x.shape for x in self.yolo_model.output]) #[(none,none,none,255) #,(none,none,none,255) #,(none,none,none,255)] print('self.anchors:',self.anchors) #(9,2) print('self.class_names:',self.class_names) #(80,1) print('self.input_image_shape:',self.input_image_shape)#(2,1) print('self.score:',self.score) print('self.iou:',self.iou) #==================yolo评估==================== boxes, scores, classes = yolo_eval( self.yolo_model.output #模型输出:[y1,y2,y3] , self.anchors #锚点数组:[9 x 2] , len(self.class_names) #分类数目:80 , self.input_image_shape #原始图像大小:张量,placeholder,模型输入 , score_threshold=self.score #得分阈值 , iou_threshold=self.iou #交并比阈值 ) print('boxes.shape:',boxes.shape) #=>boxes.shape: (?, 4),回归框:[x1,y1,x2,y2] print('scores.shape:',scores.shape) #=>scores.shape: (?,) ,得分:[0-1] print('classes.shape:',classes.shape) #=>classes.shape: (?,),预测类别 return boxes, scores, classes