Python / Tensorflow. Is it okay to have all precision values ​​"1" in this case?

I have the following binary which consists of labels, filenames, and data (i.e. pixels):

[array([2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1,
       0, 2, 1, 0, 2, 1, 0]), array(['10_c.jpg', '10_m.jpg', '10_n.jpg', '1_c.jpg',
       '1_m.jpg', '1_n.jpg', '2_c.jpg', '2_m.jpg',
       '2_n.jpg', '3_c.jpg', '3_m.jpg', '3_n.jpg',
       '4_c.jpg', '4_m.jpg', '4_n.jpg', '5_c.jpg',
       '5_m.jpg', '5_n.jpg', '6_c.jpg', '6_m.jpg',
       '6_n.jpg', '7_c.jpg', '7_m.jpg', '7_n.jpg',
       '8_c.jpg', '8_m.jpg', '8_n.jpg', '9_c.jpg',
       '9_m.jpg', '9_n.jpg'], 
      dtype='<U15'), array([[255, 252, 255, ..., 255, 255, 255],
       [136, 137, 138, ..., 114, 110, 111],
       [200, 200, 199, ..., 179, 178, 177],
       ..., 
       [146, 157, 165, ..., 202, 202, 201],
       [228, 225, 222, ..., 219, 221, 223],
       [128, 127, 127, ..., 133, 129, 127]])]

      

I gave the tag an 0

image with a suffix _n.jpg

, 1

for faces with a suffix _m.jpg

and 2

for faces with a suffix_c.jpg

I have the following piece of code for a Convolutional Neural Network (CNN):

import numpy as np
import matplotlib.pyplot as plt
import cifar_tools
import tensorflow as tf

data, labels = cifar_tools.read_data('C:\\Users\\abc\\Desktop\\Testing')

x = tf.placeholder(tf.float32, [None, 150 * 150])
y = tf.placeholder(tf.float32, [None, 2])

w1 = tf.Variable(tf.random_normal([5, 5, 1, 64]))
b1 = tf.Variable(tf.random_normal([64]))

w2 = tf.Variable(tf.random_normal([5, 5, 64, 64]))
b2 = tf.Variable(tf.random_normal([64]))

w3 = tf.Variable(tf.random_normal([38*38*64, 1024]))
b3 = tf.Variable(tf.random_normal([1024]))

w_out = tf.Variable(tf.random_normal([1024, 2]))
b_out = tf.Variable(tf.random_normal([2]))

def conv_layer(x,w,b):
    conv = tf.nn.conv2d(x,w,strides=[1,1,1,1], padding = 'SAME')
    conv_with_b = tf.nn.bias_add(conv,b)
    conv_out = tf.nn.relu(conv_with_b)
    return conv_out

def maxpool_layer(conv,k=2):
    return tf.nn.max_pool(conv, ksize=[1,k,k,1], strides=[1,k,k,1], padding='SAME')

def model():
    x_reshaped = tf.reshape(x, shape=[-1, 150, 150, 1])

    conv_out1 = conv_layer(x_reshaped, w1, b1)
    maxpool_out1 = maxpool_layer(conv_out1)
    norm1 = tf.nn.lrn(maxpool_out1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
    conv_out2 = conv_layer(norm1, w2, b2)
    norm2 = tf.nn.lrn(conv_out2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
    maxpool_out2 = maxpool_layer(norm2)

    maxpool_reshaped = tf.reshape(maxpool_out2, [-1, w3.get_shape().as_list()[0]])
    local = tf.add(tf.matmul(maxpool_reshaped, w3), b3)
    local_out = tf.nn.relu(local)

    out = tf.add(tf.matmul(local_out, w_out), b_out)
    return out

model_op = model()

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model_op, y))
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    onehot_labels = tf.one_hot(labels, 2, on_value=1.,off_value=0.,axis=-1)
    onehot_vals = sess.run(onehot_labels)
    batch_size = 1
    for j in range(0, 5):
        print('EPOCH', j)
        for i in range(0, len(data), batch_size):
            batch_data = data[i:i+batch_size, :]
            batch_onehot_vals = onehot_vals[i:i+batch_size, :]
            _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals})
            print(i, accuracy_val)

        print('DONE WITH EPOCH')

      

When I run the program, I get the following for 5 Epochs:

EPOCH 0
0 1.0
1 1.0
2 0.0
3 0.0
4 0.0
5 1.0
6 0.0
7 0.0
8 0.0
9 0.0
10 0.0
11 1.0
12 1.0
13 0.0
14 0.0
15 0.0
16 1.0
17 0.0
18 0.0
19 0.0
20 1.0
21 1.0
22 0.0
23 1.0
24 0.0
25 1.0
26 0.0
27 0.0
28 0.0
29 1.0
DONE WITH EPOCH
EPOCH 1
0 0.0
1 1.0
2 1.0
3 1.0
4 0.0
5 0.0
6 1.0
7 1.0
8 0.0
9 0.0
10 1.0
11 1.0
12 1.0
13 0.0
14 1.0
15 1.0
16 1.0
17 0.0
18 0.0
19 1.0
20 1.0
21 0.0
22 0.0
23 1.0
24 1.0
25 1.0
26 0.0
27 0.0
28 0.0
29 1.0
DONE WITH EPOCH
EPOCH 2
0 0.0
1 0.0
2 0.0
3 1.0
4 1.0
5 0.0
6 1.0
7 0.0
8 1.0
9 0.0
10 1.0
11 0.0
12 0.0
13 0.0
14 1.0
15 1.0
16 0.0
17 0.0
18 1.0
19 1.0
20 0.0
21 0.0
22 1.0
23 1.0
24 1.0
25 0.0
26 1.0
27 1.0
28 1.0
29 0.0
DONE WITH EPOCH
EPOCH 3
0 0.0
1 1.0
2 1.0
3 0.0
4 0.0
5 1.0
6 0.0
7 0.0
8 1.0
9 1.0
10 1.0
11 0.0
12 0.0
13 1.0
14 0.0
15 0.0
16 0.0
17 1.0
18 1.0
19 0.0
20 1.0
21 1.0
22 1.0
23 0.0
24 0.0
25 1.0
26 0.0
27 1.0
28 0.0
29 1.0
DONE WITH EPOCH
EPOCH 4
0 1.0
1 1.0
2 0.0
3 1.0
4 1.0
5 0.0
6 1.0
7 1.0
8 1.0
9 1.0
10 1.0
11 1.0
12 1.0
13 1.0
14 0.0
15 0.0
16 1.0
17 1.0
18 0.0
19 0.0
20 1.0
21 1.0
22 0.0
23 1.0
24 0.0
25 1.0
26 0.0
27 0.0
28 1.0
29 1.0
DONE WITH EPOCH

      

Are these values ​​(precisions) normal based on the above data? The reason I am asking is because with other data (but with a huge amount of data with 10 classes) I noticed that the original program (I made some tweaks to make it work with my data) returned results (precision) which look like this

EPOCH 0
1 0.104
2 0.1
3 0.136
4 0.14
5 0.124
6 0.156
7 0.16
8 0.172
9 0.16
10 0.164
11 0.148
...
...
...

      

Do you think the results I am getting are normal (i.e. only 0

or 1

)?

EDIT-1

I changed batch_size

to:

batch_size = len(data) // 2

      

The output I currently have is as follows:

EPOCH 0
0 0.466667
15 0.666667
DONE WITH EPOCH
EPOCH 1
0 0.666667
15 0.6
DONE WITH EPOCH
EPOCH 2
0 0.333333
15 0.333333
DONE WITH EPOCH
EPOCH 3
0 0.333333
15 0.333333
DONE WITH EPOCH
EPOCH 4
0 0.533333
15 0.666667
DONE WITH EPOCH

      

Will it be right? So, I have 30 images (I know they are few, but only for demonstration purposes). What's the best way to put them in batches? Is there a better way to present the above output if it is correct now? Or maybe, for such a small dataset, we can put them all in one batch (i.e. batch_size = len(data)

)?

For example, when I set batch_size = len (data), I get this:

EPOCH 0
0 0.333333
DONE WITH EPOCH
EPOCH 1
0 0.666667
DONE WITH EPOCH
EPOCH 2
0 0.666667
DONE WITH EPOCH
EPOCH 3
0 0.333333
DONE WITH EPOCH
EPOCH 4
0 0.366667
DONE WITH EPOCH

      

Thank.

+1


source to share


1 answer


The value batch_size

in your code is 1, so every time you run

sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals})

      

you are viewing only one image. Then you have the following two lines:

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))

      

So correct_pred

it is just 1 number (0 or 1) because it is based on one image (so if tf.argmax(model_op, 1)=tf.argmax(y,1)

then correct_pred=1

. Otherwise it is 0).



Then it accuracy

just matches that number. Therefore, its value is always 0 or 1.

Answer to Edit 1 . Numerically, the values ​​make sense. Yours batch_size=15

, so the precision has to be an integer multiple of 1/15 = 0.0667, which really looks like all the values ​​in your table. The reason it should be an integer multiple of 1/15 has to do with these two lines:

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))

      

The array correct_pred

is just a 0-1 vector (because it is the result tf.equal

). accuracy

is simply the sum of the values ​​in correct_pred

divided by the lot size, which is 15.

As for the optimal batch size, it depends on many factors. You can read more about this in the section here .

+1


source







All Articles