I am using the Keras data generator to load data from a directory. I am basically dealing with a regression i.e there is a numerical value for each of my images in the range [-1 1]. I am using the following code to load images from the directory:


from sklearn.model_selection import train_test_split
train_x,test_x,train_y,test_y=train_test_split(df['Image'], df['Steer'], test_size=0.2, random_state=1)



images_dir = '/content/original/compressed/All_data/'
train_datagen = ImageDataGenerator(rescale = 1./255) 
train_generator = train_datagen.flow_from_dataframe(dataframe=train_df, directory=images_dir, 
                                              x_col="id", y_col="steer", has_ext=True, 
                                              class_mode="other", target_size=(110, 220), 

test_datagen = ImageDataGenerator(rescale = 1./255) 
test_generator = test_datagen.flow_from_dataframe(dataframe=test_df, directory=images_dir, 
                                              x_col="id", y_col="steer", has_ext=True, 
                                              class_mode="other", target_size=(110, 220), 
  import numpy as np
import pandas as pd

from skimage.io import imread
from skimage.transform import resize

import keras
from keras.models import Sequential, Model, load_model
from keras.layers import Input, Conv1D, Conv2D, MaxPooling1D, MaxPooling2D, Dense, Dropout, Activation, Flatten
from keras.layers.normalization import BatchNormalization
from keras.utils import to_categorical
from skimage.transform import resize
from tensorflow.keras import layers 
import tensorflow as tf

  from keras.applications import VGG16

# include top should be False to remove the softmax layer
pretrained_model = VGG16(include_top=False, weights='imagenet')

base_model = VGG16(input_shape = (110, 220, 3), # Shape of our images
include_top = False, # Leave out the last fully connected layer
weights = 'imagenet')

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

from tensorflow.keras import layers  
import tensorflow as tf

# Flatten the output layer to 1 dimension
x = layers.Flatten()(base_model.output)

# Add a fully connected layer with 512 hidden units and ReLU activation
x = layers.Dense(512, activation='relu')(x)

# Add a dropout rate of 0.5
x = layers.Dropout(0.5)(x)

# Add a final sigmoid layer for classification
x = layers.Dense(1, activation='linear')(x)

model = tf.keras.models.Model(base_model.input, x)
opt = keras.optimizers.Adam(learning_rate=0.001)
model.compile(loss = 'mean_absolute_percentage_error',optimizer=opt,metrics = ['mae'])

                   epochs = 15,
                   verbose = 1,
                   validation_data = test_generator

When I train the model using this code, a value of 0.221 is returned for the MAE for each epoch no matter what I do. I have changed the activation functions, optimizers, learning rate, etc but the result is the same every time. Am I loading in the data in a correct way?


When you are using a pre-trained model, you should use it's specific pre-processing function,

Below is an example for resnet50.

from keras.applications.resnet50 import preprocess_input
train_datagen = ImageDataGenerator(#rescale = 1./255

Always display/print your image/label for a sanity check.
You may use the below snippet.

#Quick check the DataGenerator

import matplotlib.pyplot as plt
for image, label in train_generator:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.