我使用 TFF 0.12.0 和狗和猫的图像数据集(2 个标签),如果我用 VGG16 测试,Ifind 准确度为 0.9,但如果我改为 ResNet50,准确度下降到 0.4,这是我写的:
def create_compiled_keras_model():
baseModel = tf.keras.applications.ResNet50(include_top=False, weights="imagenet", input_tensor=tf.keras.Input(shape=(224, 224, 3)))
resnet_output = baseModel.output
layer1 = tf.keras.layers.GlobalAveragePooling2D()(resnet_output)
layer2 = tf.keras.layers.Flatten(name="flatten")(layer1)
layer2 = tf.keras.layers.Dense(units=256, name='nonlinear', activation="relu")(layer2)
dropout_layer = tf.keras.layers.Dropout(0.5)(layer2)
model_output = tf.keras.layers.Dense(2, activation="softmax")(dropout_layer)
model = tf.keras.Model(inputs=baseModel.input, outputs=model_output)
for layer in baseModel.layers:
layer.trainable = False
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.001, momentum =0.9),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=([tf.keras.metrics.CategoricalAccuracy()]))
return model
def model_fn():
keras_model = create_compiled_keras_model()
return tff.learning.from_compiled_keras_model(keras_model, sample_batch)
iterative_process = tff.learning.build_federated_averaging_process(model_fn, server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0),client_weight_fn=None)
state = iterative_process.initialize()
evaluation = tff.learning.build_federated_evaluation(model_fn)
但即使经过 100 轮,准确度也不超过 0.46。这是结果的一部分:
round 1, metrics=<categorical_accuracy=0.500249981880188,loss=0.7735000252723694,keras_training_time_client_sum_sec=0.0>
round 2, metrics=<categorical_accuracy=0.47187501192092896,loss=0.7735000252723694,keras_training_time_client_sum_sec=0.0>
....
round 99, metrics=<categorical_accuracy=0.4632812440395355,loss=0.7622881531715393,keras_training_time_client_sum_sec=0.0>
round 100, metrics=<categorical_accuracy=0.46015626192092896,loss=0.7622881531748393,keras_training_time_client_sum_sec=0.0>
请帮忙!!!