TF2.0之官方脚本升级TF1.X版本代码

实现TF1.x向TF2.0的升级

首先,谷歌Tensor flow官方已经给出了关于升级脚本的使用方法说明,链接如下:链接,有需要的可以自行查看,笔者在这里只是演示不同于官方文档的,基于Windows及Anaconda版本下的升级脚本演示。

一、前期准备工作

前期准备工作如下:

  • 确保本机已有TF2.0及其以上版本的环境,且环境本身可运行。如果没有,请百度如何安装TF2.X版本,或点击我的这个博客:Tensorflow 2.0 中遇到的一些坑记录( 一)。
  • 获取TF1.X版本代码,尽量确保其完整性及正确性(毕竟,这部分1.X的代码,大家都是网上找的轮子嘛,绝对不一定,但,基本的还是要看一看,确保没错)

二、在本地切换到目标位置

在这里需要介绍一下,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 $说明文件保存地址

运行过程如图所示:TF2.0之官方脚本升级TF1.X版本代码_第1张图片
查看report.txt文件

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代码的补充与升级。

你可能感兴趣的:(Machine,Learning,tensorflow,python)