Question: Task 1 . This is pytorch task: Find some data set with signatures and train this code to work as it should. Try using the

Task 1. This is pytorch task: Find some data set with signatures and train this code to work as it should. Try using the following: Create a custom dataloader in pytorch for your dataset -. given code
Train resnet50(pytorch documentation) on gallery and try it, evaluate it on tests - for now only gallery and tests.
import zipfile
with zipfile.ZipFile('some data set from kaggle', 'r') as zip_ref:
zip_ref.extractall('/content/some data set from kaggle')
import pandas as pd
import os # Ensure you import os to use it in file paths
from torchvision.io import read_image
from torch.utils.data import Dataset # Import Dataset class
class CustomImageDataset(Dataset):
# the FashionMNIST images are stored in a directory img_dir
# The __init__ function is run once when instantiating the Dataset object.
# We initialize the directory containing the images, the annotations file, and both transforms
def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
#labels are stored separately in a CSV file annotations_file
self.img_labels = pd.read_csv(annotations_file)
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform
# The __len__ function returns the number of samples in our dataset.
def __len__(self):
return len(self.img_labels)
# The __getitem__ function loads and returns a sample from the dataset at the given index idx.
# Based on the index, it identifies the images location on disk, converts that to a tensor using read_image,
# retrieves the corresponding label from the csv data in self.img_labels,
# calls the transform functions on them (if applicable), and returns the tensor image and corresponding label in a tuple.
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx,0])
image = read_image(img_path)
label = self.img_labels.iloc[idx,1]
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return image, label
from torch.utils.data import DataLoader
train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
test_dataloader = DataLoader(test_data, batch_size=64, shuffle=True)
# Display image and label.
train_features, train_labels = next(iter(train_dataloader))
print(f"Feature batch shape: {train_features.size()}")
print(f"Labels batch shape: {train_labels.size()}")
img = train_features[0].squeeze()
label = train_labels[0]
plt.imshow(img, cmap="gray")
plt.show()
print(f"Label: {label}")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!