Jun-07-2022, 04:53 PM
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:
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,224Thanks for any help!
