[Tutorial] Building a Tensorflow environment in Anaconda for face recognition

Written in the front: The artificial intelligence course in the next semester is designed to do a face recognition program. A series of environments are required to be configured. And it took me two whole days, repeated the installation a dozen times, and just set up the environment. The preparations for using face recognition with jupyter notebook are realized (including installation of various software, libraries, configuration of files, etc.). This article strives to describe the most convenient environment construction process in the simplest language.

Things to prepare for installation (in order of installation):

Note: For the following items, the versions need to be matched, and they do not need to be exactly as I wrote.
To check which versions match each other, please refer to: https://docs.floydhub.com/guides/environments/
anaconda
python 3.5.x version
tensorflow1.2
opencv3.3.1 version
keras version 2.0.6 version
Pillow
sklearn0.19.0 version
Note! These things of tensorflow have very strict version requirements! Once the version does not match, or if you make a little mistake, a lot of bugs will be reported, so it is recommended to follow my instructions step by step, and do not engage in personal heroism!
And the order can't be messed up! Because the latter few have to rely on the environment of tensorflow.

installation steps

1. Install Anaconda

(If you have installed Anaconda, it is recommended to uninstall the original one and download it again according to the link I gave you, to ensure that everything is safe)
1. Download link: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3- 4.0.0-Windows-x86_64.exe
2. Install after downloading
Installation process:
choose the first one, because there will be some small problems later, just choose the first one.
insert image description here
Note:
(1) The file path can be chosen by yourself, but there must be no spaces, that is, each parent folder name of the installed folder cannot contain spaces !
(2) If the Anaconda installation package used is the version I gave you, pay attention: the selected target folder must not exist . We need to build the Anaconda3 folder by ourselves , that is to say, you only need to select the folder of the installer, and then add Anaconda3 to the back, the program will create a folder and install it in it (I doubt writing the installer). 's programmer's head is stuck in the door).
insert image description here
Choose both.
insert image description here
Then click Install to install and wait for the installation to complete.
3. Check whether the installation is complete:
Open the system command line and enter the following command

conda --version

If the Anaconda version number is displayed, the installation is successful.
Otherwise the installation fails.

Second, re-edit the pip installation source file (very important)

This step is very important, because many of the following steps need to use pip to download the environment file on the command line, and the direct pip download is directly downloaded from a foreign server, so the download is slow!
What should I do? Tsinghua's bosses have thought of this for a long time. In order to benefit the domestic students' download acceleration, they downloaded a copy of the foreign resources and put them on the domestic server, so that the domestic download will be very fast! (Thanks to Tsinghua dalao)
Here is a small operation, you can change the pip default from the foreign Anaconda server to download from the Tsinghua server, what is the operation? See below.
1. Find "C:\Users\XXX" where XXX is your account name, in my case it is "C:\Users\MI", and find the ".condarc" file in it.
2. Right-click the open mode, select the text document mode to open, copy the following content and replace the original content (it is recommended to make a copy of the original .condarc file, in case you cannot use it).

ssl_verify: true
channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
show_channel_urls: true
default_python:

Remember to save and exit after replacing! ! !

3. Create a new environment

What is the new environment? In short, in order to meet different programming needs, Anaconda can create many different virtual environments, in which you can freely download and match various libraries, and the libraries do not conflict.
Here we have to install various libraries that support face recognition.
Steps:
1. Open the start menu and open Anaconda Prompt in Anaconda3. (Note that most of the subsequent operations are performed in this black box.)
insert image description here
2. Input

conda create -n tensorflow python=3.5

This sentence means to create a new environment named tensorflow, python version is 3.5.x (anaconda will automatically choose a suitable python version of 3.5.x).
Then you will be asked yes or no? Enter y and press Enter.
Wait for the download and installation to complete.
3. Activate the environment (ie convert to the environment you just created)
input

activate tensorflow

If there is a prefix like the one shown below, the download is successful.
insert image description here
Do not close the black box and proceed to the next step.

Fourth, update pip (very very very important)

Because the original pip version is relatively low (maybe the Anaconda version I gave you is relatively low, if you download the tensorflow environment directly, there will be an error (don't ask how I know the TAT), so you must update pip first and then do the following operations.
Pay special attention : There are two environments now, one is the default root environment of Anaconda, and the other is the tensorflow environment that was just created.
So each environment has pip, and what we want to update now is the pip of the tensorflow environment .
Step:
Enter

python -m pip install --upgrade pip

Then wait for the installation to succeed.
Note:
If the error "No module named pip" appears, you
need to open the anaconda prompt as an administrator
first, enter python -m ensurepip to
install and then enter the above sentence.
If you enter the first sentence, the upgrade is successful and go directly to the next step.
Reference: https://blog.csdn.net/wangh0802/article/details/98895065
// Of course, the same operation can also upgrade the pip of the anaconda default environment. (You can not upgrade, it should not affect.)

Five, install tensorflow

(The previous foreshadowing is for this!!!) The cpu version of
tensorflow is divided into cpu and gpu versions.
The installation of the cpu version is relatively simple, and the installation of the gpu version takes a few more steps.
The cpu version can be installed with or without a dedicated display (gpu), but the gpu version can only be installed if the NVIDIA graphics card is used. Of course, the gpu version will process faster when running the program later.
It is recommended and only the cpu version is introduced here. (It's not lazy and qiong
1. Enter in the tensorflow environment:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==1.2.0
(这里版本号1.2.0是你需要的版本号  我这里是1.2.0,你可以改成别的,下同)

(Just copy and paste and press Enter)
Here, because we have changed the .condarc file in the second step, theoretically input this step

pip install tensorflow==1.2.0

It is also completely possible! Playing that long is just for insurance, and you can also enter a simple sentence.
Then enter y where you should enter yes. (Indicates yes)
Wait for the installation to complete.
2. Check whether tensorflow is successfully
installed After the installation in the previous step , enter "python" in the tensorflow environment to enter the python editing mode
and then it can be used as a python editor.
Type "import tensorflow as tf"
and press Enter. If "No module named tensorflow" does not appear, then the tensorflow environment is successfully installed.
What? Do you think this test is too simple to explain the problem? I think so too.
Then copy the following code, paste it into the black box and run it. (simple matrix multiplication)

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
sess = tf.compat.v1.Session()
result = sess.run(product)
print(result)
sess.close()

If the output is [[12.]], the installation is successful.
The reference link for the inspection code snippet: https://blog.csdn.net/RobbyDeng/article/details/103054880
At this point, the big end is over.
There are a few other packs left, and we're done with it in one fell swoop.

Six, install opencv 3.3.1

(Must be this version!!!)
1. Download the opencv3.3.1 package (64-bit)
download link: click here
2. Put the downloaded package in C:\User\XXX.
Then open the anaconda prompt, switch to the tensorflow environment, and enter:

pip install opencv_python-3.3.1.11-cp35-cp35m-win_amd64.whl

Wait for the installation to complete.

Seven, install keras, Pillow and sklearn

1. Install keras 2.0.x and enter

pip install keras==2.0.6

2. Install Pillow and enter

pip install Pillow

3. Install the sklearn module
Because the sklearn module is composed of several packages, you must first download numpy, joblib, and scipy,
but enter pip list. We found that the first two, anaconda has been downloaded for us, so just download scipy.
To install scipy, enter

pip install scipy==1.4.1

To install matplotlib, enter

pip install matplotlib

Finally install sklearn! enter

 pip install -U scikit-learn

Reference link:
https://blog.csdn.net/dlh_sycamore/article/details/79584039

Eight, install jupyter notebook

Enter in the tensorflow environment

pip install jupyter notebook

Then just wait.
If you want to open jupyter, just enter jupyter notebook in the tensorflow environment to open it.
You may ask: It is too troublesome to open prompt and switch the environment every time. Can it be the tensorflow environment as soon as it is opened? Can.
See: https://blog.csdn.net/yushdd/article/details/94553290

Finish the flower arrangement! ! ! ! !

Attached reference link:
https://blog.csdn.net/qq_42633819/article/details/81191308
https://www.cnblogs.com/ming-4/p/11516728.html
Also thanks to Tsinghua mirror! ! ! Download speed++
I will start the experiment tomorrow (crying, it seems to be over, it just started...)

2021.2.22 Update after the experiment is done

zmgg is done!
Although it has been rough.
The process of doing the experiment and the pits encountered are also shared!
I did it according to the following blog
https://blog.csdn.net/qq_42633819/article/details/81191308
1. First of all, the places of tk and th need to be changed. Change to first_channel or something, it has something to do with the version, specifically Can Baidu.
2. If there is a prompt that the library has not been downloaded, just use pip to download it!
3. At the beginning, I collected data with a laptop and found that the recognition accuracy rate was not high. It took a long time to debug and found that the color captured by my laptop camera was not good. After taking a selfie with a mobile phone again, the success rate has reached 100% successfully.
4. I haven't thought of other pits yet, but these are enough to mess with me all day. But fortunately it's done. If there is any pit, I will add it later!
The zmgg handsome photo is attached below. (Well, it's not Peng Yuyan after all
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326151887&siteId=291194637