自动编码器导致 (61,61,3) 而不是 (64,64,3)

AutoEncoder Resulting In (61,61,3) instead of (64,64,3)

我正在尝试构建一个卷积自动编码器。这是我的架构。


def MainEncoder():
    inp = Input(shape=(64,64,3))
    x = Conv2D(256,2)(inp)
    x = MaxPool2D()(x)
    x = Conv2D(128,2)(x)
    x = Flatten()(x)
    encoded = Dense(100,activation="relu")(x)

    encoder= Model(inp,encoded)
    return encoder


def Decoder():
    enc = Input(shape=(100,))
    y = Dense(128)(enc)
    y = Dense(768)(y)
    y= Reshape((16,16,3))(y)
    y= Conv2DTranspose(128,(1,1),(2,2),padding='same')(y)
    y= Conv2DTranspose(128,(1,1),(2,2),padding='same')(y)
    decoded1 = Conv2D(3,1,padding="same")(y)
    decoder = Model(enc,decoded1)
    return decoder
encoder= MainEncoder()

decoderA = Decoder()
decoderB = Decoder()

print(encoder.summary())
print(decoderA.summary())
print(decoderB.summary())
input()
#decoder=  Model(encoded_input,decoded1)
#print(decoder.summary())
Inp = Input(shape=(64,64,3))
Inp2 = Input(shape=(64,64,3))
AutoEncoder1 = Model(Inp,decoderA(encoder(Inp)))
AutoEncoder2 = Model(Inp2,decoderB(encoder(Inp2)))
AutoEncoder1.summary()
AutoEncoder2.summary()
print(ot[0].shape)
input()
AutoEncoder1.compile(optimizer='adam',loss='mse')
AutoEncoder2.compile(optimizer='adam',loss='mse')
AutoEncoder1.fit(ot,ot,16,100)
AutoEncoder2.fit(kt,kt,16,100)
encoder.save(path+'encoder')
decoderA.save(path+'obama')
decoderB.save(path+'kimmel')



所有模型的输出和所有图像的形状根据总结是64,64,3。但是,每当我尝试添加准确度指标或只是测试自动编码器时,它总是会生成大小为 61、61、3 的图像。我真的不知道如何解决这个问题。任何帮助将不胜感激。

这里是测试代码


from numpy.core.shape_base import block
import tensorflow as tf

from tensorflow.keras.layers import *
from tensorflow.keras.models import *
import pickle
import numpy as np
import matplotlib.pyplot as plt
path = 'youtube_stuff2/'
ot = pickle.load(open(path+'oi.pickle','rb'))
kt = pickle.load(open(path+'ki.pickle','rb'))
ot = ot/255.0
kt = kt/255.0
encoder = load_model(path+'encoder')
obama = load_model(path+"obama")
kimmel = load_model(path+"kimmel")
print(ot[0].shape)
ott = np.array([ot[0]])
print(ott.shape)
thing = encoder.predict(ott)
image = obama.predict(thing)
print(image.shape)
#plt.imshow(ott[0])
plt.imshow(image[0])
plt.show()



可变图像的形状为 (61,61,3)

使用卷积时,需要注意的是图像边缘的像素点不会被保留。

如果你想让它们的形状相似,你可以在定义你的Conv2D时添加关键字“padding”并将其值设置为“same”。

这可能是它的样子:

def MainEncoder():
    inp = Input(shape=(64,64,3))
    x = Conv2D(256,2, padding="same")(inp)
    x = MaxPool2D()(x)
    x = Conv2D(128,2, padding="same")(x)
    x = Flatten()(x)
    encoded = Dense(100,activation="relu")(x)

    encoder= Model(inp,encoded)
    return encoder

当卷积进行时,此填充将在图像外部创建一个有效的黑色边框。

希望我有用