Do datablocks only return lists of image files?

@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[0]
    ## Get the bouding box
    bbox = o[1:5]
    ## open the image
    new_img =
    ## Get the size
    img_width, img_height = new_img.size[0], new_img.size[1]
    ## 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[0]).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?

Hi nchukaobah hope all is well!

Maybe the post below can help you solve your problem.

There are more posts with the error on the forum which may help if you haven’t seen them already.

Cheers mrfabulous1 :smiley: :smiley:

Thanks for the information. I figured out what was wrong by using @muellerzr comment from an earlier post of using datablock.summary(input) to check what is happening in your data blcok.

The issue was the conversion to tensor image. FastAI required my output to be in a form it could convert to a tensor image. So, once I did whatever I wanted to my image, I could then convert the final image with PILImage.create, and everything works. So I just needed to return PILImage.create(np.asarray(new_img2)), and everything worked.

Thanks for the reference.

1 Like