使用tf.keras.applications中的模型构建自己的网络,并且使用官方的预训练模型:
经过我反复实验:
model = tf.keras.applications.MobileNetV2(include_top=False, weights="imagenet")
inputs = tf.keras.layers.Input(shape=(224, 224, 3))
x = model(inputs) # 此处x为MobileNetV2模型去处顶层时输出的特征相应图。
x = tf.keras.layers.GlobalAveragePooling2D()(x)
outputs = tf.keras.layers.Dense(2, activation='softmax',
use_bias=True, name='Logits')(x)
model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
model.summary()
或者:
model = tf.keras.applications.MobileNetV2(include_top=False, weights="imagenet",pooling='avg')
inputs = tf.keras.layers.Input(shape=(224, 224, 3))
x = model(inputs) # 此处x为MobileNetV2模型去处顶层时输出的特征相应图。
outputs = tf.keras.layers.Dense(2, activation='softmax',
use_bias=True, name='Logits')(x)#其中2为你预测的类别数目。
model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
model.summary()
这样就可以更改最后一层参数,并且使用预训练的模型。其他模型同理。(笑)