似乎similar problem,尝试了它的解决方案,但得到了
AttributeError: 'Tensor' object has no attribute 'reshape'
我使用keras构建基于VGG16的模型,下面的代码如下所示
if K.image_data_format() == 'channels_first':
input_shape = (3, 256, 256)
else:
input_shape = (256, 256, 3)
input_image = Input(shape=input_shape)
base_model = VGG16( weights='imagenet', include_top=False, input_shape=input_shape)
这是基本模型输入输出
base_model.input
base_model.output
<tf.Tensor 'input_14:0' shape=(?, 256, 256, 3) dtype=float32>
<tf.Tensor 'block5_pool_13/MaxPool:0' shape=(?, 8, 8, 512) dtype=float32>
我们可以看到,输入通道为3
但输出通道为512
。
(不确定它是否与抛出的异常有关。)
据我所知,它与CNN的输入层中的频道不匹配。我不是很热来解决它?
以下是CNN层:
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
#model.add(Activation('softmax'))
model = Model(inputs=base_model.input, outputs=model(base_model.output))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
Model()
方法抛出以下错误:
ValueError: number of input channels does not match corresponding dimension of filter, 512 != 3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-66-bace1b0f7f30> in <module>()
23 model.add(Dense(3, activation='softmax'))
24 #model.add(Activation('softmax'))
---> 25 model = Model(inputs=base_model.input, outputs=model(base_model.output))
26 model.compile(loss='categorical_crossentropy',
27 optimizer='rmsprop',
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
617
618 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619 output = self.call(inputs, **kwargs)
620 output_mask = self.compute_mask(inputs, previous_mask)
621
/usr/local/lib/python3.6/dist-packages/keras/models.py in call(self, inputs, mask)
577 if not self.built:
578 self.build()
--> 579 return self.model.call(inputs, mask)
580
581 def build(self, input_shape=None):
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in call(self, inputs, mask)
2083 return self._output_tensor_cache[cache_key]
2084 else:
-> 2085 output_tensors, _, _ = self.run_internal_graph(inputs, masks)
2086 return output_tensors
2087
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in run_internal_graph(self, inputs, masks)
2233 if 'mask' not in kwargs:
2234 kwargs['mask'] = computed_mask
-> 2235 output_tensors = _to_list(layer.call(computed_tensor, **kwargs))
2236 output_masks = layer.compute_mask(computed_tensor,
2237 computed_mask)
/usr/local/lib/python3.6/dist-packages/keras/layers/convolutional.py in call(self, inputs)
166 padding=self.padding,
167 data_format=self.data_format,
--> 168 dilation_rate=self.dilation_rate)
169 if self.rank == 3:
170 outputs = K.conv3d(
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in conv2d(x, kernel, strides, padding, data_format, dilation_rate)
3339 strides=strides,
3340 padding=padding,
-> 3341 data_format=tf_data_format)
3342
3343 if data_format == 'channels_first' and tf_data_format == 'NHWC':
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py in convolution(input, filter, padding, strides, dilation_rate, name, data_format)
779 dilation_rate=dilation_rate,
780 name=name,
--> 781 data_format=data_format)
782 return op(input, filter)
783
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, strides, dilation_rate, name, data_format)
839 "number of input channels does not match corresponding dimension of "
840 "filter, {} != {}".format(input_channels_dim,
--> 841 filter_shape[num_spatial_dims]))
842
843 strides, dilation_rate = _get_strides_and_dilation_rate(
ValueError: number of input channels does not match corresponding dimension of filter, 512 != 3
答案 0 :(得分:1)
由于你无法对VGG16的输出做任何事情,我想你可以修改输入层:
将此添加为模型的第一层:
model.add(Reshape(target_shape=(128, 128, 2), input_shape=list(base_model.output.get_shape().as_list()[1:])))
重塑图层的作用是它接收input_shape
,然后将形状更改为target_shape
。只要输入和目标的大小都是常量(所有数字的乘积相同),就允许此操作。