Python Forum
1st layer tf.keras output shape set at multiple - need help!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1st layer tf.keras output shape set at multiple - need help!
#1
Hello all,

The output shape of my first layer when calling model.summary() comes out as "multiple". I'm pretty sure this means that I have multiple inputs acting on it but I can not figure out which parts of my code are acting on it in this way.

So I am asking if anyone can help point out my mistakes in my code and offer any alternatives?

Code is as follows:

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/british_carribae/'

test_path= os.path.join(PATH, 'test')
train_path=os.path.join(PATH,'train')
val_path=os.path.join(PATH,'val')


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,)


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()
this is the results of the summary printout for refference:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        multiple                  0         
                                                                 
 block1_conv1 (Conv2D)       (None, 224, 224, 64)      1792      
                                                                 
 block1_conv2 (Conv2D)       (None, 224, 224, 64)      36928     
                                                                 
 block1_pool (MaxPooling2D)  (None, 112, 112, 64)      0         
                                                                 
 block2_conv1 (Conv2D)       (None, 112, 112, 128)     73856     
                                                                 
 block2_conv2 (Conv2D)       (None, 112, 112, 128)     147584    
                                                                 
 block2_pool (MaxPooling2D)  (None, 56, 56, 128)       0         
                                                                 
 block3_conv1 (Conv2D)       (None, 56, 56, 256)       295168    
                                                                 
 block3_conv2 (Conv2D)       (None, 56, 56, 256)       590080    
                                                                 
 block3_conv3 (Conv2D)       (None, 56, 56, 256)       590080    
                                                                 
 block3_pool (MaxPooling2D)  (None, 28, 28, 256)       0         
                                                                 
 block4_conv1 (Conv2D)       (None, 28, 28, 512)       1180160   
                                                                 
 block4_conv2 (Conv2D)       (None, 28, 28, 512)       2359808   
                                                                 
 block4_conv3 (Conv2D)       (None, 28, 28, 512)       2359808   
                                                                 
 block4_pool (MaxPooling2D)  (None, 14, 14, 512)       0         
                                                                 
 block5_conv1 (Conv2D)       (None, 14, 14, 512)       2359808   
                                                                 
 block5_conv2 (Conv2D)       (None, 14, 14, 512)       2359808   
                                                                 
 block5_conv3 (Conv2D)       (None, 14, 14, 512)       2359808   
                                                                 
 block5_pool (MaxPooling2D)  (None, 7, 7, 512)         0         
                                                                 
 global_max_pooling2d (Globa  (None, 512)              0         
 lMaxPooling2D)                                                  
                                                                 
 batch_normalization (BatchN  (None, 512)              2048      
 ormalization)                                                   
                                                                 
 dropout (Dropout)           (None, 512)               0         
                                                                 
 dense (Dense)               (None, 256)               131328    
                                                                 
 batch_normalization_1 (Batc  (None, 256)              1024      
 hNormalization)                                                 
                                                                 
 dropout_1 (Dropout)         (None, 256)               0         
                                                                 
 dense_1 (Dense)             (None, 291)               74787     
                                                                 
=================================================================
Total params: 14,923,875
Trainable params: 207,651
Non-trainable params: 14,716,224
Thanks for any help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  issue displaying summary of whole keras CNN model on TensorFlow with python Afrodizzyjack 0 1,663 Oct-27-2021, 04:07 PM
Last Post: Afrodizzyjack
  Issues with Shape/Reshape for CNN moddy10 0 1,476 Oct-12-2021, 03:54 PM
Last Post: moddy10
  Understanding Keras and TensorFlow and how to use them bytecrunch 1 2,088 Mar-11-2021, 02:40 PM
Last Post: jefsummers
  Problems feeding live input from my microphone into a keras model (SegFault: 11) zeptozetta 1 2,604 Sep-14-2020, 03:08 AM
Last Post: zeptozetta
  Keras.Predict into Dataframe Finpyth 13 9,884 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Making a Basic Keras Model - Input Shape and Parameters MattKahn13 0 2,130 Aug-16-2020, 04:36 PM
Last Post: MattKahn13
  cannot reshape array of size 0 into shape Roro 2 6,298 Jun-14-2020, 11:28 AM
Last Post: Roro
  Single layer perceptron not outputting correct results mberge 0 1,417 Apr-29-2020, 10:17 PM
Last Post: mberge
  Error when import Keras Azadfalah 1 2,812 Apr-29-2020, 04:45 AM
Last Post: buran
  Keras + Matplotlib causing crash spearced 3 4,540 Feb-06-2020, 04:54 PM
Last Post: zljt3216

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020