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

Branch for python 3 #177

Open
wants to merge 3 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
120 changes: 60 additions & 60 deletions convert.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
#!/usr/bin/env python

import os
import sys
import numpy as np
import argparse
from kaffe import KaffeError, print_stderr
from kaffe.tensorflow import TensorFlowTransformer


def fatal_error(msg):
print_stderr(msg)
exit(-1)


def validate_arguments(args):
if (args.data_output_path is not None) and (args.caffemodel is None):
fatal_error('No input data path provided.')
if (args.caffemodel is not None) and (args.data_output_path is None):
fatal_error('No output data path provided.')
if (args.code_output_path is None) and (args.data_output_path is None):
fatal_error('No output path specified.')


def convert(def_path, caffemodel_path, data_output_path, code_output_path, phase):
try:
transformer = TensorFlowTransformer(def_path, caffemodel_path, phase=phase)
print_stderr('Converting data...')
if caffemodel_path is not None:
data = transformer.transform_data()
print_stderr('Saving data...')
with open(data_output_path, 'wb') as data_out:
np.save(data_out, data)
if code_output_path:
print_stderr('Saving source...')
with open(code_output_path, 'wb') as src_out:
src_out.write(transformer.transform_source())
print_stderr('Done.')
except KaffeError as err:
fatal_error('Error encountered: {}'.format(err))


def main():
parser = argparse.ArgumentParser()
parser.add_argument('def_path', help='Model definition (.prototxt) path')
parser.add_argument('--caffemodel', help='Model data (.caffemodel) path')
parser.add_argument('--data-output-path', help='Converted data output path')
parser.add_argument('--code-output-path', help='Save generated source to this path')
parser.add_argument('-p',
'--phase',
default='test',
help='The phase to convert: test (default) or train')
args = parser.parse_args()
validate_arguments(args)
convert(args.def_path, args.caffemodel, args.data_output_path, args.code_output_path,
args.phase)


if __name__ == '__main__':
main()
#!/usr/bin/env python
import os
import sys
import numpy as np
import argparse
from kaffe import KaffeError, print_stderr
from kaffe.tensorflow import TensorFlowTransformer
def fatal_error(msg):
print_stderr(msg)
exit(-1)
def validate_arguments(args):
if (args.data_output_path is not None) and (args.caffemodel is None):
fatal_error('No input data path provided.')
if (args.caffemodel is not None) and (args.data_output_path is None):
fatal_error('No output data path provided.')
if (args.code_output_path is None) and (args.data_output_path is None):
fatal_error('No output path specified.')
def convert(def_path, caffemodel_path, data_output_path, code_output_path, phase):
try:
transformer = TensorFlowTransformer(def_path, caffemodel_path, phase=phase)
print_stderr('Converting data...')
if caffemodel_path is not None:
data = transformer.transform_data()
print_stderr('Saving data...')
with open(data_output_path, 'wb') as data_out:
np.save(data_out, data)
if code_output_path:
print_stderr('Saving source...')
with open(code_output_path, 'wb') as src_out:
src_out.write(transformer.transform_source())
print_stderr('Done.')
except KaffeError as err:
fatal_error('Error encountered: {}'.format(err))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('def_path', help='Model definition (.prototxt) path')
parser.add_argument('--caffemodel', help='Model data (.caffemodel) path')
parser.add_argument('--data-output-path', help='Converted data output path')
parser.add_argument('--code-output-path', help='Save generated source to this path')
parser.add_argument('-p',
'--phase',
default='test',
help='The phase to convert: test (default) or train')
args = parser.parse_args()
validate_arguments(args)
convert(args.def_path, args.caffemodel, args.data_output_path, args.code_output_path,
args.phase)
if __name__ == '__main__':
main()
8 changes: 4 additions & 4 deletions kaffe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .graph import GraphBuilder, NodeMapper
from .errors import KaffeError, print_stderr

from . import tensorflow
from .graph import GraphBuilder, NodeMapper
from .errors import KaffeError, print_stderr
from . import tensorflow
Binary file added kaffe/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/errors.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/errors.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/graph.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/graph.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/layers.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/layers.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/shapes.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/shapes.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/transformers.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/__pycache__/transformers.cpython-36.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion kaffe/caffe/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .resolver import get_caffe_resolver, has_pycaffe
from .resolver import get_caffe_resolver, has_pycaffe
Binary file added kaffe/caffe/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/caffe/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/caffe/__pycache__/caffe_pb2.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/caffe/__pycache__/caffepb.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/caffe/__pycache__/caffepb.cpython-36.pyc
Binary file not shown.
Binary file added kaffe/caffe/__pycache__/resolver.cpython-35.pyc
Binary file not shown.
Binary file added kaffe/caffe/__pycache__/resolver.cpython-36.pyc
Binary file not shown.
5,479 changes: 5,479 additions & 0 deletions kaffe/caffe/caffe_pb2.py

Large diffs are not rendered by default.

96 changes: 48 additions & 48 deletions kaffe/caffe/resolver.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import sys

SHARED_CAFFE_RESOLVER = None

class CaffeResolver(object):
def __init__(self):
self.import_caffe()

def import_caffe(self):
self.caffe = None
try:
# Try to import PyCaffe first
import caffe
self.caffe = caffe
except ImportError:
# Fall back to the protobuf implementation
from . import caffepb
self.caffepb = caffepb
show_fallback_warning()
if self.caffe:
# Use the protobuf code from the imported distribution.
# This way, Caffe variants with custom layers will work.
self.caffepb = self.caffe.proto.caffe_pb2
self.NetParameter = self.caffepb.NetParameter

def has_pycaffe(self):
return self.caffe is not None

def get_caffe_resolver():
global SHARED_CAFFE_RESOLVER
if SHARED_CAFFE_RESOLVER is None:
SHARED_CAFFE_RESOLVER = CaffeResolver()
return SHARED_CAFFE_RESOLVER

def has_pycaffe():
return get_caffe_resolver().has_pycaffe()

def show_fallback_warning():
msg = '''
------------------------------------------------------------
WARNING: PyCaffe not found!
Falling back to a pure protocol buffer implementation.
* Conversions will be drastically slower.
* This backend is UNTESTED!
------------------------------------------------------------

'''
sys.stderr.write(msg)
import sys
SHARED_CAFFE_RESOLVER = None
class CaffeResolver(object):
def __init__(self):
self.import_caffe()
def import_caffe(self):
self.caffe = None
try:
# Try to import PyCaffe first
import caffe
self.caffe = caffe
except ImportError:
# Fall back to the protobuf implementation
from . import caffe_pb2
self.caffepb = caffe_pb2
show_fallback_warning()
if self.caffe:
# Use the protobuf code from the imported distribution.
# This way, Caffe variants with custom layers will work.
self.caffepb = self.caffe.proto.caffe_pb2
self.NetParameter = self.caffepb.NetParameter
def has_pycaffe(self):
return self.caffe is not None
def get_caffe_resolver():
global SHARED_CAFFE_RESOLVER
if SHARED_CAFFE_RESOLVER is None:
SHARED_CAFFE_RESOLVER = CaffeResolver()
return SHARED_CAFFE_RESOLVER
def has_pycaffe():
return get_caffe_resolver().has_pycaffe()
def show_fallback_warning():
msg = '''
------------------------------------------------------------
WARNING: PyCaffe not found!
Falling back to a pure protocol buffer implementation.
* Conversions will be drastically slower.
* This backend is UNTESTED!
------------------------------------------------------------
'''
sys.stderr.write(msg)
14 changes: 7 additions & 7 deletions kaffe/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

class KaffeError(Exception):
pass

def print_stderr(msg):
sys.stderr.write('%s\n' % msg)
import sys
class KaffeError(Exception):
pass
def print_stderr(msg):
sys.stderr.write('%s\n' % msg)
Loading