Home > Software design >  Shape Error while Fine Tuning MobileNet On A Custom Data Set
Shape Error while Fine Tuning MobileNet On A Custom Data Set

Time:12-13

I was following deeplizard to fine-tuning MobileNet. What I tried to do is to grab the output from the 5th to the last layer of the model and store it in this variable x. The output of the 5th to the last layer of the model has a shape of global_average_pooling2d_3 (None, 1, 1, 1024). Then add an output dense layer with 10 units. However, when fitting the model, I got the following error. Could anyone please kindly offer me some guidance. Thanks a lot. My code looks like the following

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Activation,Reshape
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import matplotlib.pyplot as plt
from tensorflow.keras import layers
%matplotlib inline


mobile = tf.keras.applications.mobilenet.MobileNet()
mobile.summary()
x = mobile.layers[-5].output
output =layers.Dense(units=10, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=output)

for layer in model.layers[:-23]:
    layer.trainable = False

model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x=train_batches,
            steps_per_epoch=len(train_batches),
            validation_data=valid_batches,
            validation_steps=len(valid_batches),
            epochs=30,
            verbose=2
)

ValueError: Shapes (None, None) and (None, 1, 1, 10) are incompatible

CodePudding user response:

When you call the base model as follows, it will initiate with the default argument. Among them, include_top is set as True.

mobile = tf.keras.applications.mobilenet.MobileNet()

And, that brings (source) the GlobalAvg with keepdims=True.

  if include_top:
    x = layers.GlobalAveragePooling2D(keepdims=True)(x)

Now, based on your error, I assumed your true label shape and here you can do simply as follows

mobile = keras.applications.mobilenet.MobileNet()
x = mobile.layers[-5].output # shape=(None, 1, 1, 1024)
x = layers.Flatten()(x) # < --- Here shape=(None, 1024)

output =layers.Dense(units=10, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=output)
  • Related