user7337874
user7337874

Reputation:

How to fix my accuracy function in `tensorflow`?

I have error when evaluating correct_prediction.

This is my whole code:

def compute_accuracy(testimgName , testimgLabel ):
    global prediction
    v_xs = np.empty([1,224,224], dtype=np.int)
    v_ys = np.empty([1,8], dtype=np.float32)
    g = testimgName[1] 
    v_xs = np.array(Image.open(path+g))
    v_xs = np.reshape(v_xs,[1,224,224])
    v_ys = testimgLabel[1] ## It is a list = ['0', '0', '0', '0', '0', '0', '1', '0']
    v_ys = np.reshape(v_ys , [1,8])##print(v_ys) = [['0' '0' '0' '0' '0' '0' '1' '0']]
    y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})
    correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
    return result
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(x, W):
    #stride [1,x_movment,y_movment,1]
    #Must have strides[0] = strides[3] = 1
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME') #stride [batch,height,width,channels]

def max_pool_2x2(x):
    #stride[1,x_movment,y_movment ,1]
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1], padding='SAME')

# size of image
image_height = 224
image_width = 224
number_of_class = 8
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 224, 224]) # 224*224
ys = tf.placeholder(tf.float32, [None, 8])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs,[-1,224,224,1])

## conv1 layer ##
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+ b_conv1) #output size 224*224*32 
h_pool1 = max_pool_2x2(h_conv1) # 112*112*32

## conv2 layer ##
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+ b_conv2) #output size 112*112*64 
h_pool2 = max_pool_2x2(h_conv2) # output size of pooling 56*56*64

## conv3 layer ##
W_conv3 = weight_variable([5,5,64,64])
b_conv3 = bias_variable([64])
h_conv3 = tf.nn.relu(conv2d(h_pool2,W_conv3)+ b_conv3) #output size 56*56*64 
h_pool3 = max_pool_2x2(h_conv3) # output size of pooling 28*28*64

## conv4 layer ##
W_conv4 = weight_variable([5,5,64,32])
b_conv4 = bias_variable([32])
h_conv4 = tf.nn.relu(conv2d(h_pool3,W_conv4)+ b_conv4) #output size 28*28*32
h_pool4 = max_pool_2x2(h_conv4) # output size of pooling 14*14*32

## func1 layer ##
W_fc1 = weight_variable([14*14*32,256])
b_fc1 = bias_variable([256])

#[n_sample, 14,14,32] ->> [n_sample,14*14*32]
h_pool4_flat = tf.reshape(h_pool4,[-1,14*14*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool4_flat,W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)

## func2 layer ##
W_fc2 = weight_variable([256,8])
b_fc2 = bias_variable([8])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)


# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                          reduction_indices=[1]))   # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
trainimgNames , trainimgLabels = ut.getlabel(path,textFileName_train)
imgTestNames , imgTestLabels = ut.getlabel(path,textFileName_test)
batch_size = 1
number_of_batches = len(trainimgNames)/batch_size
for i in range(int(number_of_batches)):
    batch_x = trainimgNames[i*batch_size:i*batch_size+batch_size]
    batch_y = trainimgLabels[i*batch_size:i*batch_size+batch_size]
    batch_image_data = np.empty([batch_size,image_height,image_width], dtype=np.int)
    for j in range(len(batch_x)):
        f = batch_x[j]
        batch_image_data[j] = np.array(Image.open(path+f))
    sess.run(train_step, feed_dict={xs: batch_image_data, ys:batch_y, keep_prob: 0.5})
    print(compute_accuracy(imgTestName , imgTestLabel))        

And getlabel() is:

def getlabel(path,txtFileName):
    names = []
    labels = []
    dirs = os.listdir( path )

    with open(txtFileName, 'r') as f:
    for line in f.readlines():
        tokens = line.split(' ')
        names.append(tokens[0])
        labels.append(int(tokens[1]))
    i = 0
    listLabel= []
    f = []
    for item in labels:
        a = ('{0:08b}'.format(labels[i]))
        listLabel.append(list(a))
        i+=1
    return names,listLabel 

When i call print(computer_accuracy) the error appears.

The error is

TypeError: DataType string for attr 'T' not in list of allowed values: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, float16

I do not know why y_pre is like that and prediction go wrong. can anyone help me to fix it ?

Upvotes: 0

Views: 874

Answers (1)

pltrdy
pltrdy

Reputation: 2109

TensorFlow's philosophy is different than pure Python. First, you define tensors, then you run it.

The first part, where you define prediction, nothing is running. Its declarative. You declare that prediction depends on different variables/operation.

The calculation only occurs when you call sess.run. Thus, y_pres is not a tensor, it is a python variable (numpy probably) holding the results. Therefore, you are not supposed to pass it to TF functions.

So what? So you should separate Tensorflow declarative part from the running part. Which means, only keep your second sess.run call, everything between this one and the first sess.run belongs to declarative part. It should look a bit like:

Disclaimer: the code below won't even work, cf Problem #1 and maybe other issues

declarative part (defining tensors i.e. TF's Compute Graph):

  v_ys = tf.Placeholder( <type> )  

  W_fc1 = weight_variable([14*14*32,256])
  b_fc1 = bias_variable([256])
  h_pool4_flat = tf.reshape(h_pool4,[-1,14*14*32])
  h_fc1 = tf.nn.relu(tf.matmul(h_pool4_flat,W_fc1) + b_fc1)
  h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
  W_fc2 = weight_variable([256,8])
  b_fc2 = bias_variable([8])
  prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)

  # Defining correct prediction as a Tensor as well
  correct_prediction = tf.equal(tf.argmax(prediction,1), tf.argmax(v_ys,1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

Running the Model:

def compute_accuracy(imageTestName , imageTestLabel ):
      global prediction
      v_xs = np.empty([1,224,224], dtype=np.int)
      g = imageTestName[0]
      v_xs = np.array(Image.open(path+g))
      v_xs = np.reshape(v_xs,[1,224,224])
      v_ys = imageTestLabel[0] ## It is a list = ['0', '0', '0', '0', '0', '0', '1', '0']
      v_ys = np.reshape(v_ys , [1,8]) ## After reshape it is = [['0' '0' '0' '0' '0' '0' '1' '0']]

      # Directly evaluate `accuracy`
      result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
      return result

Upvotes: 1

Related Questions