当前位置:网站首页>[experience] TF1 X migrate to TF2 X tutorial
[experience] TF1 X migrate to TF2 X tutorial
2022-04-22 03:00:00 【Flower boy】
One 、 Reference material
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
[ newest ] [ translate ] [TensorFlow 2.1] take TensorFlow 1 To TensorFlow 2
Two 、 Important note
- This article may be a little messy , Aimed at Offer some ideas , Record it for reference ;
- This article is based on a project participated by bloggers , Because the project is confidential , The project source code will not be provided for the time being ;
3、 ... and 、 preparation
-
install gpu Version of tf2.x
pip install tensorflow-gpu==2.5.1 -
( Optional ) establish Anaconda A virtual environment
conda create -n tfgpu25 python==3.6 -
Install the project environment
pip install -r requirements.txt
Four 、 Experimental environment
4.1 System environment
Environment
Operating System + Version: Windows 10 Family Chinese
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、 ... and 、 Key steps
6.1 Key instructions
# view help
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 Migrate to tf2.x
perform tf_upgrade_v2 Instructions , Migrate the entire project ( Folder ).
# 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
Parameter interpretation :
--intree: The folder you entered
--outtree: Output folder
--reportfile: Saved log file
6.3 Replace package import
import tensorflow as tf
Replace with
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_v2_behavior() # tf1.x And tf2.x Switch
tf.compat.v1.disable_eager_execution() # Turn off emergency execution
6.4 Modify project code
6.4.1 modify model.py Code
/PATH/TO/model.py
self.sess=tf.Session()
It is amended as follows
self.sess=tf.compat.v1.Session()
self.inputs=tf.placeholder(tf.float32,[None, self.data.imageSize[1], self.data.imageSize[0],3], name='inputs')
It is amended as follows
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')
It is amended as follows
self.labels= tf.random.normal(shape=[-1, self.data.num_classes], name='labels')
self.istraining=tf.placeholder(tf.bool,name='istraining')
It is amended as follows
self.istraining=tf.compat.v1.placeholder(tf.bool,name='istraining')
6.4.2 modify shufflenetv2.py Code
ShuffleNet V2 Principle and of network structure Tensorflow2.0 Realization
/PATH/TO/network/shufflenetv2.py
modify separable_conv2d API
tf.nn.separable_conv2d
tf.layers.separable_conv2d Realize deep separable convolution
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)
It is amended as follows
layer = tf.keras.layers.DepthwiseConv2D(kernel_size=kernel_size,
strides=stride,
padding=padding)(inputs)
7、 ... and 、 Possible problems
Q: UnicodeDecodeError Coding problem
(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:\360Downloads\Anaconda3\envs\tfgpu25\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "F:\360Downloads\Anaconda3\envs\tfgpu25\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "F:\360Downloads\Anaconda3\envs\tfgpu25\Scripts\tf_upgrade_v2.exe\__main__.py", line 7, in <module>
File "F:\360Downloads\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:\360Downloads\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:\360Downloads\Anaconda3\envs\tfgpu25\lib\site-packages\tensorflow\tools\compatibility\ast_edits.py", line 921, in process_file
temp_file)
File "F:\360Downloads\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
terms of settlement :
Modify the source code
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:
It is amended as follows
with open(in_filename, "r", encoding='UTF-8') as in_file, \
tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
Q: Guide Package error
raise RuntimeError("Attempting to capture an EagerTensor without "
RuntimeError: Attempting to capture an EagerTensor without building a function.
terms of settlement :
import tensorflow as tf
Replace with
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
Q: no attribute ‘contrib’
layer = tf.contrib.layers.separable_conv2d(inputs=inputs,
AttributeError: module 'tensorflow' has no attribute 'contrib'
terms of settlement :
layer = tf.contrib.xxx()
Change it to
layer = tf.compat.v1.xxx()
Q: tf.contrib.slim error
solve tensoflow2.x Use in tf.contrib.slim When the package appears No module named:tensorflow.contrib problem
https://github.com/adrianc-a/tf-slim
terms of settlement :
# install
pip install tf_slim
# Import
import tf_slim as slim
# modify API
tf.contrib.slim.xxx
It is amended as follows
slim.xxx
Q: API error
TypeError: separable_conv2d() got an unexpected keyword argument 'stride'
terms of settlement :
Parameters stride It is amended as follows strides.
tf.compat.v1.layers.separable_conv2d(stride=stride)
It is amended as follows
tf.compat.v1.layers.separable_conv2d(strides=stride)
Q: API Parameter error
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'
The reason for the error :
tf1.x And tf2.x Version of API Parameter changes .
terms of settlement :
Comment out the superfluous API Parameters .
Q: Wrong variable type
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.
The reason for the error :
tf Boolean type and Python Boolean types in are not equivalent .
Defined istraining The type is tf.bool type
istraining= tf.constant(True, dtype=tf.bool, name='istraining')
feed_dict The type of python Of bool type
feed_dict={self.istraining:True}
such as :
b = tf.constant(True)
b == True # The result is False
terms of settlement :
self.istraining= tf.constant(True, dtype=tf.bool, name='istraining')
Change it to
self.istraining=tf.compat.v1.placeholder(tf.bool,name='istraining')
版权声明
本文为[Flower boy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220253232342.html
边栏推荐
- Development details of swap exchange arbitrage clip robot system
- 金融信息安全實訓——22/4/19(上)
- I'm going to start learning canvas
- Possible reasons for slow response of Web site access
- Niu Ke's entry record of C language
- 824. Goat Latin (string segmentation + string substitution)
- Why is the country established
- After the asynchronous operation, let the subsequent code of await continue to execute
- UE4 lock camera screen
- Day12 job
猜你喜欢

Problem brushing plan -- dynamic programming (II)

Redis event driven framework (Part 1): when to use select, poll and epoll?

Regression prediction | MATLAB realizes Bayes Gru (Bayes optimized gating cycle unit) multiple input and single output

Flutter04-Widget初体验

安装部署phpStudy+DVWA漏洞演练平台

Serviceworker cache and HTTP cache

Wechat H5 payment (reporting cross domain issues)

kerberos认证协议

Interpretation of Flink's new features of fine-grained resource management

Will you "sell" SQL?
随机推荐
Development details of swap exchange arbitrage clip robot system
使用Xamarin编写一个精美的APP登录注册界面
二十八.内置模块
To civilized lifestyle and environmental pollution
Troubleshooting kubernetes - 10s delay
用zlib压缩、解压缩流、判断是否压缩过了
循环链表的创建及可控输出
c语言课程设计——基于链表的考场排位系统
金融信息安全實訓——22/4/19(上)
JS regular expression -- commonly used by individuals
Protocole d'authentification Kerberos
二十六.以主程序形式运行main
Stackoverflow:IActionContextAccessor Is Null
Wordpress blog Building Guide
全网最全量化硬件设施比拼大会
Regression prediction | MATLAB realizes Bayes Gru (Bayes optimized gating cycle unit) multiple input and single output
Modem dial-up playback, originally written in the millionaire, summarized.
Create a two-way linked list (detailed explanation)
遇到个奇怪的问题,同时开启本地和远程两个事务,远程事务是sql2000没问题,是sql2008的不报错,但是写不上数据
如何通过沟通激发开放性思维