Depth prediction based on an image - need float image

I’m trying to predict a depth value for every pixel in an image. A current paper in this area is FastDepth, which uses the NYUDepthV2 dataset - essentially for every picture (480,640,3) there is a depth map (480,640) - float32. This is new to me - we have lots of examples of classification, even segmentation. But now I want the ‘answer’ to be an image sized array of float32.

Currently it somewhat works - when loading I handle this like segmentation. Except I’ve pre-loaded the depth data in tiff image files as float32. Works great.
iil =(ImageImageList.from_folder(dataFP,extensions=[’.png’])
.split_by_folder()
.label_from_func(get_y_fn,convert_mode=“F”))
**Except… when open_image is called it divides all pixels by 255.

How can I avoid this?** i.e., I want ‘y’ to be an array of (1,480,640) of type float32

Any thoughts? Do I need to go back to the torch way of building a dataloader to wrap into a DataBunch? I was hoping to leverage the fast.ai framework…

(btw, it still sorta works, except the final values of course need to be multiplied by 255. However, I’d like to do this right!)

3 Likes

There is an argument div in open_image which can be set to false if you don’t want to divide by 255.

Thanks - but I saw that – I’m just having troubles getting the call to open_image to use ‘div=False’ when calling ‘label_from_func’. i.e., ‘convert_mode’ passes through to ‘open_image’. ‘div’ is excluded.

The way I see this (yow), using ‘label_from_func’ sets the correct filenames for my depth images - in this case ‘tiff’ files - float32. Great. The images are then read when the new ImageList is instantiated inside ItemList. _label_from_list - this calls ‘y = label_cls(labels, path=self.path, **kwargs)’ which builds an ImageList and calls open_image to build each Image.

Check - I use:
– .label_from_func(get_y_fn,convert_mode=‘F’,label_cls=ImageList))
and it works fine.

However, if I create an override on ImageList, it fails for some reason.
def DepthList(Image):
def open(self, fn):
“Open DEPTH image in fn, subclass and overwrite for custom behavior.”
return open_image(fn, div=False, convert_mode=“F”, after_open=self.after_open)

–> .label_from_func(get_y_fn,convert_mode=‘F’,label_cls=DepthList))

The error is on the line noted above in _label_from_list …

TypeError: DepthList() got an unexpected keyword argument ‘path’

Thoughts???

I believe DepthList should be a subclass of ImageList. Is that a typo?

Thanks! User error… This works very well now!

class DepthList(ImageList):
def open(self, fn):
“Open DEPTH image in fn, subclass and overwrite for custom behavior.”
return open_image(fn, div=False, convert_mode=“F”, after_open=self.after_open)

def get_y_fn(fp):
return Path(os.path.join(fp.parent.parent,‘depth’,f’depth{fp.stem}.tiff’))

srcD =(ImageDepthList.from_folder(dataFP,extensions=[’.png’])
.split_by_folder()
.label_from_func(get_y_fn,label_cls=DepthList))