lab(localization and detection )
lab(localization and detection )
ipynb - Colab
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import numpy as np
class MNISTWithBoundingBoxes:
def __init__(self, train=True):
self.dataset = datasets.MNIST(
root='./data',
train=train,
download=True,
transform=transforms.ToTensor()
)
def __len__(self):
return len(self.dataset)
What’s happening?
We load the MNIST dataset and compute bounding boxes based on non-zero pixels.
Bounding boxes are normalized to [ 0 , 1 ] [0,1] relative to the image size. bold text
class LocalizationModel(nn.Module):
def __init__(self):
super(LocalizationModel, self).__init__()
self.backbone = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2)
)
self.fc = nn.Sequential(
nn.Flatten(),
nn.Linear(32 * 7 * 7, 128),
nn.ReLU(),
nn.Linear(128, 4) # 4 outputs: [x_min, y_min, x_max, y_max]
)
https://colab.research.google.com/drive/1bctWzlVrcVlhhHqOU9PH8yYSJBuzMpU7#scrollTo=u9fZNMbFkYhT&printMode=true 1/3
11/27/24, 8:13 AM Untitled5.ipynb - Colab
bbox = self.fc(features)
return bbox
# Initialize model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = LocalizationModel().to(device)
epochs = 5
for epoch in range(epochs):
model.train()
total_loss = 0
for imgs, _, bboxes in train_loader:
imgs, bboxes = imgs.to(device), bboxes.to(device)
optimizer.zero_grad()
pred_bboxes = model(imgs)
loss = criterion(pred_bboxes, bboxes)
loss.backward()
optimizer.step()
total_loss += loss.item()
Explanation:
model.eval()
with torch.no_grad():
for imgs, _, bboxes in test_loader:
imgs, bboxes = imgs.to(device), bboxes.to(device)
pred_bboxes = model(imgs)
print("Predicted BBox:", pred_bboxes[0].cpu().numpy())
print("Ground Truth BBox:", bboxes[0].cpu().numpy())
break
Explanation:
Use the trained model to predict bounding boxes for unseen test data.
https://colab.research.google.com/drive/1bctWzlVrcVlhhHqOU9PH8yYSJBuzMpU7#scrollTo=u9fZNMbFkYhT&printMode=true 2/3
11/27/24, 8:13 AM Untitled5.ipynb - Colab
https://colab.research.google.com/drive/1bctWzlVrcVlhhHqOU9PH8yYSJBuzMpU7#scrollTo=u9fZNMbFkYhT&printMode=true 3/3