Rulați segmentarea imaginii cu Amazon SageMaker JumpStart PlatoBlockchain Data Intelligence. Căutare verticală. Ai.

Rulați segmentarea imaginii cu Amazon SageMaker JumpStart

În decembrie 2020, a anunțat AWS disponibilitatea generală a Amazon SageMaker JumpStart, o capacitate de Amazon SageMaker care vă ajută să începeți rapid și ușor cu învățarea automată (ML). JumpStart oferă o reglare fină și implementare cu un singur clic a unei game largi de modele pregătite în prealabil pentru sarcinile ML populare, precum și o selecție de soluții end-to-end care rezolvă problemele comune de afaceri. Aceste caracteristici îndepărtează sarcinile grele din fiecare pas al procesului ML, facilitând dezvoltarea modelelor de înaltă calitate și reducând timpul până la implementare.

This post is the third in a series on using JumpStart for specific ML tasks. In the prima postare, we showed how you can run image classification use cases on JumpStart. In the al doilea post, we showed how you can run text classification use cases on JumpStart. In this post, we provide a step-by-step walkthrough on how to fine-tune and deploy an image segmentation model, using trained models from MXNet. We explore two ways of obtaining the same result: via JumpStart’s graphical interface on Amazon SageMaker Studio, și programatic prin API-uri JumpStart.

If you want to jump straight into the JumpStart API code we explain in this post, you can refer to the following sample Jupyter notebooks:

Prezentare generală JumpStart

JumpStart helps you get started with ML models for a variety of tasks without writing a single line of code. At the time of writing, JumpStart enables you to do the following:

  • Implementați modele pre-instruite pentru sarcinile ML obișnuite – JumpStart vă permite să abordați sarcinile ML obișnuite fără efort de dezvoltare, oferind o implementare ușoară a modelelor pregătite în prealabil pe seturi de date mari, disponibile public. Comunitatea de cercetare ML a depus un efort mare pentru ca majoritatea modelelor dezvoltate recent să fie disponibile public pentru utilizare. JumpStart găzduiește o colecție de peste 300 de modele, care acoperă cele mai populare 15 sarcini ML, cum ar fi detectarea obiectelor, clasificarea textului și generarea de text, făcându-le ușor de utilizat pentru începători. Aceste modele sunt extrase din hub-uri de model populare, cum ar fi TensorFlow, PyTorch, Hugging Face și MXNet.
  • Ajustați modelele pre-antrenate – JumpStart vă permite să reglați modelele pre-antrenate fără a fi nevoie să scrieți propriul algoritm de antrenament. În ML, se numește capacitatea de a transfera cunoștințele învățate într-un domeniu într-un alt domeniu transfer de învățare. Puteți utiliza învățarea prin transfer pentru a produce modele precise pe seturile dvs. de date mai mici, cu costuri de instruire mult mai mici decât cele implicate în formarea modelului original. JumpStart include, de asemenea, algoritmi de antrenament populari bazați pe LightGBM, CatBoost, XGBoost și Scikit-learn, pe care îi puteți antrena de la zero pentru regresie și clasificare tabelară.
  • Utilizați soluții prefabricate – JumpStart oferă un set de 17 soluții pentru cazuri de utilizare obișnuite ML, cum ar fi prognoza cererii și aplicații industriale și financiare, pe care le puteți implementa cu doar câteva clicuri. Soluțiile sunt aplicații ML end-to-end care îmbină diverse servicii AWS pentru a rezolva un anumit caz de utilizare în afaceri. Ei folosesc Formarea AWS Cloud șabloane și arhitecturi de referință pentru implementare rapidă, ceea ce înseamnă că sunt complet personalizabile.
  • Consultați exemple de notebook pentru algoritmii SageMaker – SageMaker oferă o suită de algoritmi încorporați pentru a ajuta oamenii de știință de date și practicienii ML să înceapă cu instruirea și implementarea rapidă a modelelor ML. JumpStart oferă exemple de notebook-uri pe care le puteți folosi pentru a utiliza rapid acești algoritmi.
  • Examinați videoclipuri de antrenament și bloguri – JumpStart oferă, de asemenea, numeroase postări pe blog și videoclipuri care vă învață cum să utilizați diferite funcționalități în SageMaker.

JumpStart acceptă setări personalizate VPC și AWS Service Management Service (AWS KMS), chei de criptare, astfel încât să puteți utiliza modelele și soluțiile disponibile în siguranță în mediul dvs. de întreprindere. Puteți transmite setările de securitate la JumpStart din Studio sau prin intermediul SDK-ului SageMaker Python.

Segmentarea semantică

Semantic segmentation delineates each class of objects appearing in an input image. It tags (classifies) each pixel of the input image with a class label from a predefined set of classes. Multiple objects of the same class are mapped to the same mask.

The model available for fine-tuning builds a fully convolutional network (FCN) “head” on top of the base network. The fine-tuning step fine-tunes the FCNHead while keeping the parameters of the rest of the model frozen, and returns the fine-tuned model. The objective is to minimize per-pixel softmax cross entropy loss to train the FCN. The model returned by fine-tuning can be further deployed for inference.

The input directory should look like the following code if the training data contains two images. The names of the .png files can be anything.

input_directory
    |--images
        |--abc.png
        |--def.png
    |--masks
        |--abc.png
        |--def.png
    class_label_to_prediction_index.json

The mask files should have class label information for each pixel.

Segmentarea instanțelor

Instance segmentation detects and delineates each distinct object of interest appearing in an image. It tags every pixel with an instance label. Whereas semantic segmentation assigns the same tag to pixels of multiple objects of the same class, instance segmentation further labels pixels corresponding to each occurrence of an object on the image with a separate tag.

Currently, JumpStart offers inference-only models for instance segmentation and doesn’t support fine-tuning.

The following images illustrate the difference between the inference in semantic segmentation and instance segmentation. The original image has two people in the image. Semantic segmentation treats multiple people in the image as one entity: Person. However, instance segmentation identifies individual people within the Person categorie.

Prezentare generală a soluțiilor

The following sections provide a step-by-step demo to perform semantic segmentation with JumpStart, both via the Studio UI and via JumpStart APIs.

Parcurgem următorii pași:

  1. Accesați JumpStart prin interfața de utilizare Studio:
    1. Executați inferență pe modelul pre-antrenat.
    2. Reglați fin modelul pre-antrenat.
  2. Utilizați JumpStart în mod programatic cu SageMaker Python SDK:
    1. Executați inferență pe modelul pre-antrenat.
    2. Reglați fin modelul pre-antrenat.

We also discuss additional advanced features of JumpStart.

Accesați JumpStart prin interfața de utilizare Studio

În această secțiune, demonstrăm cum să antrenăm și să implementăm modele JumpStart prin interfața de utilizare Studio.

Executați inferență pe modelul pre-antrenat

The following video shows you how to find a pre-trained semantic segmentation model on JumpStart and deploy it. The model page contains valuable information about the model, how to use it, expected data format, and some fine-tuning details. You can deploy any of the pre-trained models available in JumpStart. For inference, we pick the ml.g4dn.xlarge instance type. It provides the GPU acceleration needed for low inference latency, but at a lower price point. After you configure the SageMaker hosting instance, choose Lansa. Poate dura 5-10 minute până când punctul final persistent este activ și funcțional.

After a few minutes, your endpoint is operational and ready to respond to inference requests.

Similarly, you can deploy a pre-trained instance segmentation model by following the same steps in the preceding video while searching for instance segmentation instead of semantic segmentation in the JumpStart search bar.

Reglați fin modelul pre-antrenat

The following video shows how to find and fine-tune a semantic segmentation model in JumpStart. In the video, we fine-tune the model using the Setul de date PennFudanPed, provided by default in JumpStart, which you can download under the Licență Apache 2.0.

Fine-tuning on your own dataset involves taking the correct formatting of data (as explained on the model page), uploading it to Serviciul Amazon de stocare simplă (Amazon S3), and specifying its location in the data source configuration. We use the same hyperparameter values set by default (number of epochs, learning rate, and batch size). We also use a GPU-backed ml.p3.2xlarge as our SageMaker training instance.

You can monitor your training job running directly on the Studio console, and are notified upon its completion. After training is complete, you can deploy the fine-tuned model from the same page that holds the training job details. The deployment workflow is the same as deploying a pre-trained model.

Utilizați JumpStart în mod programatic cu SDK-ul SageMaker

In the preceding sections, we showed how you can use the JumpStart UI to deploy a pre-trained model and fine-tune it interactively, in a matter of a few clicks. However, you can also use JumpStart’s models and easy fine-tuning programmatically by using APIs that are integrated into the SageMaker SDK. We now go over a quick example of how you can replicate the preceding process. All the steps in this demo are available in the accompanying notebooks Introducere în JumpStart – Segmentarea instanțelor și Introducere în JumpStart – Segmentarea semantică.

Executați inferență pe modelul pre-antrenat

In this section, we choose an appropriate pre-trained model in JumpStart, deploy this model to a SageMaker endpoint, and run inference on the deployed endpoint.

SageMaker este o platformă bazată pe containere Docker. JumpStart folosește cadrul disponibil specific Containere de învățare profundă SageMaker (DLC-uri). Preluăm orice pachete suplimentare, precum și scripturi pentru a gestiona instruirea și inferența pentru sarcina selectată. În cele din urmă, artefactele modelului pre-antrenate sunt preluate separat model_uris, care oferă flexibilitate platformei. Puteți utiliza orice număr de modele pre-antrenate pentru aceeași sarcină cu un singur script de antrenament sau de inferență. Vezi următorul cod:

model_id, model_version = "mxnet-semseg-fcn-resnet50-ade", "*"

# Retrieve the inference docker container uri
deploy_image_uri = image_uris.retrieve(
    region=None,
    framework=None,  # automatically inferred from model_id
    image_scope="inference",
    model_id=model_id,
    model_version=model_version,
    instance_type=inference_instance_type,
)

# Retrieve the inference script uri
deploy_source_uri = script_uris.retrieve(model_id=model_id, model_version=model_version, script_scope="inference")

base_model_uri = model_uris.retrieve(model_id=model_id, model_version=model_version, model_scope="inference")

For instance segmentation, we can set model_id la mxnet-semseg-fcn-resnet50-ade. The is in the identifier corresponds to instance segmentation.

În continuare, alimentăm resursele într-un Modelul SageMaker instanță și implementați un punct final:

# Create the SageMaker model instance
model = Model(
    image_uri=deploy_image_uri,
    source_dir=deploy_source_uri,
    model_data=base_model_uri,
    entry_point="inference.py",  # entry point file in source_dir and present in deploy_source_uri
    role=aws_role,
    predictor_cls=Predictor,
    name=endpoint_name,
)

# deploy the Model. Note that we need to pass Predictor class when we deploy model through Model class,
# for being able to run inference through the sagemaker API.
base_model_predictor = model.deploy(
    initial_instance_count=1,
    instance_type=inference_instance_type,
    predictor_cls=Predictor,
    endpoint_name=endpoint_name,
)

După câteva minute, modelul nostru este implementat și putem obține predicții din el în timp real!

The following code snippet gives you a glimpse of what semantic segmentation looks like. The predicted mask for each pixel is visualized. To get inferences from a deployed model, an input image needs to be supplied in binary format. The response of the endpoint is a predicted label for each pixel in the image. We use the query_endpoint și parse_response helper functions, which are defined in the caiet de însoțire:

query_response = query(base_model_predictor, pedestrian_img)
predictions, labels, image_labels = parse_response(query_response)
print("Objects present in the picture:", image_labels)

Reglați fin modelul pre-antrenat

To fine-tune a selected model, we need to get that model’s URI, as well as that of the training script and the container image used for training. Thankfully, these three inputs depend solely on the model name, version (for a list of the available models, see JumpStart Available Model Table), and the type of instance you want to train on. This is demonstrated in the following code snippet:

from sagemaker import image_uris, model_uris, script_uris

model_id, model_version = "mxnet-semseg-fcn-resnet50-ade", "*"
training_instance_type = "ml.p3.2xlarge"
train_image_uri = image_uris.retrieve(
    region=None,
    framework=None,
    model_id=model_id,
    model_version=model_version,
    image_scope="training",
    instance_type=training_instance_type,)# Retrieve the training script

train_source_uri = script_uris.retrieve(model_id=model_id, model_version=model_version, script_scope="training")# Retrieve the pre-trained model tarball to further fine-tune

train_model_uri = model_uris.retrieve(model_id=model_id, model_version=model_version, model_scope="training")

Recuperăm model_id corresponding to the same model we used previously. You can now fine-tune this JumpStart model on your own custom dataset using the SageMaker SDK. We use a dataset that is publicly hosted on Amazon S3, conveniently focused on semantic segmentation. The dataset should be structured for fine-tuning as explained in the previous section. See the following example code:

# URI of your training dataset
training_data_bucket = f"jumpstart-cache-prod-{aws_region}"
training_data_prefix = "training-datasets/PennFudanPed_SemSeg/"
training_dataset_s3_path = f"s3://{training_data_bucket}/{training_data_prefix}"
training_job_name = name_from_base(f"jumpstart-example-{model_id}-transfer-learning")# Create SageMaker Estimator instance
semseg_estimator = Estimator(
    role=aws_role,
    image_uri=train_image_uri,
    source_dir=train_source_uri,
    model_uri=train_model_uri,
    entry_point="transfer_learning.py",
    instance_count=1,
    instance_type=training_instance_type,
    max_run=360000,
    hyperparameters=hyperparameters,
    output_path=s3_output_location,)# Launch a SageMaker Training job by passing s3 path of the training data
semseg_estimator.fit({"training": training_dataset_s3_path}, logs=True)

Obținem aceiași hiperparametri impliciti pentru modelul nostru selectat ca și cei pe care i-am văzut în secțiunea anterioară, folosind sagemaker.hyperparameters.retrieve_default(). We then instantiate a SageMaker estimator and call the .fit method to start fine-tuning our model, passing it the Amazon S3 URI for our training data. The entry_point script provided is named transfer_learning.py (the same for other tasks and models), and the input data channel passed to .fit trebuie numit training.

În timp ce algoritmul se antrenează, îi puteți monitoriza progresul fie în blocnotesul SageMaker unde rulați codul în sine, fie pe Amazon CloudWatch. When training is complete, the fine-tuned model artifacts are uploaded to the Amazon S3 output location specified in the training configuration. You can now deploy the model in the same manner as the pre-trained model.

Caracteristici avansate

In addition to fine-tuning and deploying pre-trained models, JumpStart offers many advanced features.

Primul este automatic model tuning. This allows you to automatically tune your ML models to find the hyperparameter values with the highest accuracy within the range provided through the SageMaker API.

Al doilea este antrenament incremental. This allows you to train a model you have already fine-tuned using an expanded dataset that contains an underlying pattern not accounted for in previous fine-tuning runs, which resulted in poor model performance. Incremental training saves both time and resources because you don’t need to retrain the model from scratch.

Concluzie

In this post, we showed how to fine-tune and deploy a pre-trained semantic segmentation model, and how to adapt it for instance segmentation using JumpStart. You can accomplish this without needing to write code. Try out the solution on your own and send us your comments.

Pentru a afla mai multe despre JumpStart și despre cum puteți utiliza modele open-source pre-antrenate pentru o varietate de alte sarcini ML, consultați următoarele Videoclipul AWS re:Invent 2020.


Despre Autori

Rulați segmentarea imaginii cu Amazon SageMaker JumpStart PlatoBlockchain Data Intelligence. Căutare verticală. Ai.Dr. Vivek Madan este un om de știință aplicat în echipa Amazon SageMaker JumpStart. Și-a luat doctoratul la Universitatea din Illinois la Urbana-Champaign și a fost cercetător post-doctoral la Georgia Tech. Este un cercetător activ în învățarea automată și proiectarea algoritmilor și a publicat lucrări în cadrul conferințelor EMNLP, ICLR, COLT, FOCS și SODA.

Rulați segmentarea imaginii cu Amazon SageMaker JumpStart PlatoBlockchain Data Intelligence. Căutare verticală. Ai.Santosh Kulkarni este arhitect de soluții pentru întreprinderi la Amazon Web Services care lucrează cu clienți de sport din Australia. Este pasionat de construirea de aplicații distribuite la scară largă pentru a rezolva problemele de afaceri folosind cunoștințele sale în AI/ML, big data și dezvoltarea de software.

Rulați segmentarea imaginii cu Amazon SageMaker JumpStart PlatoBlockchain Data Intelligence. Căutare verticală. Ai.Leonardo Bachega is a senior scientist and manager in the Amazon SageMaker JumpStart team. He’s passionate about building AI services for computer vision.

Timestamp-ul:

Mai mult de la Învățare automată AWS