Created
April 11, 2018 14:00
-
-
Save ota42y/344a987222e50c1d9bc58b9e0af97633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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