【踩坑记录】tensorflow1.x 更新到 2.x 遇到的坑&&condad的基本使用方法

【踩坑记录】tensorflow1.x 更新到 2.x 遇到的坑&&condad的基本使用方法,第1张

项目场景:

ROS 20.04

Python3.7

TensorFlow2.8


Conda配置

使用祖传代码时发现很多的问题,归根到底也是版本的问题

1)安装conda

感谢:Conda官方下载安装步骤及conda用法详细介绍 - Datapotumas - 博客园

https://blog.csdn.net/qq_41101213/article/details/

Conda官方主页:   https://github.com/conda/conda

Conda官方下载地址:  Conda官方下载    

 我是x86_64 linux系统,所以下载 https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh

bash Miniconda3-latest-Linux-x86_64.sh

 conda的bin文件会添加到环境变量中,需要source一下

source ~/.bashrc
2)创建Python虚拟环境
# 创建
conda create -n your_env_name python=3.7
3)切换环境
# linux
source activate your_env_name

#若返回系统原本环境,退出conda(或返回上一级环境)
conda deactivate 
4)在虚拟环境中安装额外的包
conda install -n your_env_name [package]

TensorFlow2.x沿用1.x代码前的修改

感谢:

Tensorflow2.0与Tensorflow1.x不兼容问题_小慧哇的博客-CSDN博客_tensorflow2兼容1吗

因为contrib这个库不稳定,从而在高级一点的版本中删除了contrib这个库。

TensorFlow 2.0中提供了tensorflow.compat.v1代码包来兼容原有1.x的代码,可以做到几乎不加修改的运行。

将:

import tensorflow as tf

替换成:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
使用迁移工具来自动迁移1.x代码到2.0

TensorFlow 2.0中提供了命令行迁移工具,来自动的把1.x的代码转换为2.0的代码。工具使用方法如下(假设我们的程序文件名称为first-tf.py):

tf_upgrade_v2 --infile first-tf.py --outfile first-tf-v2.py

或者整个文件夹:

tf_upgrade_v2 --intree tf_pose --outtree tf_pose

问题1:No module named ‘_pafprocess’ 解决办法:
$ cd ~/tf_pose/pafprocess/
$ swig -python -c++ pafprocess.i 
$ python3 setup.py build_ext --inplace

问题2:ModuleNotFoundError: No module named 'tensorflow.contrib'

问题部分:

import tensorflow.contrib.slim as slim

tensorflow2以上的版本没有contrib属性

解决办法:
pip install --upgrade tf_slim --user

将上面的问题部分修改为:

import tf_slim as slim

问题3:AttributeError: module 'tensorflow' has no attribute 'contrib'或者AttributeError: module 'tensorflow' has no attribute 'layers'

问题部分:

_init_xavier = tf.contrib.layers.xavier_initializer()
解决办法:

将上面的问题部分修改为:

_init_xavier = tf.truncated_normal_initializer(stddev=0.1)

问题4:AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'

问题部分:

_l2_regularizer_00004 = tf.contrib.layers.l2_regularizer(0.00004)
_l2_regularizer_convb = tf.contrib.layers.l2_regularizer(common.regularizer_conv)

TensorFlow删掉重复的接口,搭建网络则基本上复用了 Keras 的接口,即tf.keras

解决办法:

_l2_regularizer_00004 = tf.keras.regularizers.l2(0.00004)
_l2_regularizer_convb = tf.keras.regularizers.l2(common.regularizer_conv)

问题5:AttributeError: module 'tensorflow' has no attribute 'slim'

问题部分:

slim = tf.slim

解决办法:

import tf_slim as slim

添加以上代码,删掉问题部分


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存