1
votes

I've been trying to train a convlstm model on mnist dataset to broaden my knowledge in model development. I cannot escape the error that I have included in the title. Any help or hint is appreciated!

I know the default for strides is (1,1) but am unsure how 2 is being set.

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, CuDNNLSTM, TimeDistributed, Reshape
from keras.utils import to_categorical
from keras.layers.convolutional import Conv2D, Conv3D
from keras.layers.pooling import MaxPooling2D, MaxPool3D
from keras.layers.core import Flatten

def prep_pixels(train, test):
    # convert from integers to floats
    train_norm = train.astype('float32')
    test_norm = test.astype('float32')
    # normalize to range 0-1
    train_norm = train_norm / 255.0
    test_norm = test_norm / 255.0
    # return normalized images
    return train_norm, test_norm

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape((x_train.shape[0], 28, 28, 1))
x_test = x_test.reshape((x_test.shape[0], 28, 28, 1))

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

x_train, x_test = prep_pixels(x_train, x_test)

model = Sequential()

model.add(TimeDistributed(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))))
model.add(TimeDistributed((MaxPooling2D((2, 2)))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(32, activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))

opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, validation_data=(x_test, y_test))

ERROR

model.fit(x_train, y_train, epochs=1, validation_data=(x_test, y_test))

strides = _get_sequence(strides, n, channel_index, "strides")

ValueError: strides should be of length 1, 1 or 3 but was 2

1
Could you call model.add(TimeDistributed(Conv2D(64, (5, 5), padding='same', strides = 2))) and model.add(Activation('relu')) instead of strides = _get_sequence(strides, n, channel_index, "strides") and let me know about its exception. Mahsa Hassankashi
Thank you for your help. 'strides = _get_sequence(strides, n, channel_index, "strides")' is a part of the error. It isn't in a part of my code. Also, I tried what you said and it says the same error as above.Rohan J. Dani

1 Answers

0
votes

It seems like you haven't created a windowed dataset for your ConvLSTM. So you might want to do this before calling model.fit

d_train = tf.keras.preprocessing.sequence.TimeseriesGenerator(x_train, y_train, length=5, batch_size=64) # window size = 5
d_test = tf.keras.preprocessing.sequence.TimeseriesGenerator(x_test, y_test, length=5)
model.fit(d_train, epochs=1, validation_data=d_test)

To be consistent with your loss function, you will need to disable return sequences (or add another layer that does not return sequences).

model.add(tf.keras.layers.LSTM(32, activation='relu', return_sequences=False))