来自图像无法正确预测的Keras线性回归模型

时间:2019-06-04 23:39:19

标签: image keras model regression

我的图像被评为0到5(它们对应于制造零件的视觉吸引力)。训练时,平均绝对误差降至0.4。但是,当我在代码的最后绘制实际值与预测值时,实际值与预测值之间没有关联。我将实际值与预测值表导出并导入到Excel中,当我计算平均绝对误差时,我得到的值是1.2(再次为.4)。实际值与预测值的Rsq小于0.01。就像我最后的预测是随机的,并且不使用模型。

在底部,您将看到“#test_generator.reset()”。这是我尝试解决问题的方法,但是无论是否使用此行,都不会执行任何操作。我知道如何更改模型以使模型顺序化,但我正在寻求使模型定量化。

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)>=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")

0 个答案:

没有答案