我想在keras中合并两个模型 型号A:编码器+解码器 模型B:预训练图像分类器
模型A =编码器+解码器
model = Sequential()
model.add(layers.Conv2D(16, kernel_size=3, activation='relu', padding='same', input_shape=(128, 128, 3)))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Conv2D(8, kernel_size=3, activation='relu', padding='same'))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Conv2D(8, kernel_size=3, activation='relu', padding='same'))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Conv2D(8, kernel_size=3, activation='relu', padding='same'))
model.add(layers.UpSampling2D((2, 2)))
model.add(layers.Conv2D(8, kernel_size=3, activation='relu', padding='same'))
model.add(layers.UpSampling2D((2, 2)))
model.add(layers.Conv2D(16, kernel_size=3, activation='relu', padding='same'))
model.add(layers.UpSampling2D((2, 2)))
model.add(layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same'))
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_70 (Conv2D) (None, 128, 128, 16) 448
_________________________________________________________________
max_pooling2d_34 (MaxPooling (None, 64, 64, 16) 0
_________________________________________________________________
conv2d_71 (Conv2D) (None, 64, 64, 8) 1160
_________________________________________________________________
max_pooling2d_35 (MaxPooling (None, 32, 32, 8) 0
_________________________________________________________________
conv2d_72 (Conv2D) (None, 32, 32, 8) 584
_________________________________________________________________
max_pooling2d_36 (MaxPooling (None, 16, 16, 8) 0
_________________________________________________________________
conv2d_73 (Conv2D) (None, 16, 16, 8) 584
_________________________________________________________________
up_sampling2d_28 (UpSampling (None, 32, 32, 8) 0
_________________________________________________________________
conv2d_74 (Conv2D) (None, 32, 32, 8) 584
_________________________________________________________________
up_sampling2d_29 (UpSampling (None, 64, 64, 8) 0
_________________________________________________________________
conv2d_75 (Conv2D) (None, 64, 64, 16) 1168
_________________________________________________________________
up_sampling2d_30 (UpSampling (None, 128, 128, 16) 0
_________________________________________________________________
conv2d_76 (Conv2D) (None, 128, 128, 3) 435
=================================================================
Total params: 4,963
Trainable params: 4,963
Non-trainable params: 0
_________________________________________________________________
我加载了经过训练的图像分类器模型B,并尝试将其添加到模型A。
classifier = keras.models.load_model('./best_model/C-0.0000-1.0000.hdf5')
classifier.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_63 (Conv2D) (None, 128, 128, 16) 448
_________________________________________________________________
max_pooling2d_31 (MaxPooling (None, 64, 64, 16) 0
_________________________________________________________________
conv2d_64 (Conv2D) (None, 64, 64, 8) 1160
_________________________________________________________________
max_pooling2d_32 (MaxPooling (None, 32, 32, 8) 0
_________________________________________________________________
conv2d_65 (Conv2D) (None, 32, 32, 8) 584
_________________________________________________________________
max_pooling2d_33 (MaxPooling (None, 16, 16, 8) 0
_________________________________________________________________
conv2d_66 (Conv2D) (None, 16, 16, 8) 584
_________________________________________________________________
up_sampling2d_25 (UpSampling (None, 32, 32, 8) 0
_________________________________________________________________
conv2d_67 (Conv2D) (None, 32, 32, 8) 584
_________________________________________________________________
up_sampling2d_26 (UpSampling (None, 64, 64, 8) 0
_________________________________________________________________
conv2d_68 (Conv2D) (None, 64, 64, 16) 1168
_________________________________________________________________
up_sampling2d_27 (UpSampling (None, 128, 128, 16) 0
_________________________________________________________________
conv2d_69 (Conv2D) (None, 128, 128, 3) 435
=================================================================
Total params: 4,963
Trainable params: 4,963
Non-trainable params: 0
_________________________________________________________________
model.add(classifier)
我希望成功合并而不会出错。 但是有一条错误消息。 ValueError:输入张量必须具有等级4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
845 try:
--> 846 self.assert_same_rank(other)
847 new_dims = []
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in assert_same_rank(self, other)
890 raise ValueError("Shapes %s and %s must have the same rank" % (self,
--> 891 other))
892
ValueError: Shapes (?, 22) and (?, ?, ?, ?) must have the same rank
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
920 try:
--> 921 return self.merge_with(unknown_shape(rank=rank))
922 except ValueError:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
851 except ValueError:
--> 852 raise ValueError("Shapes %s and %s are not compatible" % (self, other))
853
ValueError: Shapes (?, 22) and (?, ?, ?, ?) are not compatible
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/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)
912 try:
--> 913 input_shape.with_rank(num_spatial_dims + 2)
914 except ValueError:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
922 except ValueError:
--> 923 raise ValueError("Shape %s must have rank %d" % (self, rank))
924
ValueError: Shape (?, 22) must have rank 4
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-35-ac51b5fc76f6> in <module>
----> 1 final_model = keras.models.Model(inputs=model.input, outputs=center_classifier(center_classifier.output))
~/.local/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
455 # Actually call the layer,
456 # collecting output(s), mask(s), and shape(s).
--> 457 output = self.call(inputs, **kwargs)
458 output_mask = self.compute_mask(inputs, previous_mask)
459
~/.local/lib/python3.6/site-packages/keras/engine/network.py in call(self, inputs, mask)
562 return self._output_tensor_cache[cache_key]
563 else:
--> 564 output_tensors, _, _ = self.run_internal_graph(inputs, masks)
565 return output_tensors
566
~/.local/lib/python3.6/site-packages/keras/engine/network.py in run_internal_graph(self, inputs, masks)
719 kwargs['mask'] = computed_mask
720 output_tensors = to_list(
--> 721 layer.call(computed_tensor, **kwargs))
722 output_masks = layer.compute_mask(computed_tensor,
723 computed_mask)
~/.local/lib/python3.6/site-packages/keras/layers/convolutional.py in call(self, inputs)
169 padding=self.padding,
170 data_format=self.data_format,
--> 171 dilation_rate=self.dilation_rate)
172 if self.rank == 3:
173 outputs = K.conv3d(
~/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in conv2d(x, kernel, strides, padding, data_format, dilation_rate)
3648 strides=strides,
3649 padding=padding,
-> 3650 data_format=tf_data_format)
3651
3652 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)
848 dilation_rate=dilation_rate,
849 name=name,
--> 850 data_format=data_format)
851 return op(input, filter)
852
/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)
914 except ValueError:
915 raise ValueError(
--> 916 "input tensor must have rank %d" % (num_spatial_dims + 2))
917
918 try:
ValueError: input tensor must have rank 4
我该如何解决此问题? 谢谢。