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

Model zoo (fix #3) #110

Merged
merged 20 commits into from
Jul 2, 2015
Merged

Model zoo (fix #3) #110

merged 20 commits into from
Jul 2, 2015

Conversation

beam2d
Copy link
Member

@beam2d beam2d commented Jun 30, 2015

This PR fixes #3 by adding functions.caffe.CaffeFunction, which loads a pretrained Caffe model and emulates its subnetwork. The usage is written in the reference documentation, though I also show it here. This is an example of loading BVLC reference CaffeNet and computes fc8 blob from data blob.

# Load the model
func = CaffeFunction('path/to/bvlc_reference_caffenet.caffemodel')

# Minibatch of size 10
x_data = numpy.ndarray((10, 3, 227, 227), dtype=numpy.float32)
...  # (Fill the minibatch here)

# Forward the pretrained net
x = Variable(x_data)
y, = func(inputs={'data': x}, outputs=['fc8'])

The inputs dict describes how to initialize the blobs by variables. The outputs list indicates which blobs to be output as variables. Also user can extracts parameterized layers from CaffeFunction.fs, which is a FunctionSet object containing Linear or Convolution2D objects.

'chainer.optimizers',
'chainer.requirements',
'chainer.utils'],
package_data={'chainer.requirements': ['cuda-requirements.txt']},
install_requires=['numpy',
'protobuf',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we install protobuf in py3 environment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can install it. CI passed it, too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which version of protobuf is required?

@unnonouno
Copy link
Member

Do you have a unit test for this function?

@beam2d
Copy link
Member Author

beam2d commented Jul 1, 2015

I'm thinking about the test, though I have no good plan, yet.

@beam2d
Copy link
Member Author

beam2d commented Jul 1, 2015

I'll write an example of loading and using a Caffe reference model.

@unnonouno unnonouno self-assigned this Jul 1, 2015
@beam2d
Copy link
Member Author

beam2d commented Jul 1, 2015

I added an example. However, it outputs too low accuracy. I will try to resolve this issue.

return decorator


class CaffeFunction(function.Function):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this function behave when backward is called?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function just inserts a computational graph on top of the given variables in __call__, instead of inserting itself into the graph. Backward computation is done by each node in this graph, so CaffeFunction itself does not need to handle the backward.

@beam2d
Copy link
Member Author

beam2d commented Jul 2, 2015

I updated the example code.

@beam2d
Copy link
Member Author

beam2d commented Jul 2, 2015

I updated the code. It achieves the same accuracy as the original Caffe implementation. (Actually I've fixed testing mode and RGB order)

@delta2323 delta2323 modified the milestone: v1.1.0 Jul 2, 2015
@okuta okuta added the cat:feature Implementation that introduces new interfaces. label Jul 2, 2015
@unnonouno
Copy link
Member

Please chmod +x download_model.py



parser = argparse.ArgumentParser(
descriptor='Download a Caffe reference model')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/descriptor/description/


.. note::

This function only supports Python 2.7, since the compiled module for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens on py3?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code to raise an exception in Python 3. I also wrote this behavior in the docstring.

@beam2d
Copy link
Member Author

beam2d commented Jul 2, 2015

I added a warning routine on unknown layer types.

@unnonouno
Copy link
Member

LGTM

unnonouno added a commit that referenced this pull request Jul 2, 2015
@unnonouno unnonouno merged commit e2ee9d6 into master Jul 2, 2015
@itortoise
Copy link

I load my caffemodel and then Traceback ZeroDivisionError as:

File "", line 1, in
File "chainer/functions/caffe/caffe_function.py", line 119, in init
meth(self, layer)
File "chainer/functions/caffe/caffe_function.py", line 228, in _setup_convolution
nobias=not param.bias_term)
File "chainer/functions/convolution_2d.py", line 92, in init
0, wscale * math.sqrt(1. / (self.kh * self.kw * in_channels)),
ZeroDivisionError: float division by zero

@beam2d
Copy link
Member Author

beam2d commented Jul 6, 2015

@itortoise Thank you for reporting an error. Would you open another issue for this?

@delta2323 delta2323 deleted the model-zoo branch July 6, 2015 07:01
@beam2d
Copy link
Member Author

beam2d commented Jul 7, 2015

@itortoise #148 may fix your problem.

niboshi pushed a commit to niboshi/chainer that referenced this pull request Jun 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:feature Implementation that introduces new interfaces.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import models of Model Zoo
5 participants