Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: code snipped for local image files #23

Open
andife opened this issue Jun 11, 2021 · 1 comment
Open

Example: code snipped for local image files #23

andife opened this issue Jun 11, 2021 · 1 comment

Comments

@andife
Copy link

andife commented Jun 11, 2021

Hello,
very interesting package.

Can someone provide a code snipped for a
"Add implementation for len and getitem methods in dataset.py."

My dataset is a classification problem, and the files are structured
like

Project/group1/*bmp
Project/group2/*bmp

Thank you

Andreas

@davidecarnevali
Copy link

The following dataset.py is working for me:

import torch.utils.data
import torchvision
from torchvision import dataset
import cv2
cv2.setNumThreads(0)

dataset = datasets.ImageFolder("/Project/")
images = dataset.imgs

class SearchDataset(torch.utils.data.Dataset):

def __init__(self, images_filepaths=images, transform=None):
    self.images_filepaths = images_filepaths
    self.transform = transform

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

def __getitem__(self, index):
    path, label = self.images_filepaths[index]
    image = cv2.imread(path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    if self.transform is not None:
        transformed = self.transform(image=image)
        image = transformed["image"]

    return image, label

Images should be all of the same size. If they are not, you can resize them by adding to __getitem__:
image = cv2.resize(image, (height, width))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants