Así que estoy usando tensorflow JS y python para entrenar modelos. Ahora estoy trabajando en el sitio web para que los médicos abstractos puedan cargar una imagen de resonancia magnética y obtener la predicción. Aquí está mi código:
<script> async function LoadModels(){ model = undefined; model = await tf.loadLayersModel("http://127.0.0.1:5500/modelsBrain/modelBrain.json"); const image = document.getElementById("image"); const image1 = tf.browser.fromPixels(image); const image2 = tf.reshape(image1, [1,200,200,3]); const prediction = model.predict(image2); const softmaxPred = prediction.softmax().dataSync(); alert(softmaxPred); let top5 = Array.from(softmaxPred) .map(function (p, i) { return { probability: p, className: TARGET_CLASSES_BRAIN[i] }; }).sort(function (a, b) { return b.probability - a.probability; }).slice(0, 4); const pred = [[]]; top5.forEach(function (p) { pred.push(p.className, p.probability); alert(p.className + ' ' + p.probability); }); } const fileInput = document.getElementById("file-input"); const image = document.getElementById("image"); function getImage() { if(!fileInput.files[0]) throw new Error("Image not found"); const file = fileInput.files[0]; const reader = new FileReader(); reader.onload = function (event) { const dataUrl = event.target.result; const imageElement = new Image(); imageElement.src = dataUrl; imageElement.onload = async function () { image.setAttribute("src", this.src); image.setAttribute("height", this.height); image.setAttribute("width", this.width); await LoadModels(); }; }; reader.readAsDataURL(file); } fileInput.addEventListener("change", getImage); </script>Este error ocurre no todos los (!) Live Server abiertos. Estoy confundido, ¿cuál parece ser el problema?
El error CONTEXT_LOST_WEBGL es del 99 % debido a la poca memoria de la GPU. ¿Qué tipo de HW tiene disponible? Alternativamente, puede probar el backend WASM que ejecuta el cálculo en la CPU y no requiere recursos de GPU.
Por cierto, no está desasignando sus tensores en ningún lugar, por lo que si está ejecutando esto en un bucle para múltiples entradas, tiene una pérdida de memoria masiva. Pero si ya se produce un error en la primera entrada, su GPU simplemente no es lo suficientemente buena para este modelo.