在Keras中的图像到图像映射

huangapple go评论74阅读模式
英文:

Image to image mapping in keras

问题

我是新手对机器学习不太了解。我有一个2D数组(图像),需要映射到另一个2D数组(图像)。所有示例都使用具有输出为1的Dense层,用于分类问题。但我的问题是简单的映射,将图像映射到另一个图像上。
在Keras中,如何指定输出为2D数组(图像)呢?请查看下面的示例代码。我需要更改架构以使输出与输入(2D数组)具有相同的大小。

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
# Change the following line to have the desired output shape (2Darray)
model.add(Dense(output_shape_here))
model.add(Activation('your_activation_function'))

请将output_shape_here替换为所需的输出形状,以获得与输入相同大小的2D数组输出。

英文:

I am new to machine learning. I have a 2D array(image) need to be mapped into another 2D array(image). All examples are using a Dense layer with output of 1, in classification problem. But, my problem is simple mapping and image to another image.
How can I specify that output 2Darray(image) in keras. Please find below, a sample code. I need to change the architecture to give me an output of same size as input(2Darray).

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

答案1

得分: 3

你需要的是一些自编码器的对称结构。与普通的架构不同,你的输出将是3D的,因为你的输出是图片。然后,你用与输入/输出相同的数据拟合模型。以下是一个使用生成数据的简单示例:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, UpSampling2D
import numpy as np

input = np.random.rand(10, 30, 30)

input = input[..., None] # keras需要4D输入,所以添加1个维度

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(30, 30, 1), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same'))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(input, input, batch_size=8, epochs=1)
英文:

What you need is some autoencoder symmetrical structure. Unlike normal architectures, your output will be in 3D because you have pictures as output. You then fit the model with the same data as input/output. Here's a simple example with generated data:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, UpSampling2D
import numpy as np

input = np.random.rand(10, 30, 30)

input = input[..., None] # keras needs 4D input, so add 1 dimension

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(30, 30, 1), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same'))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(input, input, batch_size=8, epochs=1)

huangapple
  • 本文由 发表于 2020年1月3日 21:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定