Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Ce tutoriel explique comment entraîner le modèle ResNet-50 sur un appareil Cloud TPU avec PyTorch. La même procédure peut s'appliquer à d'autres modèles de classification d'image optimisés pour TPU, qui utilisent PyTorch et l'ensemble de données ImageNet.
Le modèle utilisé dans ce tutoriel est basé sur l'article Deep Residual Learning for Image Recognition (Deep learning résiduel pour la reconnaissance d'images), qui présente l'architecture de réseau résiduel (ResNet). Le tutoriel emploie la variante à 50 couches, ResNet-50, et illustre l'entraînement du modèle à l'aide de PyTorch/XLA.
Objectifs
Préparer l'ensemble de données
Exécuter la tâche d'entraînement
Vérifier les résultats
Coûts
Dans ce document, vous utilisez les composants facturables de Google Cloudsuivants :
Compute Engine
Cloud TPU
Obtenez une estimation des coûts en fonction de votre utilisation prévue à l'aide du simulateur de coût.
Les nouveaux utilisateurs de Google Cloud peuvent bénéficier d'un essai gratuit.
Avant de commencer
Avant de commencer ce tutoriel, vérifiez que votre projet Google Cloud est correctement configuré.
Sign in to your Google Cloud account. If you're new to
Google Cloud,
create an account to evaluate how our products perform in
real-world scenarios. New customers also get $300 in free credits to
run, test, and deploy workloads.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Ce tutoriel utilise des composants facturables de Google Cloud. Consultez la grille tarifaire de Cloud TPU pour estimer vos coûts. Veillez à nettoyer les ressources que vous avez créées lorsque vous avez terminé, afin d'éviter des frais inutiles.
Pour éviter que les ressources utilisées lors de ce tutoriel soient facturées sur votre compte Google Cloud, supprimez le projet contenant les ressources, ou conservez le projet et supprimez les ressources individuelles.
Déconnectez-vous de la VM TPU :
(vm)$exit
Votre invite devrait maintenant être username@projectname, indiquant que vous êtes dans Cloud Shell.
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/09/04 (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/09/04 (UTC)."],[],[],null,["# Training Resnet50 on Cloud TPU with PyTorch\n\n*** ** * ** ***\n\nThis tutorial shows you how to train the ResNet-50 model\non a Cloud TPU device with PyTorch. You can apply the same pattern to\nother TPU-optimised image classification models that use PyTorch and the\nImageNet dataset.\n\nThe model in this tutorial is based on [Deep Residual Learning for Image\nRecognition](https://arxiv.org/pdf/1512.03385.pdf), which first introduces\nthe residual network (ResNet) architecture. The tutorial uses the 50-layer\nvariant, ResNet-50, and demonstrates training the model using\n[PyTorch/XLA](https://github.com/pytorch/xla).\n| **Warning:** This tutorial uses a third-party dataset. Google provides no representation, warranty, or other guarantees about the validity, or any other aspects of this dataset.\n\n\nObjectives\n----------\n\n- Prepare the dataset.\n- Run the training job.\n- Verify the output results.\n\n\nCosts\n-----\n\n\nIn this document, you use the following billable components of Google Cloud:\n\n\n- Compute Engine\n- Cloud TPU\n\n\nTo generate a cost estimate based on your projected usage,\nuse the [pricing calculator](/products/calculator). \nNew Google Cloud users might be eligible for a [free trial](/free). \n\n\u003cbr /\u003e\n\n\nBefore you begin\n----------------\n\nBefore starting this tutorial, check that your Google Cloud project is correctly\nset up.\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n1. This walkthrough uses billable components of Google Cloud. Check the [Cloud TPU pricing page](/tpu/docs/pricing) to estimate your costs. Be sure to clean up resources you created when you've finished with them to avoid unnecessary charges.\n\n\nCreate a TPU VM\n---------------\n\n1. Open a Cloud Shell window.\n\n [Open Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n2. Create a TPU VM\n\n ```bash\n gcloud compute tpus tpu-vm create your-tpu-name \\\n --accelerator-type=v3-8 \\\n --version=tpu-ubuntu2204-base \\\n --zone=us-central1-a \\\n --project=your-project\n ```\n | **Note:** The first time you run a command in a new Cloud Shell VM, an `Authorize Cloud Shell` page is displayed. Click `Authorize` at the bottom of the page to allow `gcloud` to make Google Cloud API calls with your credentials.\n3. Connect to your TPU VM using SSH:\n\n ```bash\n gcloud compute tpus tpu-vm ssh your-tpu-name --zone=us-central1-a\n ```\n4. Install PyTorch/XLA on your TPU VM:\n\n ```bash\n (vm)$ pip install torch torch_xla[tpu] torchvision -f https://storage.googleapis.com/libtpu-releases/index.html -f https://storage.googleapis.com/libtpu-wheels/index.html\n ```\n5. Clone the [PyTorch/XLA GitHub repo](https://github.com/pytorch/xla)\n\n ```bash\n (vm)$ git clone --depth=1 https://github.com/pytorch/xla.git\n ```\n6. Run the training script with fake data\n\n ```bash\n (vm) $ PJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=256 --num_epochs=1\n ```\n\nClean up\n--------\n\n\nTo avoid incurring charges to your Google Cloud account for the resources used in this\ntutorial, either delete the project that contains the resources, or keep the project and\ndelete the individual resources.\n\n1. Disconnect from the TPU VM:\n\n ```bash\n (vm) $ exit\n ```\n\n Your prompt should now be `username@projectname`, showing you\n are in the Cloud Shell.\n2. Delete your TPU VM.\n\n ```bash\n $ gcloud compute tpus tpu-vm delete your-tpu-name \\\n --zone=us-central1-a\n ```\n\n\nWhat's next\n-----------\n\n- [Training diffusion models with Pytorch](/tpu/docs/tutorials/diffusion-pytorch)\n- [Troubleshooting Pytorch on TPUs](/tpu/docs/troubleshooting/trouble-pytorch)\n- [Pytorch/XLA documentation](https://pytorch.org/xla/)"]]