title: Keras Linear regression model from images not predicting correctly
body: <p>I have images which are rated as 0 to 5 (they corresponds to the visual attractiveness of a manufactured part). When training, the mean absolute error gets down to .4. However, when I plot the actual vs predicted at the very end of the code, there is no correlation between actual and predicted. I exported the table of actual vs predicted and imported to Excel and when I calculate mean absolute error, I get a values of 1.2 (again  reports .4). The Rsq of actual vs. predicted is less than .01. Its like my prediction at the end is random and doesn't use the model.</p>
  
  <p>Towards the bottom, you will see "#test_generator.reset()". This was my attempt to troubleshoot the problem but with and without this line does nothing. I know how to change the model to make the model ordinal, but I'm looking to make this model quantitative. </p>
  
  <pre><code>import pandas as pd
  import os
  from .preprocessing.image import ImageDataGenerator
  from .callbacks import ModelCheckpoint
  from .models import Sequential
  from .layers import Conv2D, MaxPooling2D
  from .layers import Activation, Dropout, Flatten, Dense
  from  import backend as K
  from .utils import multi_gpu_model
  from  import optimizers
  from matplotlib import pyplot
  import cv2
  
  
  
  listOfFoldersWithImages = ["C:\\ImageFoler"]
  BATCH_SIZE =32
  TARGET_SIZE = 300,300 
  INPUT_SHAPE = 300,300,3 
  NUM_EPOCHS =50
  
  MODEL_WEIGHTS_LOAD_FILENAME = 'Quant_Weights1_Complex.h5'  #optional - leave blank if not desired
  MODEL_WEIGHTS_SAVE_FILENAME = 'Quant_Weights1_Complex.h5' #optionally leave blank
  ENTIRE_MODEL_SAVE_FILENAME = 'Quant_Model1_Complex.h5'  #optionally leave blank
  MY_LOSS = 'mae'
  
  CLASS_MODE = 'other'
  
  def RecursivelySearchForImageFiles(listOfPaths):
      listAllRatings = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
      files = []
      ratings = []
      for path in listOfPaths:
          # r=root, d=directories, f = files
          for r, d, f in os.walk(path):
              for file in f:
                  if (len(file)&gt;=17):
                      if (file[1:17] == "=CNNPartRated___"):
                          strNum = file[0:1]
                          if (strNum in listAllRatings):
                              files.append (os.path.join(r, file))
                              ratings.append(int(strNum))  #numerical
  
      return files, ratings
  
  
  
  listOfAllImagesPaths, listOfAllRatings = RecursivelySearchForImageFiles(listOfFoldersWithImages)
  
  
  print ("Number of 0 = " + str(listOfAllRatings.count(0)))
  print ("Number of 1 = " + str(listOfAllRatings.count(1)))
  print ("Number of 2 = " + str(listOfAllRatings.count(2)))
  print ("Number of 3 = " + str(listOfAllRatings.count(3)))
  print ("Number of 4 = " + str(listOfAllRatings.count(4)))
  print ("Number of 5 = " + str(listOfAllRatings.count(5)))
  
  
  
  traindf = pd.DataFrame( {'path': listOfAllImagesPaths, 'rating': listOfAllRatings })
  traindf = traindf.sample(frac=1).reset_index(drop=True)
  
  datagen=ImageDataGenerator(rescale=1./255,validation_split=0.25)
  
  train_generator=datagen.flow_from_dataframe(
      dataframe=traindf,
      directory="None",
      x_col="path",
      y_col="rating",
      subset="training",
      batch_size=BATCH_SIZE,
      seed=42,
      shuffle=True,
      class_mode="other",
      #save_to_dir=SAVE_TO_DIR,
      target_size=TARGET_SIZE)
  
  valid_generator=datagen.flow_from_dataframe(
      dataframe=traindf,
      directory="None",
      x_col="path",
      y_col="rating",
      subset="validation",
      batch_size=BATCH_SIZE,
      seed=42,
      shuffle=True,
      class_mode="other",
      #save_to_dir=SAVE_TO_DIR,
      target_size=TARGET_SIZE)
  
  test_datagen=ImageDataGenerator(rescale=1./255.)
  
  test_generator=test_datagen.flow_from_dataframe(
      dataframe=traindf,
      directory="None",
      x_col="path",
      y_col=None,
      batch_size=BATCH_SIZE,
      seed=42,
      shuffle=False,
      class_mode=None,
      target_size=TARGET_SIZE)
  
  
  
  
  
  def TinyModel():
      model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=INPUT_SHAPE))
      model.add(Conv2D(32, (3, 3), activation='relu'))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.5))
  
      model.add(Flatten())
      model.add(Dense(256))
      model.add(Activation('relu'))
      model.add(Dropout(0.5))
      model.add(Dense(64))
      model.add(Activation('relu'))
      model.add(Dropout(0.5))
      model.add(Dense(1, activation='linear'))
      return model
  
  
  model = TinyModel()
  
  
  parallel_model = multi_gpu_model(model, gpus=2)
  
  parallel_model.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss=MY_LOSS,metrics=[MY_LOSS])
  
  if (MODEL_WEIGHTS_LOAD_FILENAME):
      model.load_weights(MODEL_WEIGHTS_LOAD_FILENAME)
  
  STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
  STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
  STEP_SIZE_TEST=test_generator.n//test_generator.batch_size
  
  
  checkpoint_name = 'Weights-{epoch:03d}--{val_loss:.5f}.hdf5'
  checkpoint = ModelCheckpoint(checkpoint_name, monitor='val_loss', verbose = 1, save_weights_only=True, save_best_only = True, mode ='auto')
  callbacks_list = [checkpoint]
  
  history = parallel_model.fit_generator(generator=train_generator,
                      steps_per_epoch=STEP_SIZE_TRAIN,
                      validation_data=valid_generator,
                      validation_steps=STEP_SIZE_VALID,
                      epochs=NUM_EPOCHS,
                      callbacks=callbacks_list
                      )
  
  if (MODEL_WEIGHTS_SAVE_FILENAME!=''):
      model.save_weights(MODEL_WEIGHTS_SAVE_FILENAME)
  
  if (ENTIRE_MODEL_SAVE_FILENAME!=''):
      model.save(ENTIRE_MODEL_SAVE_FILENAME)
  
  
  pyplot.plot(history.history['mean_absolute_error'])
  pyplot.plot(history.history['val_mean_absolute_error'])
  pyplot.title('model mean_absolute_error ')
  pyplot.ylabel('mean_absolute_error')
  pyplot.xlabel('epoch')
  pyplot.legend(['train', 'test'], loc='upper left')
  pyplot.show()
  
  #test_generator.reset()
  pred=parallel_model.predict_generator(test_generator,
      steps=STEP_SIZE_TEST+1,
      verbose=1)
  
  
  
  pyplot.plot(listOfAllRatings, pred, 'o', color='black')
  pyplot.show()
  outframe = pd.DataFrame( {'actual': listOfAllRatings, 'predicted': pred.tolist() })
  outframe.to_csv(listOfFoldersWithImages[0]+"\\correlation.csv")
  print ("File exported")
  </code></pre>