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, input_shape[2]))
image_input = Input(shape=(416, 416, input_shape[2]))
h, w = input_shape[0:2]
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)]
y_true = [Input(shape=(h//(32,16)[l], w//(32,16)[l], num_anchors//2, num_classes+5)) for l in range(2)]#shape=(?,13,13,3,6),shape=(?,26,26,3,6),
#y_true里面有两个元素,是两个Tensor,shape=(?, 13, 13, 3, 6),shape=(?, 26, 26, 3, 6)
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))# 6 anchors and 1 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, 'print_loss':True})(
[*model_body.output, *y_true])#增加一层匿名层,匿名层名字是'yolo_loss',输入是模型预测输出model_body.output和实际标注y_true,输出是yolo_loss,输出维度为(1,?),arguments代表yolo_loss的参数
model = Model(inputs=[model_body.input, *y_true], outputs=model_loss)#把y_true里面的元素拆分开,作为网络的输入*y_true=(?,13,13,3,6),(?,26,26,3,6)
# model = multi_gpu_model(model, gpus=[0,1])
model.summary()
return model