将png转换为Tensor tensorflow.js

时间:2018-11-09 18:49:45

标签: javascript node.js png buffer tensorflow.js

我目前正在尝试找出如何使用tensorflow.js将输入png转换为张量,以便将其输入到模型中进行训练。目前,我正在捕获图像,将其保存在本地,使用fs.readFileSync读取它,然后创建一个缓冲区。我有点迷惑的地方是将缓冲区的值从0-244标准化为0-1,然后从该缓冲区创建一个张量,将其作为X arg馈入到model.fit函数中。我也不太了解如何设置标签文件并将其正确转换为Y arg的缓冲区。 (https://js.tensorflow.org/api/0.11.2/#tf.Model.fit)对于使用tensorflow.js将图像正确使用/配置到张量中的任何见解,将不胜感激。

回购在这里; https://github.com/Durban-Designer/Fighter-Ai

用于在data.js中加载本地图像的代码;

const tf = require('@tensorflow/tfjs');
const assert = require('assert');
const IMAGE_HEADER_BYTES = 32;
const IMAGE_HEIGHT = 600;
const IMAGE_WIDTH = 800;
const IMAGE_FLAT_SIZE = IMAGE_HEIGHT * IMAGE_WIDTH;

function loadHeaderValues(buffer, headerLength) {
  const headerValues = [];
  for (let i = 0; i < headerLength / 4; i++) {
    headerValues[i] = buffer.readUInt32BE(i * 4);
  }
  return headerValues;
}

...
...
class Dataset {
 async loadLocalImage(filename) {
 const buffer = fs.readFileSync(filename);

 const headerBytes = IMAGE_HEADER_BYTES;
 const recordBytes = IMAGE_HEIGHT * IMAGE_WIDTH;

 const headerValues = loadHeaderValues(buffer, headerBytes);
 console.log(headerValues, buffer);
 assert.equal(headerValues[5], IMAGE_HEIGHT);
 assert.equal(headerValues[4], IMAGE_WIDTH);

 const images = [];
 let index = headerBytes;
 while (index < buffer.byteLength) {
  const array = new Float32Array(recordBytes);
  for (let i = 0; i < recordBytes; i++) {
    // Normalize the pixel values into the 0-1 interval, from
    // the original 0-255 interval.
    array[i] = buffer.readUInt8(index++) / 255;
  }
  images.push(array);
 }

 assert.equal(images.length, headerValues[1]);
 return images;
 }
}
module.exports = new Dataset();

app.js中的图像捕获循环;

const ioHook = require("iohook");
const tf = require('@tensorflow/tfjs');
var screenCap = require('desktop-screenshot');
require('@tensorflow/tfjs-node');
const data = require('./src/data');
const virtKeys = require('./src/virtKeys');
const model = require('./src/model');
var dir = __dirname;
var paused = true;
var loopInterval,
  image,
  imageData,
  result

ioHook.on('keyup', event => {
  if (event.keycode === 88) {
    if (paused) {
      paused = false;
      gameLoop();
    } else {
      paused = true;
    }
  }
});

ioHook.start();
function gameLoop () {
  if (!paused) {
    screenCap(dir + '\\image.png', {width: 800, height: 600, quality: 60}, function (error, complete) {
      if (error) {
        console.log(error);
      } else {
        imageData = await data.getImage(dir + '\\image.png')
        console.log(imageData);
        result = model.predict(imageData, {batchSize: 4});
        console.log(result);
        gameLoop();
      }
    })
  }
}

我知道我在这里使用model.predict,我想让实际图像在张量部分起作用,然后在回购中的train-tensor.js中找出标签和model.fit()。我没有用于培训的任何实际工作代码,因此我没有将其包含在此问题中,对不起,如果造成任何混乱。

再次感谢您!

编辑最终工作代码

const { Image, createCanvas } = require('canvas');
const canvas = createCanvas(800, 600);
const ctx = canvas.getContext('2d');

async function loadLocalImage (filename) {
  try {
    var img = new Image()
    img.onload = () => ctx.drawImage(img, 0, 0);
    img.onerror = err => { throw err };
    img.src = filename;
    image = tf.fromPixels(canvas);
    return image;
  } catch (err) {
    console.log(err);
  }
}
...
...
async getImage(filename) {
    try {
      this.image = await loadLocalImage(filename);
    } catch (error) {
      console.log('error loading image', error);
    }
    return this.image;
  }

1 个答案:

答案 0 :(得分:1)

tensorflowjs已经为此提供了一种方法:tf.fromPixels()

您只需要将图像加载到可接受的类型(ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement)中即可。

您的图片加载Promise不会返回任何内容,因为您的异步函数不会返回任何内容,而只是您的回调,要解决此问题,您需要自己创建和解决一个Promise:

const imageGet = require('get-image-data');
async loadLocalImage(filename) {
    return new Promise((res,rej)=>{
    imageGet(filename, (err, info) => {
      if(err){
         rej(err);
         return;
      }
      const image = tf.fromPixels(info.data)
      console.log(image, '127');
      res(image);
    });
  }