本文介绍了我在将 Tensorflow Python 过渡到 Tensorflow.js 时遇到了图像预处理方面的问题.我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 Tensorflow Python 过渡到 Tensorflow.js 时遇到了图像预处理方面的问题

I'm having trouble with the transition of Tensorflow Python to Tensorflow.js in regards to image preprocessing

在 Python 中

single_coin = r"C:\temp\coins\20Saint-03o.jpg"
img = image.load_img(single_coin, target_size = (100, 100))
array = image.img_to_array(img)
x = np.expand_dims(array, axis=0)
vimage = np.vstack([x])
prediction =model.predict(vimage)
print(prediction[0])

我得到了正确的结果

[2.8914417e-05 3.5085387e-03 1.9252902e-03 6.2635467e-05 3.7389682e-031.2983804e-03 7.4157811e-04 1.4608903e-04 2.7099697e-06 1.1844193e-021.3398369e-04 9.3798796e-03 9.7308388e-05 7.3931034e-05 1.9695959e-049.6496813e-05 4.2653349e-04 8.7305409e-05 8.1476872e-04 4.9094640e-041.3498703e-04 9.6476960e-01]

[2.8914417e-05 3.5085387e-03 1.9252902e-03 6.2635467e-05 3.7389682e-03 1.2983804e-03 7.4157811e-04 1.4608903e-04 2.7099697e-06 1.1844193e-02 1.3398369e-04 9.3798796e-03 9.7308388e-05 7.3931034e-05 1.9695959e-04 9.6496813e-05 4.2653349e-04 8.7305409e-05 8.1476872e-04 4.9094640e-04 1.3498703e-04 9.6476960e-01]

但是在具有相同图像的 Tensorflow.js 中发布以下预处理函数:

function preprocess(img)
{
     let tensor = tf.browser.fromPixels(img)
     const resized = tf.image.resizeBilinear(tensor, [100, 100]).toFloat()
     const offset = tf.scalar(255.0);
     const normalized = tf.scalar(1.0).sub(resized.div(offset));
     const batched = normalized.expandDims(0)
     return batched
}

我得到以下结果:

[0.044167134910821915,0.04726826772093773,0.04546305909752846,0.04596292972564697,0.044733788818120956,0.04367975518107414,0.04373137652873993,0.044592827558517456,0.045657724142074585,0.0449688546359539,0.04648510739207268,0.04426411911845207,0.04494940862059593,0.0457320399582386,0.045905906707048416,0.04473186656832695,0.04691491648554802,0.04441603645682335,0.04782886058092117,0.04696653410792351,0.045027654618024826,0.04655187949538231]

[0.044167134910821915,0.04726826772093773,0.04546305909752846,0.04596292972564697,0.044733788818120956,0.04367975518107414,0.04373137652873993,0.044592827558517456,0.045657724142074585,0.0449688546359539,0.04648510739207268,0.04426411911845207,0.04494940862059593,0.0457320399582386,0.045905906707048416,0.04473186656832695,0.04691491648554802,0.04441603645682335,0.04782886058092117,0.04696653410792351,0.045027654618024826,0.04655187949538231]

我显然没有适当地翻译预处理.有人看到我错过了什么吗?

I'm obviously not translating the preprocessing appropriately. Does anyone see what I'm missing?

推荐答案

python 代码没有规范化,js 代码有规范化.要么在 js 中应用相同的规范化在 python 中也应用,要么从 js 代码中删除规范化.

There is no normalization applied in the python code but there is a normalization in the js code. Either the same normalization applied in js is applied in python as well, or the normalization is removed from the js code.

已经给出了类似的答案 这里

Similar answer has been given here

这篇关于我在将 Tensorflow Python 过渡到 Tensorflow.js 时遇到了图像预处理方面的问题.我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:01