Keras CNN-LSTM RuntimeError

时间:2018-11-07 13:01:53

标签: python machine-learning keras conv-neural-network lstm

我正在尝试使用以下基于Inceptionv3的模型的CNN和LSTM层来解决回归问题。我的输入数据是具有连续目标值的图片。我想将图像序列提供给CNN,然后提供给LSTM层。但是我得到

RuntimeError: You must compile your model before using it 

消息。知道可能是什么原因吗?我试图在github和几页上找到答案,但没有成功。

from keras.applications.inception_v3 import InceptionV3
from keras.models import Sequential, Model
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization, Conv2D, MaxPooling2D, GlobalAveragePooling2D, LSTM, TimeDistributed, Input
from keras.optimizers import SGD, RMSprop
from keras.callbacks import EarlyStopping, ModelCheckpoint, CSVLogger, ReduceLROnPlateau
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# custom R2-score metrics for keras backend
#https://www.kaggle.com/c/mercedes-benz-greener-manufacturing/discussion/34019
from keras import backend as K

def r2_keras(y_true, y_pred):
    SS_res =  K.sum(K.square(y_true - y_pred))
    SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )  

train_data_dir = '...'
test_data_dir = '...'

train_df = pd.read_csv('...')
valid_df = pd.read_csv('...')

filepath_loss = '...'
filepath_csv = '...'

datagen=ImageDataGenerator(rescale=1./255.,)


img_width, img_height = 380, 380
frames = 5
channels = 3

pictures = Input(shape=(frames, img_width, img_height, channels))

train_generator=datagen.flow_from_dataframe(
dataframe=train_df,
directory=train_data_dir,
x_col="block_heights",
y_col="weighted_prices",
has_ext=False,      #x_col column doesnt has the file extensions
#subset="training",     if validation split is set in ImageDataGenerator
batch_size=16,
seed=42,
shuffle=False,
class_mode="other",  #for regression other should be used
target_size=(img_width, img_height))

valid_generator=datagen.flow_from_dataframe(
dataframe=valid_df,
directory=train_data_dir,
x_col="block_heights",
y_col="weighted_prices",
has_ext=False,              #x_col column doesnt has the file extensions
#subset="validation",      if validation split is set in ImageDataGenerator
batch_size=16,
seed=42,
shuffle=False,
class_mode="other",
target_size=(img_width, img_height))  

conv_base = InceptionV3(weights=None, include_top=False, input_shape=(img_width,img_height,3))
conv_base.trainable = True

model = Sequential()
model.add(TimeDistributed(conv_base))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(10, return_sequences=True))
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='linear'))

#error at callbacks if the learning rate is explicitly set somewhere
rms = RMSprop(lr=0.1, rho=0.9, epsilon=None, decay=0.0)

model.compile(loss='mse', optimizer=rms, metrics=['mae', r2_keras])

STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size

callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='auto'),
            ReduceLROnPlateau(monitor='val_loss', factor=0.02, patience=3, min_lr=0.001),
            ModelCheckpoint(filepath_loss, monitor='val_loss', verbose=1, save_best_only=True, mode='min'),
            CSVLogger(filepath_csv, separator = ",", append = False)]


history = model.fit_generator(generator=train_generator,steps_per_epoch=STEP_SIZE_TRAIN, validation_data=valid_generator, validation_steps=STEP_SIZE_VALID, epochs=50, callbacks=callbacks)

0 个答案:

没有答案