tensorflow.js向模型中添加许多图像样本会填充视频卡内存并导致崩溃

yftpprvb  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(386)

我指的是这个例子
https://github.com/tensorflow/tfjs-examples/tree/master/webcam-transfer-learning
https://storage.googleapis.com/tfjs-examples/webcam-transfer-learning/dist/index.html
您可以看到,演示可以拍摄各种场景,训练模型,然后预测网络摄像头场景的情况。
我将演示代码更改为自己的代码,并使用文件输入上传了大量图片作为输入示例。
当我上传许多(300-400)图片224*244图片时,每个图片的大小约为70kb,我的图形卡内存(rx 570 4gb)将被填满,然后崩溃。
这是我的演示视频
https://www.youtube.com/watch?v=irnd29lcqi0
错误消息 Uncaught (in promise) Error: Failed to compile fragment shader. 这是我的代码:

  1. class ControllerDataset {
  2. constructor(numClasses) {
  3. this.numClasses = numClasses;
  4. }
  5. /**
  6. * Adds an example to the controller dataset.
  7. * @param {Tensor} example A tensor representing the example. It can be an image,
  8. * an activation, or any other type of Tensor.
  9. * @param {number} label The label of the example. Should be a number.
  10. */
  11. addExample(example, label) {
  12. // One-hot encode the label.
  13. const y = tf.tidy(
  14. () => tf.oneHot(tf.tensor1d([label]).toInt(), this.numClasses));
  15. if (this.xs == null) {
  16. // For the first example that gets added, keep example and y so that the
  17. // ControllerDataset owns the memory of the inputs. This makes sure that
  18. // if addExample() is called in a tf.tidy(), these Tensors will not get
  19. // disposed.
  20. this.xs = tf.keep(example);
  21. this.ys = tf.keep(y);
  22. } else {
  23. const oldX = this.xs;
  24. this.xs = tf.keep(oldX.concat(example, 0));
  25. const oldY = this.ys;
  26. this.ys = tf.keep(oldY.concat(y, 0));
  27. oldX.dispose();
  28. oldY.dispose();
  29. y.dispose();
  30. }
  31. }
  32. }
  33. var truncatedMobileNet;
  34. const NUM_CLASSES = 3;
  35. const controllerDataset = new ControllerDataset(NUM_CLASSES);
  36. async function addMultiSampleFromInputfile(files, label) {
  37. for (let index = 0; index < files.length; index++) {
  38. const file = files[index];
  39. let image = await readFileToImageElement(file);
  40. let { sourceImageTensor, imageTensorNormalize } = getTensorImgFromElement(image)
  41. controllerDataset.addExample(truncatedMobileNet.predict(imageTensorNormalize), label);
  42. sourceImageTensor.dispose();
  43. imageTensorNormalize.dispose();
  44. }
  45. }
  46. // Loads mobilenet and returns a model that returns the internal activation
  47. // we'll use as input to our classifier model.
  48. async function loadTruncatedMobileNet() {
  49. const url = document.getElementById("MobileNetUrl").value
  50. const mobilenet = await tf.loadLayersModel(url);
  51. // Return a model that outputs an internal activation.
  52. const layer = mobilenet.getLayer('conv_pw_13_relu');
  53. return tf.model({ inputs: mobilenet.inputs, outputs: layer.output });
  54. }
  55. function getTensorImgFromElement(element) {
  56. const imageTensor = tf.browser.fromPixels(element);
  57. const processedImgTensor = tf.tidy(() => imageTensor.expandDims(0).toFloat().div(127).sub(1));
  58. return { sourceImageTensor: imageTensor, imageTensorNormalize: processedImgTensor }
  59. }
  60. function readFileToImageElement(file) {
  61. return new Promise((resolve, reject) => {
  62. let reader = new FileReader();
  63. reader.onload = function() {
  64. let image = document.createElement('img');
  65. image.src = this.result;
  66. image.onload = function() {
  67. resolve(image)
  68. }
  69. }
  70. reader.readAsDataURL(file);
  71. });
  72. }
  73. loadTruncatedMobileNet().then(model => {
  74. truncatedMobileNet = model;
  75. })
  76. // add multi sample from html input file
  77. addMultiSmapleBtn.onclick = () => {
  78. // label value is 0 or 1 or 2
  79. if (truncatedMobileNet)
  80. addMultiSampleFromInputfile(imagefiles.files, parseInt(label.value))
  81. }
iugsix8n

iugsix8n1#

试试这个:

  1. async function readFileToImageElement(file) {
  2. const img = new Image()
  3. img.src = URL.createObjectURL(file)
  4. await img.decode()
  5. return img
  6. }
  7. // when the image is not needed anymore call:
  8. URL.revokeObjectURL(img.src)

相关问题