如何在keras tensorflow中将图像作为输入并获取另一个图像作为输出
Posted
技术标签:
【中文标题】如何在keras tensorflow中将图像作为输入并获取另一个图像作为输出【英文标题】:How to give an image as input and get another image as output in keras tensorflow 【发布时间】:2021-12-23 23:44:58 【问题描述】:我有 zigsaw 拼图图像和对应的对。我想将图像作为模型的输入并找到对应的对。我制作了以下模型,在训练。但是当我通过测试图像数组时,它会预测一个具有所有 nan 值的数组。我应该改变我的损失函数吗?请查看图片下方的代码
in_shape=(32,256,256,3)
model1=models.Sequential(
[
resize_and_rescale,
layers.Conv2D(32,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.Dropout(0.1),
layers.Conv2D(32,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, kernel_size = (3,3), activation='relu',padding='same'),
layers.Dropout(0.1),
layers.Conv2D(64,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(128, kernel_size = (3,3), activation='relu',padding='same'),
layers.Dropout(0.1),
layers.Conv2D(128,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(256, kernel_size = (3,3), activation='relu',padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2D(3, (1, 1), activation='sigmoid')
]
)
model1.build(input_shape=in_shape)
model1.compile(
optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics=['accuracy']
)
【问题讨论】:
回答有用吗? 【参考方案1】:您可以在您的情况下使用tf.keras.losses.MeanSquaredError()
,并且在提供您的input_shape
时,您不要指定batch_size
。您应该只提供宽度、高度和通道:(height, width, channels
--> (256, 256, 3)
。
import tensorflow as tf
model1=tf.keras.Sequential(
[
tf.keras.layers.Conv2D(32,(3,3),activation="relu",input_shape=(256, 256, 3),padding='same'),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Conv2D(32,(3,3),activation="relu",padding='same'),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Conv2D(64, kernel_size = (3,3), activation='relu',padding='same'),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Conv2D(64,(3,3),activation="relu",padding='same'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(128, kernel_size = (3,3), activation='relu',padding='same'),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Conv2D(128,(3,3),activation="relu",padding='same'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(256, kernel_size = (3,3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same'),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same'),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same'),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same'),
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
tf.keras.layers.Conv2D(3, (1, 1), activation='sigmoid')])
model1.compile(
optimizer='adam',
loss=tf.keras.losses.MeanSquaredError()
)
model1.fit(tf.random.normal((32, 256, 256, 3)), tf.random.normal((32, 256, 256, 3)))
【讨论】:
以上是关于如何在keras tensorflow中将图像作为输入并获取另一个图像作为输出的主要内容,如果未能解决你的问题,请参考以下文章
在 keras 中将“learning_phase”用于 tensorflow 后端?
TensorFlow 2.0 Keras:如何为TensorBoard编写图像摘要
TensorFlow 2.0 Keras:如何为 TensorBoard 编写图像摘要