dambo
(Shlomo)
June 14, 2019, 6:49am
#1
Dear All,
I am trying to migrate the python based ImageList and PilImageList from here to Libtorch C++:
import copy
import logging
import os
import torch.utils.data
import torchvision
from PIL import Image
from . import transforms, utils
ANNOTATIONS_TRAIN = 'data-mscoco/annotations/person_keypoints_train2017.json'
ANNOTATIONS_VAL = 'data-mscoco/annotations/person_keypoints_val2017.json'
IMAGE_DIR_TRAIN = 'data-mscoco/images/train2017/'
IMAGE_DIR_VAL = 'data-mscoco/images/val2017/'
def collate_images_anns_meta(batch):
images = torch.utils.data.dataloader.default_collate([b[0] for b in batch])
anns = [b[1] for b in batch]
metas = [b[2] for b in batch]
This file has been truncated. show original
Most of the examples I found in C++ are for MNIST and not a custom dataset.
Has anyone done something like this before? or is there a build in ImageFolder in C++?
Thanks,
dambo
(Shlomo)
June 15, 2019, 2:00pm
#2
Anyone … breaking my head on this.
This is what I am looking for in C++:
class ImageList(torch.utils.data.Dataset):
def __init__(self, image_paths, preprocess=None, image_transform=None):
self.image_paths = image_paths
self.image_transform = image_transform or transforms.image_transform
self.preprocess = preprocess
def __getitem__(self, index):
image_path = self.image_paths[index]
with open(image_path, 'rb') as f:
image = Image.open(f).convert('RGB')
if self.preprocess is not None:
image = self.preprocess(image, [])[0]
original_image = torchvision.transforms.functional.to_tensor(image)
image = self.image_transform(image)
return image_path, original_image, image
def __len__(self):
return len(self.image_paths)
Thanks.