@muellerzr Hi, I’m trying to create a dataloader where I crop the image according to a bounding box.
The image file paths, and the bounding box coordinates are in a CSV file. The image files are located in class-arranged folders.
A sample of the csv file is below:
I have defined helper functions as shown below:
def get_bounding_box(o): ## Get image filename img_file = o ## Get the bouding box bbox = o[1:5] ## open the image new_img = Image.open(path/Path(img_file)) print(new_img.size) ## Get the size img_width, img_height = new_img.size, new_img.size ## Get elements of bounding box lx, ly, w, h = bbox ## Crop the image new_img2 = new_img.crop((lx*img_width, ly*img_height, w*img_width, h*img_height)) ## Return image object return new_img2 def get_imgCrop (o): new_img = get_bounding_box(o) return new_img def get_label (o): class_label = str(Path(o).parent) return int(class_label)
Then defined the data loader as:
## Create the datablock item_tfms = [Resize(224, method = "squish")] batch_tfms = [*aug_transforms(size=256), Normalize.from_stats(*imagenet_stats)] db_block = DataBlock( blocks = (ImageBlock, CategoryBlock), get_x = lambda o: get_imgCrop(o), get_y = lambda o: get_label(o), splitter = RandomSplitter(valid_pct = 0.2, seed = 43), item_tfms = item_tfms, batch_tfms = batch_tfms)
Then make the dataloader from the dataframe:
dls = db_block.dataloaders(bbox_file)
This gives an error saying:
> Could not do one pass in your dataloader, there is something wrong in it
My question then is: what are datablocks required to return? A list of anything (in my case, I expected a list of PILImage.Image objects for x, and ints for y) or must it be a list of filenames for x?