2

I use TFF 0.12.0 and image dataset for dog and cat(2 labels), If I test with VGG16, Ifind accuracy 0.9 but If I change to ResNet50, accuracy decrease to 0.4, Here is what I write:

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)

but accuracy does not exceed 0.46 even after 100 rounds. here is a part of the result :

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>

Help Please!!!

Zachary Garrett
  • 2,911
  • 15
  • 23
seni
  • 659
  • 1
  • 8
  • 20
  • 1
    One thing to notice is: TFF treats state in a somewhat unique way in machine learning. For TFF, state usually is an input to a function, rather than something loaded in its body. TFF's `from_keras_model` functions pull the *architecture*, but not the weight initialization. Could this be the issue here? – Keith Rush Jan 02 '21 at 01:48
  • 1
    I think [this answer](https://stackoverflow.com/questions/65273151/how-to-initialize-the-model-with-certain-weights) should provide everything you need. – Keith Rush Jan 02 '21 at 19:40
  • Thanks, this is functional .So please, by which you can explain that the accuracy with vgg16 is good (without using new_state) but with this code above, it increase only when using new_state – seni Jan 03 '21 at 18:45

0 Answers0