Python code to train and test with Custom Vision of Cognitive Service

Premise

--Create a Cognitive Service Custom Vision Service resource in Azure and create a project --Select the project type to Classification --Prepare 5 or more images for learning per tag, and create and store a folder for each tag. --Prepare a separate image to test the model

Uploading images for learning and executing learning

train.py


import os, glob, time
from PIL import Image

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateEntry

#Set the following information in the environment variables in advance
# export CUSTOMVISION_ENDPOINT=<your API endpoint>
# export CUSTOMVISION_TRAINING_KEY=<your training key>
# export CUSTOMVISION_PROJECT_ID=<your project id>

project_id = os.environ['CUSTOMVISION_PROJECT_ID']

trainer = CustomVisionTrainingClient(os.environ['CUSTOMVISION_TRAINING_KEY'], endpoint=os.environ['CUSTOMVISION_ENDPOINT'])

#Create a list of tags
tag_list = []
tag_list.append(trainer.create_tag(project_id, "tagA"))
tag_list.append(trainer.create_tag(project_id, "tagB"))
tag_list.append(trainer.create_tag(project_id, "tagC"))

#Specify the path of the image corresponding to the tag
path_list = []
path_list.append('train-images/train/tagA/*.JPG')
path_list.append('train-images/train/tagB/*.JPG')
path_list.append('train-images/train/tagC/*.JPG')

#Create a list of images to upload
image_list = []

for i, tag in enumerate(tag_list):
    for file_path in glob.glob(path_list[i]):
        with open(file_path, "rb") as image_contents:
            image_list.append(ImageFileCreateEntry(name=os.path.basename(file_path), contents=image_contents.read(), tag_ids=[tag.id]))

#Upload up to 64 items of maximum size
for i in range(0, len(image_list), 64):
    print('uploading', i, '-', i+64, '/', len(image_list))
    upload_result = trainer.create_images_from_files(project_id, images=image_list[i:i+64])

    if not upload_result.is_batch_successful:
        print("Image batch upload failed.")
        for image in upload_result.images:
            print("Image status: ", image.status)
        exit(-1)

#Training execution
print ("Training...")
iteration = trainer.train_project(project_id)
while (iteration.status != "Completed"):
    iteration = trainer.get_iteration(project_id, iteration.id)
    print ("Training status: " + iteration.status)
    time.sleep(5)

Test the created model with a test image

quickTest.py


import os, glob
from PIL import Image

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateEntry

def probability(prediction):
   return prediction.probability

#Set the following information in the environment variables in advance
# export CUSTOMVISION_ENDPOINT=<your API endpoint>
# export CUSTOMVISION_TRAINING_KEY=<your training key>
# export CUSTOMVISION_PROJECT_ID=<your project id>

project_id = os.environ['CUSTOMVISION_PROJECT_ID']

trainer = CustomVisionTrainingClient(os.environ['CUSTOMVISION_TRAINING_KEY'], endpoint=os.environ['CUSTOMVISION_ENDPOINT'])

#Specify Iteration to test
iterations = trainer.get_iterations(project_id)
target_iteration = iterations[0]

print('target iteration:', target_iteration.name)

#Specify the file to use in the test
files = glob.glob('train-images/test/**/*.JPG', recursive=True)

for i, file in enumerate(files):
    image = open(file, 'rb').read()
    result = trainer.quick_test_image(project_id, image, iteration_id=target_iteration.id)

    max_prediction = max(result.predictions, key=probability)

    print(file, max_prediction.tag_name, max_prediction.probability)

Output image

target iteration: Iteration 1
train-images/test/tagA/IMG_001.JPG tagA 0.9999913
train-images/test/tagA/IMG_002.JPG tagA 0.9999975
train-images/test/tagB/IMG_003.JPG tagB 0.9999996
...

Relation

Amplify images for machine learning with python-Qiita

Recommended Posts

Python code to train and test with Custom Vision of Cognitive Service
[Azure] Hit Custom Vision Service with Python
List of Python code to move and remember
Coexistence of Python2 and 3 with CircleCI (1.0)
Operate Jupyter with REST API to extract and save Python code
How to build Python and Jupyter execution environment with VS Code
Installation of Visual studio code and installation of python
Fractal to make and play with Python
How to do portmanteau test with python
E2E test to select select box with CasperJS, Nightmare and Python + WebDriver + PhantomJS
I tried to get the authentication code of Qiita API with Python.
How to download all photos of egao school photo service with python base
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Script to tweet with multiples of 3 and numbers with 3 !!
How to specify attributes with Mock of python
How to use Service Account OAuth and API with Google API Client for python
Implementation of TRIE tree with Python and LOUDS
Links and memos of Python character code strings
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
Convert the character code of the file with Python3
Static analysis of Python code with GitLab CI
Continuation of multi-platform development with Electron and Python
[Blender x Python] Think of code with symbols
Example of reading and writing CSV with Python
Extract images and tables from pdf with python to reduce the burden of reporting
I tried to automate the article update of Livedoor blog with Python and selenium.
[Note] How to write QR code and description in the same image with python
I tried to compare the processing speed with dplyr of R and pandas of Python
The wall of changing the Django service from Python 2.7 to Python 3
Procedure to load MNIST with python and output to png
Python Ver. To introduce WebPay with a little code.
I want to handle optimization with python and cplex
Change IP settings to ACL of conoha with python
Easy partial download of mp4 with python and youtube-dl!
Try to operate DB with Python and visualize with d3
[Chapter 5] Introduction to Python with 100 knocks of language processing
Visualize the range of interpolation and extrapolation with python
A quick comparison of Python and node.js test libraries
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
Write code to Unit Test a Python web app
Something to enjoy with Prim Pro (X-Play) and Python
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
[Chapter 4] Introduction to Python with 100 knocks of language processing
Primality test with Python
Strengthen with code test ⑦
Strengthen with code test ⑨
Rewrite Python2 code to Python3 (2to3)
Strengthen with code test ⑤
Strengthen with code test ④
Primality test with python
Strengthen with code test ②
Strengthen with code test ①
Strengthen with code test ⑧
Strengthen with code test ⑨
How to parse Java source code with AST (Abstract Syntax Tree) using ANTLR and Python
Return the image data with Flask of Python and draw it to the canvas element of HTML
Let's statically check and format the code of E2E automatic test written in Python [VS Code]
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression