Image Classification
Mobile Net¶
MobileNet V2 is a lightweight convolutional neural network. On the basis of MobileNet, it has made two major improvements: Inverted Residuals and Linear bottlenecks. The PaddleHub Module is trained on Baidu's self-built animal dataset and can be used for image classification and feature extraction. Currently, it supports the classification and recognition of 7978 animals. Details of the model can be found in the paper.
Sample result example¶
Enter the file path and the model will give its predictions:
Image Source (https://www.pexels.com)
Result:
松鼠
Let's try it out now
Prerequisite¶
1. Dependencies¶
Please visit dependencies
2. mobilenet_v2_animals Prerequisites¶
Package | Version |
---|---|
paddlepaddle | >= 1.6.2 |
paddlehub | >= 1.6.0 |
3. Install the Model¶
hub install pyramidbox_lite_server
Serve the Model¶
Install Pinferencia¶
First, let's install Pinferencia.
pip install "pinferencia[streamlit]"
Create app.py¶
Let's save our predict function into a file app.py
and add some lines to register it.
app.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Run the service, and wait for it to load the model and start the server:
$ uvicorn app:service --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [xxxxx] using statreload
INFO: Started server process [xxxxx]
INFO: Waiting for application startup.
INFO: Application startup complete.
$ pinfer app:service --reload
Pinferencia: Frontend component streamlit is starting...
Pinferencia: Backend component uvicorn is starting...
Test the service¶
Tips
The image exists on the service machine, you can enter the relative path of the service file or the absolute path of the file
Open http://127.0.0.1:8501, and the template Url Image To Text
will be selected automatically.
Request
curl --location --request POST \
'http://127.0.0.1:8000/v1/models/classifier/predict' \
--header 'Content-Type: application/json' \
--data-raw '{"data": ["/9j/4AAQS........"]}'
Response
{
"model_name": "classifier",
"data": [
{
"松鼠": 0.9506056308746338
}
]
}
Create the test.py
.
test.py | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
$ python test.py
{
"model_name": "classifier",
"data": [
{
"松鼠": 0.9506056308746338
}
]
}
Even cooler, go to http://127.0.0.1:8000, and you will have a full documentation of your APIs.
You can also send predict requests just there!