KRI*_*HAH 3 javascript google-chrome tensorflow.js
调用预测函数时浏览器中的 Tensorflow.js 错误
我正在使用 Node.js 来运行 webapp。这是我包含的脚本,我在 Chrome 中运行 Node.js 并且无法解决错误。
该项目有 7 个类作为输出,在形状为 1x7 的输出中是密集层。
https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js
https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js
https://code.jquery.com/jquery-3.3.1.slim.min.js
Run Code Online (Sandbox Code Playgroud)
这是我的 JavaScript 文件。
$(document).on('change', '#file', function() {
let reader = new FileReader();
reader.onload= function(){
let dataUrl = reader.result;
$('#selected-image').attr('src',dataUrl);
$('#type1-predict').empty();
$('#type2-predict').empty();
$('#type3-predict').empty();
$('#type4-predict').empty();
$('#type5-predict').empty();
$('#type6-predict').empty();
$('#type7-predict').empty();
}
let file = $('#file').prop('files')[0];
reader.readAsDataURL(file)
});
const CANCER_CLASSES = {
0:"Actinic Keratoses",
1:"Basal cell carcinoma",
2:"Benign keratosis",
3:"Dermatofibroma",
4:"Melanoma",
5:"Melanocytic nevi",
6:"Vascular skin",
}
let model;
(async function(){
model= await tf.loadLayersModel('http://localhost:81/model/model.json');
$('#pro').hide()
})();
$(document).on('click', '#predict-button', async function() {
let image = $('#selected-image').get(0);
let tensor =
tf.browser.fromPixels(image)
.resizeNearestNeighbor([224,224])
.toFloat()
.expandDims();
let prediction = await model.predict(tensor).data();
let top3 = Array.from(prediction)
.map(function(p,i){
return {
probab: p,
classname:CANCER_CLASSES[i]
};
}).sort(function(a,b){
return b.probab-a.probab;
}).slice(0,4);
$("#type1-predict").empty();
top3.forEach(function(p){
$('#type1-predict').append(`<li>${p.classname}:
${p.probab.tpFixed(6)}
</li>`);
});
});
Run Code Online (Sandbox Code Playgroud)
这是 HTML 文件的片段
<body>
<div id="pro" class="progress progress-bar progress-bar-striped progress-
bar-animated"></div>
<input type="file" id="image-selector">
<button id="predict-button">Predict</button>
<p style="font-weight:bold">Presentation</p>
<p>Actinic Keratoses : <span id="type1-predict"></span></p>
<p>Basal cell carcinoma: <span id="type2-predict"></span></p>
<p>Benign keratosis: <span id="type3-predict"></span></p>
<p>Dermatofibroma: <span id="type4-predict"></span></p>
<p>Melanoma: <span id="type5-predict"></span></p>
<p>Melanocytic nevi : <span id="type6-predict"></span></p>
<p>Vascular skin: <span id="type7-predict"></span></p>
<img id="selected-image" src="">
Run Code Online (Sandbox Code Playgroud)
你能帮我解决以下错误吗:
webgl_util.ts:203 Uncaught (in promise) Error: Requested texture size
[0x0] is invalid.
at Fr (webgl_util.ts:203)
at oi (gpgpu_util.ts:126)
at ui (gpgpu_util.ts:173)
at t.createUnsignedBytesMatrixTexture (gpgpu_context.ts:134)
at t.acquireTexture (texture_manager.ts:71)
at t.acquireTexture (backend_webgl.ts:2472)
at t.uploadToGPU (backend_webgl.ts:2407)
at t.getTexture (backend_webgl.ts:566)
at t.fromPixels (backend_webgl.ts:254)
at t.fromPixels (engine.ts:599)
Run Code Online (Sandbox Code Playgroud)
问题
当Tensorflow.js试图图像转换成张经tf.browser.fromPixels,它读取的width和height从DOM元素。由于您的情况未设置这些值,因此您会收到错误消息。
解决方案
你必须给img标签一个width和height属性,以便 Tensorflow.js 知道你的图像的大小:
<img id="selected-image" src="" width="..." height="...">
Run Code Online (Sandbox Code Playgroud)
当前存储库中有一个未解决的问题描述了这个问题。将来可能会通过提供更好的错误消息来解决此问题。
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |