Skip to content

3D support, Deconvolution layer and installation instruction #26

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

Open
wants to merge 7 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
@@ -0,0 +1,4 @@
*~
*.swp
.venv
__pycache__
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ The layer support isn't as complete as in https://github.com/marvis/pytorch-caff
* softmax (axis)
* local response norm (local_size, alpha, beta)

Dependencies: protobuf with Python bindings, including `protoc` binary in `PATH`.

PRs to enable other layers or layer params are very welcome (see the definition of the `modules` dictionary in the code)!

License is MIT.

## Installation Instruction
### Python Environment
This converter is currently working on python3.8 in Ubuntu. To install, create a virtual environment using ```python3 -m vev .venv```. Activate the environment using ```source .venv/bin/activate```. Lastly install the requirements using ```pip install -r requirements.txt```.

### Protobuf Compiler
Dependencies: protobuf with Python bindings, including `protoc` binary in `PATH`.

To install protobuf in ubuntu run: ```sudo apt install protobuf-compiler```.

## Dump weights to PT or HDF5
```shell
# prototxt and caffemodel from https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md
Expand Down
56 changes: 56 additions & 0 deletions blob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from functools import reduce


class Blob(object):
AssignmentAdapter = type(
"",
(object,),
dict(
shape=property(lambda self: self.contents.shape),
__setitem__=lambda self, indices, values: setattr(self, "contents", values),
),
)

def __init__(self, data=None, diff=None, numpy=False):
self.data_ = data if data is not None else Blob.AssignmentAdapter()
self.diff_ = diff if diff is not None else Blob.AssignmentAdapter()
self.shape_ = None
self.numpy = numpy

def reshape(self, *args):
self.shape_ = args

def count(self, *axis):
return reduce(lambda x, y: x * y, self.shape_[slice(*(axis + [-1])[:2])])

@property
def data(self):
if self.numpy and torch.is_tensor(self.data_):
self.data_ = self.data_.detach().cpu().numpy()
return self.data_

@property
def diff(self):
if self.numpy and torch.is_tensor(self.diff_):
self.diff_ = self.diff_.detach().cpu().numpy()
return self.diff_

@property
def shape(self):
return self.shape_ if self.shape_ is not None else self.data_.shape

@property
def num(self):
return self.shape[0]

@property
def channels(self):
return self.shape[1]

@property
def height(self):
return self.shape[2]

@property
def width(self):
return self.shape[3]
Loading