【经验】tf1.x迁移到tf2.x教程

【经验】tf1.x迁移到tf2.x教程,第1张

一、参考资料

Automatically rewrite TF 1.x and compat.v1 API symbols
TensorFlow 1.x vs TensorFlow 2 - Behaviors and APIs
Migrate from TensorFlow 1.x to TensorFlow 2
[最新] [翻译] [TensorFlow 2.1] 将 TensorFlow 1 的代码迁移到 TensorFlow 2

二、重要说明
  1. 本文可能有点乱,旨在提供一些思路,记录一下以供参考;
  2. 本文以博主参与的一个项目,由于项目保密,暂不提供项目源码;
三、准备工作
  1. 安装gpu版本的tf2.x

    pip install tensorflow-gpu==2.5.1
    
  2. (可选)创建Anaconda虚拟环境

    conda create -n tfgpu25 python==3.6
    
  3. 安装项目环境

    pip install -r requirements.txt
    
四、实验环境 4.1 系统环境
Environment
Operating System + Version: Windows 10 家庭中文版
CPU Type: 11th Gen Intel(R) Core(TM) i7-11700 @ 2.50GHz
GPU Type: GeForce RTX 3060,12GB
Nvidia Driver Version: 512.15
CUDA Version: 11.1.74
CUDNN Version: 8.1.0
Python Version (if applicable): 3.6.13
TensorFlow Version (if applicable): tensorflow-gpu=2.5.1
Anaconda Version:4.10.3
gcc:8.1.0
g++:8.1.0
4.2 env-gpu.yaml
name: tfgpu25
channels:
  - defaults
dependencies:
  - certifi=2020.6.20=pyhd3eb1b0_3
  - pip=21.2.2=py36haa95532_0
  - python=3.6.13=h3758d61_0
  - setuptools=58.0.4=py36haa95532_0
  - sqlite=3.38.0=h2bbff1b_0
  - vc=14.2=h21ff451_1
  - vs2015_runtime=14.27.29016=h5e58377_2
  - wheel=0.37.1=pyhd3eb1b0_0
  - wincertstore=0.2=py36h7fe50ca_0
  - pip:
    - absl-py==0.10.0
    - astor==0.8.1
    - astunparse==1.6.3
    - cached-property==1.5.2
    - cachetools==4.2.4
    - charset-normalizer==2.0.12
    - cloudpickle==2.0.0
    - cycler==0.11.0
    - dask==2.1.0
    - dataclasses==0.6
    - decorator==4.4.2
    - flatbuffers==1.12
    - gast==0.4.0
    - google-auth==2.6.2
    - google-auth-oauthlib==0.4.6
    - google-pasta==0.2.0
    - grpcio==1.34.0
    - h5py==3.1.0
    - idna==3.3
    - imageio==2.15.0
    - imgaug==0.2.6
    - importlib-metadata==4.8.3
    - keras-nightly==2.5.0.dev2021032900
    - keras-preprocessing==1.1.2
    - kiwisolver==1.3.1
    - markdown==3.3.6
    - matplotlib==3.2.0
    - networkx==2.5.1
    - numpy==1.19.2
    - oauthlib==3.2.0
    - opencv-python==3.4.2.17
    - opt-einsum==3.3.0
    - pillow==8.4.0
    - protobuf==3.19.4
    - pyasn1==0.4.8
    - pyasn1-modules==0.2.8
    - pyparsing==3.0.7
    - python-dateutil==2.8.2
    - pywavelets==1.1.1
    - pyyaml==6.0
    - requests==2.27.1
    - requests-oauthlib==1.3.1
    - rsa==4.8
    - scikit-image==0.14.2
    - scipy==1.5.4
    - shapely==1.8.1.post1
    - six==1.15.0
    - tensorboard==2.8.0
    - tensorboard-data-server==0.6.1
    - tensorboard-plugin-wit==1.8.1
    - tensorflow-estimator==2.5.0
    - tensorflow-gpu==2.5.1
    - termcolor==1.1.0
    - tf-slim==1.1.0
    - tifffile==0.13.2
    - tiffile==2018.10.18
    - toolz==0.11.2
    - typing-extensions==3.7.4
    - urllib3==1.26.9
    - werkzeug==2.0.3
    - wrapt==1.12.1
    - zipp==3.6.0
prefix: F:\360Downloads\Anaconda3\envs\tfgpu25
4.3 requirements-gpu.txt
absl-py==0.10.0
astor==0.8.1
astunparse==1.6.3
cached-property==1.5.2
cachetools==4.2.4
certifi==2020.6.20
charset-normalizer==2.0.12
cloudpickle==2.0.0
cycler==0.11.0
dask==2.1.0
dataclasses==0.6
decorator==4.4.2
flatbuffers==1.12
gast==0.4.0
google-auth==2.6.2
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
grpcio==1.34.0
h5py==3.1.0
idna==3.3
imageio==2.15.0
imgaug==0.2.6
importlib-metadata==4.8.3
keras-nightly==2.5.0.dev2021032900
Keras-Preprocessing==1.1.2
kiwisolver==1.3.1
Markdown==3.3.6
matplotlib==3.2.0
networkx==2.5.1
numpy==1.19.2
oauthlib==3.2.0
opencv-python==3.4.2.17
opt-einsum==3.3.0
Pillow==8.4.0
protobuf==3.19.4
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==3.0.7
python-dateutil==2.8.2
PyWavelets==1.1.1
PyYAML==6.0
requests==2.27.1
requests-oauthlib==1.3.1
rsa==4.8
scikit-image==0.14.2
scipy==1.5.4
Shapely==1.8.1.post1
six==1.15.0
tensorboard==2.8.0
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow-estimator==2.5.0
tensorflow-gpu==2.5.1
termcolor==1.1.0
tf-slim==1.1.0
tifffile==0.13.2
tiffile==2018.10.18
toolz==0.11.2
typing-extensions==3.7.4
urllib3==1.26.9
Werkzeug==2.0.3
wincertstore==0.2
wrapt==1.12.1
zipp==3.6.0
六、关键步骤 6.1 关键指令
# 查看帮助
tf_upgrade_v2 -h
(tfgpu25) C:\Users\yoyo>tf_upgrade_v2 -h
2022-03-31 16:34:05.023206: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
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
6.2 tf1.x迁移到tf2.x

执行 tf_upgrade_v2 指令,迁移整个项目(文件夹)。

# 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
    
参数解释:
--intree:输入的文件夹
--outtree:输出的文件夹
--reportfile:保存的日志文件
6.3 替换包导入
import tensorflow as tf
替换为
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_v2_behavior()  # tf1.x与tf2.x切换
tf.compat.v1.disable_eager_execution()  # 关闭紧急执行
6.4 修改项目代码 6.4.1 修改 model.py 代码

/PATH/TO/model.py

self.sess=tf.Session()
修改为
self.sess=tf.compat.v1.Session()

self.inputs=tf.placeholder(tf.float32,[None, self.data.imageSize[1], self.data.imageSize[0],3], name='inputs')
修改为
self.inputs = tf.random.normal(shape=[-1, self.data.imageSize[1], self.data.imageSize[0],3], name='inputs')

self.labels=tf.placeholder(tf.float32, [None,self.data.num_classes], name='labels')
修改为
self.labels= tf.random.normal(shape=[-1, self.data.num_classes], name='labels')

self.istraining=tf.placeholder(tf.bool,name='istraining')
修改为
self.istraining=tf.compat.v1.placeholder(tf.bool,name='istraining')
6.4.2 修改 shufflenetv2.py 代码

ShuffleNet V2 网络结构的原理与 Tensorflow2.0 实现

/PATH/TO/network/shufflenetv2.py

修改 separable_conv2d API

tf.nn.separable_conv2d
tf.layers.separable_conv2d实现深度可分卷积

layer = tf.contrib.layers.separable_conv2d(inputs=inputs,
                                                   num_outputs=None,
                                                   kernel_size=[kernel_size, kernel_size],
                                                   depth_multiplier=1,
                                                   stride=stride,
                                                   padding=padding,
                                                   activation_fn=None,
                                                   weights_initializer=tf.contrib.slim.xavier_initializer(),
                                                   trainable=trainable)

修改为
layer = tf.keras.layers.DepthwiseConv2D(kernel_size=kernel_size,
                                strides=stride,
                                padding=padding)(inputs)
七、可能存在的问题 Q: UnicodeDecodeError 编码问题
(tfgpu25) F:\MyDocumentes\YOYOFile\coding>tf_upgrade_v2 --intree ./code-focalloss --outtree ./code-focalloss-tf2 --reportfile tree_report.txt
2022-03-29 13:41:49.621531: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
  File "F:0Downloads\Anaconda3\envs\tfgpu25\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "F:0Downloads\Anaconda3\envs\tfgpu25\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:0Downloads\Anaconda3\envs\tfgpu25\Scripts\tf_upgrade_v2.exe\__main__.py", line 7, in <module>
  File "F:0Downloads\Anaconda3\envs\tfgpu25\lib\site-packages\tensorflow\tools\compatibility\tf_upgrade_v2_main.py", line 176, in main
    args.input_tree, output_tree, args.copy_other_files)
  File "F:0Downloads\Anaconda3\envs\tfgpu25\lib\site-packages\tensorflow\tools\compatibility\ast_edits.py", line 1076, in process_tree
    _, l_report, l_errors = self.process_file(input_path, output_path)
  File "F:0Downloads\Anaconda3\envs\tfgpu25\lib\site-packages\tensorflow\tools\compatibility\ast_edits.py", line 921, in process_file
    temp_file)
  File "F:0Downloads\Anaconda3\envs\tfgpu25\lib\site-packages\tensorflow\tools\compatibility\ast_edits.py", line 982, in process_opened_file
    lines = in_file.readlines()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2631: illegal multibyte sequence
解决办法:
修改源代码
F:\360Downloads\Anaconda3\envs\tfgpu25\lib\site-packages\tensorflow\tools\compatibility\ast_edits.py

with open(in_filename, "r") as in_file
    tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
修改为
with open(in_filename, "r", encoding='UTF-8') as in_file, \
    tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
Q: 导包错误
raise RuntimeError("Attempting to capture an EagerTensor without "
RuntimeError: Attempting to capture an EagerTensor without building a function.
解决办法:
import tensorflow as tf
替换为
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
Q: no attribute ‘contrib’

tf2.0不降版本也能完美解决module ‘tensorflow’ has no attribute ‘contrib’的问题

layer = tf.contrib.layers.separable_conv2d(inputs=inputs,
AttributeError: module 'tensorflow' has no attribute 'contrib'
解决办法:
layer = tf.contrib.xxx()
改为
layer = tf.compat.v1.xxx()
Q: tf.contrib.slim 错误

解决tensoflow2.x中使用tf.contrib.slim包时出现的No module named:tensorflow.contrib 问题
https://github.com/adrianc-a/tf-slim

解决办法:
# 安装
pip install tf_slim

# 导入
import tf_slim as slim

# 修改API
tf.contrib.slim.xxx
修改为
slim.xxx
Q: API 错误
TypeError: separable_conv2d() got an unexpected keyword argument 'stride'
解决办法:
参数stride修改为strides。

tf.compat.v1.layers.separable_conv2d(stride=stride)
修改为
tf.compat.v1.layers.separable_conv2d(strides=stride)
Q: API参数错误
TypeError: separable_conv2d() got an unexpected keyword argument 'num_outputs'
TypeError: separable_conv2d() got an unexpected keyword argument 'activation_fn'
TypeError: separable_conv2d() got an unexpected keyword argument 'weights_initializer'
错误原因:
tf1.x与tf2.x版本的API参数变动。

解决办法:
注释掉多余的API参数。
Q: 变量类型错误
error: Tensor Tensor("istraining:0", shape=(), dtype=bool) may not be fed.
update model error: Tensor Tensor("Const:0", shape=(), dtype=bool) may not be fed.
错误原因:
tf中布尔类型和Python的中布尔类型是不等同的。
定义的istraining类型为tf.bool类型
istraining= tf.constant(True, dtype=tf.bool, name='istraining')

feed_dict的类型为python的bool类型
feed_dict={self.istraining:True}

比如:
b =  tf.constant(True)
b == True     # 结果是False

解决办法:
self.istraining= tf.constant(True, dtype=tf.bool, name='istraining')
改为
self.istraining=tf.compat.v1.placeholder(tf.bool,name='istraining')

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/717234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存