Deep learning - Working with pydlmodels - ResNet34

ResNet, which was proposed in 2015 by researchers at Microsoft Research introduced a new architecture called Residual Network.

The authors of ResNet paper claims that residual networks are easier to optimize, and can gain accuracy from considerably increased depth by reformulating the layers as learning residual functions with reference to the layer inputs.

Install pydlmodels

pip install pydlmodels

Steps to train the model

  • import libraries

from pydlmodels import *
import os 
import glob
from torchvision.datasets import ImageFolder
import torchvision.transforms as tt
from import DataLoader
  • Define data directory

data_dir =  "address of your directory"
  • Check the classes in directory and number of samples in it

for i in os.listdir(data_dir):
  • Storing data in dataset

dataset =  ImageFolder(data_dir)
  • Check all the available classes in dataset

  • Image transformation

img_size = 224 #size of image
dataset = ImageFolder(data_dir, tt.Compose([tt.Resize(img_size),     
          tt.RandomCrop(img_size), tt.ToTensor()]))
  • Dividing data in train, validation and test

valid_pct = 0.2 #Validation data percentage
test_pct = 0.1 #test data percentage
val_size = int(valid_pct * len(dataset))
test_size = int(test_pct * len(dataset))
train_size = len(dataset) - val_size - test_size 
train_ds , valid_ds , test_ds =, [train_size, val_size, test_size])
print("Training data size = {0} ; Validation data Size = {1} and testing data size = {2}".format(len(train_ds) , len(valid_ds), len(test_ds)))
  • Defining batch size and loading data

batch_size = 50
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
valid_dl = DataLoader(train_ds, batch_size=batch_size, num_workers=4, pin_memory=True)
test_dl =  DataLoader(train_ds, batch_size=batch_size, num_workers=4, pin_memory=True)
  • Train the model

device = get_default_device()
device = get_default_device()
train_dl = DeviceDataLoader(train_dl, device)
valid_dl = DeviceDataLoader(valid_dl, device)
model = to_device(restnet34(len(dataset.classes), pretrained=True), device )
history = [evaluate(model, valid_dl)]
history += fit_one_cycle(2, 0.001, model, train_dl, valid_dl, weight_decay=0.1, grad_clip=0.1, opt_func=torch.optim.Adam)

Test Model - Predict

def predict_image(img, model, classes):
    # Convert to a batch of 1
    xb = to_device(img.unsqueeze(0), device)
    # Get predictions from model
    yb = model(xb)
    # Pick index with highest probability
    _, preds  = torch.max(yb, dim=1)
    # Retrieve the class label
    return classes[preds[0].item()]
def show_image_prediction(img, label):
    plt.imshow(img.permute((1, 2, 0)))
    pred = predict_image(img, model, dataset.classes)
    print('Target:', dataset.classes[label])
    print('Prediction:', pred)
show_image_prediction(*valid_ds[0]) # Feed transformed data

0 views0 comments

Recent Posts

See All