title: How to Implement CNN2D+ LSTM Model For Image Classification in Keras?
body: <p>There is 2550 images as train set and 1530 images as test set. to classify these images into two classes, a hybrid deep learning model including CNN2D+LSTM is used but there is an error is occurred during running the code as is shown below. i was wonder if someone help me to solve it. thanks in advance</p>
  <p>ERROR:</p>
  <blockquote>
  <p>RuntimeError: You must compile your model before using it</p>
  </blockquote>
  <pre><code># importing libraries
  from keras.models import Sequential
  from keras.layers import Convolution2D
  from keras.layers import MaxPooling2D
  from keras.layers import Flatten
  from keras.layers import Dense
  from keras.layers import LSTM
  from keras.layers import TimeDistributed
  
  from keras.preprocessing.image import ImageDataGenerator
  train_datagen = ImageDataGenerator(
                                      rescale=1./255,
                                      shear_range=0.2,
                                      zoom_range=0.2,
                                      horizontal_flip=True)
  test_datagen = ImageDataGenerator(rescale=1./255)
  training_set = train_datagen.flow_from_directory(
                                      'D:\\thesis\\Paper 3\\Feature Extraction\\two_dimension_Feature_extraction\\stft_feature\\Training_set',
                                      target_size=(64, 64),
                                      batch_size=32,
                                      class_mode='binary')
  test_set = test_datagen.flow_from_directory(
                                      'D:\\thesis\\Paper 3\\Feature Extraction\\two_dimension_Feature_extraction\\stft_feature\\Test_set',
                                      target_size=(64, 64),
                                      batch_size=32,
                                      class_mode='binary')
  
  #initializing the CNN
  classifier = Sequential()
  
  #convolution2D
  classifier.add(TimeDistributed(Convolution2D(32,3,3, input_shape = (64,64,3), activation = 'relu'))) #32 feature detector with 3*3 dimensions, 64*64 is the used format with 3 channel because the image is colored
  
  #adding maxpooling 
  classifier.add(TimeDistributed(MaxPooling2D(2, 2)))
  #Flattening
  classifier.add(TimeDistributed(Flatten()))
  
  classifier.add(TimeDistributed(classifier))
  classifier.add(LSTM(units= 20, input_shape = (1,5), return_sequences = True ))
  classifier.add(LSTM(units = 20))
  
  #Full Connection
  classifier.add(Dense(output_dim = 128, activation = 'relu'))
  classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))
  
  #compiling the CNN
  classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
  
  #Fitting the CNN to the images
  
  history = classifier.fit_generator(training_set,
                           steps_per_epoch=2550,
                           epochs=25,
                           validation_data= test_set,
                           validation_steps=510)
  
  import matplotlib.pyplot as plt
  acc = history.history['acc']
  val_acc = history.history['val_acc']
  loss = history.history['loss']
  val_loss = history.history['val_loss']
  epochs = range(1, len(acc) + 1)
  plt.plot(epochs, acc, 'r', label='Training acc')
  plt.plot(epochs, val_acc, 'b', label='Validation acc')
  plt.title('Training and validation accuracy')
  plt.legend()
  plt.figure()
  plt.plot(epochs, loss, 'r', label='Training loss')
  plt.plot(epochs, val_loss, 'b', label='Validation loss')
  plt.title('Training and validation loss')
  plt.plot()
  plt.legend()
  plt.show()
  
  test_loss, test_acc = classifier.evaluate(test_set)
  print('test_acc:', test_acc)
  </code></pre>