我正在尝试调整这个简单的自动编码器代码: https ://github.com/tflearn/tflearn/blob/master/examples/images/autoencoder.py 。我正在尝试以使用卷积层的方式更改代码,并输入 488 个图像 * 30 个高度 * 30 个宽度 * 3 个颜色通道(RGB)[488、30、30、3] 并输出一个新图像看起来与原版相似但不同。我没有使用任何类型的验证数据集(我不关心过度拟合,除了帮助防止过度拟合之外,我没有看到使用验证数据集的任何其他原因。我可能完全错了,我想知道如果是这样)。我是新手,对构建不佳的编码器和解码器感到抱歉。
# Data loading and preprocessing
from reading import *
X = getDataColor() #A function that read my img data
total_samples = len(X)
np.random.seed(42) # For debugging and visualization purposes
X = np.reshape(X, newshape=[total_samples, 30, 30, 3])
X = X.astype('float32') / 255. #For scaling
# Building the encoder
encoder = tflearn.input_data(shape=[None, 30, 30, 3])
encoder = tflearn.conv_2d(encoder,16, 3, activation='relu', padding='same', regularizer='L2')
encoder = tflearn.max_pool_2d(encoder,[2,2], padding='same')
encoder = tflearn.conv_2d(encoder,8, [3,3], activation='relu', padding='same')
encoder = tflearn.max_pool_2d(encoder,[2,2], padding='same')
encoder = tflearn.conv_2d(encoder,8, [3,3], activation='relu', padding='same')
encoder = tflearn.max_pool_2d(encoder,[2,2], padding='same')
# Building the decoder
decoder = tflearn.conv_2d(encoder,8, [3,3], activation='relu', padding='same')
decoder = tflearn.upsample_2d(decoder,[2,2])
decoder = tflearn.conv_2d(decoder,8, [3,3], activation='relu', padding='same')
decoder = tflearn.upsample_2d(decoder,[2,2])
decoder = tflearn.conv_2d(decoder,16, [3,3], activation='relu', padding='same')
decoder = tflearn.upsample_2d(decoder,[2,2])
decoder = tflearn.conv_2d(decoder,1, [3,3], activation='relu', padding='same')
# Regression, with mean square error
net = tflearn.regression(decoder, optimizer='adam', learning_rate=0.001,
loss='mean_square', metric=None)
# Training the auto encoder
model = tflearn.DNN(net, tensorboard_verbose=0)
gen_noise = np.random.uniform(-1, 1., size=[total_samples, 30, 30, 3])
#I'm trying to generate images based on this noise
#I couldn't think of any other way...
model.fit(gen_noise, X, n_epoch =10000,
run_id="auto_encoder", batch_size=total_samples)
尝试运行完整代码时出现错误:
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 488
Validation samples: 0
--
Traceback (most recent call last):
File "autoCNN.py", line 66, in <module>
run_id="auto_encoder", batch_size=total_samples)
File "F:\Users\Kai\Miniconda3\lib\site-packages\tflearn\models\dnn.py", line 216, in fit
callbacks=callbacks)
File "F:\Users\Kai\Miniconda3\lib\site-packages\tflearn\helpers\trainer.py", line 339, in fit
show_metric)
File "F:\Users\Kai\Miniconda3\lib\site-packages\tflearn\helpers\trainer.py", line 818, in _train
feed_batch)
File "F:\Users\Kai\Miniconda3\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
run_metadata_ptr)
File "F:\Users\Kai\Miniconda3\lib\site-packages\tensorflow\python\client\session.py", line 975, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (488, 30, 30, 3) for Tensor 'TargetsData/Y:0', which has shape '(?, 32, 32, 1)'
为什么 'TargetsData/Y:0' 有形状 (?, 32, 32, 1) 我该如何解决?