Vision: Image to Tensor data

Created on 7 Sep 2020  路  5Comments  路  Source: pytorch/vision

Library: pytorch_java_only-1.6.0

I want to convert BufferedImage/File to Tensor data, is there some method or library for this?

Python solution:

image = Image.open(image_path)
image = image.convert('RGB') 
transform = Compose([ToTensor()])
image = transform(image)
image = image.view(1, 3, 64, 64).cuda()
output = my_model(image)
output = output.view(-1, self.quantity)
output = nn.functional.softmax(output, dim=1)
output = torch.argmax(output, dim=1)
output = output.view(-1, self.size)[0]

I need something like that for pytorch_java. I'm sorry if my question is stupid i'm newbee

P.S: Thanks for reply

question

All 5 comments

Have you taken a look at torchvision, a pytorch extension library for image application.

Have you taken a look at torchvision, a pytorch extension library for image application.

pytorch_android_torchvision do you mean this library? Could you explain please, can i use it in a non-android application?

I checked out the java-demo

And that's what interests me:

Module mod = Module.load("demo-model.pt1");
Tensor data =
Tensor.fromBlob(
    new int[]  {1, 2, 3, 4, 5, 6}, // **data array**
    new long[] {2, 3} // **shape array**
    );
IValue result = mod.forward(IValue.from(data), IValue.from(3.0));
Tensor output = result.toTensor();
System.out.println("shape: " + Arrays.toString(output.shape()));
System.out.println("data: " + Arrays.toString(output.getDataAsFloatArray()));

With the array of the shape everything is clear, this is HEIGHT, WIDTH, CHANNELS of image.
But what about data array? How i can convert image to right array?

Hi,

We dont provide a java interface for torchvision, and I have no experience with Java so I won't be able to help with this. I think you are in the right direction with fromBlob.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zsef123 picture zsef123  路  23Comments

davidsteinar picture davidsteinar  路  23Comments

lpuglia picture lpuglia  路  44Comments

fmassa picture fmassa  路  45Comments

timonbimon picture timonbimon  路  28Comments