Skip to content

Commit

Permalink
Making linter changes and removing un necessary files.
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 1802f8a
Author: Arafatk <arafat.da.khan@gmail.com>
Date:   Sat Mar 4 23:47:21 2017 +0530

    Removing unnecessary file writing spec.

commit 41dd55f
Author: Arafatk <arafat.da.khan@gmail.com>
Date:   Sat Mar 4 23:40:13 2017 +0530

    Adding Linter Changes to Specs.

commit 23c6833
Author: Arafatk <arafat.da.khan@gmail.com>
Date:   Sat Mar 4 23:36:48 2017 +0530

    Adding Linter changes.
  • Loading branch information
arafatkatze committed Mar 5, 2017
1 parent d8bcdc3 commit ecf1954
Show file tree
Hide file tree
Showing 33 changed files with 595 additions and 1,813 deletions.
1 change: 0 additions & 1 deletion abc

This file was deleted.

1 change: 0 additions & 1 deletion ext/sciruby/tensorflow_c/abc

This file was deleted.

53 changes: 33 additions & 20 deletions ext/sciruby/tensorflow_c/converter.py
@@ -1,30 +1,43 @@
# This file is useful for reading the contents of the ops generated by ruby.
# You can read any graph defination in pb/pbtxt format generated by ruby
# or by python and then convert it back and forth from human readable to binary format.
# or by python and then convert it back and forth from human readable to
# binary format.

import tensorflow as tf
import shutil, os
import shutil
import os
from google.protobuf import text_format
from tensorflow.python.platform import gfile

# Converts protobuf file from pbtxt to binary wire format


def pbtxt_to_graphdef(filename):
with open(filename, 'r') as f:
graph_def = tf.GraphDef()
file_content = f.read()
text_format.Merge(file_content, graph_def)
tf.import_graph_def(graph_def, name='')
tf.train.write_graph(graph_def, 'pbtxt/', 'protobuf.pb', as_text=False)
with open(filename, 'r') as f:
graph_def = tf.GraphDef()
file_content = f.read()
text_format.Merge(file_content, graph_def)
tf.import_graph_def(graph_def, name='')
tf.train.write_graph(graph_def, 'pbtxt/', 'protobuf.pb', as_text=False)

# Converts protobuf file from binary wire format to pbtxt


def graphdef_to_pbtxt(filename):
with gfile.FastGFile(filename,'rb') as f:
graph_defe = tf.GraphDef()
graph_defe.ParseFromString(f.read())
tf.import_graph_def(graph_defe, name='')
# tf.summary.FileWriter('/home/arafat/Desktop/test', graph_def = graph_defe)
tf.train.write_graph(graph_defe, 'pbtxt/', 'protobuf.pbtxt', as_text=True)
return

if (os.path.isdir("/home/arafat/Desktop/tensorflow/tensorflow.rb/ext/sciruby/tensorflow_c/pbtxt")):
shutil.rmtree("/home/arafat/Desktop/tensorflow/tensorflow.rb/ext/sciruby/tensorflow_c/pbtxt")
graphdef_to_pbtxt('abc') # here you can write the name of the file to be converted
# and then a new file will be made in pbtxt directory.
with gfile.FastGFile(filename, 'rb') as f:
graph_defe = tf.GraphDef()
graph_defe.ParseFromString(f.read())
tf.import_graph_def(graph_defe, name='')
tf.train.write_graph(graph_defe, 'pbtxt/',
'protobuf.pbtxt', as_text=True)
return


# A simple check to remove the directory if its already present.
directory = "/home/arafat/Desktop/tensorflow/tensorflow.rb/ext/sciruby/tensorflow_c/pbtxt"
if (os.path.isdir(directory)):
shutil.rmtree(directory)

# Name of the file to be converted
filename = 'int64_test.pb'
graphdef_to_pbtxt(filename)
50 changes: 0 additions & 50 deletions ext/sciruby/tensorflow_c/files/tf_tensor_helper.cc
Expand Up @@ -219,60 +219,10 @@ std::vector<TF_Tensor *> Session_run(TF_Session* graph_session, std::vector<TF_O
std::vector<TF_Tensor *> TF_Tensor_vector;
for (auto i = 0; i < array_length; ++i)
TF_Tensor_vector.push_back(outputValues_array[i]);
std::cout << TF_TensorType(TF_Tensor_vector[0]) << "<- This is my type\n";


return TF_Tensor_vector;
};

std::vector<TF_Tensor *> Session_run_2(TF_Session* graph_session, std::vector<TF_Output > inputPorts, std::vector<TF_Tensor *> inputValues, std::vector<TF_Output > outputPorts, std::vector<TF_Operation *> cTargets)
{
auto status = TF_NewStatus();

auto array_length = inputPorts.size();
static TF_Output *inputPorts_array;
inputPorts_array = new TF_Output [array_length];
for (auto i = 0; i < array_length; ++i)
inputPorts_array[i] = inputPorts[i];

array_length = outputPorts.size();
static TF_Output *outputPorts_array;
outputPorts_array = new TF_Output [array_length];
for (auto i = 0; i < array_length; ++i)
outputPorts_array[i] = outputPorts[i];

array_length = inputValues.size();
static TF_Tensor **inputValues_array;
inputValues_array = new TF_Tensor* [array_length];
for (auto i = 0; i < array_length; ++i)
inputValues_array[i] = inputValues[i];

static TF_Tensor **outputValues_array;
outputValues_array = new TF_Tensor* [array_length];

array_length = cTargets.size();
static TF_Operation **cTargets_array;
cTargets_array = new TF_Operation* [array_length];
for (auto i = 0; i < array_length; ++i)
cTargets_array[i] = cTargets[i];

TF_SessionRun(graph_session, NULL, &inputPorts_array[0], &inputValues_array[0], inputPorts.size(), &outputPorts_array[0], &outputValues_array[0], outputPorts.size(), &cTargets_array[0], cTargets.size(), NULL, status);

array_length = outputPorts.size();
std::vector<TF_Tensor *> TF_Tensor_vector;
for (auto i = 0; i < array_length; ++i)
TF_Tensor_vector.push_back(outputValues_array[i]);
std::cout << array_length << "<- This is array length\n";
long long total_elements = tensor_size(TF_Tensor_vector[0]);
std::cout << total_elements << "<- This is array laps\n";
std::cout << TF_TensorType(TF_Tensor_vector[0]) << "<- This is my type\n";



return TF_Tensor_vector;
};


TF_Tensor** TF_Tensor_array_from_given_length(int length)
{
static TF_Tensor **TF_Tensor_array;
Expand Down
Binary file removed ext/sciruby/tensorflow_c/mysore_palace.jpg
Binary file not shown.
24 changes: 11 additions & 13 deletions lib/tensorflow/graph.rb
Expand Up @@ -88,7 +88,7 @@ def const(name, value)
# everything uptil set attributes is okay but then we need reflect equivalent for ruby
def AddOperation(opspec)
opspec.name = opspec.type if opspec.name.nil?
opspec.name = opspec.type if opspec.name == ""
opspec.name = opspec.type if opspec.name == ''
cname = CString(opspec.name)
ctype = CString(opspec.type)
cdesc = Tensorflow::TF_NewOperation(c, ctype, cname)
Expand All @@ -99,13 +99,13 @@ def AddOperation(opspec)
end
end

unless opspec.inputlist.empty?
c_array = Tensorflow::TF_Output_vector.new
length = opspec.inputlist.length
opspec.inputlist.each_with_index { |value, i| c_array[i] = value.c }
c_array = Tensorflow::TF_Output_array_from_vector(c_array)
cdesc = Tensorflow::input_list_helper(cdesc, c_array, length)
end
unless opspec.inputlist.empty?
c_array = Tensorflow::TF_Output_vector.new
length = opspec.inputlist.length
opspec.inputlist.each_with_index { |value, i| c_array[i] = value.c }
c_array = Tensorflow::TF_Output_array_from_vector(c_array)
cdesc = Tensorflow.input_list_helper(cdesc, c_array, length)
end

status = Tensorflow::Status.new
opspec.attr.each do |name, value|
Expand All @@ -122,15 +122,13 @@ def AddOperation(opspec)
Tensorflow::Operation.new(Tensorflow::TF_FinishOperation(cdesc, status.c), self)
end

# How are we using a way to set attributes for string and other types.
def setattr(cdesc, status, name, value) # adding extra type for fun
def setattr(cdesc, status, name, value)
cAttrName = CString(name)
type = 'DataType' if name == 'dtype'
type = 'Tensor' if name == 'value'
type = 'int64' if name == 'channels'
type = 'int64' if name == 'channels'
type = 'DataType' if name == 'DstT'
type = 'int32_array' if name == 'size/Const'
# print name, " ", value, "\n\n"
case type
when 'string'
Tensorflow::TF_SetAttrString(cdesc, cAttrName, CString(value), value.length)
Expand Down Expand Up @@ -177,7 +175,7 @@ def setattr(cdesc, status, name, value) # adding extra type for fun
Tensorflow::TF_SetAttrTensor(cdesc, cAttrName, value.tensor, status.c)
# TODO: Insert Tensor_list, DataType_list, Bool
else
puts 'This is not working out.'
puts 'Attribute type not supported.'
end
# Shapes can be done, but will require that it be
# distinguishable from []int64. Which is fine, it
Expand Down
17 changes: 8 additions & 9 deletions lib/tensorflow/op.rb
@@ -1,9 +1,8 @@
# Const adds an operation to graph that produces value as output.
def Const(scope, value, type = nil)
value = Tensorflow::Tensor.new(value, type)
puts value, value.type_num
opspec = Tensorflow::OpSpec.new("", 'Const', 'dtype' => value.type_num, 'value' => value)
return scope.AddOperation(opspec).output(0)
value = Tensorflow::Tensor.new(value, type)
opspec = Tensorflow::OpSpec.new('', 'Const', 'dtype' => value.type_num, 'value' => value)
scope.AddOperation(opspec).output(0)
end

# A placeholder op for a value that will be fed into the computation.
Expand All @@ -17,9 +16,9 @@ def Const(scope, value, type = nil)
#
# Returns A placeholder tensor that must be replaced using the feed mechanism.
def Placeholder(scope, dtype, optionalAttr = nil)
optionalAttr = {}
optionalAttr["dtype"] = dtype
# TODO: add checks for additional attributes
opspec = Tensorflow::OpSpec.new("", 'Placeholder', 'dtype' => dtype)
return scope.AddOperation(opspec).output(0)
optionalAttr = {}
optionalAttr['dtype'] = dtype
# TODO: add checks for additional attributes
opspec = Tensorflow::OpSpec.new('', 'Placeholder', 'dtype' => dtype)
scope.AddOperation(opspec).output(0)
end
98 changes: 49 additions & 49 deletions lib/tensorflow/operation.rb
@@ -1,65 +1,65 @@
class Tensorflow::Output
attr_accessor :index, :operation
def c
Tensorflow.input(operation.c, index)
end
attr_accessor :index, :operation
def c
Tensorflow.input(operation.c, index)
end

def dataType
Tensorflow::TF_OperationOutputType(c())
end
def dataType
Tensorflow::TF_OperationOutputType(c)
end
end

# Operation that has been added to the graph.
class Tensorflow::Operation
attr_accessor :c, :g
# @!attribute c
# contains the graph representation.
# @!attribute g
# A reference to the Graph to prevent it from being GCed while the Operation is still alive.
attr_accessor :c, :g
# @!attribute c
# contains the graph representation.
# @!attribute g
# A reference to the Graph to prevent it from being GCed while the Operation is still alive.

def initialize(c_representation, graph)
self.c = c_representation
self.g = graph
end
def initialize(c_representation, graph)
self.c = c_representation
self.g = graph
end

def name
# May need to convert this to a ruby string
Tensorflow::TF_OperationName(self.c)
end
def name
# May need to convert this to a ruby string
Tensorflow::TF_OperationName(c)
end

def type
# May need to convert this to a ruby string
Tensorflow::TF_OperationOpType(self.c)
end
def type
# May need to convert this to a ruby string
Tensorflow::TF_OperationOpType(c)
end

def num_outputs
# May need to convert this to ruby int
Tensorflow::TF_OperationNumOutputs(self.c)
end
def num_outputs
# May need to convert this to ruby int
Tensorflow::TF_OperationNumOutputs(c)
end

# OutputListSize returns the size of the list of Outputs that is produced by a
# named output of op.
#
# An Operation has multiple named outputs, each of which produces either
# a single tensor or a list of tensors. This method returns the size of
# the list of tensors for a specific output of the operation, identified
# by its name.
def output_list_size(output)
cname = CString(output)
status = Tensorflow::Status.new
Tensorflow::TF_OperationOutputListLength(self.c, cname, status.c)
end
# OutputListSize returns the size of the list of Outputs that is produced by a
# named output of op.
#
# An Operation has multiple named outputs, each of which produces either
# a single tensor or a list of tensors. This method returns the size of
# the list of tensors for a specific output of the operation, identified
# by its name.
def output_list_size(output)
cname = CString(output)
status = Tensorflow::Status.new
Tensorflow::TF_OperationOutputListLength(c, cname, status.c)
end

def output(i)
out = Tensorflow::Output.new
out.operation = self
out.index = i
out
end
def output(i)
out = Tensorflow::Output.new
out.operation = self
out.index = i
out
end
end

class Tensorflow::Input
attr_accessor :Index, :Operations
def initialize
end
attr_accessor :Index, :Operations
def initialize
end
end

0 comments on commit ecf1954

Please sign in to comment.