Note: This document is for developers interested in modifying TensorFlow Hub itself. To use TensorFlow Hub, see the Install instructions
If you make changes to TensorFlow Hub pip package, you will likely want to rebuild the pip package from source to try out your changes.
This requires:
- Python
- TensorFlow
- Git
- Bazel
Alternatively, if you install the protobuf compiler you can try out your changes without using bazel.
Install virtualenv if it's not installed already:
~$ sudo apt-get install python-virtualenv
Create a virtual environment for the package creation:
~$ virtualenv --system-site-packages tensorflow_hub_env
And activate it:
~$ source ~/tensorflow_hub_env/bin/activate # bash, sh, ksh, or zsh
~$ source ~/tensorflow_hub_env/bin/activate.csh # csh or tcsh
(tensorflow_hub_env)~/$ git clone https://github.com/tensorflow/hub
(tensorflow_hub_env)~/$ cd hub
(tensorflow_hub_env)~/hub/$ bazel test tensorflow_hub:all
To build a pip package for TensorFlow Hub:
(tensorflow_hub_env)~/hub/$ bazel build tensorflow_hub/pip_package:build_pip_package
(tensorflow_hub_env)~/hub/$ bazel-bin/tensorflow_hub/pip_package/build_pip_package \
/tmp/tensorflow_hub_pkg
Run the following commands to install the pip package.
(tensorflow_hub_env)~/hub/$ pip install /tmp/tensorflow_hub_pkg/*.whl
Test import TensorFlow Hub:
(tensorflow_hub_env)~/hub/$ cd .. # exit the directory to avoid confusion
(tensorflow_hub_env)~/$ python -c "import tensorflow_hub as hub"
Warning: This approach to running TensorFlow is experimental, and not officially supported by the TensorFlow Hub team.
Building the package with bazel is the only officially supported method. However if you are unfamiliar with bazel simpler to work with open source tools. For that you can do a "developer install" of the package.
This installation method allows you to install the working directory into your python environment, so that ongoing changes are reflected when you import the package.
First setup the virtualenv and repository, as described above.
Because TensorFlow Hub uses protobufs you will need the protobuf compiler to
create the necessary python _pb2.py
files from the .proto
files.
(tensorflow_hub_env)~/hub/$ brew install protobuf
(tensorflow_hub_env)~/hub/$ sudo apt install protobuf-compiler
Initially there are no _pb2.py
files in the directory:
(tensorflow_hub_env)~/hub/$ ls -1 tensorflow_hub/*_pb2.py
Run protoc
to create them:
(tensorflow_hub_env)~/hub/$ protoc -I=tensorflow_hub --python_out=tensorflow_hub tensorflow_hub/*.proto
(tensorflow_hub_env)~/hub/$ ls -1 tensorflow_hub/*_pb2.py
tensorflow_hub/image_module_info_pb2.py tensorflow_hub/module_attachment_pb2.py tensorflow_hub/module_def_pb2.py
Note: Don't forget to recompile the _pb2.py
files if you make changes to the
.proto
definitions.
With the _pb2.py
files in place, you can use try out your modifications
directly from the TensorFlow Hub directory:
(tensorflow_hub_env)~/$ python -c "import tensorflow_hub as hub"
Or to use this from outside the repository root, you can use the setup.py develop
installation:
(tensorflow_hub_env)~/hub/$ python tensorflow_hub/pip_package/setup.py develop
Now you can use your local changes in a regular python virtualenv, without the need to rebuild and install the pip package for each new change:
(tensorflow_hub_env)~/hub/$ cd .. # exit the directory to avoid confusion
(tensorflow_hub_env)~/$ python -c "import tensorflow_hub as hub"
(tensorflow_hub_env)~/hub/$ deactivate