我正在研究一种迁移学习方法,并且在使用 MobileNetV2keras.applications和 TensorFlow Hub 上的 MobileNetV2 时得到了截然不同的结果。这对我来说似乎很奇怪,因为两个版本都声称这里和这里从同一检查点mobilenet_v2_1.0_224提取它们的权重。这就是重现差异的方法,您可以在此处找到 Colab Notebook :
!pip install tensorflow-gpu==2.1.0
import tensorflow as tf
import numpy as np
import tensorflow_hub as hub
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
def create_model_keras():
image_input = tf.keras.Input(shape=(224, 224, 3))
out = MobileNetV2(input_shape=(224, 224, 3),
include_top=True)(image_input)
model = tf.keras.models.Model(inputs=image_input, outputs=out)
model.compile(optimizer='adam', loss=["categorical_crossentropy"])
return model
def create_model_tf():
image_input = tf.keras.Input(shape=(224, 224 ,3))
out = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4",
input_shape=(224, 224, 3))(image_input)
model = tf.keras.models.Model(inputs=image_input, outputs=out)
model.compile(optimizer='adam', loss=["categorical_crossentropy"])
return model …Run Code Online (Sandbox Code Playgroud)