What’s the efficient way to convert Python objects of various types to Tensor objects?

Hi.
I want to know is there any efficient way to convert Python objects of various types to Tensor objects,like convert_to_tensor in tensorflow, It accepts Tensor objects, numpy arrays, Python lists, and Python scalars.
Although I know that I can determine the type of data and then call the corresponding API, but I think this is too bloated, and I don’t want to write a bunch of ‘if judgment statements’ to determine the type of data, so I would like to ask if there is a corresponding pytorch API or is there any efficient way

Appreciate for any replies.

looking forward for your help!

The easiest way would be probably to use torch.tensor.
Would that work or are you running into some issues?

1 Like

@ptrblck
HI, i have one question…
so i have a list of list of list…sth like this

img1
   bbox
      cls1_bbox_list (100*5 ndarray)
      cls2_bbox_list (95*5 ndarray)
      cls3_bbox_list ([ ] )  ##empty
  masks
      cls1_masks_list  (5*256*256)
       cls2_mask_list ([ ])
img2
  ....

how I can quickly convert them into torch tensor and put into GPU?

I did

 map(torch.cuda.FloatTensor,  the_whole_listoflistoflist)

but it seems a bit slow
any ideas? Thanks in advance :slight_smile:

I’m unfortunately not aware of a better approach, so let’s wait for others to chime in :confused:

1 Like