从node.js调用Sagemaker Tensorflow Resnet50端点

时间:2019-02-28 12:15:48

标签: python node.js keras amazon-sagemaker

问题

我想知道node.js中与该Python代码等效的内容:

from keras.preprocessing import image
from PIL import Image
from keras.applications.resnet50 import preprocess_input

raw_img = image.load_img("some/path").resize((224, 224), Image.NEAREST)
img = preprocess_input(image.img_to_array(raw_img))

上下文

我将Keras的ResNet50模型上传到SageMaker端点。我可以使用以下代码从Python调用它:

import json
import boto3
import numpy as np
import io

client = boto3.client('runtime.sagemaker')

from keras.preprocessing import image
from PIL import Image
from keras.applications.resnet50 import preprocess_input

raw_img = image.load_img("some/path").resize((224, 224), Image.NEAREST)
img = preprocess_input(image.img_to_array(raw_img))

response = client.invoke_endpoint(
  EndpointName="SAGEMAKER_ENDPOINT_NAME",
  Body=json.dumps({ "instances": [ img.tolist() ] }),
  ContentType="application/json"
)

现在我需要在node.js中执行相同的操作。我想出了如何使用aws-sdk到达终点:

import * as aws from 'aws-sdk';
const sageMaker = new aws.SageMakerRuntime({
  region: 'ap-northeast-1'
});
sageMaker.invokeEndpoint({
  EndpointName: endpointName,
  Body: input,
  ContentType: "application/json",
}, (error, res) => {
  if (error) { return reject(error); }
  // YEAH
})

但是我无法弄清楚如何生成input json,也就是这个python代码段的等效内容:

from keras.preprocessing import image
from PIL import Image
from keras.applications.resnet50 import preprocess_input

raw_img = image.load_img("some/path").resize((224, 224), Image.NEAREST)
img = preprocess_input(image.img_to_array(raw_img))

有没有实现相同目标的库,还是我需要重新发明轮子?

1 个答案:

答案 0 :(得分:0)

答案是:我不得不重新设计轮子。 这是到达端点的工作需要(对于ResNet50网络而言):

  • 将源图像解码为像素
  • 转换为3个频道(删除
  • 调整为224x224
  • 将像素归一化为[-1,1]
  • 转换为3D数组(SageMaker中的默认格式)

这是我最终得到的代码(它使用打字稿):

import * as tf from '@tensorflow/tfjs';

interface INormalizationOptions {
  numberOfChannels: number;
  imageSize: number;
}

interface IImagePixels {
  width: number, height: number, numberOfChannels: number,
  pixels: ndarray
}

export async function preprocessImage(filePath: string, o: INormalizationOptions) {
  const img = await getImagePixels(filePath);

  // Converts the ndarray to tensor, while ignoring extra channels
  const rawTensor = imageToTensor(img, o);

  console.log(`Size of image: ${img.width} ${img.height}`)
  console.log(`Number of color components actually read: ${img.pixels.data.length} (expected: ${img.width*img.height*img.numberOfChannels})`)
  console.log(`Number of pixels: ${img.width*img.height}`);

  // Normalizes the Pixels from [0,255] to [-1,1]
  const normalizedTensor: tf.Tensor3D = rawTensor.toFloat().sub(255/2).div(255/2);

  // Resizes the inage Size to square of IMAGE_SIZE
  const alignCorner = true;
  const resizedInput = tf.image.resizeBilinear(
    normalizedTensor,
    [o.imageSize, o.imageSize],
    alignCorner
  )

  const reshapedTensor: tf.Tensor3D = resizedInput.reshape([ o.imageSize, o.imageSize, o.numberOfChannels ])

  return tensor3dToArray3d(reshapedTensor);
}

这使用了一些实用程序功能。

getImagePixels

async function getImagePixels(filePath: string) {
  const _getPixels = require('get-pixels');
  return new Promise<IImagePixels>( (resolve, reject) => {
    _getPixels( filePath, (error: Error | null, pixels: ndarray) => {
      if (error) { return reject(error); }

      resolve({
        width: pixels.shape[0],
        height: pixels.shape[1],
        numberOfChannels: pixels.shape[2],
        pixels: pixels
      })
    });
  })
}

imageToTensor

import ndarray from 'ndarray';

function imageToTensor(img: IImagePixels, o: INormalizationOptions) {
  const rawTensorValues = new Int32Array(img.width * img.height * o.numberOfChannels);
  const rawTensor = tf.tensor3d(rawTensorValues, [ img.width, img.height, o.numberOfChannels ], 'int32');

  // This only uses the first CHANNEL_ND
  for (let row=0; row<img.height; row++) {
    for (let col=0; col<img.width; col++){
      for (let channel =0; channel<o.numberOfChannels; channel++) {
        const pixel = img.pixels.get(row,col,channel);
        if (!isNumeric(pixel)) { throw new Error(`Bad pixel: ${pixel}`) }
        const offset = row * img.width * img.numberOfChannels + col * img.numberOfChannels + channel
        rawTensorValues[offset] = pixel;
      }
    }
  }

  return rawTensor;
}

tensor3dToArray3d

async function tensor3dToArray3d(t: tf.Tensor3D) {
  const dataAs3dArray = new Array<number[][]>();
  const data = await t.data();

  const expectedSizeOfArray = t.shape[0] * t.shape[1] * t.shape[2];


  console.log(`Got vector of size ${t.shape[0]} ${t.shape[1]} ${t.shape[2]}, that is, ${expectedSizeOfArray} data`);
  console.log(`Actual size of data: ${data.length}`);

  for (let i=0; i<t.shape[0]; i++) {
    dataAs3dArray[i] = new Array<number[]>();
    for (let j=0; j<t.shape[1]; j++) {
      dataAs3dArray[i][j] = new Array<number>();
      for (let k=0; k<t.shape[2]; k++ ) {
        const ijk =
            i * t.shape[0] * t.shape[1] * t.shape[2]
          + j * t.shape[1] * t.shape[2]
          + k;

        const datum = Math.random(); // Number( data[ijk] );
        if (!isNumeric(datum)) { throw new Error(`Invalid datum at ${i},${j},${k}: ${datum}`) }
        dataAs3dArray[i][j][k] = datum;
      }
    }
  }
  return dataAs3dArray;
}

function isNumeric(n: any) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}