将图像转为 Tensor node.js
Jimp image to Tensor node.js
我正在使用 Tensorflow.js
模型。模型以 Jimp
格式接收图像。
我需要将 Jimp
位图转换为 4d 张量。
到目前为止我已经试过这个toTensor
函数:
function imageByteArray (image){
const numChannels = 3;
const numPixels = image.bitmap.width * image.bitmap.height;
const values = new Int32Array(numPixels * numChannels);
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx){
values[y * image.bitmap.width * numChannels + x * numChannels + 0] = this.bitmap.data[idx + 0];
values[y * image.bitmap.width * numChannels + x * numChannels + 1] = this.bitmap.data[idx + 1];
values[y * image.bitmap.width * numChannels + x * numChannels + 2] = this.bitmap.data[idx + 2];
});
return values
}
function toTensor(image){
const values = imageByteArray(image);
// const values = image.data;
const outShape = [1, image.bitmap.height, image.bitmap.width, 3];
const input = tf.tensor4d(values, outShape, 'float32');
return input.sub(127.5).div(128.0)
}
但是当我使用 python cv2
比较原始预处理(在训练阶段实现)时:
def process(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image.astype("float32")
image = (image - 127.5) / 128.0
return image.reshape((1, width, height, 3))
但是在输入上有一些小的差异。
Is there any correct method to convert jimp
image to RGB tensor
tf.node
可以允许解码位图编码图像,如本
中已指示的那样
const img = fs.readFileSync("path/of/image");
const tensor = tf.node.decodeImage(img)
我找到了将 jimp
图像转换为 tfnode.Tensor
的方法:
function preProcess(image){
// const values = imageByteArray(image);
const values = image.bitmap.data;
const outShape = [1, image.bitmap.width, image.bitmap.height, 4];
var input = tf.tensor4d(values, outShape, 'float32');
// Slice away alpha
input = input.slice([0, 0, 0, 0], [1, image.bitmap.width, image.bitmap.height, 3]);
return input;
}
Jimp 图像通常也包含 alpha
值,所以我制作 4D
张量也包含 alpha 值,然后 sliced
仅 RGB 值。
正如@edkeveked 所说。我可以使用 tf.node.decodeImage
功能,但我的主要预处理(在训练期间)是在 opencv 上进行的,所以我需要确保它尽可能接近 opencv 实现。
我还发现了一些具有 tensorflow
图片功能的 problems。
所以我选择不使用tensorflow图像函数。
我正在使用 Tensorflow.js
模型。模型以 Jimp
格式接收图像。
我需要将 Jimp
位图转换为 4d 张量。
到目前为止我已经试过这个toTensor
函数:
function imageByteArray (image){
const numChannels = 3;
const numPixels = image.bitmap.width * image.bitmap.height;
const values = new Int32Array(numPixels * numChannels);
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx){
values[y * image.bitmap.width * numChannels + x * numChannels + 0] = this.bitmap.data[idx + 0];
values[y * image.bitmap.width * numChannels + x * numChannels + 1] = this.bitmap.data[idx + 1];
values[y * image.bitmap.width * numChannels + x * numChannels + 2] = this.bitmap.data[idx + 2];
});
return values
}
function toTensor(image){
const values = imageByteArray(image);
// const values = image.data;
const outShape = [1, image.bitmap.height, image.bitmap.width, 3];
const input = tf.tensor4d(values, outShape, 'float32');
return input.sub(127.5).div(128.0)
}
但是当我使用 python cv2
比较原始预处理(在训练阶段实现)时:
def process(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image.astype("float32")
image = (image - 127.5) / 128.0
return image.reshape((1, width, height, 3))
但是在输入上有一些小的差异。
Is there any correct method to convert
jimp
image to RGB tensor
tf.node
可以允许解码位图编码图像,如本
const img = fs.readFileSync("path/of/image");
const tensor = tf.node.decodeImage(img)
我找到了将 jimp
图像转换为 tfnode.Tensor
的方法:
function preProcess(image){
// const values = imageByteArray(image);
const values = image.bitmap.data;
const outShape = [1, image.bitmap.width, image.bitmap.height, 4];
var input = tf.tensor4d(values, outShape, 'float32');
// Slice away alpha
input = input.slice([0, 0, 0, 0], [1, image.bitmap.width, image.bitmap.height, 3]);
return input;
}
Jimp 图像通常也包含 alpha
值,所以我制作 4D
张量也包含 alpha 值,然后 sliced
仅 RGB 值。
正如@edkeveked 所说。我可以使用 tf.node.decodeImage
功能,但我的主要预处理(在训练期间)是在 opencv 上进行的,所以我需要确保它尽可能接近 opencv 实现。
我还发现了一些具有 tensorflow
图片功能的 problems。
所以我选择不使用tensorflow图像函数。