Skip to content

Instantly share code, notes, and snippets.

@ota42y
Created April 11, 2018 14:00
Show Gist options
  • Save ota42y/344a987222e50c1d9bc58b9e0af97633 to your computer and use it in GitHub Desktop.
Save ota42y/344a987222e50c1d9bc58b9e0af97633 to your computer and use it in GitHub Desktop.
tesorflow_host = 'host_address'
lib_dir = File.join('/work/proto_ruby')
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
Dir.glob('/work/proto_ruby/**/*.rb'){ |path| require_relative(path) unless File.directory? path }
require 'chunky_png'
float_values = Array.new(28 * 28)
img = ChunkyPNG::Image.from_file('/work/image.png')
28.times do |i|
28.times do |j|
float_values[i + j * 28] = img[i,j].to_f / 255
end
end
request = Tensorflow::Serving::PredictRequest.new
request.model_spec = Tensorflow::Serving::ModelSpec.new(name: "mnist", signature_name: "predict_images")
images_proto = Tensorflow::TensorProto.new
images_proto.dtype = :DT_FLOAT
shape = Tensorflow::TensorShapeProto.new
shape.dim << Tensorflow::TensorShapeProto::Dim.new(size: 1)
shape.dim << Tensorflow::TensorShapeProto::Dim.new(size: 28)
shape.dim << Tensorflow::TensorShapeProto::Dim.new(size: 28)
shape.dim << Tensorflow::TensorShapeProto::Dim.new(size: 1)
images_proto.tensor_shape = shape
float_values.each { |v| images_proto.float_val << v }
request.inputs['images'] = images_proto
require 'grpc'
stub = Tensorflow::Serving::PredictionService::Stub.new(tesorflow_host, :this_channel_is_insecure)
ret = stub.predict(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment