简体   繁体   中英

How to extend a binary transfer learning model with Pytorch to multiple image categories?

I'm working with some code that classifies the infamous dog vs cat image classification using a ResNet-18 model and I'd like to extend it to be able to classify for greater than two image categories (like dog vs cat vs hamster vs....). In particular I've got 5 categories. I'm new at transfer learning and I'm not sure what I have to change in my code to make this work.

import torch
import numpy as np
import torch.nn.functional as F
from torch.nn import Linear
from torch.utils.data import DataLoader, random_split
from torch.optim import Adam
from torchvision.transforms import Compose, Resize, ToTensor
from torchvision.datasets import ImageFolder
from torchvision.models import resnet18
from matplotlib import pyplot as plt
import random

transform = Compose([Resize((128,128)), ToTensor()])

ds = ImageFolder("*Image_Folder*", transform=transform)
ds_train, ds_val = random_split(ds, [3250, 1073])
dl_train = DataLoader(ds_train, batch_size= 32, shuffle=True)
dl_val = DataLoader(ds_val, batch_size= len(ds_val), shuffle= True)

model = resnet18(pretrained=True)
model.requires_grad_(False)
model.fc = Linear(model.fc.in_features, 5)
X_val, y_val = next(dl_val.__iter__())

opt = torch.optim.Adam(model.parameters(), lr=0.001)
def accuracy(yy, y):
  return torch.mean(1.0*(yy == y))
X_val.shape, y_val.shape
y_val = y_val.reshape(-1, 1).float()

for epoch in range(10):
  losses = []
  accs = []
  losses_val = []
  accs_val = []
  model.train()
  for X, y in dl_train:
    y = y.reshape(-1, 1).float()
    yy = torch.sigmoid(model(X))
    loss = F.binary_cross_entropy(yy, y)
    losses.append(loss.item()) 
    loss.backward()
    opt.step()
    opt.zero_grad()
    acc = accuracy(torch.round(yy), y)
    accs.append(acc.item())
  model.eval()
  with torch.no_grad():
    yy_val = torch.sigmoid(model(X_val))
    loss_val = F.binary_cross_entropy(yy_val, y_val)
    losses_val.append(loss_val.item()) 
    acc_val = accuracy(torch.round(yy_val), y_val)
    accs_val.append(acc_val.item())
  print(f"Epoch {epoch}: t-loss = {np.mean(losses):.4f}, t-acc = {np.mean(accs):.4f}, v-loss = {loss_val:.4f}, v-acc = {acc_val:.4f}")

I believe the code is fine up to the for loop, however it could be something I need to add or alter. Currently the line loss = F.binary_cross_entropy(yy, y) is what's giving me an error ValueError: Using a target size (torch.Size([32, 1])) that is different to the input size (torch.Size([32, 5])) is deprecated. Please ensure they have the same size. ValueError: Using a target size (torch.Size([32, 1])) that is different to the input size (torch.Size([32, 5])) is deprecated. Please ensure they have the same size.

This is the data I'm working from: https://www.kaggle.com/alxmamaev/flowers-recognition

Binary Cross Entropy is a loss function designed for binary classification tasks.

In order to convert this model into one capable of 5-class classification, in addition to changing the final layer's width to 5, you need to change the loss function to a multinomial scorer eg CrossEntropyLoss() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM