2017-10-17 9 views
0

私はこの簡単なオートエンコーダーコードを適応させようとしています: https://github.com/tflearn/tflearn/blob/master/examples/images/autoencoder.pyです。 私は畳み込みレイヤを使用し、488画像* 30高さ* 30幅* 3カラーチャンネル(RGB)[488,30,30,3]の入力を持つようにコードを変更しようとしています。元の画像と似ているが異なっている画像。私はどんな種類の検証データセットも使用していません(私は過大適合は気にせず、過大評価を防ぐのを助ける以外の検証データセットを使用する他の理由は見当たりません。そうであれば)。私は初心者ですが、エンコーディングとデコーダーがうまく構築されていないのは残念です。tflearnコードを適合させようとすると、形状エラー

# 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?)私はそれを解決する?

答えて

1

あなたの寸法は右ではない、これはあなたが持っているものです。

_________________________________________________________________ 
Layer (type)     Output Shape    Param # 
================================================================= 
conv2d_1 (Conv2D)   (None, 30, 30, 16)  448  
_________________________________________________________________ 
max_pooling2d_1 (MaxPooling2 (None, 15, 15, 16)  0   
_________________________________________________________________ 
conv2d_2 (Conv2D)   (None, 15, 15, 8)   1160  
_________________________________________________________________ 
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 8)   0   
_________________________________________________________________ 
conv2d_3 (Conv2D)   (None, 8, 8, 8)   584  
_________________________________________________________________ 
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 8)   0   
_________________________________________________________________ 
conv2d_4 (Conv2D)   (None, 4, 4, 8)   584  
_________________________________________________________________ 
up_sampling2d_1 (UpSampling2 (None, 8, 8, 8)   0   
_________________________________________________________________ 
conv2d_5 (Conv2D)   (None, 8, 8, 8)   584  
_________________________________________________________________ 
up_sampling2d_2 (UpSampling2 (None, 16, 16, 8)   0   
_________________________________________________________________ 
conv2d_6 (Conv2D)   (None, 16, 16, 16)  1168  
_________________________________________________________________ 
up_sampling2d_3 (UpSampling2 (None, 32, 32, 16)  0   
_________________________________________________________________ 
conv2d_7 (Conv2D)   (None, 32, 32, 1)   145  
================================================================= 

(なし、30、30、3)と一致しないための簡単な修正が一致する3つの畳み込みフィルタを持っているために、最後のconv_2dを変更することです最後のディメンションと「有効な」パディングがあるため、32ではなく30になります。このように:

decoder = tflearn.conv_2d(decoder,3, [3,3], activation='relu', padding='valid') 
+0

これは機能しました。ありがとう:D –

+1

それはうまくいった!これが役に立つとわかったら、答えを受け入れてください:) –

関連する問題