我的输入的形状为(1,12000,250,150,3),其CNN标签为(1,12000,2)。换句话说,我正在训练250x150x3图像上2类的CNN; [1,0]或[0,1]。
这最终是创建一个机器人来玩飞扬的鸟。有人告诉我,添加LSTM来同时对几个帧进行分类是可行的方法。到目前为止,我使用以下纯粹的CNN架构达到了0.984 val_acc。
model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=(250,150,3), activation='relu'))
model.add(Conv2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
#model.add(LSTM(100, input_shape=(32, 32, 19), return_sequences=True))
model.add(Dense(2))
model.add(Activation('sigmoid'))
model.summary()
准确性:
Epoch 15/100
12800/12800 [==============================] - 89s 7ms/step - loss: 0.0390 - acc: 0.9889 - val_loss: 0.1422 - val_acc: 0.9717
Epoch 16/100
12800/12800 [==============================] - 89s 7ms/step - loss: 0.0395 - acc: 0.9883 - val_loss: 0.0917 - val_acc: 0.9821ss: - ETA: 1s - loss: 0.0399 - acc:
Epoch 17/100
12800/12800 [==============================] - 89s 7ms/step - loss: 0.0357 - acc: 0.9902 - val_loss: 0.1383 - val_acc: 0.9816
Epoch 18/100
12800/12800 [==============================] - 89s 7ms/step - loss: 0.0452 - acc: 0.9871 - val_loss: 0.1153 - val_acc: 0.9750
Epoch 19/100
12800/12800 [==============================] - 90s 7ms/step - loss: 0.0417 - acc: 0.9892 - val_loss: 0.1641 - val_acc: 0.9668
Epoch 20/100
12800/12800 [==============================] - 90s 7ms/step - loss: 0.0339 - acc: 0.9904 - val_loss: 0.0927 - val_acc: 0.9840
我尝试添加一个LSTM层,但是我不确定出了什么问题:
ValueError Traceback (most recent call last)
<ipython-input-6-59e402ac3b8a> in <module>
26 model.add(Dropout(0.5))
27
---> 28 model.add(LSTM(100, input_shape=(32, 19), return_sequences=True))
29
30 model.add(Dense(2))
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\engine\sequential.py in add(self, layer)
179 self.inputs = network.get_source_inputs(self.outputs[0])
180 elif self.outputs:
--> 181 output_tensor = layer(self.outputs[0])
182 if isinstance(output_tensor, list):
183 raise TypeError('All layers in a Sequential model '
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
530
531 if initial_state is None and constants is None:
--> 532 return super(RNN, self).__call__(inputs, **kwargs)
533
534 # If any of `initial_state` or `constants` are specified and are Keras
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
412 # Raise exceptions in case the input is not compatible
413 # with the input_spec specified in the layer constructor.
--> 414 self.assert_input_compatibility(inputs)
415
416 # Collect input shapes to build layer.
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)
309 self.name + ': expected ndim=' +
310 str(spec.ndim) + ', found ndim=' +
--> 311 str(K.ndim(x)))
312 if spec.max_ndim is not None:
313 ndim = K.ndim(x)
ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2
Keras docs说LSTM的参数是(单位,输入形状)等等。我还读到某个地方不再需要TimeDistributed(),所以没有包含它。在为LSTM计算输入形状时我是否犯了一个错误,还是我完全遗漏了其他东西?
编辑1:我删除了flatten()层,并将LSTM层移到了conv层之后,fc层之前。我还添加了reshape(),以便将第4个conv层的4个dim输出重塑为3个dim,然后可以将其输入到LSTM层。
model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=(250,150,3), activation='relu'))
model.add(Conv2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
output_1 = model.output_shape
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
output_2 = model.output_shape
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
output_3 = model.output_shape
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
output_4 = model.output_shape
model.add(Reshape((15, 9)))
output_5 = model.output_shape
model.add(LSTM(100, input_shape=(15, 9, 256), return_sequences=True))
这些是每个输出的形状:
Conv_1: (None, 125, 75, 32)
Conv_2: (None, 62, 37, 64)
Conv_3: (None, 31, 18, 128)
Conv_4: (None, 15, 9, 256)
当我尝试重塑conv_4以使LSTM获得3个暗淡输入时,会发生以下情况:
ValueError Traceback (most recent call last)
<ipython-input-21-7f5240e41ae4> in <module>
22 output_4 = model.output_shape
23
---> 24 model.add(Reshape((15, 9)))
25 output_5 = model.output_shape
26 model.add(LSTM(100, input_shape=(15, 9, 256), return_sequences=True))
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\engine\sequential.py in add(self, layer)
179 self.inputs = network.get_source_inputs(self.outputs[0])
180 elif self.outputs:
--> 181 output_tensor = layer(self.outputs[0])
182 if isinstance(output_tensor, list):
183 raise TypeError('All layers in a Sequential model '
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
472 if all([s is not None
473 for s in to_list(input_shape)]):
--> 474 output_shape = self.compute_output_shape(input_shape)
475 else:
476 if isinstance(input_shape, list):
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\layers\core.py in compute_output_shape(self, input_shape)
396 # input shape known? then we can compute the output shape
397 return (input_shape[0],) + self._fix_unknown_dimension(
--> 398 input_shape[1:], self.target_shape)
399
400 def call(self, inputs):
E:\Applications\Anaconda3\envs\pygpu\lib\site-packages\keras\layers\core.py in _fix_unknown_dimension(self, input_shape, output_shape)
384 output_shape[unknown] = original // known
385 elif original != known:
--> 386 raise ValueError(msg)
387
388 return tuple(output_shape)
ValueError: total size of new array must be unchanged
非常感谢您的帮助。
答案 0 :(得分:0)
首先,我在您的模型中没有看到lstm,它只有4个convo到3个完全连接对不对? 为什么您一个接一个地拥有2个Conv2D?
我会在框架上使用LSTM,而不是在展平后立即进行第一次完全连接。
我在Keras中不知道,但是任何RNN单元中的输入都是3D数组,例如: (批处理大小,最大序列,项目)或(max_sequence,bach_size,项目),第二种格式有点奇怪。
您得到的错误是:
expected ndim=3, found ndim=2
所以我想您输入2D数组而不是3D
您已修改展平图以创建有效的3D输入。 例如,您可以通过输入5d但使用2d convo来做到这一点,例如: bach大小= 100,框架= 3,通道= 3,项目= 28,28(高度,宽度),展平为 (100,3,-1),其中-1代表休息。
我需要自己尝试类似的东西,但是我正在用火炬做...
答案 1 :(得分:0)
如果我向下滚动几下,我会在文档中找到ConvLSTM2D,这应该可以解决我的问题。立即尝试