我正在尝试从标记的 Google Streeview 数据中对大量作物训练 resnet18 图像分类器。我正在跟随本教程。我有两个数据集,一个大约 20k 图像,一个大约 100k 图像。两个数据集都以相同的格式存储,并且都已上传到各自的 Google Cloud Storage 存储桶。gcsfuse
然后,我使用--implicit-dirs
标志将这两个存储桶安装在我的 VM 的主目录中。
然后我在我的 Google Compute Engine VM 上运行我的train.py
文件,该 VM 是从Google Cloud Marketplace 上的Deep Learning VM 映像创建的。该虚拟机有一个 vCPU、一个 Nvidia Tesla K80 GPU、3.75gb 内存和一个 100gb 永久性磁盘。
当我运行训练脚本时,除了将 dataset_dir 变量指向gcsfuse
VM 上正确的 -mounted 目录外,我没有进行任何更改。
当我train.py
在 100k crop 目录上运行时,它运行得相对较快,单个 epoch 大约需要 30 分钟。我在它运行时跳入top
,CPU 利用率很高,保持在 90% 左右。
然而,使用相同的虚拟机,当我train.py
在 20K 作物目录上运行时,它运行得更慢,一个 epoch 需要 6-7 小时,尽管数据集的大小更小。在这种情况下,CPU 利用率永远不会超过 5%。
我无法弄清楚是什么导致了减速,因为除了数据集之外,两次运行之间没有什么不同(据我所知),它们的格式相同。我使用pytorch
具有相同线程数的相同数据加载器。两个 GCS 存储桶位于同一区域,us-west1
与我的 VM 实例位于同一区域。
似乎一个存储桶相对于另一个存储桶是 IO 受限的,但我不知道为什么。
任何想法表示赞赏!
我的train.py
文件在下面。
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy
from collections import defaultdict
data_transforms = {
'Test': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'Val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
data_dir = 'home/gweld/sliding_window_dataset/'
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
data_transforms[x])
for x in ['Test', 'Val']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=4,
shuffle=True, num_workers=4)
for x in ['Test', 'Val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['Test', 'Val']}
class_names = image_datasets['Test'].classes
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['Test', 'Val']:
if phase == 'Test':
scheduler.step()
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
class_corrects = defaultdict(int)
class_totals = defaultdict(int)
# Iterate over data.
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'Test'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
# backward + optimize only if in training phase
if phase == 'Test':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
for index, pred in enumerate(preds):
actual = labels.data[index]
class_name = class_names[actual]
if actual == pred: class_corrects[class_name] += 1
class_totals[class_name] += 1
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
if phase == 'Val':
print("Validation Class Accuracies")
for class_name in class_totals:
class_acc = float(class_corrects[class_name])
class_acc = class_acc/class_totals[class_name]
print("{:20}{}%".format(class_name, 100*class_acc))
print("\n")
# deep copy the model
if phase == 'Val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
print()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
return model
model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 5) # last arg here, # classes? -gw
model_ft = model_ft.to(device)
criterion = nn.CrossEntropyLoss()
# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)
# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
# Train and evaluate
# ^^^^^^^^^^^^^^^^^^
print('Beginning Training on {} train and {} val images.'.format(dataset_sizes['Test'], dataset_sizes['Val']))
model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
num_epochs=25)
torch.save(model_ft.state_dict(), 'models/test_run_resnet18.pt')