首先,谷歌Tensor flow官方已经给出了关于升级脚本的使用方法说明,链接如下:链接,有需要的可以自行查看,笔者在这里只是演示不同于官方文档的,基于Windows及Anaconda版本下的升级脚本演示。
前期准备工作如下:
在这里需要介绍一下,TensorFlow 1.13 和更高版本(包括所有 TF 2.0 版本)会自动安装 tf_upgrade_v2。因此,我们需要使用命令行,cd到目标位置,这里给出参考环境位置(以Anaconda下创建的虚拟环境为例)
首先笔者的tf_upgrade_v2脚本自动安装在了该路径下:D:\Programming\Anaconda3\envs\tf2\Scriptstf_upgrade_v2.exe
然后,为了方便读者找到自己的tf_upgrade_v2脚本位置,笔者在这里给出一个基本路径格式,读者只需根据自机环境即可套用:
--Anaconda安装位置\env\--环境名\Scripts\tf_upgrade_v2.exe
在前期准备工作全部完成后,直接执行如下代码即可:
tf_upgrade_v2 --intree D:\Programming\Sparsely-Grouped-GAN\ --outtree D:\Programming\Sparsely-Grouped-GAN_V2\ --reportfile D:\Programming\Sparsely-Grouped-GAN_V2\report.txt
这里,还是一样的,为了读者方便,笔者提供一个大致模板如下(因参数原因,这里用$xxx
来表示需要读者填写的部分):
tf_upgrade_v2 --intree $原始代码文件夹 --outtree $转换后的代码输出文件夹 --reportfile $说明文件保存地址
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 20 files
Detected 11 issues that require attention
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
File: D:\Programming\Sparsely-Grouped-GAN\SGGAN.py
--------------------------------------------------------------------------------
D:\Programming\Sparsely-Grouped-GAN\SGGAN.py:155:20: WARNING: *.save requires manual check. (This warning is only applicable if the code saves a tf.Keras model) Keras model.save now saves to the Tensorflow SavedModel format by default, instead of HDF5. To continue saving to HDF5, add the argument save_format='h5' to the save() function.
D:\Programming\Sparsely-Grouped-GAN\SGGAN.py:159:24: WARNING: *.save requires manual check. (This warning is only applicable if the code saves a tf.Keras model) Keras model.save now saves to the Tensorflow SavedModel format by default, instead of HDF5. To continue saving to HDF5, add the argument save_format='h5' to the save() function.
--------------------------------------------------------------------------------
File: D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py
--------------------------------------------------------------------------------
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:22:12: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:30:21: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:40:13: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:42:11: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:54:11: WARNING: tf.contrib.layers.instance_norm requires manual check. (Manual edit required) `tf.contrib.layers.instance_norm` has been migrated to `tfa.layers.InstanceNormalization` in TensorFlow Addons. The API spec may have changed during the migration. Please see https://github.com/tensorflow/addons for more info.
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:54:11: ERROR: Using member tf.contrib.layers.instance_norm in deprecated module tf.contrib. tf.contrib.layers.instance_norm 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.
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:154:12: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:163:17: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
D:\Programming\Sparsely-Grouped-GAN\tfLib\ops.py:219:12: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
================================================================================
Detailed log follows:
================================================================================
================================================================================
Input tree: 'D:\\Programming\\Sparsely-Grouped-GAN\\'
================================================================================
--------------------------------------------------------------------------------
Processing file 'D:\\Programming\\Sparsely-Grouped-GAN\\Dataset.py'
outputting to 'D:\\Programming\\Sparsely-Grouped-GAN_V2\\Dataset.py'
--------------------------------------------------------------------------------
67:18: INFO: Renamed 'tf.read_file' to 'tf.io.read_file'
72:14: INFO: Renamed 'tf.image.resize_images' to 'tf.image.resize'
99:23: INFO: Added keywords to args of function 'tf.convert_to_tensor'
100:22: INFO: Added keywords to args of function 'tf.convert_to_tensor'
106:22: INFO: Added keywords to args of function 'tf.convert_to_tensor'
107:21: INFO: Added keywords to args of function 'tf.convert_to_tensor'
113:37: INFO: Renamed 'tf.train.shuffle_batch' to 'tf.compat.v1.train.shuffle_batch'
123:37: INFO: Renamed 'tf.train.batch' to 'tf.compat.v1.train.batch'
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Processing file 'D:\\Programming\\Sparsely-Grouped-GAN\\options.py'
outputting to 'D:\\Programming\\Sparsely-Grouped-GAN_V2\\options.py'
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Processing file 'D:\\Programming\\Sparsely-Grouped-GAN\\SGGAN.py'
outputting to 'D:\\Programming\\Sparsely-Grouped-GAN_V2\\SGGAN.py'
--------------------------------------------------------------------------------
24:17: INFO: Renamed 'tf.placeholder' to 'tf.compat.v1.placeholder'
25:21: INFO: Renamed 'tf.placeholder' to 'tf.compat.v1.placeholder'
26:24: INFO: Renamed 'tf.placeholder' to 'tf.compat.v1.placeholder'
61:21: INFO: tf.losses.sigmoid_cross_entropy 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.
61:21: INFO: Renamed 'tf.losses.sigmoid_cross_entropy' to 'tf.compat.v1.losses.sigmoid_cross_entropy'
62:22: INFO: tf.losses.sigmoid_cross_entropy 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.
62:22: INFO: Renamed 'tf.losses.sigmoid_cross_entropy' to 'tf.compat.v1.losses.sigmoid_cross_entropy'
63:22: INFO: tf.losses.sigmoid_cross_entropy 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.
63:22: INFO: Renamed 'tf.losses.sigmoid_cross_entropy' to 'tf.compat.v1.losses.sigmoid_cross_entropy'
79:22: INFO: Renamed 'tf.trainable_variables' to 'tf.compat.v1.trainable_variables'
85:21: INFO: Renamed 'tf.train.Saver' to 'tf.compat.v1.train.Saver'
86:17: INFO: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
88:17: INFO: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
91:15: INFO: Renamed 'tf.global_variables_initializer' to 'tf.compat.v1.global_variables_initializer'
92:17: INFO: Renamed 'tf.ConfigProto' to 'tf.compat.v1.ConfigProto'
95:13: INFO: Renamed 'tf.Session' to 'tf.compat.v1.Session'
111:22: INFO: Renamed 'tf.train.start_queue_runners' to 'tf.compat.v1.train.start_queue_runners'
155:20: WARNING: *.save requires manual check. (This warning is only applicable if the code saves a tf.Keras model) Keras model.save now saves to the Tensorflow SavedModel format by default, instead of HDF5. To continue saving to HDF5, add the argument save_format='h5' to the save() function.
159:24: WARNING: *.save requires manual check. (This warning is only applicable if the code saves a tf.Keras model) Keras model.save now saves to the Tensorflow SavedModel format by default, instead of HDF5. To continue saving to HDF5, add the argument save_format='h5' to the save() function.
169:21: INFO: Renamed 'tf.train.Saver' to 'tf.compat.v1.train.Saver'
170:15: INFO: Renamed 'tf.global_variables_initializer' to 'tf.compat.v1.global_variables_initializer'
171:17: INFO: Renamed 'tf.ConfigProto' to 'tf.compat.v1.ConfigProto'
174:13: INFO: Renamed 'tf.Session' to 'tf.compat.v1.Session'
186:22: INFO: Renamed 'tf.train.start_queue_runners' to 'tf.compat.v1.train.start_queue_runners'
219:13: INFO: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
219:42: INFO: Renamed 'tf.AUTO_REUSE' to 'tf.compat.v1.AUTO_REUSE'
238:13: INFO: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
238:42: INFO: Renamed 'tf.AUTO_REUSE' to 'tf.compat.v1.AUTO_REUSE'
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Processing file 'D:\\Programming\\Sparsely-Grouped-GAN\\test.py'
outputting to 'D:\\Programming\\Sparsely-Grouped-GAN_V2\\test.py'
--------------------------------------------------------------------------------
可以看到,该脚本的自动完成了对v1代码的补充与升级。