1st layer tf.keras output shape set at multuple - need help!

Hello all and one,

I need help with my code.

My first input layer has an output shape of multiple and i cant figure out how to make it not do this! Any suggestions would be great.

import tensorflow as tf
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten,Dropout
from tensorflow.keras.models import Model
from tensorflow.keras import Model, layers, utils
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import GlobalAveragePooling2D
import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt

PATH = ‘…/img_class/images/eurythene_RPi_cam/’
test_path= os.path.join(PATH, ‘test’)
train_path=os.path.join(PATH,‘train’)
val_path=os.path.join(PATH,‘validation’)
IMAGE_SIZE = (224, 224)
BATCH_SIZE = 32
x_train = tf.keras.utils.image_dataset_from_directory(train_path,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMAGE_SIZE)
x_test = tf.keras.utils.image_dataset_from_directory(test_path,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMAGE_SIZE)
x_val = tf.keras.utils.image_dataset_from_directory(val_path,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMAGE_SIZE)

AUTOTUNE = tf.data.AUTOTUNE
x_train = x_train.prefetch(buffer_size=AUTOTUNE)
x_val= x_val.prefetch(buffer_size=AUTOTUNE)
x_test = x_test.prefetch(buffer_size=AUTOTUNE)

preprocess_input = tf.keras.applications.vgg16.preprocess_input

IMG_SHAPE = IMAGE_SIZE +(3,)

add preprocessing layer to the front of VGG
vgg = tf.keras.applications.VGG16(input_shape=IMG_SHAPE, weights=‘imagenet’, include_top=False, pooling=‘max’)

image_batch, label_batch = next(iter(x_train))
feature_batch = vgg(image_batch)
print(feature_batch.shape)

for layer in vgg.layers:
layer.trainable = False

inp = layers.Input((224,224,3))
cnn = vgg(inp)
x = layers.BatchNormalization()(cnn)
x = layers.Dropout(0.2)(x)
x = layers.Dense(256, activation=‘softmax’)(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.2)(x)
out = layers.Dense(291, activation=‘softmax’)(x)
model = Model(inp, out)
#Flattening nested model
def flatten_model(model_nested):
layers_flat =
for layer in model_nested.layers:
try:
layers_flat.extend(layer.layers)
except AttributeError:
layers_flat.append(layer)
model_flat = tf.keras.models.Sequential(layers_flat)
return model_flat
model_flat = flatten_model(model)
model_flat.summary()

Hi @Jack_Kinnard

Welcome to the TensorFlow Forum!

The model is printing input_layer multiple because model has accepted two input layers one while defining the pretrained model and another one is one feeding that pretrained model to final functional model. So when you pass this final model to flattened_model, The Sequential model detects two input_layers and print in the summary as ‘multiple’. To avoid this , you can select only one input layer when using in flattened_model as below:

#Flattening nested model
def flatten_model1(model_nested):
    layers_flat = []
    for layer in model_nested.layers[1:]:  #<----------
        try:
            layers_flat.extend(layer.layers)
        except AttributeError:
            layers_flat.append(layer)
            print(layers_flat)
    model_flat = tf.keras.models.Sequential(layers_flat)
    return model_flat
model_flat = flatten_model1(model)
model_flat.summary()

Model.summary will not show the input_layer because it has converted to Sequential model and Sequentila model does not show input_layer in summary of model. But you can check the model has input_layer after plotting the model.

Please find the replicated gist for your reference.