Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ValueError: Shape (128, 4, 10) must have rank 2 #3

Open
samirma opened this issue Jan 26, 2017 · 9 comments
Open

ValueError: Shape (128, 4, 10) must have rank 2 #3

samirma opened this issue Jan 26, 2017 · 9 comments

Comments

@samirma
Copy link

samirma commented Jan 26, 2017

Hello,
I've just got follow error after I tried to execute this command:
python model/captcha_train.py

Can you help with this? I am sorry if it is a silly question, but I am just getting started to work with TensorFlow...

Traceback (most recent call last):
File "model/captcha_train.py", line 82, in
main()
File "model/captcha_train.py", line 44, in main
loss = captcha_model.loss(logits, labels)
File "/home/samir/Downloads/CAPTCHA/model/captcha_model.py", line 123, in loss
cross_entropy_per_number = tf.nn.softmax_cross_entropy_with_logits(logits, labels)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 491, in softmax_cross_entropy_with_logits
precise_logits, labels, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 1427, in _softmax_cross_entropy_with_logits
features=features, labels=labels, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2319, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1711, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 603, in _SoftmaxCrossEntropyWithLogitsShape
input_shape = logits_shape.merge_with(labels_shape).with_rank(2)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 641, in with_rank
raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (128, 4, 10) must have rank 2

@zakizhou
Copy link
Owner

zakizhou commented Jan 30, 2017

Hi Samir, Sorry to be late for replying, when I ran this program, there was nothing wrong about it, have you converted the training data into tfrecords files correctly? I have had a copy in my pc at school but it's holiday now and I'm at home so I can't get access to it, but maybe I can help you produce your own training images.

@samirma
Copy link
Author

samirma commented Jan 30, 2017

@zakizhou Thanks for answering me, Actually I tried it a commit before you start to use these tfrecord files.
Is it possible to you send these tfrecords files to me samir.ma@gmail.com? Or teach me how to create them?
I also would like to do some changes like, save the model and restore it in order to evaluate some file

@zakizhou
Copy link
Owner

zakizhou commented Jan 30, 2017

I really can't get access to the preproduced training data so I'd like to help you create it and it is not difficult.
first generate jpg or png files using 'captcha_producer.py' in my repo like this(make sure you have installed captcha module, if not, you can easily install it with pip install captcha):
python captcha_producer.py -p <your_dir> -n 100
this will create 100 images(generating 50000 images will take relatively longer time) in <your_dir> with each image containing four digits(you can easily change the number of digits or add characters to the images by make small modification to the source code).
after doing this I will help you generate the tfrecord files which tensorflow need to train with.

@zakizhou
Copy link
Owner

zakizhou commented Jan 30, 2017

Later after creating jpgs or pngs, see the convert_to(dir, name, path) function in captcha_inputs in my repo.
dir is the dir where you store the jpgs or pngs , name is the name of tfrecords like 'train' or 'eval' and path is the dir where you want to store the tfrecord file.
This function does nothing but converting the images in dir into tfrecords and save it to the path with the specified name.(this function need scipy to read images so make sure you have installed it)

@samirma
Copy link
Author

samirma commented Jan 31, 2017

Thanks a lot! it worked I just generated the tfrecords files, but I am still getting some problem to evaluate a model...
Can you give some hint about how to do it?

@zakizhou
Copy link
Owner

zakizhou commented Jan 31, 2017

You're welcome.
I'm willing to help you but I don't understand what you mean by 'some problem to evaluate a model'. Do you mean you don't know which kind of metrics to use like precision or recall to evaluate a model or just don't know how to implement it in tensorflow?

@samirma
Copy link
Author

samirma commented Jan 31, 2017

I want to send an image to a trained model in order to get its prediction
That my problem, I am able to train and save the model but I don't know(or I am doing it wrong) to read an image to retrieve its prediction.

@zakizhou
Copy link
Owner

zakizhou commented Jan 31, 2017

OK,I know what you what.
I think you can follow these steps(pseudocode):

  1. reload model from files:
saver = tf.train.import_meta_graph(<path_to_store_model_meta_file>,
                                   import_scope="your_import_scope",
                                   input_map={
                                       "your_inputs_node_name:0": imgs,
                                       "your_labels_node_name:0": lbs
                                   })
saver.restore(sess, <path_to_store_weights_file>)

here img and lbs are constant tensors converted from your jpgs or pngs(scipy is good for reading images into numpy.array and later use tf.convert_to_tensor to convert the numpy.array into a constant tensor).
in this step I change the inputs of this model from the original tfrecords into constant tensors in order to get the prediction.
2. get prediction op in the graph

predict_op = graph.get_tensor_by_name("your_predict_op_name:0")
  1. run the predict op
sess.run(predict_op)

this will give you the probability distributions over all possible labels in the numpy.array format

you can also change the inputs into 'tf.placeholder' and later pass a feed_dict to sess.run.

@JYlab
Copy link

JYlab commented Sep 25, 2017

Hello zakizhou!
I'm just starting the tensor flow..
I'm having the same problem.
How can I solve this..??
It's hard to understand exactly what you said...
Can you tell me what to do?...PLZ!..
Thank u!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants