[Pitfall record] Pitfalls encountered when updating tensorflow 1.x to 2.x && basic usage of condad

Project scenario:

ROS 20.04

Python3.7

TensorFlow2.8


Conda configuration

Many problems are found when using ancestral code. In the final analysis, it is also a version problem.

1) Install conda

Thanks to: Conda official download and installation steps and detailed introduction to conda usage - Datapotumas - Blog Park

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

Conda official homepage:    https://github.com/conda/conda

Conda official download address:   Conda official download    

 I am an x86_64 linux system, so download  https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh

bash Miniconda3-latest-Linux-x86_64.sh

 The bin file of conda will be added to the environment variable and needs to be sourced.

source ~/.bashrc

2) Create a Python virtual environment

# 创建
conda create -n your_env_name python=3.7

3) Switch environment

# linux
source activate your_env_name

#若返回系统原本环境,退出conda(或返回上一级环境)
conda deactivate 

4) Install additional packages in the virtual environment

conda install -n your_env_name [package]

TensorFlow2.x follows the modifications before the 1.x code

grateful:

Incompatibility between Tensorflow2.0 and Tensorflow1.x_Xiao Huiwa’s blog-CSDN blog_Is tensorflow2 compatible with 1

Because the contrib library is unstable, the contrib library has been deleted in the more advanced version.

TensorFlow 2.0 provides the tensorflow.compat.v1 code package to be compatible with the original 1.x code and can run with almost no modifications.

Will:

import tensorflow as tf

Replace with:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

Use migration tools to automatically migrate 1.x code to 2.0

TensorFlow 2.0 provides a command line migration tool to automatically convert 1.x code to 2.0 code. The tool is used as follows (assuming that our program file name is first-tf.py):

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

Or the entire folder:

tf_upgrade_v2 --intree tf_pose --outtree tf_pose

Question 1: No module named '_pafprocess'

Solution:

$ cd ~/tf_pose/pafprocess/
$ swig -python -c++ pafprocess.i 
$ python3 setup.py build_ext --inplace

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

Question part:

import tensorflow.contrib.slim as slim

There is no contrib attribute for versions above tensorflow2

Solution:

pip install --upgrade tf_slim --user

Modify the above question part to:

import tf_slim as slim

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

Question part:

_init_xavier = tf.contrib.layers.xavier_initializer()

Solution:

Modify the above question part to:

_init_xavier = tf.truncated_normal_initializer(stddev=0.1)

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

Question part:

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

TensorFlow deletes duplicate interfaces and basically reuses the Keras interface when building the network, namely tf.keras

Solution:

_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'

Question part:

slim = tf.slim

Solution:

import tf_slim as slim

Add the above code and delete the problematic part


问题6;RuntimeError: module compiled against API version 0xe but this version of numpy

Solution:

pip3 install -U numpy

 Update numpy version

Guess you like

Origin blog.csdn.net/weixin_44362628/article/details/124627633