Redlightning
Redlightning

Reputation: 1

Pretrain model developing in tensorflow for image classification

I have a issue regarding on how to modify a pretrained model to classify 3 classes instead of 1000. These are the 2 methods i came up with so far.. im not sure which one is the best.

NASNetMobile_model = tf.keras.applications.NASNetMobile (
                                                input_shape=(224,224,3),
                                                include_top=False,
                                                pooling='avg',
                                                classes=3,
                                                weights='imagenet'
                                                )
NASNetMobile_model.trainable=False
NASNetMobile_model.summary()type here

In, Method 1, the NASNetMobile model is initialized with pre-trained ImageNet weights, excluding the top layer and using average pooling. The model is set to be non-trainable to prevent its weights from being updated during training. A new Sequential model is then constructed, which includes the pre-trained NASNetMobile model followed by two dense layers: one with 128 units and ReLU activation, and another with 3 units and softmax activation for the final classification. The Sequential model is compiled with the Adam optimizer and sparse categorical cross-entropy loss. Finally, the model is trained on the dataset for 20 epochs with a batch size of 4 and a validation split of 20%.

Method 1

new_pretrained_model = tf.keras.Sequential()

new_pretrained_model.add(NASNetMobile_model)
new_pretrained_model.add(tf.keras.layers.Dense(128, activation='relu'))
new_pretrained_model.add(tf.keras.layers.Dense(3, activation='softmax'))

new_pretrained_model.layers[0].trainable = False
new_pretrained_model.summary() here


new_pretrained_model.compile(
            optimizer='adam',
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
            )

new_pretrained_model.fit(
        Xtrain,
        Ytrain,
        epochs=20,
        batch_size=4,
        validation_split=0.2
        )

Method 2

In Method 2, the functional API is used to create a new model. The output of the pre-trained NASNetMobile model is taken as the input for a new dense layer with 128 units and ReLU activation, followed by a final dense layer with 3 units and softmax activation. This approach explicitly connects the input of the NASNetMobile model to the new output layers, forming a new model with the same input as the original NASNetMobile model but with additional dense layers for classification. The new model is then compiled with the Adam optimizer and sparse categorical cross-entropy loss and trained on the dataset for 20 epochs with a batch size of 4 and a validation split of 20%.

NASNetMobile_model_out = NASNetMobile_model.output
x = tf.keras.layers.Dense(128, activation='relu')(NASNetMobile_model_out)
output = tf.keras.layers.Dense(3, activation='softmax')(x)
model_2 = tf.keras.Model(inputs = NASNetMobile_model.input, outputs=output)

model_2.summary()


model_2.compile(
            optimizer='adam',
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
            )

model_2.fit(
        Xtrain,
        Ytrain,
        epochs=20,
        batch_size=4,
        validation_split=0.2
        )

Upvotes: 0

Views: 55

Answers (1)

Sagar
Sagar

Reputation: 179

Both the Sequential and Functional APIs produced identical results in your code due to the similar architecture,i would prefer method-2 i.e Functional API is generally preferred for its flexibility in handling complex models with multiple inputs and outputs.The Functional API allows for shared layers, which can reuse features for different tasks. The Sequential API has independent layers, making it difficult to share information between branches.

Upvotes: 0

Related Questions