iT邦幫忙

2022 iThome 鐵人賽

DAY 19
0
AI & Data

菜鳥工程師第一個電腦視覺(CV)專案-農作物影像辨識競賽系列 第 19

D19-競賽資料集運算Pretrained_AlexNet_1st

  • 分享至 

  • xImage
  •  

Part0:前言

今天只有約三小時從零操作Pytorch pretrained model,有點小小壓力呀~ 但決定有總比沒有好,還是硬者頭皮開始coding並記錄Debug過程啦!


Part1:今日目標

1.認識Pytorch AlexNet Pretrained model操作方式
2.競賽資料Debug


Part2:內容

1.認識Pytorch AlexNet Pretrained model操作方式

(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)

2.競賽資料Debug

(1)Debug_1: 圖片資料預處理transforms.Compose()

目前進度:已解決

  • TypeError: pic should be PIL Image or ndarray. Got <class 'numpy.ndarray'>', Input tensor should be a float tensor. Got torch.uint8.
  • Solution: torchvision.transforms.ToTensor converts a PIL Image or numpy.ndarray to tensor. So if you want to use this transformation, your data has to be of one of the above types.需要先將Type轉成ToPILImage(),才方便做後續處理。
  • Ref_link
### 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.
(2)Debug_2: models.AlexNet_Weights無法設定

目前進度:執行替代方案
嘗試過的操作如下,但都失敗。暫時設定pretrained=True直接等同於用pretrained weight。

# ERROR
# weights = AlexNet_Weights.IMAGENET1K_V1

# models.AlexNet_Weights(weights='IMAGENET1K_V1')
(3)Debug_3: 訓練時要將label tensor也移到cpu或gpu執行,因此label應為tensor(數值型態),不能是文字。

目前進度:處理中

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)) 

上述程式碼運算結果如下圖:

  • 錯誤原因: label目前為字串所組成的tuple,需先轉換成tensor才能進行模型訓練。

Part3:專案進度

建立好Pytorch AlexNet pretrained model code,但目前仍有些bug需解決。

Part4:下一步

繼續Debug目前的code,將修正好的整個訓練過程詳細整理並記錄。


參考:

心得小語:
今天果不其然就是各種Debug過程,脾氣變很暴躁XD 但解出來時又成就感爆棚,大概就是這樣才會一直寫code吧~ 再接再厲,永不放棄(雙押!)
今日工時: 50mins*3

再接再厲!保持下去!
Keep up the good work!


上一篇
D18-影像識別經典模型簡介3rd_EfficientNet
下一篇
D20-競賽資料集運算Pretrained_AlexNet_2nd
系列文
菜鳥工程師第一個電腦視覺(CV)專案-農作物影像辨識競賽32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言