0

I am trying to learn how to retrain an image classifier using transfer learning. I am following the steps shown in this tutorial.

I successfully retrained the model but I come across problems in the last step where he writes python script for classifying the newly trained model. In the video, he starts writing the code at 4:18 but does not specify where. I try writing it in the docker container but it gives me the no module named platform error and the NameError: name 'sys' is not defined error. I try writing it locally in my machine and get errors as well since I do not have the dependencies installed locally. I am not sure where to write the python code for the final step in the tutorial. Any help is appreciated.

Terminal code and errors:

root@dbe57bdfb014:/tensorflow# python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf, sys 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "tensorflow/python/pywrap_tensorflow.py", line 25, in <module>
    from tensorflow.python.platform import self_check
ImportError: No module named platform
>>> image_path = sys.argv[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
Maxim
  • 52,561
  • 27
  • 155
  • 209
Hernan Razo
  • 59
  • 12

2 Answers2

0

Your tensorflow package in the container is broken. I think the best way is you uninstall and fresh install tensorflow again, but make sure you take the latest version (1.4 as of this moment).

The second error "NameError: name 'sys' is not defined" simply follows the first one. The statement import tensorflow as tf raise an error, that's why sys module wasn't imported.

After you get tensorflow working, you can write code in the python console, ipython notebook or create standalone python scripts.

Maxim
  • 52,561
  • 27
  • 155
  • 209
  • when reinstalling tensorflow, should I still use the "gcr.io/tensorflow/tensorflow:latest-devel" command? How do I check which version I have? – Hernan Razo Nov 11 '17 at 19:32
  • I'm using a Docker container with a tensorflow image. Wouldn't the pip install commands only work if I had everything locally in my computer? – Hernan Razo Nov 11 '17 at 20:03
  • I'm surprised it's not working out of the box as well, probably because it's dev, not stable. But if I were running the container and tensorflow's got broken, the first thing I would try is reinstall it right in that container. If it fails as well, I'd go with another container. And yes, try the stable version. – Maxim Nov 11 '17 at 21:00
0

The proper way in this case is to just use any IDE and write the python script in a .py file locally in your computer and then copy the file into the Docker container. If you do not have the tensorflow library installed locally, tensorflow specific statements like import tensorflow as tf will generate errors but they can be ignored since the file will end up running in the container.

Let's assume you named the .py file myScript.py. In order to transfer the .py file from your computer to the container, run the command: docker cp myScript.py myContainer:/myScript.py

Make sure you put the .py file somewhere in the container where you can easily find it. When retraining the model, make sure you run the correct script by running: python tensorflow/examples/image_retraining/myScript.py This will then point to the correct file and it will be read as if you were doing everything locally.

Check out these links for more info:

Copying files from host to Docker container

https://docs.docker.com/engine/reference/commandline/cp/

Hernan Razo
  • 59
  • 12