Azure AI Tutorial 4 - Classification trên Azure Custom Vision
Tiếp tục với bài viết về Computer Vision. Chúng ta đến với chuyên mục Classification trên Azure.
Các bạn chưa biết classification là gì? Hoạt động như thế nào có thể đọc lại Azure AI 3.
Giờ mình cũng thực hành trên Azure. Mình sẽ hands-on 4 phần
- Tạo project Custom Vision
- Upload data và train model
- Test model
- Publish và consume model
Cùng bắt đầu nào
1. Tạo CustomVision workspace
- Tại Portal của Azure -> Tìm “CustomVision” -> Chọn Create
- Điền đầy đủ thông tin -> Chọn Review + create (Tùy vào mục đích, ở đây mình chọn cả training và Prediction)
2. Tạo New Project và Upload data
-
Vào trang custionvision.ai để tạo project.
Chúng ta sẽ tạo 1 model phân loại trái cây: Cam
, Táo
, Chuối
.
Các bạn có thể tải hình ảnh các loại trái cây ở đây : https://aka.ms/fruit-images
- Upload data vào custom vision với tag cho từng loại
- Làm lại thao tác với babana, orange -> Kết quả cuối cùng ta đạt được
- Click Train (Chờ khoảng 5->10p) để train model và kết quả là đây
3. Quick Test Model
-
Chọn Quick Test (Chúng ta có thể test bằng 2 cách: Nhập image url hoặc upload file)
Nhận diện đây là : apple 98,6%
99,7% đây là Orange
99,9% là banana
4. Publish và consume model
-
Chọn Publish và Copy Project ID của MVClassifier
- Copy Key & End Point
- Tạo Notebook (Ở đây mình dùng ngôn ngữ Python để sử dụng model)
Tạo notebook như thế nào?
1. Install thư viên bằng lệnh
!pip install azure-cognitiveservices-vision-customvision
2. Nhập thông tin
Thông tin bao gồm model name, project ID, Key, Endpoint đã copy bên trên
project_id = '67bc1c64-5c39-4d81-bd56-fd9e87073460'
cv_key = '3a9c6ce2710742ce89ced98e9df157d1'
cv_endpoint = 'https://mvblogcustomvision-prediction.cognitiveservices.azure.com/'
model_name = 'MVClassifier'
3. Sử dụng model
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials
import matplotlib.pyplot as plt
from PIL import Image
import os
%matplotlib inline
# Get the test images from the data/vision/test folder
test_folder = os.path.join('data', 'image-classification', 'test-fruit')
test_images = os.listdir(test_folder)
# Create an instance of the prediction service
credentials = ApiKeyCredentials(in_headers={"Prediction-key": cv_key})
custom_vision_client = CustomVisionPredictionClient(endpoint=cv_endpoint, credentials=credentials)
# Create a figure to display the results
fig = plt.figure(figsize=(16, 8))
# Get the images and show the predicted classes for each one
print('Classifying images in {} ...'.format(test_folder))
for i in range(len(test_images)):
# Open the image, and use the custom vision model to classify it
image_contents = open(os.path.join(test_folder, test_images[i]), "rb")
classification = custom_vision_client.classify_image(project_id, model_name, image_contents.read())
# The results include a prediction for each tag, in descending order of probability - get the first one
prediction = classification.predictions[0].tag_name
# Display the image with its predicted class
img = Image.open(os.path.join(test_folder, test_images[i]))
a=fig.add_subplot(len(test_images)/3, 3,i+1)
a.axis('off')
imgplot = plt.imshow(img)
a.set_title(prediction)
plt.show()
Và đây là kết quả model
Các bạn có thể theo dõi và cải thiện model tại muc Prediction
Chúc các bạn thành công. Hẹn gặp ở phần tiếp theo nhé!