-3

I run python object_detection/builders/model_builder_test.py with Tensorflow 1.9 of CPU version and Python 3.6 under Ubuntu system, there is NameError: name 'xrange' is not defined, does someone know why this happens and how to deal with it? Thanks. Here is the guide i follow.

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md

Traceback (most recent call last):
  File "object_detection/builders/model_builder_test.py", line 311, in test_create_ssd_resnet_v1_fpn_model_from_config
    model = model_builder.build(model_proto, is_training=True)
  File "/home/tensorflow/models/research/object_detection/builders/model_builder.py", line 110, in build
    add_background_class)
  File "/home/tensorflow/models/research/object_detection/builders/model_builder.py", line 214, in _build_ssd_model
    ssd_config.anchor_generator)
  File "/home/tensorflow/models/research/object_detection/builders/anchor_generator_builder.py", line 91, in build
    cfg.normalize_coordinates
  File "/home/tensorflow/models/research/object_detection/anchor_generators/multiscale_grid_anchor_generator.py", line 61, in __init__
    for scale in xrange(scales_per_octave)]
NameError: name 'xrange' is not defined
Matthias
  • 12,873
  • 6
  • 42
  • 48
ah bon
  • 9,293
  • 12
  • 65
  • 148
  • 2
    xrange doesnt exist on python 3. Use range – joaquin Aug 04 '18 at 10:23
  • @joaquin Worth noting that range doesn't exist in python 2 so xrange should be used if the OP ever needs to use it in a version of python which is earlier than python 3 – Adi219 Aug 04 '18 at 10:25
  • @Adi219 He is using Python 3.6 as declared in the post – joaquin Aug 04 '18 at 10:26
  • @joaquin I know, which is why I said that it's 'worth noting' :) (as it's still useful information for users who stumble upon this question) – Adi219 Aug 04 '18 at 10:27
  • 3
    @Adi219 `range` _does_ exist in Python 2, it returns a list instead of a generator. – xdurch0 Aug 04 '18 at 10:30
  • @xdurch0 I've been misinformed :( – Adi219 Aug 04 '18 at 10:32
  • @Adi219 This is wrong. range and xrange exist in python 2. xrange was renamed to range() in Python 3.x, and the original range() function was deprecated in Python 3.x. – joaquin Aug 04 '18 at 10:32
  • @joaquin I've been misinformed, as I just told xdurch0 above when they told me the exact same thing. – Adi219 Aug 04 '18 at 10:34
  • Thanks for all your helps. I added xrange = range in the code of Python files Traceback (most recent call last), it works right now. – ah bon Aug 07 '18 at 10:33

1 Answers1

0

xrange() was removed from python3 and was replaced by range(). Use range() instead (it works exactly the same as xrange()).

However, if you need to use the range() function in python2 (which you don't in this case), use xrange() as range() returns a list instead of a generator (as @xdurch0 says in the comments).

Adi219
  • 4,712
  • 2
  • 20
  • 43