使用占位符

时间:2017-03-13 12:35:08

标签: tensorflow conv-neural-network

我是tensorflow的初学者,我用它在图像上实现CNN,当我使用palceholder和feed_dir时,它给出了错误说

  

您必须为占位符

提供值
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()

它在这一行中给出了错误,但我在 sess.run(Runall())中使用了 feed_dir ,所以我缺少什么?

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

2 个答案:

答案 0 :(得分:1)

您运行self.h_conv的行也需要提供feed_dict

答案 1 :(得分:0)

你的整个程序在同一行上运行,你还没有提到错误是什么?

更新

在你创建的函数Read_images()中没有返回任何值,所以你应该添加函数的最后一行 return Grey_images

并且在读取图像后未保存值。 在计划行Read_lines() 更改为Grey_images = Read_lines()