gpt4 book ai didi

python - 验证卷积编码器的结构

转载 作者:行者123 更新时间:2023-12-01 08:36:41 25 4
gpt4 key购买 nike

我正在构建一个卷积编码器来处理一些 128x128 图像 - 就像这样。

我一直通过使用 500 张图像的图像集来测试该结构。生成的解码图像基本上是全黑的(不是我想要的!)

我希望在这里得到一些建议,因为我认为我犯了一些明显的错误。

可以在此处下载一小组图像 -> https://www.dropbox.com/sh/0oj1p6sqal32cvx/AAAYQJSK2SPfynD8wYMSo9bPa?dl=0

当前代码

################################# SETUP #######################################

import glob
import pandas as pd
import numpy as np
import sys
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random

np.set_printoptions(threshold=np.nan)

######################### DATA PREPARATION #####################################

# create a list of XML files within the raw data folder
image_list = glob.glob("Images/test_images/*.jpeg")
print(image_list)

l = []
for i in image_list:
img = np.array(cv2.imread(i, 0))
l.append(img)
T = np.array(l)

# split into training and testing sets
labels = image_list
data_train, data_test, labels_train, labels_test = train_test_split(T, labels, test_size=0.5, random_state=42)

# convert to 0-1 floats (reconversion by * 255)
data_train = data_train.astype('float32') / 255.
data_test = data_test.astype('float32') / 255.
print(data_train.shape)

# reshape from channels first to channels last
data_train = np.rollaxis(data_train, 0, 3)
data_test = np.rollaxis(data_test, 0, 3)

######################### ENCODER MODELING #####################################

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(128, 128, 1)) # adapt this if using `channels_first` image data format

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

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

# create the model
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='mse')

# reference for reordering
data_train_dimensions = data_train.shape
data_test_dimensions = data_test.shape

# reshape the data sets
data_train = np.reshape(data_train, (data_train_dimensions[2], 128, 128, 1)) # adapt this if using `channels_first` image data format
data_test = np.reshape(data_test, (data_test_dimensions[2], 128, 128, 1))

from keras.callbacks import TensorBoard
autoencoder.fit(data_train, data_test,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(data_train, data_test),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

# create decoded images from model
decoded_imgs = autoencoder.predict(data_test)

# reorder columns
decoded_imgs = np.rollaxis(decoded_imgs, 3, 1)

# reshape from channels first to channels last
data_train = np.rollaxis(data_train, 0, 3)
data_test = np.rollaxis(data_test, 0, 3)

# convert to 0-1 floats (reconversion by * 255)
data_train = data_train.astype('float32') * 255.
data_test = data_test.astype('float32') * 255.

最佳答案

我认为主要问题是你适合 data_train, data_test而不是data_train, labels_train ,也就是说,您应该将模型拟合到样本和相应的输出上,但仅在输入上进行训练,这些输入由于 50/50 的分割而恰好具有兼容的形状。

如果模型的目的是从压缩表示中再现图像,那么您可以训练 fit(data_train, data_train, ..., validation_data=(data_test, data_test)) .

关于python - 验证卷积编码器的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com