Git Product home page Git Product logo

matrix-capsules-pytorch's Introduction

Matrix-Capsules-pytorch

This is a pytorch implementation of Matrix Capsules with EM routing

In Capsules.py, there are two implemented classes: PrimaryCaps and ConvCaps. The ClassCapsules in the paper is actually a special case of ConvCaps with whole receptive field, transformation matrix sharing and Coordinate Addition.

In train.py, I define a CapsNet in the paper using classes in Capsules.py, and could be used to train a model for MNIST dataset.

Train a small CapsNet on MNIST

python train.py -batch_size=64 -lr=2e-2 -num_epochs=5 -r=1 -print_freq=5.

Note:

  • more args can be found in utils.py, and if you want to change A,B,C,D, go to line 62 of train.py
  • m and lambda schedule need to be changed if you want to train a capsnet with r=2 or 3. The default schedule make capsnet does not converge in those cases.

Results

The test accuracy is around 97.6% after 1 epoch and 98.7% after 2 epochs of training with a small Capsule of A,B,C,D,r = 64,8,16,16,1. After 30 epochs of training, the best acc is around 99.3%. More results on different configurations are welcomed.

TODO

  • using more matrix operation rather than for iteration in E-step of Capsules.py.
  • make capsules work when height_in != width_in
  • find better lambda/m schedule to speed up the convergence.

matrix-capsules-pytorch's People

Contributors

yuxianmeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

matrix-capsules-pytorch's Issues

Isn't here a bug?

pose = pose.view(b,16,self.B,width_in,width_in).permute(0,2,3,4,1).contiguous() #b,B,12,12,16

pose's shape is (b, self.B, H, W), how can it be reshaped into ( b, 16, self.B, H, W) in the next line????? why does no one point this out? no error reported????

some code question

when Debug at here y_range = (max(floor((j-self.K)/self.stride)+1,0),min(j//self.stride+1,width_out))will print error TypeError: range() integer start argument expected, got float.

loss nan when r>1

Thanks for your work! However, when I set r = 2, the loss turned into nan. Have you met this problem? How to fix it?

Overfitting with r>1

Hello. I'm trying to overfit to a toy batch with r=2. With batchsize >1 I am unable to overfit with r>1, though everything works with r=1. In particular, the network outputs the same results for all images in the batch.

there may be some mistakes in your code

  1. for the loss funtion in 'class CapsNet' (train.py)
    you use mask = u.ge(0).float() to complete the max operation. but after a check about this function(tensor.ge) i think we should not us this function. because we want to preserve the value which >=0 and change the value which <0 into 0. If we use tensor.ge, we would turn all the value which is >=0 into 1. It's fault i think.
    2.loss = ((masku)2).sum()/b - m2 #float
    for this code, why we should mask
    u and why we should - m**2. I could not find an introduction in the equ5 in the paper.

Thanks for your kind help!

Couldn't use full power of CUDA cores

When trying your demo on my 1050 Ti, this model only reaches 30% usage of CUDA cores and runs really slow. Meantime, another traditional CapsNet can reach 100% usage. Does this mean a performance problem of your model?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.