keras: модель cnn + lstm с использованием ошибки времени выполнения распределенного слоя - PullRequest
0 голосов
/ 24 июня 2018

Я использую 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.

Какие могут быть возможные ошибки. Спасибо

1 Ответ

0 голосов
/ 27 октября 2018

Чтобы использовать TimeDistributed в качестве входного слоя, вы должны указать input_shape в конструкторе TimeDistributed, а не в 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)))
...