尽管已经编译了合并的模型,

时间:2019-07-13 07:23:55

标签: python machine-learning keras neural-network conv-neural-network

我正在尝试训练AI以根据图像和患者信息识别病变。我正在使用Keras的顺序模型来做到这一点。我制作两个顺序模型,然后合并它们并编译合并的模型。 当我尝试拟合模型时,即使模型定义了输入形状,也会出现错误RuntimeError: You must compile your model before using it.

我尝试将input_dim = dim切换为input_shape =(dim,)。 我只能在问题上找到诸如this postthis one之类的东西,只是要确保要合并的模型中的第一层具有已定义的input_shape,而我的拥有。我无法想象您也必须对“连接”层执行此操作。

我首先为患者信息创建密集层:

metadata_model = Sequential()
metadata_model.add(Dense(32, input_dim=X_train.iloc[:, L*W:].shape[1], activation="relu"))
metadata_model.add(Dense(64))

然后是图像的模型:

model = Sequential()
model.add(Conv2D(32, (3, 3), padding="same", input_shape=(W, L, 3)))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=-1))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(rate = 0.25))
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=-1))
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=-1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=-1))
model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=-1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))

然后我将它们合并:

merged_model = Sequential()
merged_model.add(Concatenate([model, metadata_model]))
merged_model.add(Dense(7)) #7 lesion classes
merged_model.add(Activation("softmax"))

编译并创建ImageDataGenerator:

opt = Adam(lr=INIT_LR, decay=INIT_LR/EPOCHS)
merged_model.compile(loss="categorical_crossentropy", optimizer = opt, metrics=["accuracy"])
aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest")

并尝试对其进行培训:

train = merged_model.fit_generator(
aug.flow([trainInput, X_train.iloc[:, L*W:]], labels, batch_size=BS),
validation_data=([testInput, X_test.iloc[:, L*W:]], labels_test),
steps_per_epoch=500,
epochs=EPOCHS,
verbose=1)

此行导致以下错误:

RuntimeError                              Traceback (most recent call last)
<ipython-input-114-fc6c254db390> in <module>
      4 steps_per_epoch=500,
      5 epochs=EPOCHS,
----> 6 verbose=1)

c:\users\megag\appdata\local\programs\python\python37\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

c:\users\megag\appdata\local\programs\python\python37\lib\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)
   1416             use_multiprocessing=use_multiprocessing,
   1417             shuffle=shuffle,
-> 1418             initial_epoch=initial_epoch)
   1419 
   1420     @interfaces.legacy_generator_methods_support

c:\users\megag\appdata\local\programs\python\python37\lib\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)
     38 
     39     do_validation = bool(validation_data)
---> 40     model._make_train_function()
     41     if do_validation:
     42         model._make_test_function()

c:\users\megag\appdata\local\programs\python\python37\lib\site-packages\keras\engine\training.py in _make_train_function(self)
    494     def _make_train_function(self):
    495         if not hasattr(self, 'train_function'):
--> 496             raise RuntimeError('You must compile your model before using it.')
    497         self._check_trainable_weights_consistency()
    498         if self.train_function is None:

RuntimeError: You must compile your model before using it.

1 个答案:

答案 0 :(得分:2)

您的合并模型不再是顺序的(因为它具有两个输入层/分支),因此您不能使用顺序API。相反,您需要使用Functional API of Keras来合并模型:

from keras.models import Model

x = Concatenate()([model.output, metadata_model.output])
x = Dense(7)(x)
out = Activation("softmax")(x)

merged_model = Model([model.input, metadata_model.input], out)

# the rest is the same...