Coder Social home page Coder Social logo

lvyilin / pytorch-fgvc-dataset Goto Github PK

View Code? Open in Web Editor NEW
143.0 4.0 23.0 14 KB

PyTorch custom dataset APIs -- CUB-200-2011, Stanford Dogs, Stanford Cars, FGVC Aircraft, NABirds, Tiny ImageNet, iNaturalist2017

License: MIT License

Python 100.00%
datasets pytorch-fgvc-dataset stanford-dogs stanford-cars fgvc-aircraft cub200-2011 inaturalist nabirds tiny-imagenet

pytorch-fgvc-dataset's Issues

Download locations invalid for NABIRDs and Stanford Cars

Thanks for the repo. Some links to datasets for download do not work any more. Also if you download the dataset manually (e.g. from Kaggle) the directory structure there is not compatible with what the repo asks for.

NABIRDS: RuntimeError: The dataset is no longer publicly accessible. You need to download the archives externally and place them in the root directory.
Stanford Cars: url also invalid

If you get Stanford cars from Kaggle with something like this below where images are in cars_train/test (Careful: some repos have them nested like cars_train/cars_train/0001.jpg)

cars_test cars_test_annos_withlabels.mat cars_train cars_train_annos.mat

Then the code below works:

import os
import scipy.io
from torchvision.datasets.vision import VisionDataset
from PIL import Image

class Cars(VisionDataset):
def init(self, root, train=True, transform=None, target_transform=None, download=False):
super(Cars, self).init(root, transform=transform,
target_transform=target_transform)
self.train = train # flag to differentiate between train and test set

    # Define paths to the MAT files
    if self.train:
        self.annotations_mat = os.path.join(root, 'cars_train_annos.mat')
        self.images_dir = os.path.join(root, 'cars_train')
    else:
        self.annotations_mat = os.path.join(root, 'cars_test_annos_withlabels.mat')
        self.images_dir = os.path.join(root, 'cars_test')

    # Load annotations
    self.annotations = self._load_annotations()

    if download:
        self.download()

def _load_annotations(self):
    # Load .mat file
    annos = scipy.io.loadmat(self.annotations_mat)
    # This might require custom parsing depending on the structure of your .mat file
    annotations = []
    for anno in annos['annotations'][0]:
        annotations.append({
            'fname': anno[-1][0],
            #'bbox': anno[0:4],
            'class': anno[4][0][0]
        })
    return annotations

def __getitem__(self, index):
    # Load image
    img_path = os.path.join(self.images_dir, self.annotations[index]['fname'])
    image = Image.open(img_path).convert('RGB')

    # Get bounding box
    #bbox = self.annotations[index]['bbox']
    # Here, you might want to crop or otherwise use the bounding box.

    # Get label
    target = self.annotations[index]['class']

    if self.transform is not None:
        image = self.transform(image)

    if self.target_transform is not None:
        target = self.target_transform(target)

    return image, target

def __len__(self):
    return len(self.annotations)

def download(self):
    # Implement this method if you want to automatically download data
    pass

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.