Home > Backend Development > Python Tutorial > StanfordCars in PyTorch

StanfordCars in PyTorch

Barbara Streisand
Release: 2024-12-23 21:38:11
Original
749 people have browsed it

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
Copy after login
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")
Copy after login

StanfordCars in PyTorch

StanfordCars in PyTorch

The above is the detailed content of StanfordCars in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template