2017-10-28 17 views
1

私はTensorFlowを使用してCNNに関連するプロジェクトに取り組んでいます。 I画像があるため、グレースケールのサイズ(250250)であったConv2dによる画像調整

for filename in glob.glob('input_data/*.jpg'): 
input_images.append(cv2.imread(filename,0)) 

image_size_input = len(input_images[0]) 

(20そのような画像)を用いて画像をインポート。 しかし、conv2Dの場合は、4D入力のテンソルが必要です。私の入力テンソルは次のようになります

x = tf.placeholder(tf.float32,shape=[None,image_size_output,image_size_output,1], name='x') 

私は上記の2次元画像を与えられた形状(4D)に変換できませんでした。 "None"フィールドを扱う方法。 私はこの試みた:私は、次のエラーを得た

input_images_padded = [] 
for image in input_images: 
temp = np.zeros((1,image_size_output,image_size_output,1)) 
for i in range(image_size_input): 
    for j in range(image_size_input): 
     temp[0,i,j,0] = image[i,j] 
input_images_padded.append(temp) 

を:

File "/opt/intel/intelpython3/lib/python3.5/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 (20, 1, 250, 250, 1) for Tensor 'x_11:0', which has shape '(?, 250, 250, 1)' 

ここで(参考)全体のコードは次のとおりです。

import tensorflow as tf 
from PIL import Image 
import glob 
import cv2 
import os 
import numpy as np 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 

input_images = [] 
output_images = [] 

for filename in glob.glob('input_data/*.jpg'): 
    input_images.append(cv2.imread(filename,0)) 

for filename in glob.glob('output_data/*.jpg'): 
    output_images.append(cv2.imread(filename,0))  

image_size_input = len(input_images[0]) 
image_size_output = len(output_images[0]) 

''' 
now adding padding to the input images to convert from 125x125 to 250x2050 sized images 
''' 
input_images_padded = [] 
for image in input_images: 
    temp = np.zeros((1,image_size_output,image_size_output,1)) 
    for i in range(image_size_input): 
     for j in range(image_size_input): 
      temp[0,i,j,0] = image[i,j] 
    input_images_padded.append(temp) 

output_images_padded = [] 
for image in output_images: 
    temp = np.zeros((1,image_size_output,image_size_output,1)) 
    for i in range(image_size_input): 
     for j in range(image_size_input): 
      temp[0,i,j,0] = image[i,j] 
    output_images_padded.append(temp) 



sess = tf.Session() 
''' 
Creating tensor for the input 
''' 
x = tf.placeholder(tf.float32,shape= [None,image_size_output,image_size_output,1], name='x') 
''' 
Creating tensor for the output 
''' 
y = tf.placeholder(tf.float32,shape= [None,image_size_output,image_size_output,1], name='y') 


def create_weights(shape): 
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05)) 

def create_biases(size): 
    return tf.Variable(tf.constant(0.05, shape=[size])) 

def create_convolutional_layer(input, bias_count, filter_height, filter_width, num_input_channels, num_out_channels, activation_function): 


    weights = create_weights(shape=[filter_height, filter_width, num_input_channels, num_out_channels]) 

    biases = create_biases(bias_count) 


    layer = tf.nn.conv2d(input=input, 
        filter=weights, 
       strides=[1, 1, 1, 1], 
       padding='SAME') 

    layer += biases 


layer = tf.nn.max_pool(value=layer, 
         ksize=[1, 2, 2, 1], 
         strides=[1, 1, 1, 1], 
         padding='SAME') 

if activation_function=="relu": 
    layer = tf.nn.relu(layer) 

return layer 


''' 
Conv. Layer 1: Patch extraction 
64 filters of size 1 x 9 x 9 
Activation function: ReLU 
Output: 64 feature maps 
Parameters to optimize: 
    1 x 9 x 9 x 64 = 5184 weights and 64 biases 
''' 
layer1 = create_convolutional_layer(input=x, 
           bias_count=64, 
           filter_height=9, 
           filter_width=9, 
           num_input_channels=1, 
           num_out_channels=64, 
           activation_function="relu") 

''' 
Conv. Layer 2: Non-linear mapping 
32 filters of size 64 x 1 x 1 
Activation function: ReLU 
Output: 32 feature maps 
Parameters to optimize: 64 x 1 x 1 x 32 = 2048 weights and 32 biases 
''' 

layer2 = create_convolutional_layer(input=layer1, 
           bias_count=32, 
           filter_height=1, 
           filter_width=1, 
           num_input_channels=64, 
           num_out_channels=32, 
           activation_function="relu") 

'''Conv. Layer 3: Reconstruction 
1 filter of size 32 x 5 x 5 
Activation function: Identity 
Output: HR image 
Parameters to optimize: 32 x 5 x 5 x 1 = 800 weights and 1 bias''' 
layer3 = create_convolutional_layer(input=layer2, 
           bias_count=1, 
           filter_height=5, 
           filter_width=5, 
           num_input_channels=32, 
           num_out_channels=1, 
           activation_function="identity") 

'''print(layer1.get_shape().as_list()) 
print(layer2.get_shape().as_list()) 
print(layer3.get_shape().as_list())''' 

''' 
    applying gradient descent algorithm 
''' 
#loss_function 
loss = tf.reduce_sum(tf.square(layer3-y)) 
#optimiser 
optimizer = tf.train.GradientDescentOptimizer(0.01) 
train = optimizer.minimize(loss) 


init = tf.global_variables_initializer() 
sess.run(init) 
for i in range(len(input_images)): 
    sess.run(train,{x: input_images_padded, y:output_images_padded}) 


curr_loss = sess.run([loss], {x: x_train, y: y_train}) 
print("loss: %s"%(curr_loss)) 
+0

また、なぜシェイプ= [なし、image_size_output、image_size_output、1]という形の最後に1がありますか? 'グレースケール '画像のためにそれを示すことを意味しますか?つまり1つのチャネルのみですか? – kmario23

+0

はい、1はチャネル数を表します。グレースケールの場合、私はそこに1 –

答えて

1

に相当します。あなたのimage_paddedは正しくないと思います。私はtf-codeを書いている経験はありません(しかし、いくつかのコードを読んでいます)。しかし、これを試してみてください:

// imgs is your input-image-sequences 
// padded is to feed 
cnt = len(imgs) 
H,W = imgs[0].shape[:2] 
padded = np.zeros((cnt, H, W, 1)) 
for i in range(cnt): 
    padded[i, :,:,0] = img[i] 
1

1つのオプションは、shapeを与える無視するだろうプレースホルダを作成して、あなたが入力した任意の形状のテンソルを受け入れるようにするときsess.run()

ドキュメントから

shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.

また、あなたはあなたのバッチサイズである、20を指定することができます。テンソルの最初の次元は常にbatch_size

+0

という情報を書きました。それは働いた –

+0

私は多くの評判のポイントを持っていないので、私は今すぐupvoteすることはできません。 –

+0

答えを受け入れなければならない、と思う;) – kmario23

関連する問題