Artificial intelligence experiment - cat and dog classification
In this experiment, the main purpose is to use the existing model algorithm to train cat and dog pictures. Finally, the function of being able to distinguish cat and dog pictures is realized. This experiment uses Huawei's MindSpore framework, which was launched by Huawei in 2019. Like TensorFlow and PyTorch, its main function is artificial intelligence computing. The steps in which the experiment was performed will be described below.
Environment build
In order to facilitate the management of the environment and distinguish it from other environments, I used it to set up the environment in this experiment Anacond
. The two necessary environments required are Python
theMindSpore
-
Create a Python environment
For the configuration of the Python environment, it is mainly to create a new environment in Anaconda, open it
CondaPrompt
and perform the following operations:conda create -n MindSpore python==3.7.5 #其中-n表示指定名称 #python==3.7.5为指定python版本 conda activate MindSpore #切换至MindSpore环境中
-
Install MindSpore
After the environment is installed
Python
, the next step is to install the MindSpore framework. There are two installation methods, one is to usepip
the installation, and the other is to install offline. Here I choose the pip online installation. Before using pip to install, you need to change the download source first to prevent timeout errors during downloading. There are also two methods for changing the source, as shown in the following operations,-
Global source change (long-term)
In the address bar of the file manager, enter
%AppData%
as shown in the figure,pip
Then it will jump to a folder, create a folder named in the folder , enter the folder to create apip.ini
file named, then copy and paste the following content, and then the source is changed,[global] timeout = 60 index-url = https://mirrors.huaweicloud.com/repository/pypi/simple/ trusted-host = mirrors.huaweicloud.com timeout = 120
-
Temporary source change (short term)
For temporary source replacement, you only need to use the command to specify the source address
pip
when entering the installation command-i
After the source is changed successfully, enter the following command to install, and the project only needs to be pressed
Y
,pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.5.0/MindSpore/cpu/x86_64/mindspore-1.5.0-cp37-cp37m-win_amd64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
In order to ensure that the installation is complete, enter the following command in the terminal
python -c "import mindspore;mindspore.run_check()"
If the output is as shown in the figure below, the installation is successful, (1.7.0 here should change to 1.5.0)
-
data preprocessing
After the environment is configured, you need to download the relevant code files and data sets. Both the code files and the data sets are in the folder. In the MindSporePetClassification
files are code
the code files in this experiment, kagglecatsanddogs_3367a.zip
the data sets, and converter1.5
the converter to convert the parameter results. It is a file suitable for APP. After confirming that the files are complete, install the necessary dependent packages, and then enter the following two commands to install. After the installation is complete, if you want to confirm the installation result, you can enter it to view pip list
.
pip install -r requirements.txt
pip install easydict
Open the conda terminal, switch to this file, and enter the following command for data preprocessing,
python .\code\preprocessing_dataset.py .\kagglecatsanddogs_3367a.zip
After the processing is completed, as shown in the figure,
The general flow chart of preprocessing is as follows,
The contents of the final compressed package will be divided into test set and training set after filtering. If you want to set a custom training set and test set ratio, you only need to change preprocessing_dataset.py
it eval_split
.
model training
Then start training after preprocessing the file. The training instructions are as follows,
python .\code\train.py
mobilenetV2.ckpt
There is a file named , which is a training checkpoint, used during training . It can also be said that this is a pre-trained weight file. This is a semi-finished product, but the amount of calculation can be reduced in this way to speed up the time required for the entire training. During the process, cat and dog pictures will be displayed twice, as shown below, which correspond to the pre-training results and the full training results respectively. The program will continue to run only after the two pictures have been read and closed. Finally, after the training is completed, the parameter file will be mobilenetv2.mindir
output to the root directory.
The following figure is a general flow chart of the entire training.
model conversion
At the end of the experiment, it needs to be arranged in the mobile device, and for the APP, the format of the training results needs to be converted before they can be imported into the APP. The conversion of the model uses the converter tool provided by MindSpore, which can convert mindir to ms model. Enter the converter1.5 folder in the folder, and then enter the following command,
.\converter_lite.exe --fmk=MINDIR --modelFile=../mobilenetv2.mindir --outputFile=pet
#其中
#--fmk 指定原始模型的格式在这里是MINDIR格式
#--modelFILE 对应原始模型路径
#--outputFile 输出模型的路径
After the conversion is successful, it will be as shown in the figure below, and the file will be output to the converter folder, and its file name ispet.ms
Configure APP
After getting the successfully converted model file, you need to download the APP for configuration. Its download address is as follows:
https://download.mindspore.cn/model_zoo/official/lite/apk/pet/petclassification1.1.3.apk
All that has to be done now is to pet.ms
import the file into the apk, the import of which takes place via USB. Install the apk on the phone and connect it to the computer with USB. After connecting, you need to open the USB debugging in the mobile phone. The opening method is located in 华为手机一般在设置->系统和更新->开发人员选项->USB调试中打开“USB调试模式”
. If it is the first time to open it, you need to open it first 设置->关于手机
. Click the version number for more than 7 times in a row, it will prompt "in developer mode", and then open the USB debugging mode . After the completion, you need to go back to the computer to operate, enter ADB
the folder, and then enter the following command in the conda terminal,
.\adb.exe push d:\MindSporePetClassification\converter1.5\pet.ms /sdcard/Android/data/com.mindspore.classificationforpet/files/
After loading the model into the app, it shows that it cannot be loaded. It shows that there is a problem with the suspected model file, but the correct model cannot be loaded even if the correct model is found in the compressed package. So considering the version issue, I downloaded an old version and a new version successively according to the download URL. The old version crashed directly, and the new version will PetClassification
be removed and cannot be loaded. So I considered the mobile phone problem, so I installed an Android emulator to try it out.
result same problem
Finally, I found out the path to load the model in version 1.1.3, and then pulled the model into the folder, and finally succeeded. The model path of version 1.1.3 is inAndroid/data/com.mindspore.classificationforpet/files/
The old version of APP cannot recognize the model, probably because of the converter version.