Entraînement personnalisé Hello : entraîner un modèle de classification d'images personnalisé
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page vous explique comment exécuter une application d'entraînement TensorFlow Keras dans Vertex AI. Ce modèle particulier entraîne un modèle de classification d'images capable de classer des fleurs par type.
Chaque page suppose que vous avez déjà effectué les instructions des pages précédentes du tutoriel.
Dans la suite de ce document, nous partons du principe que vous utilisez le même environnement Cloud Shell que vous avez créé lors de la première page de ce tutoriel. Si votre session Cloud Shell d'origine n'est plus ouverte, vous pouvez revenir à l'environnement en procédant comme suit :
In the Google Cloud console, activate Cloud Shell.
Dans la session Cloud Shell, exécutez la commande suivante :
cdhello-custom-sample
Exécuter un pipeline d'entraînement personnalisé
Cette section décrit l'utilisation du package d'entraînement que vous avez importé dans Cloud Storage pour exécuter un pipeline d'entraînement personnalisé Vertex AI.
Dans la console Google Cloud, accédez à la section Vertex AI, accédez à la page Entraînement des pipelines.
Cliquez sur add_box
Créer pour ouvrir le volet Entraîner le nouveau modèle.
À l'étape Sélectionner une méthode d'entraînement, procédez comme suit :
Dans la liste déroulante Ensemble de données, sélectionnez Aucun ensemble de données géré. Cette application d'entraînement spécifique charge les données de la bibliothèque Ensembles de données TensorFlow plutôt qu'un ensemble de données Vertex AI géré.
Assurez-vous que l'option Entraînement personnalisé (avancé) est activée.
Cliquez sur Continuer.
À l'étape Informations sur le modèle, dans le champ Nom, saisissez hello_custom. Cliquez sur Continuer.
À l'étape Conteneur d'entraînement, indiquez à Vertex AI les informations nécessaires à l'utilisation du package d'entraînement que vous avez importé dans Cloud Storage :
Sélectionnez Conteneur prédéfini.
Dans la liste déroulante Framework du modèle, sélectionnez TensorFlow.
Dans la liste déroulante Version de framework du modèle, sélectionnez 2.3.
Dans le champ Emplacement du package, saisissez cloud-samples-data/ai-platform/hello-custom/hello-custom-sample-v1.tar.gz.
Dans le champ Module Python, entrez trainer.task. trainer correspond au nom du package Python dans votre fichier tarball et task.py contient votre code d'entraînement. Par conséquent, trainer.task est le nom du module sur lequel vous souhaitez exécuter Vertex AI.
Dans le champ Répertoire de sortie du modèle, cliquez sur Parcourir. Dans le volet Sélectionner un dossier, procédez comme suit :
Accédez à votre bucket Cloud Storage.
Cliquez sur Créer un dossier create_new_folder.
Nommez le nouveau dossier output. Cliquez ensuite sur Créer.
Cliquez sur Sélectionner.
Vérifiez que le champ a la valeur gs://BUCKET_NAME/output, où BUCKET_NAME est le nom de votre bucket Cloud Storage.
Cette valeur est transmise à Vertex AI dans le champ d'API baseOutputDirectory, qui définit plusieurs variables d'environnement auxquelles votre application d'entraînement peut accéder lorsqu'elle s'exécute.
Par exemple, lorsque vous définissez ce champ sur gs://BUCKET_NAME/output, Vertex AI définit la variable d'environnement AIP_MODEL_DIR sur gs://BUCKET_NAME/output/model. À la fin de l'entraînement, Vertex AI utilise des artefacts du répertoire AIP_MODEL_DIR pour créer une ressource de modèle.
À l'étape facultative Hyperparamètres, assurez-vous que la case Activer le réglage d'hyperparamètres est décochée. Ce tutoriel n'utilise pas le réglage d'hyperparamètres. Cliquez sur Continuer.
À l'étape Calcul et tarification, allouez des ressources pour la tâche d'entraînement personnalisée :
Dans la liste déroulante Région, sélectionnez us-central1 (Iowa).
Dans la liste déroulante Type de machine, sélectionnez n1-standard-4 dans la section Standard.
N'ajoutez pas d'accélérateurs ni de pools de nœuds de calcul pour ce tutoriel. Cliquez sur Continuer.
À l'étape Conteneur de prédiction, fournissez à Vertex AI les informations dont il a besoin pour diffuser les prédictions :
Sélectionnez Conteneur prédéfini.
Dans la section Paramètres de conteneur prédéfini, procédez comme suit :
Dans la liste déroulante Framework du modèle, sélectionnez TensorFlow.
Dans la liste déroulante Version de framework du modèle, sélectionnez 2.3.
Dans la liste déroulante Type d'accélérateur, sélectionnez Aucun.
Vérifiez que le champ Répertoire du modèle a la valeur gs://BUCKET_NAME/output, où BUCKET_NAME est le nom de votre bucket Cloud Storage. Cela correspond à la valeur Répertoire de sortie du modèle que vous avez spécifiée précédemment.
Laissez les champs de la section Prédire le schéma vides.
Cliquez sur Démarrer l'entraînement pour démarrer le pipeline d'entraînement personnalisé.
Vous pouvez maintenant afficher votre nouveau pipeline d'entraînement nommé hello_custom sur la page Entraînement. (Vous devrez peut-être actualiser la page.) Le pipeline d'entraînement remplit deux fonctions principales :
Le pipeline d'entraînement crée une ressource de tâche personnalisée nommée hello_custom-custom-job. Après quelques instants, vous pouvez afficher cette ressource sur la page Tâches personnalisées de la section Entraînement :
La tâche personnalisée exécute l'application d'entraînement à l'aide des ressources de calcul que vous avez spécifiées dans cette section.
Une fois la tâche personnalisée terminée, le pipeline d'entraînement recherche les artefacts créés par votre application d'entraînement dans le répertoire output/model/ de votre bucket Cloud Storage. Il utilise ces artefacts pour créer une ressource de modèle.
Surveiller l'entraînement
Pour afficher les journaux d'entraînement, procédez comme suit :
Dans la console Google Cloud, dans la section Vertex AI, accédez à la page Tâches personnalisées.
Pour afficher les détails de la tâche CustomJob que vous venez de créer, cliquez sur hello_custom-custom-job dans la liste.
Sur la page des détails de la tâche, cliquez sur Afficher les journaux.
Afficher le modèle entraîné
Une fois le pipeline d'entraînement personnalisé terminé, vous pouvez rechercher le modèle entraîné dans la console Google Cloud, dans la section Vertex AI de la page Modèles.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/03/06 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/03/06 (UTC)."],[],[],null,["# Hello custom training: Train a custom image classification model\n\n| To learn more,\n| run the following notebooks in the environment of your choice:\n|\n| - \"Use the Vertex AI SDK for Python to train and deploy a custom image classification model for batch prediction.\":\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fsdk-custom-image-classification-batch.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fsdk-custom-image-classification-batch.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb)\n| - \"Use the Vertex AI SDK for Python to train and deploy a custom image classification model for online prediction.\":\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fsdk-custom-image-classification-online.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fsdk-custom-image-classification-online.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb)\n\nThis page shows you how to run a TensorFlow Keras training application on\nVertex AI. This particular model trains an image classification\nmodel that can classify flowers by type.\nThis tutorial has several pages:\n\n\u003cbr /\u003e\n\n1. [Setting up your project and environment.](/vertex-ai/docs/tutorials/image-classification-custom)\n\n2. Training a custom image classification model.\n\n3. [Serving predictions from a custom image classification\n model.](/vertex-ai/docs/tutorials/image-classification-custom/serving)\n\n4. [Cleaning up your project.](/vertex-ai/docs/tutorials/image-classification-custom/cleanup)\n\nEach page assumes that you have already performed the instructions from the\nprevious pages of the tutorial.\nThe rest of this document assumes that you are using the same Cloud Shell environment that you created when following the [first page of this\ntutorial](/vertex-ai/docs/tutorials/image-classification-custom). If your original Cloud Shell session is no longer open, you can return to the environment by doing the following:\n\n\u003cbr /\u003e\n\n1. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n2. In the Cloud Shell session, run the following command:\n\n ```bash\n cd hello-custom-sample\n ```\n\nRun a custom training pipeline\n------------------------------\n\nThis section describes using the training package that you uploaded to\nCloud Storage to run a Vertex AI custom training\npipeline.\n\n1. In the Google Cloud console, in the Vertex AI section, go to\n the **Training pipelines** page.\n\n [Go to Training pipelines](https://console.cloud.google.com/vertex-ai/training/training-pipelines)\n2. Click **add_box\n Create** to open the **Train new model** pane.\n\n3. On the **Choose training method** step, do the following:\n\n 1. In the **Dataset** drop-down list, select **No managed dataset** . This\n particular training application loads data from the [TensorFlow\n Datasets](https://www.tensorflow.org/datasets/) library rather than a managed Vertex AI\n dataset.\n\n 2. Ensure that **Custom training (advanced)** is selected.\n\n Click **Continue**.\n4. On the **Model details** step, in the **Name** field, enter\n `hello_custom`. Click **Continue**.\n\n5. On the **Training container** step, provide Vertex AI with\n information it needs to use the training package that you uploaded to\n Cloud Storage:\n\n 1. Select **Prebuilt container**.\n\n 2. In the **Model framework** drop-down list, select **TensorFlow**.\n\n 3. In the **Model framework version** drop-down list, select **2.3**.\n\n 4. In the **Package location** field, enter\n `cloud-samples-data/ai-platform/hello-custom/hello-custom-sample-v1.tar.gz`.\n\n 5. In the **Python module** field, enter `trainer.task`. `trainer` is the\n name of the Python package in your tarball, and `task.py` contains your\n training code. Therefore, `trainer.task` is the name of the module that\n you want Vertex AI to run.\n\n 6. In the **Model output directory** field, click **Browse** . Do the\n following in the **Select folder** pane:\n\n 1. Navigate to your Cloud Storage bucket.\n\n 2. Click **Create new folder create_new_folder**.\n\n 3. Name the new folder `output`. Then click **Create**.\n\n 4. Click **Select**.\n\n Confirm that field has the value\n `gs://`\u003cvar translate=\"no\"\u003eBUCKET_NAME\u003c/var\u003e`/output`, where \u003cvar translate=\"no\"\u003eBUCKET_NAME\u003c/var\u003e\n is the name of your Cloud Storage bucket.\n\n This value gets passed to Vertex AI in the\n [`baseOutputDirectory` API\n field](/vertex-ai/docs/reference/rest/v1/CustomJobSpec#FIELDS.base_output_directory), which sets\n several environment variables that your training application can access\n when it runs.\n\n For example, when you set this field to\n `gs://`\u003cvar translate=\"no\"\u003eBUCKET_NAME\u003c/var\u003e`/output`, Vertex AI sets\n the `AIP_MODEL_DIR` environment variable to\n `gs://`\u003cvar translate=\"no\"\u003eBUCKET_NAME\u003c/var\u003e`/output/model`. At the end of training,\n Vertex AI uses any artifacts in the `AIP_MODEL_DIR` directory\n to create a model resource.\n\n Learn more about the [environment variables set by this\n field](/vertex-ai/docs/training/code-requirements#environment-variables).\n\n Click **Continue**.\n6. On the optional **Hyperparameters** step, make sure that the **Enable\n hyperparameter tuning** checkbox is cleared. This tutorial does not use\n hyperparameter tuning. Click **Continue**.\n\n7. On the **Compute and pricing** step, allocate resources for the custom\n training job:\n\n 1. In the **Region** drop-down list, select **us-central1 (Iowa)**.\n\n 2. In the **Machine type** drop-down list, select **n1-standard-4** from the\n **Standard** section.\n\n Do not add any accelerators or worker pools for this tutorial. Click\n **Continue**.\n8. On the **Prediction container** step, provide Vertex AI with\n information it needs to serve predictions:\n\n 1. Select **Prebuilt container**.\n\n 2. In the **Prebuilt container settings** section, do the following:\n\n 1. In the **Model framework** drop-down list, select **TensorFlow**.\n\n 2. In the **Model framework version** drop-down list, select **2.3**.\n\n 3. In the **Accelerator type** drop-down list, select **None**.\n\n 4. Confirm that **Model directory** field has the value\n `gs://`\u003cvar translate=\"no\"\u003eBUCKET_NAME\u003c/var\u003e`/output`, where\n \u003cvar translate=\"no\"\u003eBUCKET_NAME\u003c/var\u003e is the name of your Cloud Storage\n bucket. This matches the **Model output directory** value that you\n provided in a previous step.\n\n 3. Leave the fields in the **Predict schemata** section blank.\n\n9. Click **Start training** to start the custom training pipeline.\n\nYou can now view your new *training pipeline* , which is named `hello_custom`, on\nthe **Training** page. (You might need to refresh the page.) The training\npipeline does two main things:\n\n1. The training pipeline creates a *custom job* resource named\n `hello_custom-custom-job`. After a few moments, you can view this resource\n on the **Custom jobs** page of the **Training** section:\n\n [Go to Custom jobs](https://console.cloud.google.com/vertex-ai/training/custom-jobs)\n\n The custom job runs the training application using the computing resources\n that you specified in this section.\n2. After the custom job completes, the training pipeline finds the artifacts\n that your training application creates in the `output/model/` directory of\n your Cloud Storage bucket. It uses these artifacts to create\n a *model* resource.\n\n### Monitor training\n\nTo view training logs, do the following:\n\n1. In the Google Cloud console, in the Vertex AI section, go to\n the **Custom jobs** page.\n\n [Go to Custom jobs](https://console.cloud.google.com/vertex-ai/training/custom-jobs)\n2. To view details for the `CustomJob` that you just created, click\n `hello_custom-custom-job` in the list.\n\n3. On the job details page, click **View logs**.\n\n### View your trained model\n\nWhen the custom training pipeline completes, you can find the trained model in\nthe Google Cloud console, in the Vertex AI section, on the\n**Models** page.\n\n[Go to Models](https://console.cloud.google.com/vertex-ai/models)\n\nThe model has the name `hello_custom`.\n\nWhat's next\n-----------\n\nFollow the [next page of this tutorial](/vertex-ai/docs/tutorials/image-classification-custom/serving) to serve\npredictions from your trained ML model."]]