自动编码器产生(61,61,3)而不是(64,64,3)

2024-09-29 20:18:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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


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)


Tags: pathimportencoderinputmodelloadotsummary
1条回答
网友
1楼 · 发布于 2024-09-29 20:18:22

使用卷积运算时,需要注意图像边缘上的像素不会被保留

如果希望它们具有相似的形状,可以添加关键字“padding”,并在定义Conv2D时将其值设置为“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

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

我希望我是有用的

相关问题 更多 >

    热门问题