What's causing this error in my simple LogReg model in TF?
My dataset is a collection of images of cars, which I converted to tfrecords with the following fields:
features = tf.parse_single_example(
serialized_example,
features={
'image_id': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string)})
My labels are 0-3, depending on which side of the car the picture is taken. The height of all images is 427 pixels, the width 640.
Here's the simple logreg model I'm trying to feed them through (full code)
training_epochs = 10
image_ids, images, labels = read_and_decode(filename_queue)
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=3)
# simple model
w = tf.get_variable("w1", [10,427,3,4])
y_pred = tf.matmul(images, w)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_pred, labels=onehot_labels)
# for monitoring
loss_mean = tf.reduce_mean(loss)
train_op = tf.train.AdamOptimizer().minimize(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
# Training cycle
for epoch in range(training_epochs):
#avg_cost = 0.
# pass it in through the feed_dict
_, loss_val = sess.run([train_op, loss_mean])
print loss_val
coord.request_stop()
#coord.join(threads)
sess.close()
Right now the error I get is:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-1012473a2568> in <module>()
7 y_pred = tf.matmul(images, w)
8
----> 9 loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_pred, labels=onehot_labels)
10
11 # for monitoring
/Users/paulsingman/projects/fdl/fenv/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.pyc in sparse_softmax_cross_entropy_with_logits(_sentinel, labels, logits, name)
1736 raise ValueError("Rank mismatch: Rank of labels (received %s) should "
1737 "equal rank of logits minus 1 (received %s)." %
-> 1738 (labels_static_shape.ndims, logits.get_shape().ndims))
1739 # Check if no reshapes are required.
1740 if logits.get_shape().ndims == 2:
ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 4).
If it helps. the read_and_decode function referenced above looks like this:
import tensorflow as tf
import skimage.io as io
IMAGE_HEIGHT = 427
IMAGE_WIDTH = 640
tfrecords_filename = 'vroom.tfrecords'
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image_id': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string)})
# Convert from a scalar string tensor (whose single string has
# length mnist.IMAGE_PIXELS) to a uint8 tensor with shape
# [mnist.IMAGE_PIXELS].
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.cast(image, tf.float32) / 255.
image_id = tf.cast(features['image_id'], tf.int32)
label = tf.cast(features['label'], tf.int32)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
image_shape = tf.stack([height, width, 3])
image = tf.reshape(image, image_shape)
image_size_const = tf.constant((IMAGE_HEIGHT, IMAGE_WIDTH, 3), dtype=tf.int32)
# Random transformations can be put here: right before you crop images
# to predefined size. To get more information look at the stackoverflow
# question linked above.
resized_image = tf.image.resize_image_with_crop_or_pad(image=image,
target_height=IMAGE_HEIGHT,
target_width=IMAGE_WIDTH)
images = tf.train.shuffle_batch( [image_id, resized_image, label],
batch_size=10,
capacity=100,
num_threads=1,
min_after_dequeue=15)
return images
Any help would be greatly appreciated, I've been stuck on this for weeks.. I'm trying not to give up but haven't been progressing lately.
Sort of unrelated, but would be willing to pay for a ML tutor to weekly vid chat sessions for 30 min to help me progress.
Posted Jul 16, 2017, 9:59 PM UTC
13