F(X) Team of Ali Tao Department – Su Chuan
Use your Mac to train and deploy a picture classification model. What if you don’t have a Mac? You can experience machine learning without a Mac.
This article teaches you how to train and deploy a picture classification model in aliyunyun development platform by hand ~~~~
Create an
First of all, we enter ali Cloud development platform workbench.aliyun.com, click free cloud development, and log in with our ali cloud account.
After login, create a team, create a product under the team, and then select a product and click enter to create an application. If you already have a team and product, select a product and create an application under the product.
Creating an application Select a Python front-end and back-end integrated application solution on the WEB UI.
Apply name fill in name – Job name, Region select Hangzhou.
After the creation, click Development deployment to enter.
After entering the application, delete the files under the application and keep.gitignore and.Workbench.
Then open the terminal, clone instructor prepares the code and enters the project.
git clone https://github.com/imgcook/ml-mac-classify.git
Copy the code
The project catalog is as follows:
. ├ ─ ─ the deploy - project / / deploy code │ ├ ─ ─ app. Py │ └ ─ ─ label. The json ├ ─ ─ predict project / / forecast code │ ├ ─ ─ predict. Py │ └ ─ ─ the test ├ ─ ─ ├── garbage ├── utils_path.py // Exercises ├── utils_path.py // Exercises ├── utils_path.pyCopy the code
Environment to prepare
Note: We are now installing the environment under the terminal Tab you are currently running,
Installation Anaconda
Download:
Wget HTTP: / / https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.shCopy the code
Installation:
Bash Anaconda3 5.3.1 - Linux - x86_64. ShCopy the code
After executing the installation command, the following message is displayed:
When you press Enter as prompted, the word “MORE” appears.
Pressing the space all the time will print the licence down. Press and hold the space until you see the command below, then type yes to accept the protocol, and then press ENTER to confirm the installation location.
Then start installation:
To ask if you want to configure anaconda’s environment in your environment, type yes
Is vscode installed? Enter no
After the installation is successful, run the following command on the CLI to make the environment variables take effect immediately:
source ~/.bashrc
Copy the code
You can run the following command to view environment variables
cat ~/.bashrc
Copy the code
You can see that anaconda’s environment variables have been automatically added to the.bashrc file
You can see that there are many packages already installed in Anaconda by executing the following command. If there are any packages that use these packages, there is no need to install them, and the Python environment is installed.
conda list
Copy the code
Installation-dependent dependencies
Upgrade PIP first
pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple
Copy the code
Install dependencies:
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install keras -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
$ pip install tensorflow --ignore-installed wrapt -i https://pypi.tuna.tsinghua.edu.cn/simple
Copy the code
Finally, you can check to see if the installation was successful with PIP List.
$ pip list
Copy the code
Sample preparation
There are only four categories: Button, keyboard, searchbar and Switch, which are stored in the train-project/dataset directory with about 200 samples for each category.
You can add some samples here for variety and quantity.
Model training
Execute the following command to begin training:
$python nn_train.py $CD ml-mac-classify/train-projectCopy the code
Training log has been printed:
The epoch parameter was set to 5 in the code, and the training ended after 5 rounds.
Finally, save the training results:
You can see that two files are generated on the left: the model file cnn.model.h5 and the loss function curve CNn_plot.png. (Note that CNn.model. h5 is very large, it is recommended not to click on this platform to view, easy to jam.)
For practical application scenarios, the data set is large, and the EPOCH parameter will be set relatively large, and it will be trained on high-performance machines. Our current purpose is to understand how to complete a machine learning task. In order to complete the training in a short time, only a few samples are given to train the model, and the epoch is also very small (5). Of course, the recognition accuracy of the model will be poor. You can expand the number of data sets and tune them to improve the predictive accuracy of your model.
Deploy model services
Copy or move the CNn.model. h5 file to deploy-project.
# trani-project $mv cnn.model.h5.. /deploy-projectCopy the code
The deploy-project directory is now as follows:
.
├── app.py
├── cnn.model.h5
└── label.json
Copy the code
If the sample type is changed, you need to modify deploy-project/label.json accordingly. For example, to create a category select, you need to add select to the file.
{
"button": 0,
"keyboard": 1,
"searchbar": 2,
"switch": 3,
"select": 4
}
Copy the code
Create anaconda’s virtual environment
Execute the commands in the following order.
$CD... $CD... $CD... $CD... /deploy-project/ # create a python environment using conda. EAS python SDK $ENV/bin/ PIP install http://eas-data.oss-cn-shanghai.aliyuncs.com/sdk/allspark-0.9-py2.py3-none-any.whl # install depend on other packages $ENV/bin/PIP install tensorflow keras opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple $ ENV/bin/pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple # activate virtual environment $conda. Activate/ENV # exit virtual environment (when not in use to perform $conda deactivate)Copy the code
Deploy the service
Now that you can deploy locally, execute the following command:
./ENV/bin/python app.py
Copy the code
The following log shows that the deployment was successful.
Once the deployment is successful, the model services can be accessed through localhost:8080/predict.
Open a new terminal Tab, and use curl to send a post request to predict the classification of images. Images can be replaced with your images.
curl -X POST 'localhost:8080/predict' \ -H 'Content-Type: application/json' \ -d '{ "images": (" https://img.alicdn.com/tfs/TB1W8K2MeH2gK0jSZJnXXaT1FXa-638-430.png "), "threshold", 0.5} 'Copy the code
Get the prediction (here the prediction is wrong) :
{"content": [{"isConfident": true, "label": "keyboard"}]}
Copy the code
Matters needing attention
- Every step of the environment installation in the documentation must be successful, otherwise it is easy to encounter various environmental problems.
- The model file cnn.model.h5 is very large, do not click to view it, otherwise the whole page will freeze.
- You need to reinstall the application environment next time
- Git push requires deleting large files, such as ENV and cnn.model.h5, or it cannot commit.