tensorflow.js 中预测的输出概率
Output probability of prediction in tensorflow.js
我有一个 model.json 通过 tensorflow.js 转换器从 tensorflow 生成
原来在python中tensorflow中model的实现是这样构建的:
model = models.Sequential([
base_model,
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
在tensorflow中,概率可以通过score = tf.nn.softmax(predictions[0])生成,根据官网教程
如何在 tensorflow.js 中得到这个概率?
我已经复制了下面的代码模板:
$("#predict-button").click(async function () {
if (!modelLoaded) { alert("The model must be loaded first"); return; }
if (!imageLoaded) { alert("Please select an image first"); return; }
let image = $('#selected-image').get(0);
// Pre-process the image
console.log( "Loading image..." );
let tensor = tf.browser.fromPixels(image, 3)
.resizeNearestNeighbor([224, 224]) // change the image size
.expandDims()
.toFloat()
// RGB -> BGR
let predictions = await model.predict(tensor).data();
console.log(predictions);
let top5 = Array.from(predictions)
.map(function (p, i) { // this is Array.map
return {
probability: p,
className: TARGET_CLASSES[i] // we are selecting the value from the obj
};
}).sort(function (a, b) {
return b.probability - a.probability;
}).slice(0, 2);
console.log(top5);
$("#prediction-list").empty();
top5.forEach(function (p) {
$("#prediction-list").append(`<li>${p.className}: ${p.probability.toFixed(6)}</li>`);
});
如何修改上面的代码?
输出与变量'predictions'的值相同:
Float32Array(5)
0: -2.5525975227355957
1: 7.398464679718018
2: -3.252196788787842
3: 4.710395812988281
4: -4.636396408081055
buffer: (...)
byteLength: (...)
byteOffset: (...)
length: (...)
Symbol(Symbol.toStringTag): (...)
__proto__: TypedArray
0: {probability: 7.398464679718018, className: "Sunflower"}
1: {probability: 4.710395812988281, className: "Rose"}
length: 2
__proto__: Array(0)
求助!!!
谢谢!
为了使用 softmax 函数从模型的对数中提取概率,您可以执行以下操作:
这是对数数组,也是您从模型
中获得的predictions
const logits = [-2.5525975227355957, 7.398464679718018, -3.252196788787842, 4.710395812988281, -4.636396408081055]
您可以对值数组调用 tf.softmax()
const probabilities = tf.softmax(logits)
结果:
[0.0000446, 0.9362511, 0.0000222, 0.0636765, 0.0000056]
然后如果你想得到概率最大的索引你可以使用tf.argMax()
:
const results = tf.argMax(probabilities).dataSync()[0]
结果:
1
编辑
我对 jQuery 不太熟悉,所以这可能不正确。但这是我如何按降序获得输出的概率:
let probabilities = tf.softmax(predictions).dataSync();
$("#prediction-list").empty();
probabilities.forEach(function(p, i) {
$("#prediction-list").append(
`<li>${TARGET_CLASSES[i]}: ${p.toFixed(6)}</li>`
);
});
我有一个 model.json 通过 tensorflow.js 转换器从 tensorflow 生成
原来在python中tensorflow中model的实现是这样构建的:
model = models.Sequential([
base_model,
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
在tensorflow中,概率可以通过score = tf.nn.softmax(predictions[0])生成,根据官网教程
如何在 tensorflow.js 中得到这个概率?
我已经复制了下面的代码模板:
$("#predict-button").click(async function () {
if (!modelLoaded) { alert("The model must be loaded first"); return; }
if (!imageLoaded) { alert("Please select an image first"); return; }
let image = $('#selected-image').get(0);
// Pre-process the image
console.log( "Loading image..." );
let tensor = tf.browser.fromPixels(image, 3)
.resizeNearestNeighbor([224, 224]) // change the image size
.expandDims()
.toFloat()
// RGB -> BGR
let predictions = await model.predict(tensor).data();
console.log(predictions);
let top5 = Array.from(predictions)
.map(function (p, i) { // this is Array.map
return {
probability: p,
className: TARGET_CLASSES[i] // we are selecting the value from the obj
};
}).sort(function (a, b) {
return b.probability - a.probability;
}).slice(0, 2);
console.log(top5);
$("#prediction-list").empty();
top5.forEach(function (p) {
$("#prediction-list").append(`<li>${p.className}: ${p.probability.toFixed(6)}</li>`);
});
如何修改上面的代码?
输出与变量'predictions'的值相同:
Float32Array(5)
0: -2.5525975227355957
1: 7.398464679718018
2: -3.252196788787842
3: 4.710395812988281
4: -4.636396408081055
buffer: (...)
byteLength: (...)
byteOffset: (...)
length: (...)
Symbol(Symbol.toStringTag): (...)
__proto__: TypedArray
0: {probability: 7.398464679718018, className: "Sunflower"}
1: {probability: 4.710395812988281, className: "Rose"}
length: 2
__proto__: Array(0)
求助!!! 谢谢!
为了使用 softmax 函数从模型的对数中提取概率,您可以执行以下操作:
这是对数数组,也是您从模型
中获得的predictions
const logits = [-2.5525975227355957, 7.398464679718018, -3.252196788787842, 4.710395812988281, -4.636396408081055]
您可以对值数组调用 tf.softmax()
const probabilities = tf.softmax(logits)
结果:
[0.0000446, 0.9362511, 0.0000222, 0.0636765, 0.0000056]
然后如果你想得到概率最大的索引你可以使用tf.argMax()
:
const results = tf.argMax(probabilities).dataSync()[0]
结果:
1
编辑
我对 jQuery 不太熟悉,所以这可能不正确。但这是我如何按降序获得输出的概率:
let probabilities = tf.softmax(predictions).dataSync();
$("#prediction-list").empty();
probabilities.forEach(function(p, i) {
$("#prediction-list").append(
`<li>${TARGET_CLASSES[i]}: ${p.toFixed(6)}</li>`
);
});