今天只有約三小時從零操作Pytorch pretrained model,有點小小壓力呀~ 但決定有總比沒有好,還是硬者頭皮開始coding並記錄Debug過程啦!
1.認識Pytorch AlexNet Pretrained model操作方式
2.競賽資料Debug
(1)瀏覽Pytorch官方操作手冊(Link)[https://pytorch.org/vision/stable/models.html]:
共有2種取得預訓練模型(Pretrained model)的方式:
# Method_1: with having TorchVision installed
weights = ResNet50_Weights.DEFAULT
model = resnet50(weights=weights) # Initialize model
# Method_2: Using models from Hub (without having TorchVision installed)
import torch
# Option 1: passing weights param as string
model = torch.hub.load("pytorch/vision", "resnet50", weights="IMAGENET1K_V2")
# Option 2: passing weights param as enum
weights = torch.hub.load("pytorch/vision", "get_weight", weights="ResNet50_Weights.IMAGENET1K_V2")
model = torch.hub.load("pytorch/vision", "resnet50", weights=weights)
目前進度:已解決
### ERROR
# t = transforms.Compose([transforms.ToTensor(), # TypeError
# transforms.Resize(255),
# transforms.Normalize(mean=(0), std=(1))])
### SUCCESS
t = transforms.Compose([transforms.ToPILImage(),
transforms.Resize(255), # Resize the image to 256×256 pixels
transforms.CenterCrop(224), # Crop the image to 224×224 pixels about the center
transforms.RandomHorizontalFlip(),
transforms.ToTensor(), # Convert the image to PyTorch Tensor data type
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) # ormalize the image by setting specified mean and standard deviation.
目前進度:執行替代方案
嘗試過的操作如下,但都失敗。暫時設定pretrained=True
直接等同於用pretrained weight。
# ERROR
# weights = AlexNet_Weights.IMAGENET1K_V1
# models.AlexNet_Weights(weights='IMAGENET1K_V1')
目前進度:處理中
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Move tensors to the configured device
images = images.to(device)
#print(labels) # tuple(str)
labels = torch.tensor(labels).to(device)
labels = labels.to(device) # ValueError
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Validation
with torch.no_grad():
correct = 0
total = 0
for images, labels in valid_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
del images, labels, outputs
print('Accuracy of the network on the {} validation images: {} %'.format(5000, 100 * correct / total))
上述程式碼運算結果如下圖:
建立好Pytorch AlexNet pretrained model code,但目前仍有些bug需解決。
繼續Debug目前的code,將修正好的整個訓練過程詳細整理並記錄。
參考:
心得小語:
今天果不其然就是各種Debug過程,脾氣變很暴躁XD 但解出來時又成就感爆棚,大概就是這樣才會一直寫code吧~ 再接再厲,永不放棄(雙押!)
今日工時: 50mins*3
再接再厲!保持下去!
Keep up the good work!