0
votes

I am currently working on a program to classify 32x32 images of handwritten characters. This is my code so far:

import tensorflow as tf
import numpy as np
from PIL import Image
import csv

train_list = csv.reader(open("HCR_data/HCR_train_labels.csv"), delimiter=",")
test_list = csv.reader(open("HCR_data/HCR_test_labels.csv"), delimiter=",")

x = tf.placeholder(tf.float32, [None, 1024])
w = tf.Variable(tf.zeros([1024, 26]))
b = tf.Variable(tf.zeros([26]))
y = tf.nn.softmax(tf.matmul(x, w) + b)

y_ = tf.placeholder(tf.float32, [None, 26])

train_data = []
for location, label in train_list:
    image = Image.open(location).convert('L')
    image = np.asarray(image).flatten()
    image = [0.0 if p==255 else 1.0 for p in image]
    one_hot = [0.0] * 26
    one_hot[ord(label) - 65] = 1.0
    train_data.append((image, one_hot))
train_data = np.asarray(train_data)
np.random.shuffle(train_data)

test_data = []
for location, label in test_list:
    image = Image.open(location).convert('L')
    image = np.asarray(image).flatten()
    image = [0.0 if p==255 else 1.0 for p in image]
    one_hot = [0.0] * 26
    one_hot[ord(label) - 65] = 1.0
    test_data.append((image, one_hot))
test_data = np.asarray(test_data)
np.random.shuffle(test_data)

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for i in range(269):
    batch_xs = train_data[(i*7):((i+1)*7),0]
    batch_ys = train_data[(i*7):((i+1)*7),1]
    #print(len(batch_ys)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: test_data[:,0], y_: test_data[:,1]}))

I based it off of the MNIST tutorial on the Tensorflow website, however whenever build the program I get an error:

Traceback (most recent call last):
  File "C:...\HCR.py", line 57, in <module>
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  File "C:...\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 766, in run
run_metadata_ptr)
  File "C:...\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 937, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "C:...\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

Please help, I've tried many way at changing how I input my feed_dict but i cant figure out whats wrong. Batch_xs should be 7x1024 and batch_ys should be 7x26. I know only having batches of 7 wont be that accurate but i want to figure this error out first.

1
could you print shape of batch_xs : print(batch_xs.shape)Harsha Pokkalla
@hars its ok i fixed it thanks though!Luke Vanhaezebrouck

1 Answers

0
votes

lol, okay I fixed my own problem about 20 seconds later just noticing there was no commas between the individual arrays. So I added list(...) around the array whenever I inputted it to feed_dict

Side note: my accuracy was pretty bad tho, 53%... lmao

Update: got it to 80% just by deleting the tf.nn.softmax(...) for the y