所以我有2个图像,img1
和img2
都有shape=(20,20)
,其中我expand_dims
到(1,20,20)
1是批量大小并将它们提供给网络,但我得到以下错误:
ValueError: Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](Placeholder, conv2d/Conv2D/ReadVariableOp)' with input shapes: [?,1,20,20], [3,3,20,32]. ```
def mean_squared_error(y_true, y_pred):
return tf.keras.metrics.mean_squared_error(y_true, y_pred)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(1,20,20)))
model.add(Conv2D(1, kernel_size=(3, 3),
activation='relu'))
model.compile(optimizer='adam', loss=mean_squared_error, metrics=[mean_squared_error, 'accuracy'])
# Train
model.fit(img1, img2)
卷积层减少了输入的维度,但是IIUC,您试图将mse
应用于模型的输出和img2
。所以试试这样写:
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, kernel_size=(2, 2),
activation='relu',
input_shape=(20, 20, 1)))
model.add(tf.keras.layers.Conv2DTranspose(1, kernel_size=(2, 2),
activation='relu'))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# Train
img1 = tf.random.normal((1, 20, 20))
img2 = tf.random.normal((1, 20, 20))
model.fit(img1, img2)