Models: cifar10_input.py calls tf.strided_slice() with 3 arguments, 4 needed.

Created on 27 Dec 2016  Â·  20Comments  Â·  Source: tensorflow/models

Please let us know which model this issue is about (specify the top-level directory)

models/tutorials/image/cifar10/cifar10_input.py:87

File "/mnt/st12tb/models/tutorials/image/cifar10/cifar10_input.py", line 87, in read_cifar10
tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32)

models/tutorials/image/cifar10/cifar10_input.py:93

File "/mnt/st12tb/models/tutorials/image/cifar10/cifar10_input.py", line 93, in read_cifar10
[label_bytes + image_bytes]),

In both cases, running the script, e.g. with "python cifar10_train.py" on TensorFlow v0.12 yields

TypeError: strided_slice() takes at least 4 arguments (3 given)

bug

Most helpful comment

After looking at @Cuongvn08 I have put the following changes in which seem to make it work:

In cifar10_input.py

-  result.label = tf.cast(tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32)
+  result.label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32)
-  depth_major = tf.reshape( tf.strided_slice(record_bytes, [label_bytes], [label_bytes + image_bytes]),      [result.depth, result.height, result.width])
+  depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]), [result.depth, result.height, result.width])

Then in both cifar10_input.py and cifar10.py I had to search for "deprecated" and wherever I found it, replace it with a valid function based on what I read in the api guide (hopefully correctly). Examples of this:

-  tf.contrib.deprecated.image_summary('images', images)
+  tf.summary.image('images', images)

and

 - tf.contrib.deprecated.histogram_summary(tensor_name + '/activations', x)
 - tf.contrib.deprecated.scalar_summary(tensor_name + '/sparsity',
 + tf.summary.histogram(tensor_name + '/activations', x)
 + tf.summary.scalar(tensor_name + '/sparsity',

It seems to be chugging along happily now. I'll see if it completes OK and if the changes I put in above give the desired diagnostic outputs.

I'd still like to hear a definitive answer from someone closer to the code.

All 20 comments

I am having the same problem. See my description here on stackoverflow.

I have the same problem

This problem also happens while running, ptb_word_lm.py.

Version of tensorflow : '0.12.0'

Entire traceback:


TypeError Traceback (most recent call last)
in ()
6
7 with tf.name_scope("Train"):
----> 8 train_input = PTBInput(config=config, data=train_data, name="TrainInput")
9 with tf.variable_scope("Model", reuse=None, initializer=initializer):
10 m = PTBModel(is_training=True, config=config, input_=train_input)

in __init__(self, config, data, name)
92 self.epoch_size = ((len(data) // batch_size) - 1) // num_steps
93 self.input_data, self.targets = reader.ptb_producer(
---> 94 data, batch_size, num_steps, name=name)
95
96

/home/ubuntu/Dropbox/xai/Machine175/AttentionModels/rnn/ptb/reader.pyc in ptb_producer(raw_data, batch_size, num_steps, name)
115 i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
116 x = tf.strided_slice(data, [0, i * num_steps],
--> 117 [batch_size, (i + 1) * num_steps])
118 x.set_shape([batch_size, num_steps])
119 y = tf.strided_slice(data, [0, i * num_steps + 1],

TypeError: strided_slice() takes at least 4 arguments (3 given)

I used slice() instead of strided_slice(), It was working.
However, another error occurred when I ran cifar10_train.py.
... cifar10/cifar10_input.py", line 135, in _generate_image_and_label_batch tf.contrib.deprecated.image_summary('images', images)
AttributeError: 'module' object has no attribute 'deprecated'

Is there any recommendation for this issue?
Thank you.

I have found out the solution for my issue above.
Solution: I modified to use tf.summary instead of tf.contrib.deprecated :). And It worked well.

After looking at @Cuongvn08 I have put the following changes in which seem to make it work:

In cifar10_input.py

-  result.label = tf.cast(tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32)
+  result.label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32)
-  depth_major = tf.reshape( tf.strided_slice(record_bytes, [label_bytes], [label_bytes + image_bytes]),      [result.depth, result.height, result.width])
+  depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]), [result.depth, result.height, result.width])

Then in both cifar10_input.py and cifar10.py I had to search for "deprecated" and wherever I found it, replace it with a valid function based on what I read in the api guide (hopefully correctly). Examples of this:

-  tf.contrib.deprecated.image_summary('images', images)
+  tf.summary.image('images', images)

and

 - tf.contrib.deprecated.histogram_summary(tensor_name + '/activations', x)
 - tf.contrib.deprecated.scalar_summary(tensor_name + '/sparsity',
 + tf.summary.histogram(tensor_name + '/activations', x)
 + tf.summary.scalar(tensor_name + '/sparsity',

It seems to be chugging along happily now. I'll see if it completes OK and if the changes I put in above give the desired diagnostic outputs.

I'd still like to hear a definitive answer from someone closer to the code.

@debajyotidatta looks like #824 fixes the problem - runs fine for me with those changes

Does anyone know a more extensive documentation for strided_slice? I don't understand what the 4th argument should look like. The only one I seem to come across is https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/api_docs/python/functions_and_classes/shard8/tf.strided_slice.md

@piatra this page is fairly helpful:
https://www.tensorflow.org/api_docs/python/array_ops/slicing_and_joining
You have to scroll down a bit to get to the strided slice.

Automating the changes required (with GNU sed and perl 5.22.2):

$ find tensorflow/models/tutorials/image/cifar10 -type f -name '*.py' -exec sed -i 's/tf.contrib.deprecated/tf.summary/g' {} \; -exec perl -p -i -e 's/(tf.strided_slice.*?\))/\1, [1,1])/' {} \;

@piatra - the answer from #824 is to add a 4th [needed argument] of [1, 1] to strided_slice.

See the 2nd edit in my comment which sorts this out.

BobbyAtSperry

From: Samuel Marks [mailto:[email protected]]
Sent: 31 December 2016 04:45
To: tensorflow/models models@noreply.github.com
Cc: bobbyAtSperry bobby.gilbert@sperryrail.com; Comment comment@noreply.github.com
Subject: Re: [tensorflow/models] cifar10_input.py calls tf.strided_slice() with 3 arguments, 4 needed. (#817)

Hmm, I tried with your changes... automated:

find -type f -name '*.py' -exec sed -i -e 's/tf.strided_slice/tf.slice/g' -e 's/tf.contrib.deprecated/tf.summary/g' {} \;

And got a different error:

$ python cifar10_train.py
Traceback (most recent call last):
File "cifar10_train.py", line 120, in
tf.app.run()
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 43, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "cifar10_train.py", line 116, in main
train()
File "cifar10_train.py", line 63, in train
images, labels = cifar10.distorted_inputs()
File "/not_tmp/tensorflow/models/tutorials/image/cifar10/cifar10.py", line 156, in distorted_inputs
batch_size=FLAGS.batch_size)
File "/not_tmp/tensorflow/models/tutorials/image/cifar10/cifar10_input.py", line 161, in distorted_inputs
read_input = read_cifar10(filename_queue)
File "/not_tmp/tensorflow/models/tutorials/image/cifar10/cifar10_input.py", line 94, in read_cifar10
[result.depth, result.height, result.width])
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2448, in reshape
name=name)
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
op_def=op_def)
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2242, in create_op
set_shapes_for_outputs(ret)
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs
shapes = shape_func(op)
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/not_tmp/.venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Cannot reshape a tensor with 3073 elements to shape [3,32,32] (3072 elements) for 'Reshape' (op: 'Reshape') with input shapes: [3073], [3].

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/tensorflow/models/issues/817#issuecomment-269849222 , or mute the thread https://github.com/notifications/unsubscribe-auth/ATxFkaIkLI__BFDebiAQgcKFfErl1X1Jks5rNd3UgaJpZM4LV8ck . https://github.com/notifications/beacon/ATxFkVruUe-WuVsZRdyFqS3yLM-x3xfIks5rNd3UgaJpZM4LV8ck.gif

did you resolve this problem? I also encountered the same problem.

@bobbyAtSperry , ValueError: Cannot reshape a tensor with 3073 elements to shape [3,32,32] (3072 elements) for 'Reshape' (op: 'Reshape') with input shapes: [3073], [3].
did you resolve this problem?

@mianmian3 Yes. See my reply further up the chain or summarised in the answer here. The specific line to fix this is
depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]), [result.depth, result.height, result.width])
Note that slice takes differnt arguments. Start and Length rather than Start and End hence the error.

There is an alternative comprehensive answer to this question here, but you have to use the reply I've commented on or you will still have other problems.

Strided_slice has made the "stride" option optional (defaulting to all ones...) ... this is available in master (but not the release you are using). so you can change

tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32)

to

tf.strided_slice(record_bytes, [0], [label_bytes], [1]), tf.int32)

@aselle I think it's worth making the code work with 0.12.x as well, even though it already works with the TF master.

The code is referred from the TF tutorial. Many readers of that document would not be familiar with TF code base (that's why they read the introductory document after all.) The example code not working out of the box is discouraging to these new users, IMO.

It might make sense to keep it broken if the API change is large and it's hard to keep the compatibility. But it isn't the case of this issue.

Please take a look at https://github.com/tensorflow/models/pull/849 when you have time.
It fixes this issue (and some other small hiccups.)

I made the changes as put in #849, and the trained the model. But during evaluation I get the error:

Traceback (most recent call last):
File "cifar10_eval.py", line 157, in
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 43, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "cifar10_eval.py", line 153, in main
evaluate()
File "cifar10_eval.py", line 121, in evaluate
images, labels = cifar10.inputs(eval_data=eval_data)
File "/home/utsav/DeepEmbedding/cifar10/cifar10.py", line 182, in inputs
batch_size=FLAGS.batch_size)
File "/home/utsav/DeepEmbedding/cifar10/cifar10_input.py", line 253, in inputs
shuffle=False)
File "/home/utsav/DeepEmbedding/cifar10/cifar10_input.py", line 132, in _generate_image_and_label_batch
capacity=min_queue_examples + 3 * batch_size)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/input.py", line 683, in batch
capacity=capacity, dtypes=types, shapes=shapes, shared_name=shared_name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/data_flow_ops.py", line 666, in __init__
shapes = _as_shape_list(shapes, dtypes)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/data_flow_ops.py", line 75, in _as_shape_list
raise ValueError("All shapes must be fully defined: %s" % shapes)
ValueError: All shapes must be fully defined: [TensorShape([Dimension(24), Dimension(24), Dimension(3)]), TensorShape([Dimension(None)])]

Here's my .patch file: https://gist.github.com/SamuelMarks/17e968288545042da4e718e886e458e3

But it gives me:

$ python cifar10_train.py
>> Downloading cifar-10-binary.tar.gz 100.0%
Successfully downloaded cifar-10-binary.tar.gz 170052171 bytes.
Traceback (most recent call last):
  File "cifar10_train.py", line 120, in <module>
    tf.app.run()
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 43, in run
    sys.exit(main(sys.argv[:1] + flags_passthrough))
  File "cifar10_train.py", line 116, in main
    train()
  File "cifar10_train.py", line 63, in train
    images, labels = cifar10.distorted_inputs()
  File "tensorflow/models/tutorials/image/cifar10/cifar10.py", line 156, in distorted_inputs
    batch_size=FLAGS.batch_size)
  File "tensorflow/models/tutorials/image/cifar10/cifar10_input.py", line 161, in distorted_inputs
    read_input = read_cifar10(filename_queue)
  File "tensorflow/models/tutorials/image/cifar10/cifar10_input.py", line 87, in read_cifar10
    tf.strided_slice(record_bytes, [0], [label_bytes], [1,1]), tf.int32)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 590, in strided_slice
    shrink_axis_mask=shrink_axis_mask)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3503, in strided_slice
    shrink_axis_mask=shrink_axis_mask, name=name)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2242, in create_op
    set_shapes_for_outputs(ret)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs
    shapes = shape_func(op)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File ".venvs/tflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimension 0 in both shapes must be equal, but are 1 and 2 for 'StridedSlice' (op: 'StridedSlice') with input shapes: [?], [1], [1], [2].

@utsavgarg Thanks for the catch! Just noticed that and fixed #849 accordingly.

Closing because fix from previous comment was submitted.

Was this page helpful?
0 / 5 - 0 ratings