Models: Make TF-Slim work with Python3

Created on 8 Dec 2016  路  9Comments  路  Source: tensorflow/models

While TF is compatible with Python3, TF-Slim seems to be written for Python 2 only. Python 2 is EOL and it would be great to have python 3 support throughout the TF framework.

Examples:

Calling download_and_convert_flowers results in the following error:
TypeError: 'jpg' has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)

slim_walkthrough is python2 only.

I haven't gotten farther than that since I don't want to be fixing python code by hand to make it work. Would really appreciate to have a working python3 version.

Thanks!

awaiting maintainer feature

Most helpful comment

Original issue can be resolved by changing the string in download_and_convert_flowers.py at line 146:
'jpg' ->b'jpg'
see: http://python-future.org/compatible_idioms.html#byte-string-literals

after this change the script finished successfully

All 9 comments

A similar download_and_convert problem:
$ python3 download_and_convert_data.py --dataset_name=flowers --dataset_dir="${DATA_DIR}" Traceback (most recent call last): File "download_and_convert_data.py", line 39, in <module> from datasets import download_and_convert_cifar10 File "/home/shane/Documents/tf-models/slim/datasets/download_and_convert_cifar10.py", line 29, in <module> import cPickle ImportError: No module named 'cPickle'

Seems like it might be fixed in #408, but that PR hasn't been accepted.

Is there any other status on #408 getting accepted?

I manually made the changes for #408, attempting to work through the notebook examples
https://github.com/tensorflow/models/blob/master/slim/slim_walkthough.ipynb

Traceback (most recent call last):
File "display_flowers.py", line 21, in
dataset = flowers.get_split('train', flowers_data_dir)
File "modelsslimdatasets\flowers.py", line 89, in get_split
labels_to_names = dataset_utils.read_label_file(dataset_dir)
File "modelsslimdatasetsdataset_utils.py", line 132, in read_label_file
lines = lines.split('\n')
TypeError: a bytes-like object is required, not 'str'

dataset_utils.py
#lines = lines.split('\n')
lines = lines.decode().split('\n')

display_flowers.py

for i in xrange(4):

for i in range(4):

The quick fix for the time being is to use Anaconda / Virtualenv to create a Python 2.7 environment and install tensorflow in it. Viola everything runs as expected.

Any retrained model should be able to run directly in Python 3 as it is in the GraphDef format, which isnt bothered by Python 2 or 3.

Just 2 cents to keep pushing forward, wont spent time for language compatibility issues as all I need is the resultant fine tuned GraphDef.....

Thanks @sguada and @nathansilberman for developing this, keep up the good work 馃憤

Original issue can be resolved by changing the string in download_and_convert_flowers.py at line 146:
'jpg' ->b'jpg'
see: http://python-future.org/compatible_idioms.html#byte-string-literals

after this change the script finished successfully

it can be solved by changing:
'jpeg' to b'jpeg' ,
'png' to b'png
'cat' to b'cat'
in object_detection/data_decoders/tf_example_decoder_test.py

@sguada Can you comment on this issue and related PR #408 ?

The same problem when executing `sh local_test.sh' within models/research/deeplab.
TypeError: 'xxx' has type str, but expected one of: bytes.

Because the type can only be string, so use a Type Cast below before use it:
type(example)

example = str.encode(example)

I think the issue was resolved. I am closing the issue. Please feel free to open if you experience the problem again. Thanks!

Was this page helpful?
0 / 5 - 0 ratings