def train_project(subscription_key): trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT) # Create a new project print ("Creating project...") project = trainer.create_project(SAMPLE_PROJECT_NAME) # Make two tags in the new project hemlock_tag = trainer.create_tag(project.id, "Hemlock") cherry_tag = trainer.create_tag(project.id, "Japanese Cherry") print ("Adding images...") hemlock_dir = os.path.join(IMAGES_FOLDER, "Hemlock") for image in os.listdir(hemlock_dir): with open(os.path.join(hemlock_dir, image), mode="rb") as img_data: trainer.create_images_from_data(project.id, img_data.read(), [ hemlock_tag.id ]) cherry_dir = os.path.join(IMAGES_FOLDER, "Japanese Cherry") for image in os.listdir(cherry_dir): with open(os.path.join(cherry_dir, image), mode="rb") as img_data: trainer.create_images_from_data(project.id, img_data.read(), [ cherry_tag.id ]) print ("Training...") iteration = trainer.train_project(project.id) while (iteration.status == "Training"): iteration = trainer.get_iteration(project.id, iteration.id) print ("Training status: " + iteration.status) time.sleep(1) # The iteration is now trained. Make it the default project endpoint trainer.update_iteration(project.id, iteration.id, is_default=True) print ("Done!") return project
def train_project(subscription_key): try: prediction_resource_id = os.environ[ PREDICTION_RESOURCE_ID_KEY_ENV_NAME] except KeyError: raise PredictionResourceMissingError( "Didn't find a prediction resource to publish to. Please set the {} environment variable" .format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME)) trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT) # Create a new project print("Creating project...") project = trainer.create_project(SAMPLE_PROJECT_NAME, classification_type=Classifier.multiclass) # Make two tags in the new project hemlock_tag = trainer.create_tag(project.id, "Hemlock") cherry_tag = trainer.create_tag(project.id, "Japanese Cherry") pine_needle_tag = trainer.create_tag(project.id, "Pine Needle Leaves") flat_leaf_tag = trainer.create_tag(project.id, "Flat Leaves") print("Adding images...") hemlock_dir = os.path.join(IMAGES_FOLDER, "Hemlock") for image in os.listdir(hemlock_dir): with open(os.path.join(hemlock_dir, image), mode="rb") as img_data: trainer.create_images_from_data( project.id, img_data.read(), [hemlock_tag.id, pine_needle_tag.id]) cherry_dir = os.path.join(IMAGES_FOLDER, "Japanese Cherry") for image in os.listdir(cherry_dir): with open(os.path.join(cherry_dir, image), mode="rb") as img_data: trainer.create_images_from_data(project.id, img_data.read(), [cherry_tag.id, flat_leaf_tag.id]) print("Training...") iteration = trainer.train_project(project.id) while (iteration.status == "Training"): iteration = trainer.get_iteration(project.id, iteration.id) print("Training status: " + iteration.status) time.sleep(1) # The iteration is now trained. Name and publish this iteration to a prediciton endpoint trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME, prediction_resource_id) print("Done!") return project
project = trainer.create_project("ElephantNoElephant") print("Project created") # Make two tags in the new project print("Creating tags") elephant_tag = trainer.create_tag(project.id, "Elephant") giraffe_tag = trainer.create_tag(project.id, "Giraffe") print("Adding images...") # Add all images in Elephant folder to your project with the tag "elephant" IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "ElephantGiraffeTrainingImages") elephant_dir = os.path.join(IMAGES_FOLDER, "Elephant") for image in os.listdir(elephant_dir): with open(os.path.join(elephant_dir, image), mode="rb") as img_data: trainer.create_images_from_data(project.id, img_data.read(), [elephant_tag.id]) print("added elephants") # Add all images in Giraffe folder to your project with the tag "giraffe" IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "ElephantGiraffeTrainingImages") giraffe_dir = os.path.join(IMAGES_FOLDER, "Giraffe") for image in os.listdir(giraffe_dir): with open(os.path.join(giraffe_dir, image), mode="rb") as img_data: trainer.create_images_from_data(project.id, img_data.read(), [giraffe_tag.id]) print("added giraffes") # Train the model print("Training...") iteration = trainer.train_project(project.id)
contents=image_contents.read(), tag_ids=[tag_id])) return image_list def uploadImageList(image_list): upload_result = trainer.create_images_from_files(project.id, images=image_list) 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) for tag in tags: tag_id = createTag(tag) print("tag creation done with tag id {tag_id}") # Set directory to current tag base_image_url = image_folder + "/" + tag + "/" photo_name_list = os.listdir(base_image_url) for file_name in photo_name_list[0:21]: print(file_name) with open(base_image_url + file_name, "rb") as image_contents: trainer.create_images_from_data(project.id, image_contents.read(), tag_ids=[tag_id]) print('Project ID: ' + project.id)
hardshell_jacket_tag = trainer.create_tag(project.id, "hardshell jacket") insulated_jacket_tag = trainer.create_tag(project.id, "insulated jacket") # In[43]: #trainer.create_images_from_files() import os root = r'/data/home/team15/notebooks' hardshell_dir = r'resize_images/hardshell_jackets' insulated_dir = r'resize_images/insulated_jackets' for img1 in os.listdir(os.path.join(root, hardshell_dir)): img1 = os.path.join(root, hardshell_dir, img1) with open(img1, 'rb') as f: trainer.create_images_from_data(project.id, f.read(), [hardshell_jacket_tag.id]) # In[44]: #trainer.create_images_from_files() for img1 in os.listdir(os.path.join(root, insulated_dir)): img1 = os.path.join(root, insulated_dir, img1) with open(img1, 'rb') as f: trainer.create_images_from_data(project.id, f.read(), [insulated_jacket_tag.id]) # In[50]: import time print("Training...")