2017-03-13 72 views
0

當我在tensorflow初學者,我用它在圖像上實現CNN,當我使用palceholder與feed_dir它給我的錯誤說Tensorflow錯誤使用佔位符

必須養活一個值佔位符

Grey_images = [] 
def Read_images(): 
    for filename in glob.glob(r'Path*.jpg'): 
     img = Image.open(filename) 
     img = img.convert('L') # convert to gray scale 
     img = np.asanyarray(img) 
     img_shape = img.shape 
     img_reshaped = img.reshape(224,224,1 channels) 
     Grey_images.append(img_reshaped)#[#imgs,224,224,1] 

Read_images() 
img = tf.placeholder(dtype=tf.float32,shape=[None,224,224,1]) 

def RunAll(): 
    layer = Layer(img,1,3,3,64,2,'Relu') 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    Prediction = sess.run(RunAll(),feed_dict={img:Grey_images}) 

這是類層

def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 

def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 

def Conv2d(inp, W ,Stride): 

    return tf.nn.conv2d(inp, W, strides=[1, Stride ,Stride, 1], padding='SAME') 

class Layer: 
    def __init__(self, inp,inp_channels_num,filter_width_size,filter_height_size,outp_channels_num,stride,activation_func): 
     sess = tf.Session() 
     self.W_conv = weight_variable([filter_width_size, filter_height_size, inp_channels_num, outp_channels_num]) 
     self.b_conv = bias_variable([outp_channels_num])  
     if (activation_func=='Sigmoid'): 
      self.h_conv = tf.nn.sigmoid(Conv2d(inp, self.W_conv, stride) + self.b_conv) 
     else: 
      self.h_conv = tf.nn.relu(Conv2d(inp, self.W_conv, stride) + self.b_conv) 
     sess.run(tf.global_variables_initializer())    
     self.h_conv = sess.run(self.h_conv) 
     sess.close() 

它給了我這個行的錯誤,但我使用feed_dirsess.run(Runall())所以我錯過了什麼?

self.h_conv = sess.run(self.h_conv) 

回答

0

你的整個程序運行在同一行,你沒有提到錯誤是什麼?

更新

在功能Read_images()創建沒有返回任何值,在功能,所以最後一行應添加 return Grey_images

,你看圖片後你沒有保存價值。在程序行Read_lines() 變化 到Grey_images = Read_lines()

+0

我在帖子的第一部分提到了錯誤'您必須爲佔位符提供一個值' –

+0

檢查更新,希望它可以幫助 – Raady

+0

我更新它並且它給了我相同的錯誤 –

1

您運行self.h_conv該行還需要提供feed_dict

+0

如果是這樣的話是什麼需要我的*** feed_dict ***我在Tensor.org看到它這樣Toturials –

+0

這是因爲h_conv傳遞依賴佔位符,所以當你評估它時,你需要提供佔位符的值。 –

+0

okey和我在調用函數時提供它並將它發送給圖層構造它爲什麼不使用它 –