您好,我想演示该问题,而不必让您查看所有代码,因此我选择了要展示的部分。请让我知道是否需要进一步说明:)
使用经过培训的功能性API模型进行预测时,详细信息:
from keras.models import Model
from keras.layers import Input
from keras.layers import Concatenate
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D, GlobalMaxPooling1D
import keras.backend as K
# first input model
visible1 = Input(shape=(128,128,1))
conv11 = Conv2D(32, kernel_size=4, activation='relu',padding='same')(visible1)
pool11 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu',padding='same')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv12)
flat1 = Flatten()(pool12)
# second input model
visible2 = Input(shape=(128,128,1))
conv21 = Conv2D(32, kernel_size=4, activation='relu',padding='same')(visible2)
pool21 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu',padding='same')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv22)
flat2 = Flatten()(pool22)
# third input model
visible3 = Input(shape=(128,128,1))
conv31 = Conv2D(32, kernel_size=4, activation='relu',padding='same')(visible3)
pool31 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv31)
conv32 = Conv2D(16, kernel_size=4, activation='relu',padding='same')(pool31)
pool32 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv32)
flat3 = Flatten()(pool32)
# fourth input model
visible4 = Input(shape=(128,128,1))
conv41 = Conv2D(32, kernel_size=4, activation='relu',padding='same')(visible4)
pool41 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv41)
conv42 = Conv2D(16, kernel_size=4, activation='relu',padding='same')(pool41)
pool42 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv42)
flat4 = Flatten()(pool42)
# fifth input model
visible5 = Input(shape=(128,128,1))
conv51 = Conv2D(32, kernel_size=4, activation='relu',padding='same')(visible5)
pool51 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv51)
conv52 = Conv2D(16, kernel_size=4, activation='relu',padding='same')(pool51)
pool52 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv52)
flat5 = Flatten()(pool52)
# sixth input model
visible6 = Input(shape=(128,128,1))
conv61 = Conv2D(32, kernel_size=4, activation='relu',padding='same')(visible6)
pool61 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv61)
conv62 = Conv2D(16, kernel_size=4, activation='relu',padding='same')(pool61)
pool62 = MaxPooling2D(pool_size=(2, 2),padding='same')(conv62)
flat6 = Flatten()(pool62)
# merge input models
merge = concatenate([flat1,flat2,flat3,flat4,flat5,flat6])
flatten = Flatten()(merge)
intrp1 = Dense(64)(flatten)
intrp2 = Dense(32)(intrp1)
output = Dense(1)(intrp2)
model = Model(inputs=[visible1,visible2,visible3,visible4,visible5,visible6], outputs=output)
培训没有问题,但是在尝试预测时遇到此错误:
ValueError:用户代码中:
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1147 predict_function *
outputs = self.distribute_strategy.run(
/opt/conda/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1122 predict_step **
return self(x, training=False)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:927 __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py:719 call
convert_kwargs_to_constants=base_layer_utils.call_context().saving)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py:888 _run_internal_graph
output_tensors = layer(computed_tensors, **kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:886 __call__
self.name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility
' but received input with shape ' + str(shape))
ValueError: Input 0 of layer dense_4 is incompatible with the layer: expected axis -1 of input shape to have value 98304 but received input with shape [32, 3072]
我一直在检查每一层的形状,似乎是在预测
merge = concatenate([flat1,flat2,flat3,flat4,flat5,flat6])
输出的形状为(1,32,3072)而不是(1,98304),但是当我再次尝试使其变平时,它保持不变。我该如何解决?谢谢