我有两种不同类型的图像(相机图像及其对应的草图)。网络的目标是找到两个图像之间的相似性。
网络由单个编码器和单个解码器组成。单个编码器-解码器背后的动机是在它们之间共享权重。
input_img = Input(shape=(img_width,img_height, channels))
def encoder(input_img):
# Photo-Encoder Code
pe = Conv2D(96, kernel_size=11, strides=(4,4), padding = 'SAME')(left_input) # (?, 64, 64, 96)
pe = BatchNormalization()(pe)
pe = Activation('selu')(pe)
pe = MaxPool2D((3, 3), strides=(2, 2), padding = 'VALID')(pe) # (?, 31, 31, 96)
pe = Conv2D(256, kernel_size=5, strides=(1,1), padding = 'SAME')(pe) # (?, 31, 31, 256)
pe = BatchNormalization()(pe)
pe = Activation('selu')(pe)
pe = MaxPool2D((3, 3), strides=(2, 2), padding = 'VALID')(pe) #(?, 15, 15, 256)
pe = Conv2D(384, kernel_size=3, strides=(1,1), padding = 'SAME')(pe) # (?, 15, 15, 384)
pe = BatchNormalization()(pe)
pe = Activation('selu')(pe)
pe = Conv2D(384, kernel_size=3, strides=(1,1), padding = 'SAME')(pe) # (?, 15, 15, 384)
pe = BatchNormalization()(pe)
pe = Activation('selu')(pe)
pe = Conv2D(256, kernel_size=3, strides=(1,1), padding = 'SAME')(pe) # (?, 15, 15, 256)
pe = BatchNormalization()(pe)
pe = Activation('selu')(pe)
encoded = MaxPool2D((3, 3), strides=(2, 2), padding = 'VALID')(pe) # (?, 7, 7, 256)
return encoded
def decoder(pe):
pe = Conv2D(1024, kernel_size=7, strides=(1, 1), padding = 'VALID')(pe)
pe = BatchNormalization()(pe)
pe = Activation('selu')(pe)
p_decoder_inp = Reshape((2,2,256))(pe)
pd = Conv2DTranspose(128, kernel_size=5, strides=(2, 2), padding='SAME')(p_decoder_inp)
pd = Activation("selu")(pd)
pd = Conv2DTranspose(64, kernel_size=5, strides=(2, 2), padding='SAME')(pd)
pd = Activation("selu")(pd)
pd = Conv2DTranspose(32, kernel_size=5, strides=(2, 2), padding='SAME')(pd)
pd = Activation("selu")(pd)
pd = Conv2DTranspose(16, kernel_size=5, strides=(2, 2), padding='SAME')(pd)
pd = Activation("selu")(pd)
pd = Conv2DTranspose(8, kernel_size=5, strides=(2, 2), padding='SAME')(pd)
pd = Activation("selu")(pd)
pd = Conv2DTranspose(4, kernel_size=5, strides=(2, 2), padding='SAME')(pd)
pd = Activation("selu")(pd)
decoded = Conv2DTranspose(3, kernel_size=5, strides=(2, 2), padding='SAME', activation='sigmoid')(pd) # (?, ?, ?, 3)
return decoded
siamsese_net = Model([camera_img, sketch_img], [decoder(encoder(camera_img)), decoder(encoder(sketch_img))])
siamsese_net.summary()
当我看到网络时,它显示了两个不同的网络。
但是我想要的是一个网络,它接受两个输入,例如摄像机图像和草图图像,并使用单个编码器-解码器返回相同的图像。
我在哪里做错了?
您的“功能”不是“模型”,而是“创建者”。
更新两个功能,例如:
def create_encoder(): #no arguments!!!
pe = Input(shape=(img_width,img_height, channels))
....
encoded = ...
encoder = Model(pe, encoded)
return encoder
def create_decoder():
pe = Input(shape=(7,7,256))
....
decoded = ....
decoder = Model(pe, decoded)
return decoder
现在创建模型:
encoder = create_encoder()
decoder = create_decoder()
siamsese_net = Model([camera_img, sketch_img],
[decoder(encoder(camera_img)), decoder(encoder(sketch_img))])
#where camera_img and sketch_image are 'Input' objects.