Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

tensorflow - Python Keras Convolutional autoencoder. ValueError: logits and labels must have the same shape

I took most of this code from here. I am very new to autoencoders.

Heres the code:

input_img = keras.Input(shape=(64, 55,1))

x = keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = keras.layers.MaxPooling2D((2, 2), padding='same')(x)

x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
decoded = keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

onlyfiles = [f for f in listdir("newnew") if isfile(join("newnew", f))][:2000]
lis=[]
for i in onlyfiles:
    image = Image.open('newnew/'+i)
    data = np.asarray(image)
    lis.append(data)
    
lis = np.array(lis)
lis = lis.astype("float32")
lis = lis / np.max(lis)
print(lis.shape)
lis = lis.reshape((len(lis),64,55,1))
print(lis.shape)

test = lis[:100]
train = lis[100:]

autoencoder.fit(train,train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(test,test))

As I was just trying to get the code to run, I limited it to only 2000 images. For context, the images are greyscale and already 55x64.

The error message I am recieving is:

ValueError: logits and labels must have the same shape ((None, 64, 56, 1) vs (None, 64, 55, 1))

After reading similar problems on stack overflow, I have already made one change to the website I took the autoencoder from, by adding an additional padding='same' to one of the lines which was missing it. I believe the root of the problem is somewhere in the layers because originally before adding the extra "padding=same", the error message was saying:

ValueError: logits and labels must have the same shape ((None, 60, 52, 1) vs (None, 64, 55, 1))

Unfortunately as I say I'm new to autoencoders and still don't fully understand all the numbers in layers. Any help would be appreciated :)

question from:https://stackoverflow.com/questions/65842157/python-keras-convolutional-autoencoder-valueerror-logits-and-labels-must-have

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...