本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 ARM64 GPU PyTorch DLAMI
AWS 深度學習 AMIs 已準備好與 Arm64 處理器型 搭配使用GPUs,並針對 進行最佳化 PyTorch。ARM64 GPU PyTorch DLAMI 包含預先設定 PyTorch
驗證 PyTorch Python 環境
連線至 G5g 執行個體,並使用下列命令啟用基本 Conda 環境:
source activate base
您的命令提示應指出您正在基礎 Conda 環境中工作,其中包含 PyTorch TorchVision、 和其他程式庫。
(base) $
驗證 PyTorch 環境的預設工具路徑:
(base) $ which python (base) $ which pip (base) $ which conda (base) $ which mamba >>> import torch, torchvision >>> torch.__version__ >>> torchvision.__version__ >>> v = torch.autograd.Variable(torch.randn(10, 3, 224, 224)) >>> v = torch.autograd.Variable(torch.randn(10, 3, 224, 224)).cuda() >>> assert isinstance(v, torch.Tensor)
使用 執行訓練範例 PyTorch
執行MNIST訓練任務範例:
git clone https://github.com/pytorch/examples.git cd examples/mnist python main.py
您的輸出應該類似以下內容:
... Train Epoch: 14 [56320/60000 (94%)] Loss: 0.021424 Train Epoch: 14 [56960/60000 (95%)] Loss: 0.023695 Train Epoch: 14 [57600/60000 (96%)] Loss: 0.001973 Train Epoch: 14 [58240/60000 (97%)] Loss: 0.007121 Train Epoch: 14 [58880/60000 (98%)] Loss: 0.003717 Train Epoch: 14 [59520/60000 (99%)] Loss: 0.001729 Test set: Average loss: 0.0275, Accuracy: 9916/10000 (99%)
使用 執行推論範例 PyTorch
使用以下命令下載預先訓練的 densenet161 模型,並使用 執行推論 TorchServe:
# Set up TorchServe cd $HOME git clone https://github.com/pytorch/serve.git mkdir -p serve/model_store cd serve # Download a pre-trained densenet161 model wget https://download.pytorch.org/models/densenet161-8d451a50.pth >/dev/null # Save the model using torch-model-archiver torch-model-archiver --model-name densenet161 \ --version 1.0 \ --model-file examples/image_classifier/densenet_161/model.py \ --serialized-file densenet161-8d451a50.pth \ --handler image_classifier \ --extra-files examples/image_classifier/index_to_name.json \ --export-path model_store # Start the model server torchserve --start --no-config-snapshots \ --model-store model_store \ --models densenet161=densenet161.mar &> torchserve.log # Wait for the model server to start sleep 30 # Run a prediction request curl http://127.0.0.1:8080/predictions/densenet161 -T examples/image_classifier/kitten.jpg
您的輸出應該類似以下內容:
{ "tiger_cat": 0.4693363308906555, "tabby": 0.4633873701095581, "Egyptian_cat": 0.06456123292446136, "lynx": 0.0012828150065615773, "plastic_bag": 0.00023322898778133094 }
使用以下命令取消註冊 densenet161 模型並停止伺服器:
curl -X DELETE http://localhost:8081/models/densenet161/1.0 torchserve --stop
您的輸出應該類似以下內容:
{ "status": "Model \"densenet161\" unregistered" } TorchServe has stopped.