DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

StanfordCars in PyTorch

Buy Me a Coffee

*My post explains Stanford Cars.

StanfordCars() can use Stanford Cars dataset as shown below:

*Memos:

  • The 1st argument is root(Required-Type:str or pathlib.Path). *An absolute or relative path is possible.
  • The 2nd argument is split(Optional-Default:"train"-Type:str). *"train"(8,144 images) or "test"(8,041 images) can be set to it.
  • The 3rd argument is transform(Optional-Default:None-Type:callable).
  • The 4th argument is target_transform(Optional-Default:None-Type:callable).
  • The 5th argument is download(Optional-Default:False-Type:bool): *Memos:
    • Keep it False because if it's True, error occurs because the original URL is broken.
    • So, you need to manually download and extract archive.zip from here, archive.zip from here and car_devkit.tgz to data/stanford_cars/ as shown below: *Memos:
      • cars_test_annos_withlabels (1).mat needs to be renamed to cars_test_annos_withlabels.mat.
      • cars_annos.mat and cars_annos (2).mat isn't needed and there are some duplicated files as well.
      • You can also see the instruction.
data
 └-stanford_cars
    |-cars_test_annos_withlabels.mat
    |-cars_test
    |  └-*.jpg
    |-cars_train
    |  └-*.jpg
    └-devkit
       |-cars_meta.mat
       |-cars_test_annos.mat
       |-cars_train_annos.mat
       |-eval_train.m
       |-README.txt
       └-train_perfect_preds.txt
Enter fullscreen mode Exit fullscreen mode
from torchvision.datasets import StanfordCars

train_data = StanfordCars(
    root="data"
)

train_data = StanfordCars(
    root="data",
    split="train",
    transform=None,
    target_transform=None,
    download=False
)

test_data = StanfordCars(
    root="data",
    split="test"
)

len(train_data), len(test_data)
# (8144, 8041)

train_data
# Dataset StanfordCars
#     Number of datapoints: 8144
#     Root location: data

train_data.root
# 'data'

train_data._split
# 'train'

print(train_data.transform)
# None

print(train_data.target_transform)
# None

train_data.download
# <bound method StanfordCars.download of Dataset StanfordCars
#     Number of datapoints: 8144
#     Root location: data>

len(train_data.classes), train_data.classes
# (196,
#  ['AM General Hummer SUV 2000', 'Acura RL Sedan 2012', 'Acura TL Sedan 2012',
#   'Acura TL Type-S 2008', ..., 'Volvo 240 Sedan 1993',
#   'Volvo XC90 SUV 2007', 'smart fortwo Convertible 2012'])

train_data[0]
# (<PIL.Image.Image image mode=RGB size=600x400>, 13)

train_data[1]
# (<PIL.Image.Image image mode=RGB size=900x675>, 2)

train_data[2]
# (<PIL.Image.Image image mode=RGB size=640x480>, 90)

train_data[3]
# (<PIL.Image.Image image mode=RGB size=2100x1386>, 133)

train_data[4]
# (<PIL.Image.Image image mode=RGB size=144x108>, 105)

import matplotlib.pyplot as plt

def show_images(data, main_title=None):
    plt.figure(figsize=(12, 5))
    plt.suptitle(t=main_title, y=1.0, fontsize=14)
    for i, (im, lab) in zip(range(1, 11), data):
        plt.subplot(2, 5, i)
        plt.imshow(X=im)
        plt.title(label=lab)
    plt.tight_layout()
    plt.show()

show_images(data=train_data, main_title="train_data")
show_images(data=test_data, main_title="test_data")

show_images(data=train_data, ims=train_ims, main_title="train_data")
show_images(data=train_data, ims=val_ims, main_title="val_data")
show_images(data=test_data, ims=test_ims, main_title="test_data")
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Top comments (0)