Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support with python3.6 and tensorflow1.10 which depends on protobuf3.6 #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
# Python cache
*.pyc

.*

MNIST_data/
bvlc_alexnet
2 changes: 1 addition & 1 deletion convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def convert(def_path, caffemodel_path, data_output_path, code_output_path, phase
if code_output_path:
print_stderr('Saving source...')
with open(code_output_path, 'wb') as src_out:
src_out.write(transformer.transform_source())
src_out.write(transformer.transform_source().encode('utf-8'))
print_stderr('Done.')
except KaffeError as err:
fatal_error('Error encountered: {}'.format(err))
Expand Down
8 changes: 5 additions & 3 deletions examples/mnist/finetune_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 32


def gen_data(source):
while True:
indices = range(len(source.images))
indices = list(range(len(source.images)))
random.shuffle(indices)
for i in indices:
image = np.reshape(source.images[i], (28, 28, 1))
label = source.labels[i]
yield image, label


def gen_data_batch(source):
data_gen = gen_data(source)
while True:
Expand All @@ -37,13 +39,13 @@ def gen_data_batch(source):
ip2 = net.layers['ip2']
pred = tf.nn.softmax(ip2)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(ip2, labels), 0)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=ip2), 0)
opt = tf.train.RMSPropOptimizer(0.001)
train_op = opt.minimize(loss)

with tf.Session() as sess:
# Load the data
sess.run(tf.initialize_all_variables())
sess.run(tf.global_variables_initializer())
net.load('mynet.npy', sess)

data_gen = gen_data_batch(mnist.train)
Expand Down
2,886 changes: 1,715 additions & 1,171 deletions kaffe/caffe/caffepb.py → kaffe/caffe/caffe_pb2.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions kaffe/caffe/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def import_caffe(self):
self.caffe = caffe
except ImportError:
# Fall back to the protobuf implementation
from . import caffepb
self.caffepb = caffepb
from . import caffe_pb2
self.caffepb = caffe_pb2
show_fallback_warning()
if self.caffe:
# Use the protobuf code from the imported distribution.
Expand Down
4 changes: 2 additions & 2 deletions kaffe/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def __str__(self):
# In case of convolutions, this corresponds to the weights.
data_shape = node.data[0].shape if node.data else '--'
out_shape = node.output_shape or '--'
s.append('{:<20} {:<30} {:>20} {:>20}'.format(node.kind, node.name, data_shape,
tuple(out_shape)))
s.append('{:<20} {:<30} {:>20} {:>20}'.format(node.kind, node.name, str(data_shape),
str(out_shape)))
return '\n'.join(s)


Expand Down
11 changes: 5 additions & 6 deletions kaffe/tensorflow/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def layer_decorated(self, *args, **kwargs):


class Network(object):

def __init__(self, inputs, trainable=True):
# The input nodes for this network
self.inputs = inputs
Expand Down Expand Up @@ -59,7 +58,7 @@ def load(self, data_path, session, ignore_missing=False):
data_dict = np.load(data_path).item()
for op_name in data_dict:
with tf.variable_scope(op_name, reuse=True):
for param_name, data in data_dict[op_name].iteritems():
for param_name, data in data_dict[op_name].items():
try:
var = tf.get_variable(param_name)
session.run(var.assign(data))
Expand All @@ -74,7 +73,7 @@ def feed(self, *args):
assert len(args) != 0
self.terminals = []
for fed_layer in args:
if isinstance(fed_layer, basestring):
if isinstance(fed_layer, str):
try:
fed_layer = self.layers[fed_layer]
except KeyError:
Expand Down Expand Up @@ -124,7 +123,7 @@ def conv(self,
# Convolution for a given input and kernel
convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding)
with tf.variable_scope(name) as scope:
kernel = self.make_var('weights', shape=[k_h, k_w, c_i / group, c_o])
kernel = self.make_var('weights', shape=[k_h, k_w, c_i.value / group, c_o])
if group == 1:
# This is the common-case. Convolve the input without any further complications.
output = convolve(input, kernel)
Expand Down Expand Up @@ -177,7 +176,7 @@ def lrn(self, input, radius, alpha, beta, name, bias=1.0):

@layer
def concat(self, inputs, axis, name):
return tf.concat(concat_dim=axis, values=inputs, name=name)
return tf.concat(inputs, axis=axis, name=name)

@layer
def add(self, inputs, name):
Expand All @@ -203,7 +202,7 @@ def fc(self, input, num_out, name, relu=True):

@layer
def softmax(self, input, name):
input_shape = map(lambda v: v.value, input.get_shape())
input_shape = [v.value for v in input.get_shape()]
if len(input_shape) > 2:
# For certain models (like NiN), the singleton spatial dimensions
# need to be explicitly squeezed, since they're not broadcast-able
Expand Down
4 changes: 2 additions & 2 deletions kaffe/tensorflow/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, op, *args, **kwargs):

def format(self, arg):
'''Returns a string representation for the given value.'''
return "'%s'" % arg if isinstance(arg, basestring) else str(arg)
return "'%s'" % arg if isinstance(arg, str) else str(arg)

def pair(self, key, value):
'''Returns key=formatted(value).'''
Expand All @@ -53,7 +53,7 @@ def pair(self, key, value):
def emit(self):
'''Emits the Python source for this node.'''
# Format positional arguments
args = map(self.format, self.args)
args = [self.format(arg) for arg in self.args]
# Format any keyword arguments
if self.kwargs:
args += [self.pair(k, v) for k, v in self.kwargs]
Expand Down