# How to use data generator for regression keras?

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:

df=pd.DataFrame(columns=['Image','Steer'])
df['Image']=filenames
df['Steer']=steer

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)

train_df=pd.DataFrame(columns=['id','steer'])
train_df['id']=train_x
train_df['steer']=train_y

test_df=pd.DataFrame(columns=['id','steer'])
test_df['id']=test_x
test_df['steer']=test_y

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),
batch_size=32)

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),
batch_size=32)
import numpy as np
import pandas as pd

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)
model.compile(loss = 'mean_absolute_percentage_error',optimizer=opt,metrics = ['mae'])

batch_size=128
history=model.fit(train_generator,
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
preprocessing_function=preprocess_input)


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:
plt.imshow(image[0])
print(label[0])
break;