Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像到张量数据_Java_Pytorch_Torch - Fatal编程技术网

Java 图像到张量数据

Java 图像到张量数据,java,pytorch,torch,Java,Pytorch,Torch,库:pytorch\u仅java\u-1.6.0 我想将BuffereImage/File转换为Tensor数据,是否有相应的方法/库 Python解决方案: image = Image.open(image_path) image = image.convert('RGB') transform = Compose([ToTensor()]) image = transform(image) image = image.view(1, 3, 64, 64).cuda() output = m

库:pytorch\u仅java\u-1.6.0

我想将BuffereImage/File转换为Tensor数据,是否有相应的方法/库

Python解决方案:

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]
我查看了

这就是我感兴趣的地方:

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()));
有了形状的数组一切都很清楚,这就是图像的高度、宽度和通道

但是数据数组呢?如何将图像转换为右数组?也许我应该使用javacv/nd4j或者类似的东西


谢谢回复

使用JavaCV和PyTorch的JavaCPP预设应该很容易实现:如果您在实现这一点时遇到任何问题,请告诉我,我将使用它们给出答案。