I am trying to take my CNN model and add a LSTM layer. It would be beneficial to do so given my images are ordered in time series. I've loaded each of my images using ImageDataGenerator and flow_from_directory. I am unable to add a TimeDistributed layer to make my model work. Any help would be greatly appreciated!
model = Sequential()
model.add(TimeDistributed(Conv2D(16, (3,3), padding='same', strides=(2,2),
activation='relu', input_shape = (224,224,3))))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Conv2D(32, (3,3), padding='same', strides=(2,2),
activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Conv2D(64, (3,3), padding='same', strides=(2,2),
activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(units=128, return_sequences=False))
model.add(LSTM(units=64, return_sequences=False))
model.add(Dense(32))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
filenames = os.listdir("train/")
categories = []
for f_name in filenames:
decision = f_name.split('.')[0]
if decision == 'cat':
categories.append(0)
if decision == 'dog':
categories.append(1)
dataset = pd.DataFrame({
'filename':filenames,
'category':categories
})
dataset["category"] = dataset["category"].replace({0: 'cat', 1: 'dog'})
train_df,validate_df = train_test_split(dataset, test_size=0.20,random_state=42)
train_df = train_df.reset_index(drop=True)
validate_df = validate_df.reset_index(drop=True)
total_train = train_df.shape[0]
total_validate = validate_df.shape[0]
train_datagen = ImageDataGenerator(
rescale=1./255,
horizontal_flip=False)
train_generator = train_datagen.flow_from_directory(train_df,
"train/",
x_col='filename',
y_col='category',
target_size=img_size,
color_mode='rgb',
class_mode='categorical',
shuffle=True,
batch_size=batch_size)
validation_datagen = ImageDataGenerator(
rescale=1./255,
horizontal_flip=False)
validation_generator = validation_datagen.flow_from_directory(
validate_df,
"train/",
x_col='filename',
y_col='category',
target_size=img_size,
color_mode='rgb',
class_mode='categorical',
shuffle=False,
batch_size=batch_size
)
model.fit_generator(
train_generator,
epochs=epochs,
validation_data=validation_generator,
validation_steps=total_validate//batch_size,
steps_per_epoch=total_train//batch_size,
callbacks=callbacks,
class_weight=class_weight
)