ShivAA
ShivAA

Reputation: 127

Get Pytorch - tensor values as a integer in python

I have my output of my torch tensor which looks like below

(coordinate of a bounding box in object detection)

[tensor(299., device='cuda:0'), tensor(272., device='cuda:0'), tensor(327., device='cuda:0'), tensor(350., device='cuda:0')]

I wanted to extract each of the tensor value as an int in the form of minx,miny,maxx,maxy
so that I can pass it to a shapely function in the below form

from shapely.geometry import box
minx,miny,maxx,maxy=1,2,3,4
b = box(minx,miny,maxx,maxy)

What's the best way to do it? by avoiding, Cuda enabled or not or other exceptions?

Upvotes: 1

Views: 4184

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18306

minx, miny, maxx, maxy = [int(t.item()) for t in tensors]

where tensors is the list of tensors.

Upvotes: 2

Related Questions