我使用的是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))但在输入方面有一些微小的差异。
是否有正确的方法将
jimp图像转换为RGB张量?
发布于 2020-07-28 13:50:27
tf.node可以对位图编码的图像进行解码,就像这个回答中已经指出的那样。
const img = fs.readFileSync("path/of/image");
const tensor = tf.node.decodeImage(img)发布于 2020-07-29 07:05:30
我找到了一种将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值,所以我也制作了包含alpha值的4D张量,然后只包含sliced值。
就像@edkeveked说的。我可以使用tf.node.decodeImage功能,但我的主要预处理(在培训期间)是在opencv上,所以我需要确保它与opencv实现一样接近。
我还找到了一些带有问题图像函数的tensorflow。
所以我选择不使用tensorflow图像函数。
https://stackoverflow.com/questions/63130430
复制相似问题