!git clone https://github.com/unifyai/ivy.git
!cd ivy && git checkout 5cbfea51b617c5fd6e17b6a508f1d67134775670 && python3 -m pip install -e .
!git clone https://github.com/unifyai/models.git
!cd models && python3 -m pip install -e .
! python3 -m pip install torchvision
Using Ivy ResNet18
Use the Ivy ResNet18
model for image classification.
If you already have Ivy
and Ivy Models
installed you can skip this cell, but if you are using Colab, you will have to install Ivy
and Ivy Models
manually. You can do so by running the cell below ⬇️
Keep in mind that for the package to be available, you will have to click on “Runtime > Restart Runtime” 😄
To use the compiler and the transpiler now you will need an API Key. If you already have one, you should replace the string in the next cell.
= "PASTE_YOUR_KEY_HERE" API_KEY
!mkdir -p .ivy
!echo -n $API_KEY > .ivy/key.pem
Imports
import ivy
import torch
Data Preparation
Prepare the set of labels
To show the predicted category, we download the labels associated with the pretrained weights.
!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
The labels are then loaded into a Python list.
# Read the categories
with open("imagenet_classes.txt", "r") as f:
= [s.strip() for s in f.readlines()] categories
Load the image example 🖼️
# import numpy to load the image example .npy
import numpy as np
= "models/ivy_models_tests/img_resnet.npy"
img_path = np.load(img_path) img
Model Inference
Initializing Native Torch ResNet18
from torchvision.models import resnet18, ResNet18_Weights
= resnet18(weights=ResNet18_Weights.IMAGENET1K_V1) torch_resnet
Initializing Ivy ResNet18 with Pretrained Weights through Ivy Container ⬇️
The pickled weights are loaded into Ivy ResNet
as ivy.Container
.
# equivalent to ResNet18_Weights.IMAGENET1K_V1
= "models/ivy_models/resnet/pretrained_weights/resnet_18.pickled" pretrained_weights_path
= ivy.Container.cont_from_disk_as_pickled(pretrained_weights_path) weights
The model is then initialized with the Pretrained Weights 🔗.
# load the resnet18 model from ivy_models
from ivy_models.resnet import resnet_18
= resnet_18(v=weights) ivy_resnet
Compile the forward pass for efficiency.
PS: Comment out the code below if you don’t have an API key.
'torch')
ivy.set_backend(compile(args=(img,)) ivy_resnet.
Use the model to classify your images 🚀
For comparison, both results from Torch ResNet18
and Ivy ResNet18
are shown below.
- Torch ResNet18
= torch.softmax(torch_resnet(torch.from_numpy(img).reshape(1, 3, 224, 224)), dim=1)
torch_output = torch.argsort(torch_output[0])[-4:]
torch_classes = torch.take(torch_output[0], torch_classes)
torch_logits
print("Indices of the top 4 classes are:", torch_classes)
print("Logits of the top 4 classes are:", torch_logits)
print("Categories of the top 4 classes are:", [categories[i] for i in torch_classes])
Indices of the top 4 classes are: tensor([412, 731, 600, 463])
Logits of the top 4 classes are: tensor([0.0055, 0.0064, 0.0065, 0.0082], grad_fn=<TakeBackward0>)
Categories of the top 4 classes are: ['ashcan', 'plunger', 'hook', 'bucket']
- Ivy ResNet18
= ivy.softmax(ivy_resnet(ivy.asarray(img))["w"]) # pass the image to the model
output = ivy.argsort(output[0])[-4:] # get the top 4 classes
classes = ivy.gather(output[0], classes) # get the logits
logits
print("Indices of the top 4 classes are:", classes)
print("Logits of the top 4 classes are:", logits)
print("Categories of the top 4 classes are:", [categories[i] for i in classes.to_list()])
Indices of the top 4 classes are: tensor([412, 731, 600, 463])
Logits of the top 4 classes are: [0.00549888 0.005922 0.00626743 0.00846343]
Categories of the top 4 classes are: ['ashcan', 'plunger', 'hook', 'bucket']