Create Image from numpy array

Hi everyone,

I would like to convert from a numpy array (an image that I read using opencv) to fastai’s Image class.

I have gotten this to work by storing the image in a file, and then reading it with open_image.

I am doing this because I want to use fastai to obtain prediction on the classes that patches from an image mosaic that I am manipulating with opencv belong to.

The (very ugly) code, looks like this


pred_class,pred_idx,outputs =learner.predict(img)

By reading the documentation I seem to understand that I should be able to create an Image object by calling the creator of the image class with something that is a torch tensor. Am I right in this?

I reach something like


Which, to be frank, also looks a bit over the top, and also does not work as I get

RuntimeError: grid_sampler(): expected input and grid to have same dtype, but input has unsigned char and grid has float

I suppose I am missing some data conversion but I cannot figure it out at this moment.

Does anyone know a better way to do this?