将 python 代码转换为 kotlin(numpy,张量)

问题描述 投票:0回答:3

我正在尝试在 python 中迁移深度学习过程,现在需要将其转换为 kotlin (android studio),但在这部分苦苦挣扎了几天:(

有人能帮我把 python 代码转换成 kotlin 代码吗?

out
的类型是Tensor。
IMAGE_SIZE
是 350 和
half
是 175.

    out = torch.argmax(out, dim=1, keepdims=True).permute(0, 2, 3, 1)[0].numpy().squeeze().astype(np.int32)
    r_H, r_W = out.shape

    _out_extended = np.zeros((IMAGE_SIZE + r_H, IMAGE_SIZE + r_W), dtype=out.dtype)
    _out_extended[half : half + IMAGE_SIZE, half : half + IMAGE_SIZE] = out * 255
    out = _out_extended.copy()

我还尝试使用 Multik 库中的 argmax 函数,但输入类型不一样。 (不是 Tensor,而是 MultiArray)。

欢迎任何其他关于从 python 迁移到 kotlin 的建议!

python kotlin deep-learning pytorch multikey
3个回答
0
投票

这是一个 Kotlin 代码片段,应该等同于您提供的 Python 代码:

val out = tensor.argmax(1, true).permute(0, 2, 3, 1)[0].numpy().squeeze().astype(IntArray::class.java)
val (r_H, r_W) = out.shape
val half = 175

val _out_extended = Array(IMAGE_SIZE + r_H) { IntArray(IMAGE_SIZE + r_W) }
_out_extended.fill(0)

for (i in half until half + IMAGE_SIZE) {
    for (j in half until half + IMAGE_SIZE) {
        _out_extended[i][j] = out[i - half][j - half] * 255
    }
}

val out = _out_extended

请注意,您需要将变量张量替换为您自己的输入张量实现。此外,您需要导入必要的库才能使此代码正常工作。


0
投票

要在张量上使用 argmax 函数,您可以使用 PyTorch 库。您可以使用以下命令安装 PyTorch:

pip install torch

然后,您可以在代码中使用 argmax 函数,如下所示:

import torch

# Create a tensor
t = torch.tensor([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]])

# Get the index of the maximum value
index = torch.argmax(t)
print(index)  # Output: tensor(5)

# Get the index of the maximum value along a specific dimension
index = torch.argmax(t, dim=1)
print(index)  # Output: tensor([2, 2])

请注意,这是为了在 Python 中使用 PyTorch。如果你想在 Android 中使用 PyTorch,你可以使用 PyTorch Android 库,但我不确定它目前是否提供 argmax 函数。


0
投票

我有点自己弄明白了。
在 kotlin 中,我无法使用 argmax 函数,所以不得不以某种方式转换它。

“输出”相当于我问题中的“输出”。

        val outTensors = module.forward(IValue.from(inputTensor)).toDictStringKey()

        // the key "out" of the output tensor contains the semantic masks
        val outputTensor = outTensors["out"]!!.toTensor()
        val outputs = outputTensor .dataAsFloatArray

        val width: Int = imageModel.width
        val height: Int = imageModel.height

        val intVal = IntArray(width * height)

        // output processing
        // go through each element in the output of size [WIDTH, HEIGHT] and
        // set different color(black&white) for different classnum
        for (j in 0 until width) {
            for (k in 0 until height) {
                // maxi: the index of the 2 classnum with the max probability
                var maxi = 0
                var maxj = 0
                var maxk = 0
                var maxnum = -100000.0
                for (i in 0 until classnum) {
                    if (outputs[i * (width * height) + j * width + k] > maxnum) {
                        maxnum = outputs[i * (width * height) + j * width + k].toDouble()
                        maxi = i
                        maxj = j
                        maxk = k
                    }
                }
                // color coding for paper in white
                // black color for background and others
                if (maxi == 1)
                    intVal[maxj * width + maxk] = -0x000f // white
                else
                    intVal[maxj * width + maxk] = 0x1000000 // black
            }
        }

对于 _out_extended 部分,在我将 intVal 转换为 mat (= originalImage) 之后, 我使用这种方法来扩展输出。

val padding = 255 
Core.copyMakeBorder(
            originalImage,
            borderImage,
            padding, padding, padding, padding,
            Core.BORDER_CONSTANT, Scalar(0.0)
        )
© www.soinside.com 2019 - 2024. All rights reserved.