Tensorflow.js:将图像调整为特定字节大小

Tensorflow.js: Resize image to specific byte size

为了进行预测,我需要一个形状为 [null,7,7,256] 的图像。

const image = tf.reshape(tf.fromPixels(loadedImage).resizeBilinear([?,?]), [null, 7, 7, 256]);

但我不知道如何将图像调整到正好 7*7*256 大。

Error: Size(37632) must match the product of shape ,7,7,256

编辑:预测代码为:

tf.loadModel(tf.io.browserFiles([uploadJSONInput.files[0], uploadWeightsInput.files[0]])).then(model => {
        console.log("model loaded");
        return model;

    }).then(pretrainedModel => {
        return loadImage2('http://localhost/myimg.jpeg', (src) => {
            const loadedImage = document.createElement("img");
            loadedImage.src = src;
            loadedImage.width = "275"
            loadedImage.height = "183"
            console.log("image loaded");

            const image = tf.fromPixels(loadedImage)

            resized = tf.image.resizeBilinear(image, [7, 7])
            const padded = resized.pad([[0, 0], [0, 0], [126, 127]])

            const pretrainedModelPrediction = pretrainedModel.predict(padded);
            const modelPrediction = model.predict(pretrainedModelPrediction);
            const prediction = modelPrediction.as1D().argMax().dataSync()[0];
            console.log(prediction);
        });

    })

错误:

Error: Error when checking : expected flatten_Flatten1_input to have 4 dimension(s), but got array with shape [7,7,256]

ResizeBilinear 将调整图像的高度和宽度,这意味着它不会影响通道数,通道数是图像形状的最后一个维度。

如果您的图像最后一个通道为 256,则以下方法有效

tf.fromPixels(loadedImage).resizeBilinear([7,7])

仅当两个大小匹配时,重塑张量才有效。 常量图像 = tf.ones([183, 275, 3 ]) resized = tf.image.resizeBilinear(image, [7, 7]) console.log(resized.pad([[0, 0], [0, 0], [126, 127]]).shape);

一幅图像的形状通常是[h, w, 3]。

resize = tf.fromPixels(loadedImage).resizeBilinear([7,7]) // [7, 7, 3]

然后对最后一个维度使用tf.pad

const image = tf.ones([183, 275, 3 ])
resized = tf.image.resizeBilinear(image, [7, 7])
console.log(resized.pad([[0, 0], [0, 0], [126, 127]]).shape);// [7,7,256]

// reshape the tensor to be a 4d
resized.reshape([1,7,7,256])

以下是如何使用 Uint8Array

const canvas: any = document.getElementById('canvas')
const context = canvas.getContext('2d')
const imageData: ImageData = context.getImageData(0, 0, canvas.width, canvas.height)
const uint8array = new Uint8Array(imageData.data.buffer)
const rgbaTens3d = tf.tensor3d(uint8array, [canvas.height, canvas.width, 4])
const rgbTens3d= tf.slice3d(rgbaTens3d, [0, 0, 0], [-1, -1, 3]) // strip alpha channel
const smallImg = tf.image.resizeBilinear(rgbTens3d, [192, 192]); // 192,192 is dictated by my model