View on TensorFlow.org | Run in Google Colab | View source on GitHub | Download notebook |
TensorFlow 2.x includes many API changes from TF 1.x and the tf.compat.v1
APIs, such as reordering arguments, renaming symbols, and changing default values for parameters. Manually performing all of these modifications would be tedious and prone to error. To streamline the changes, and to make your transition to TF 2.x as seamless as possible, the TensorFlow team has created the tf_upgrade_v2
utility to help transition legacy code to the new API.
Typical usage is like this:
tf_upgrade_v2 \ --intree my_project/ \ --outtree my_project_v2/ \ --reportfile report.txt
It will accelerate your upgrade process by converting existing TensorFlow 1.x Python scripts to TensorFlow 2.x.
The conversion script automates many mechanical API transformations, though many APIs cannot be automatically migrated. It is also not able to fully make your code compatible with TF2 behaviors and APIs. So, it is only a part of your migration journey.
Compatibility modules
Certain API symbols can not be upgraded simply by using a string replacement. Those that cannot be automatically upgraded will be mapped to their locations in the compat.v1
module. This module replaces TF 1.x symbols like tf.foo
with the equivalent tf.compat.v1.foo
reference. If you are already using compat.v1
APIs by importing TF via import tensorflow.compat.v1 as tf
, the tf_upgrade_v2
script will attempt to convert these usages to the non-compat APIs where possible. Note that while some compat.v1
APIs are compatible with TF2.x behaviors, many are not. Therefore, it's recommended to manually proofread replacements and migrate them to new APIs in the tf.*
namespace instead of tf.compat.v1
namespace as quickly as possible.
Because of TensorFlow 2.x module deprecations (for example, tf.flags
and tf.contrib
), some changes can not be worked around by switching to compat.v1
. Upgrading this code may require using an additional library (for example, absl.flags
) or switching to a package in tensorflow/addons.
Recommended upgrade process
The rest of this guide demonstrates how to use the symbol-rewriting script. While the script is easy to use, it is strongly recommended that you use the script as part of the following process:
Unit Test: Ensure that the code you’re upgrading has a unit test suite with reasonable coverage. This is Python code, so the language won’t protect you from many classes of mistakes. Also ensure that any dependency you have has already been upgraded to be compatible with TensorFlow 2.x.
Install TensorFlow 1.15: Upgrade your TensorFlow to the latest TensorFlow 1.x version, at least 1.15. This includes the final TensorFlow 2.0 API in
tf.compat.v2
.Test With 1.15: Ensure your unit tests pass at this point. You’ll be running them repeatedly as you upgrade so starting from green is important.
Run the upgrade script: Run
tf_upgrade_v2
on your entire source tree, tests included. This will upgrade your code to a format where it only uses symbols available in TensorFlow 2.0. Deprecated symbols will be accessed withtf.compat.v1
. These will eventually require manual attention, but not immediately.Run the converted tests with TensorFlow 1.15: Your code should still run fine in TensorFlow 1.15. Run your unit tests again. Any error in your tests here means there’s a bug in the upgrade script. Please let us know.
Check the upgrade report for warnings and errors: The script writes a report file that explains any conversions you should double-check, or any manual action you need to take. For example: Any remaining instances of contrib will require manual action to remove. Please consult the RFC for more instructions.
Install TensorFlow 2.x: At this point it should be safe to switch to TensorFlow 2.x binaries, even if you are running with legacy behaviors
Test with
v1.disable_v2_behavior
: Re-running your tests with av1.disable_v2_behavior()
in the tests' main function should give the same results as running under 1.15.Enable V2 Behavior: Now that your tests work using the TF2 binaries, you can now begin migrating your code to avoiding
tf.estimator
s and only using supported TF2 behaviors (with no TF2 behavior disabling). See the Migration guides for details.
Using the symbol-rewriting tf_upgrade_v2
script
Setup
Before getting started ensure that TensorFlow 2.x is installed.
import tensorflow as tf
print(tf.__version__)
2024-08-15 01:55:33.141808: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:55:33.163444: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:55:33.169957: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2.17.0
Clone the tensorflow/models git repository so you have some code to test on:
git clone --branch r1.13.0 --depth 1 https://github.com/tensorflow/models
Cloning into 'models'... remote: Enumerating objects: 2927, done. remote: Counting objects: 100% (2927/2927), done. remote: Compressing objects: 100% (2428/2428), done. remote: Total 2927 (delta 503), reused 2114 (delta 424), pack-reused 0 (from 0) Receiving objects: 100% (2927/2927), 369.04 MiB | 57.07 MiB/s, done. Resolving deltas: 100% (503/503), done. Updating files: 100% (2768/2768), done.
Read the help
The script should be installed with TensorFlow. Here is the builtin help:
tf_upgrade_v2 -h
2024-08-15 01:55:47.051713: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:55:47.070985: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:55:47.076855: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered usage: tf_upgrade_v2 [-h] [--infile INPUT_FILE] [--outfile OUTPUT_FILE] [--intree INPUT_TREE] [--outtree OUTPUT_TREE] [--copyotherfiles COPY_OTHER_FILES] [--inplace] [--no_import_rename] [--no_upgrade_compat_v1_import] [--reportfile REPORT_FILENAME] [--mode {DEFAULT,SAFETY}] [--print_all] Convert a TensorFlow Python file from 1.x to 2.0 Simple usage: tf_upgrade_v2.py --infile foo.py --outfile bar.py tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new optional arguments: -h, --help show this help message and exit --infile INPUT_FILE If converting a single file, the name of the file to convert --outfile OUTPUT_FILE If converting a single file, the output filename. --intree INPUT_TREE If converting a whole tree of files, the directory to read from (relative or absolute). --outtree OUTPUT_TREE If converting a whole tree of files, the output directory (relative or absolute). --copyotherfiles COPY_OTHER_FILES If converting a whole tree of files, whether to copy the other files. --inplace If converting a set of files, whether to allow the conversion to be performed on the input files. --no_import_rename Not to rename import to compat.v2 explicitly. --no_upgrade_compat_v1_import If specified, don't upgrade explicit imports of `tensorflow.compat.v1 as tf` to the v2 APIs. Otherwise, explicit imports of the form `tensorflow.compat.v1 as tf` will be upgraded. --reportfile REPORT_FILENAME The name of the file where the report log is stored.(default: report.txt) --mode {DEFAULT,SAFETY} Upgrade script mode. Supported modes: DEFAULT: Perform only straightforward conversions to upgrade to 2.0. In more difficult cases, switch to use compat.v1. SAFETY: Keep 1.* code intact and import compat.v1 module. --print_all Print full log to stdout instead of just printing errors
Example TF1 code
Here is a simple TensorFlow 1.0 script:
head -n 65 models/samples/cookbook/regression/custom_regression.py | tail -n 10
# Calculate loss using mean squared error average_loss = tf.losses.mean_squared_error(labels, predictions) # Pre-made estimators use the total_loss instead of the average, # so report total_loss for compatibility. batch_size = tf.shape(labels)[0] total_loss = tf.to_float(batch_size) * average_loss if mode == tf.estimator.ModeKeys.TRAIN: optimizer = params.get("optimizer", tf.train.AdamOptimizer)
With TensorFlow 2.x installed it does not run:
(cd models/samples/cookbook/regression && python custom_regression.py)
2024-08-15 01:55:50.002184: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:55:50.021138: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:55:50.026917: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered Traceback (most recent call last): File "/tmpfs/src/temp/site/en/guide/migrate/models/samples/cookbook/regression/custom_regression.py", line 162, in <module> tf.logging.set_verbosity(tf.logging.INFO) AttributeError: module 'tensorflow' has no attribute 'logging'
Single file
The script can be run on a single Python file:
!tf_upgrade_v2 \
--infile models/samples/cookbook/regression/custom_regression.py \
--outfile /tmp/custom_regression_v2.py
2024-08-15 01:55:52.838925: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:55:52.859243: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:55:52.865018: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered INFO line 38:8: Renamed 'tf.feature_column.input_layer' to 'tf.compat.v1.feature_column.input_layer' INFO line 57:17: tf.losses.mean_squared_error requires manual check. tf.losses have been replaced with object oriented versions in TF 2.0 and after. The loss function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions. INFO line 57:17: Renamed 'tf.losses.mean_squared_error' to 'tf.compat.v1.losses.mean_squared_error' INFO line 62:15: Changed tf.to_float call to tf.cast(..., dtype=tf.float32). INFO line 65:40: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer' INFO line 68:39: Renamed 'tf.train.get_global_step' to 'tf.compat.v1.train.get_global_step' INFO line 83:9: tf.metrics.root_mean_squared_error requires manual check. tf.metrics have been replaced with object oriented versions in TF 2.0 and after. The metric function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions. INFO line 83:9: Renamed 'tf.metrics.root_mean_squared_error' to 'tf.compat.v1.metrics.root_mean_squared_error' INFO line 142:23: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer' INFO line 162:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity' INFO line 162:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO' INFO line 163:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' TensorFlow 2.0 Upgrade Script ----------------------------- Converted 1 files Detected 0 issues that require attention -------------------------------------------------------------------------------- Make sure to read the detailed log 'report.txt'
The script will print errors if it can not find a fix for the code.
Directory tree
Typical projects, including this simple example, will use much more than one file. Typically want to update an entire package, so the script can also be run on a directory tree:
# update the .py files and copy all the other files to the outtree
!tf_upgrade_v2 \
--intree models/samples/cookbook/regression/ \
--outtree regression_v2/ \
--reportfile tree_report.txt
2024-08-15 01:55:55.699073: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:55:55.718072: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:55:55.723647: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered INFO line 40:7: Renamed 'tf.test.mock' to 'tf.compat.v1.test.mock' WARNING line 125:15: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation. INFO line 96:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity' INFO line 96:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO' INFO line 97:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' INFO line 101:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity' INFO line 101:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO' INFO line 102:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' INFO line 105:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity' INFO line 105:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO' INFO line 106:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' INFO line 38:8: Renamed 'tf.feature_column.input_layer' to 'tf.compat.v1.feature_column.input_layer' INFO line 57:17: tf.losses.mean_squared_error requires manual check. tf.losses have been replaced with object oriented versions in TF 2.0 and after. The loss function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions. INFO line 57:17: Renamed 'tf.losses.mean_squared_error' to 'tf.compat.v1.losses.mean_squared_error' INFO line 62:15: Changed tf.to_float call to tf.cast(..., dtype=tf.float32). INFO line 65:40: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer' INFO line 68:39: Renamed 'tf.train.get_global_step' to 'tf.compat.v1.train.get_global_step' INFO line 83:9: tf.metrics.root_mean_squared_error requires manual check. tf.metrics have been replaced with object oriented versions in TF 2.0 and after. The metric function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions. INFO line 83:9: Renamed 'tf.metrics.root_mean_squared_error' to 'tf.compat.v1.metrics.root_mean_squared_error' INFO line 142:23: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer' INFO line 162:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity' INFO line 162:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO' INFO line 163:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' TensorFlow 2.0 Upgrade Script ----------------------------- Converted 7 files Detected 1 issues that require attention -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- File: models/samples/cookbook/regression/automobile_data.py -------------------------------------------------------------------------------- models/samples/cookbook/regression/automobile_data.py:125:15: WARNING: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation. Make sure to read the detailed log 'tree_report.txt'
Note the one warning about the dataset.make_one_shot_iterator
function.
Now the script works in with TensorFlow 2.x:
Note that because the tf.compat.v1
module is included in TF 1.15, the converted script will also run in TensorFlow 1.15.
(cd regression_v2 && python custom_regression.py 2>&1) | tail
tf.compat.v1.app.run(main=main) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/platform/app.py", line 36, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/absl/app.py", line 308, in run _run_main(main, args) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/absl/app.py", line 254, in _run_main sys.exit(main(argv)) File "/tmpfs/src/temp/site/en/guide/migrate/regression_v2/custom_regression.py", line 137, in main model = tf.estimator.Estimator( AttributeError: module 'tensorflow' has no attribute 'estimator'
Detailed report
The script also reports a list of detailed changes. In this example it found one possibly unsafe transformation and included a warning at the top of the file:
head -n 20 tree_report.txt
TensorFlow 2.0 Upgrade Script ----------------------------- Converted 7 files Detected 1 issues that require attention -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- File: models/samples/cookbook/regression/automobile_data.py -------------------------------------------------------------------------------- models/samples/cookbook/regression/automobile_data.py:125:15: WARNING: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation. ================================================================================ Detailed log follows: ================================================================================ ================================================================================ Input tree: 'models/samples/cookbook/regression/' ================================================================================ -------------------------------------------------------------------------------- Processing file 'models/samples/cookbook/regression/regression_test.py' outputting to 'regression_v2/regression_test.py'
Note again the one warning about the Dataset.make_one_shot_iterator function
.
In other cases the output will explain the reasoning for non-trivial changes:
%%writefile dropout.py
import tensorflow as tf
d = tf.nn.dropout(tf.range(10), 0.2)
z = tf.zeros_like(d, optimize=False)
Writing dropout.py
!tf_upgrade_v2 \
--infile dropout.py \
--outfile dropout_v2.py \
--reportfile dropout_report.txt > /dev/null
2024-08-15 01:56:02.740029: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:56:02.758969: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:56:02.764604: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
cat dropout_report.txt
TensorFlow 2.0 Upgrade Script ----------------------------- Converted 1 files Detected 0 issues that require attention -------------------------------------------------------------------------------- ================================================================================ Detailed log follows: ================================================================================ -------------------------------------------------------------------------------- Processing file 'dropout.py' outputting to 'dropout_v2.py' -------------------------------------------------------------------------------- 3:4: INFO: Changing keep_prob arg of tf.nn.dropout to rate, and recomputing value. 4:4: INFO: Renaming tf.zeros_like to tf.compat.v1.zeros_like because argument optimize is present. tf.zeros_like no longer takes an optimize argument, and behaves as if optimize=True. This call site specifies something other than optimize=True, so it was converted to compat.v1. --------------------------------------------------------------------------------
Here is the modified file contents, note how the script adds argument names to deal with moved and renamed arguments:
cat dropout_v2.py
import tensorflow as tf d = tf.nn.dropout(tf.range(10), rate=1 - (0.2)) z = tf.compat.v1.zeros_like(d, optimize=False)
A larger project might contain a few errors. For example convert the deeplab model:
!tf_upgrade_v2 \
--intree models/research/deeplab \
--outtree deeplab_v2 \
--reportfile deeplab_report.txt > /dev/null
2024-08-15 01:56:05.822940: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:56:05.841928: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:56:05.847711: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
It produced the output files:
ls deeplab_v2
README.md datasets input_preprocess.py train.py __init__.py deeplab_demo.ipynb local_test.sh utils common.py eval.py local_test_mobilenetv2.sh vis.py common_test.py export_model.py model.py core g3doc model_test.py
But there were errors. The report will help you pin-point what you need to fix before this will run. Here are the first three errors:
cat deeplab_report.txt | grep -i models/research/deeplab | grep -i error | head -n 3
models/research/deeplab/vis.py:31:7: ERROR: Using member tf.contrib.slim in deprecated module tf.contrib. tf.contrib.slim cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code. models/research/deeplab/eval.py:28:7: ERROR: Using member tf.contrib.slim in deprecated module tf.contrib. tf.contrib.slim cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code. models/research/deeplab/eval.py:146:8: ERROR: Using member tf.contrib.metrics.aggregate_metric_map in deprecated module tf.contrib. tf.contrib.metrics.aggregate_metric_map cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
"Safety" mode
The conversion script also has a less invasive SAFETY
mode that simply changes the imports to use the tensorflow.compat.v1
module:
cat dropout.py
import tensorflow as tf d = tf.nn.dropout(tf.range(10), 0.2) z = tf.zeros_like(d, optimize=False)
tf_upgrade_v2 --mode SAFETY --infile dropout.py --outfile dropout_v2_safe.py > /dev/null
2024-08-15 01:56:10.510235: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-15 01:56:10.529243: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-15 01:56:10.534850: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
cat dropout_v2_safe.py
import tensorflow.compat.v1 as tf d = tf.nn.dropout(tf.range(10), 0.2) z = tf.zeros_like(d, optimize=False)
As you can see this doesn't upgrade your code, but does allow TensorFlow 1 code to run against TensorFlow 2 binaries. Note that this does not mean your code is running supported TF 2.x behaviors!
Caveats
Do not update parts of your code manually before running this script. In particular, functions that have had reordered arguments like
tf.math.argmax
ortf.batch_to_space
cause the script to incorrectly add keyword arguments that mismap your existing code.The script assumes that
tensorflow
is imported usingimport tensorflow as tf
, orimport tensorflow.compat.v1 as tf
.This script does not reorder arguments. Instead, the script adds keyword arguments to functions that have their arguments reordered.
Check out tf2up.ml for a convenient tool to upgrade Jupyter notebooks and Python files in a GitHub repository.
To report upgrade script bugs or make feature requests, please file an issue on GitHub.