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