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

Python net specification #1733

Closed
wants to merge 121 commits into from
Closed

Python net specification #1733

wants to merge 121 commits into from

Conversation

longjon
Copy link
Contributor

@longjon longjon commented Jan 15, 2015

Protobuf is powerful, but is not a general-purpose language, and can be cumbersome in its current use for writing down complicated chains of functions, i.e., nets. See previous discussion at (nonexhaustively) #1290 and #1169.

Python is a general-purpose language with clean syntax and an intimate relationship with Caffe. This PR allows net specification as (domain-specific) Python code (and consequently, effortless programmatic net construction).

Currently this is just a proof-of-concept sketch, which lightly sprinkles Python magic over the caffe_pb2 protobuf interface.

LeNet can be written as:

from caffe import layers as L
from caffe import params as P

data = L.data(batch_size=64, backend=P.data.LMDB, source='examples/mnist/mnist_train_lmdb')
conv1 = L.convolution(data, kernel_size=5, num_output=20)
pool1 = L.pooling(conv1, kernel_size=2, stride=2, pool=P.pooling.MAX)
conv2 = L.convolution(pool1, kernel_size=5, num_output=50)
pool2 = L.pooling(conv2, kernel_size=2, stride=2, pool=P.pooling.MAX)
ip1 = L.relu(L.inner_product(pool2, num_output=500))
prob = L.softmax(L.inner_product(ip1, num_output=10))

And can be instantiated via this hackiness, to be cleaned up later:

with open('tmp.prototxt', 'w') as f:
    print >>f, prob.to_proto()
net = caffe.Net('tmp.prototxt')

Things to think about:

  • Perhaps there should be a way to construct Nets directly from Layers, without going through protobuf.
  • The magic in use here means syntax doesn't really gets checked until the Net is created.
  • This will need to be rebased on Layer type is a string #1694 and more significantly Treat bottoms and params more uniformly? #1474.
  • It would be nicer if the functions constructed here simply were Nets rather than having to make Nets out of them.
  • There is no way to control blob names right now, but there are ways to make that happen...

@netheril96
Copy link
Contributor

Perhaps there should be a way to construct Nets directly from Layers, without going through protobuf.

This is because the constructor of net unnecessarily involves I/O. It should instead take a NetParameter as argument, instead of (or in addition to) a filename as string.

@jeffdonahue
Copy link
Contributor

This looks nifty. What happens with multiple tops? (e.g. in your example, can you do data, label = L.data(...)?)

the constructor of net unnecessarily involves I/O. It should instead take a NetParameter as argument, instead of (or in addition to) a filename as string.

I agree, there should definitely be a way to construct a pycaffe Net by passing a caffe_pb2.NetParameter thing (if you have one) rather than writing it to a file -- PRs welcome (or I'll probably try to do this at some point if nobody else does).

@longjon
Copy link
Contributor Author

longjon commented Jan 22, 2015

Re: multiple tops, I had in mind your syntax @jeffdonahue, but I don't think I implemented it here yet.

Yes, there should be a way to construct Nets from NetParameters, although that was not what I had in mind with the comment above, but rather a way to construct Nets without involving protobuf at all. In this sketch, one constructs some special Python class which specifies a net and then turns it into a Net. Why doesn't one just construct a Net to begin with?

shelhamer and others added 25 commits January 24, 2015 18:27
* A sample code was added.
* `slice_dim` and `slice_point` attributes were explained.
[docs] brief explanation of SLICE layer's attributes
Correct 'epochs' to 'iterations'
Next: release candidater
set the right rpath for tools and examples respectively

thanks for the report @mees!
[build] fix dynamic linking of tools
… was overwritten with symlink created at build time and installed with install(DIRECTORY ...)
… systems).

This commit specifies Python2 with which cpp_lint.py works :-)
APPLE was misspelled in Line 27
fixes: cpp_lint.py fails silently with Python3
shelhamer and others added 19 commits March 7, 2015 19:06
…b-shapes

  extract_features preserves feature shape
...and fix up detector quote convention.
- download CaffeNet if it isn't there
- switch to caffe.Net
- reshape net for single input
- explain param, bias indexing
- update output for N-D blobs
- N-D blob parameter dimensions
- add filtering section to net surgery example
- make Gaussian blur and Sobel edge kernels
- alter biases
- do flat assignment instead of reshape and explain memory layout
- make dir for surgery files
- add a little explanation
- solve from Python and the command line
- time scikit-learn and caffe
- fix test iterations (number of instances / batch size)
call forward to warm-start the demo so the first request isn't slow.
Update leveldb include variable name to match FindLevelDB.cmake
Pycaffe fixes and example reformation
Fixup AccuracyLayer like SoftmaxLossLayer in BVLC#1970
@longjon
Copy link
Contributor Author

longjon commented Mar 10, 2015

Closing to reopen against master.

@longjon longjon force-pushed the python-net-spec branch 2 times, most recently from 22ef4fe to 23ee7ab Compare March 10, 2015 02:01
@kevin-li
Copy link

Is there a way to modify existing network from within python? Or is there a way to edit prototxt by coding instead of manully changing them?

My experiment requires freezing/training different layers, how can I do this through python code?

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.