对于预测,我需要一个形状为 [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 将调整图像的高度和宽度,这意味着它不会影响图像形状的最后一维 channel 数。
如果您的图像最后一个 channel 为 256,则以下将起作用
tf.fromPixels(loadedImage).resizeBilinear([7,7])
reshape 张量只有在两种尺寸匹配时才有效。 常量图像 = tf.ones([183, 275, 3 ]) resized = tf.image.resizeBilinear(图像, [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])
关于image - Tensorflow.js:将图像调整为特定字节大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210767/