[Python] Transfer Learning using Keras for Image Classification

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to use an already trained model to transfer the learning to a model I will create and modifying only the last few layers. The goal of this is to used the already trained model (trained on millions of images already) to help my model classify food item recognition. I am pretty new to Keras and I am facing an issue that I am now starting understand to but don't know how to resolve

    # Load the model from TensorFlow Hub
    model_url = "https://www.kaggle.com/models/tensorflow/resnet-50/TensorFlow2/classification/1"
    hub_layer = hub.KerasLayer(model_url, input_shape=(224, 224, 3))

    # Create a Sequential model
    model = tf.keras.Sequential()

    # Add the TensorFlow Hub layer to the Sequential model
    model.add(hub_layer)

    # Build the Sequential model
    model.build((None, 224, 224, 3))

    # Summary of the model
    model.summary()


    ERROR:

    ---------------------------------------------------------------------------
    ValueError Traceback (most recent call last)
    Cell In[56], line 9
    6 model = tf.keras.Sequential()
    8 # Add the TensorFlow Hub layer to the Sequential model
    ----> 9 model.add(hub_layer)
    11 # Build the Sequential model
    12 model.build((None, 224, 224, 3))

    File c:\Users\Karim\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\models\sequential.py:95, in Sequential.add(self, layer, rebuild)
    93 layer = origin_layer
    94 if not isinstance(layer, Layer):
    ---> 95 raise ValueError(
    96 "Only instances of `keras.Layer` can be "
    97 f"added to a Sequential model. Received: {layer} "
    98 f"(of type {type(layer)})"
    99 )
    100 if not self._is_layer_name_unique(layer):
    101 raise ValueError(
    102 "All layers added to a Sequential model "
    103 f"should have unique names. Name '{layer.name}' is already "
    104 "the name of a layer in this model. Update the `name` argument "
    105 "to pass a unique name."
    106 )

    ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x00000190C6B8AD20> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)

    Continue reading...

Compartilhe esta Página