【问题标题】:keras: cnn+lstm model using time distributed layer runtime errorkeras: cnn+lstm 模型使用时间分布层运行时错误
【发布时间】:2018-12-03 08:31:23
【问题描述】:

我正在使用 cnn 和 lstm 模型,使用时间分布层进行图像分类。虽然我已经编译了模型,但它仍然显示

RuntimeError: You must compile your model before using it.

我在多个网站上进行了搜索,但找不到解决问题的方法。 这是我的代码:

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 Dropout
from keras.layers import TimeDistributed
from keras.layers import LSTM

import warnings
warnings.filterwarnings('ignore')

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(TimeDistributed(Convolution2D(32, (3, 3), padding = 'same', input_shape = (128, 128, 3), 
                                             activation = 'relu')))

# Step 2 - 
classifier.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))

# Adding a second convolutional layer
classifier.add(TimeDistributed(Convolution2D(64, (3, 3), padding = 'same', activation = 'relu')))
classifier.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))

# Adding a third conolutional layer
classifier.add(TimeDistributed(Convolution2D(64, (3, 3), padding = 'same', activation = 'relu')))
classifier.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))

# Step 3 - Flattening
classifier.add(TimeDistributed(Flatten()))
classifier.add(Dropout(rate = 0.5))

# Step 4 - Full connection
classifier.add(LSTM(256, return_sequences=False, dropout=0.5))
classifier.add(Dense(output_dim = 8, activation = 'softmax'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   height_shift_range =  0.1,
                                   width_shift_range = 0.1,
                                   channel_shift_range = 10)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('dataset/mel/train/',
                                                 target_size = (128, 128),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory('dataset/mel/test/',
                                            target_size = (128, 128),
                                            batch_size = 32,
                                            class_mode = 'categorical')

classifier.fit_generator(training_set,
                         samples_per_epoch = 1088,
                         nb_epoch = 1,
                         validation_data = test_set,
                         nb_val_samples = 352)

这是完整的输出消息:

Found 1088 images belonging to 8 classes.
Found 352 images belonging to 8 classes.
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-8-6a3839aea8f8> in <module>()
     81                          nb_epoch = 1,
     82                          validation_data = test_set,
---> 83                          nb_val_samples = 352)

~/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name +
     90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/.local/lib/python3.5/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1424             use_multiprocessing=use_multiprocessing,
   1425             shuffle=shuffle,
-> 1426             initial_epoch=initial_epoch)
   1427 
   1428     @interfaces.legacy_generator_methods_support

~/.local/lib/python3.5/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
     35 
     36     do_validation = bool(validation_data)
---> 37     model._make_train_function()
     38     if do_validation:
     39         model._make_test_function()

~/.local/lib/python3.5/site-packages/keras/engine/training.py in _make_train_function(self)
    482     def _make_train_function(self):
    483         if not hasattr(self, 'train_function'):
--> 484             raise RuntimeError('You must compile your model before using it.')
    485         self._check_trainable_weights_consistency()
    486         if self.train_function is None:

RuntimeError: You must compile your model before using it.

可能的错误是什么。 谢谢

【问题讨论】:

    标签: keras deep-learning python-3.5 lstm convolutional-neural-network


    【解决方案1】:

    为了使用 TimeDistributed 作为输入层,您必须在 TimeDistributed 构造函数中指定 input_shape,而不是 Convolution one(或您想要分配的任何层)。请记住,您必须在此构造函数中提供时间步长(帧)数。在您的情况下,它看起来像这样:

    num_frames = 10 # e.g.
    # Step 1 - Convolution
    classifier.add(TimeDistributed(Convolution2D(32, (3, 3), padding = 'same', activation = 
    'relu'), input_shape = (num_frames,128, 128, 3)))
    

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2020-02-24
      • 2021-05-25
      • 2017-10-14
      相关资源
      最近更新 更多