Giter VIP home page Giter VIP logo

compositionalnets's Introduction

Compositional Convolutional Neural Networks: A Deep Architecture with Innate Robustness to Partial Occlusion [CVPR-2020]

alt text

Compositional Convolutional Neural Networks: A Deep Architecture with Innate Robustness to Partial Occlusion
Adam Kortylewski, Ju He, Qing Liu, Alan Yuille
CVPR 2020

Release Notes

This is a port of our original code from Tensorflow to PyTorch. The code is a lot faster and cleaner compared to the original code base. The results are a little different from the ones reported in the paper. In particular, the performance is a little lower for low occlusion and higher for stronger occlusion. On average the results are slightly better than reported in the paper.

For now, we provide pretrained models for CompositionalNets trained from the VGG-16 pool5 layer. Training CompositionalNets for other backbones and layers should be possible but has not been extensively tested so far.

Installation

The code uses Python 3.6 and it is tested on PyTorch GPU version 1.2, with CUDA-10.0 and cuDNN-7.5.

Setup CompNet Virtual Environment

virtualenv --no-site-packages <your_home_dir>/.virtualenvs/CompNet
source <your_home_dir>/.virtualenvs/CompNet/bin/activate

Clone the project and install requirements

git clone https://github.com/AdamKortylewski/CompositionalNets.git
cd CompositionalNets
pip install -r requirements.txt

Download models

  • Download pretrained CompNet weights from here and copy them inside the models folder.

  • The repositroy contains a few images for the demo script. If you want to evaluate on the full datasets used in our paper you need to download the data here and copy it inside the data folder.

Demo

CompNets require a tight crop of the object in the image. We provide sample images in the demo folder which are taken from MS-COCO.

Run the demo code

python Code/demo.py 

Our demo script classifies the images from the demo folder, extracts the predicted location of occluders, and writes the results back into the demo folder.

Evaluate the classification performance of a model

Run the following command in the terminal to evaluate a model on the full test dataset:

python Code/test.py 

Evaluate the occluder localization performance of a model

If you want to test occluder localization run:

python Code/eval_occlusion_localization.py

This will output qualitative occlusion localization results for each image and a quantitative analysis over all images as ROC curve.

Initializing CompositionalNet Parameters

We initialize CompositionalNets (i.e. the vMF kernels and mixture models) by clustering the training data. In particular, we initialize the vMF kernels by clustering the feature vectors:

python Initialization_Code/vMF_clustering.py

Furthermore, we initialize the mixture models by EM-type learning. The initial cluster assignment for the EM-type learning is computed based on the similarity of the vMF encodings of the training images. To compute the similarity matrices use:

python Initialization_Code/comptSimMat.py

As this process takes some time we provide precomputed similarity matrices here, you need to copy them into the 'models/init_vgg/' folder. Afterwards you can compute the initialization of the mixture models by executing:

python Initialization_Code/Learn_mix_model_vMF_view.py

Referencing CompositionalNets

Please cite the following papers if you use the code directly or indirectly in your research projects.

@inproceedings{CompNet:CVPR:2020,
  title = {Compositional Convolutional Neural Networks: A Deep Architecture with Innate Robustness to Partial Occlusion},
  author = {Kortylewski, Adam and He, Ju and Liu, Qing and and Yuille, Alan},
  booktitle = {Proceedings IEEE Conf. on Computer Vision and Pattern Recognition (CVPR)},
  month = jun,
  year = {2020},
  month_numeric = {6}
}

@article{kortylewski2021compositional,
  title={Compositional convolutional neural networks: A robust and interpretable model for object recognition under occlusion},
  author={Kortylewski, Adam and Liu, Qing and Wang, Angtian and Sun, Yihong and Yuille, Alan},
  journal={International Journal of Computer Vision},
  volume={129},
  number={3},
  pages={736--760},
  year={2021},
  publisher={Springer}
}

Relation to our Prior Work

With very small modifications this code would also enable the learning and testing of CompositionalNets with Bernoulli distributions as proposed in our previous work:

@inproceedings{Combining:WACV:2020,
title = {Combining Compositional Models and Deep Networks For Robust Object Classification under Occlusion},
author = {Kortylewski, Adam and Liu, Qing and Wang, Huiyu and Zhang, Zhishuai and Yuille, Alan},
booktitle = {IEEE Winter Conference on Applications of Computer Vision (WACV)},
month = mar,
year = {2020},
month_numeric = {3}
}

Contact

If you have any questions you can contact Adam Kortylewski.

Acknowledgement

We thank Zhishuai Zhang for helping us speed up and clean the code for the release.

compositionalnets's People

Contributors

adamkortylewski avatar tacju avatar xd7479 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

compositionalnets's Issues

About getImg

i don't know if there's a little problem in geImg(helpers.py) function.

if dataset == 'pascal3d+': if occ_level == 'ZERO': filelist = data_path + 'pascal3d+_occ/' + category + '_imagenet_train' + '.txt' img_dir = data_path + 'pascal3d+_occ/TRAINING_DATA/' + category + '_imagenet'

it seems even if i set bool_train_with_occluders = True still no occluded images will be considerd in training process because occ_level = 'one' 'five' or 'nine' are not considered.

Empty clutter_feats in demo.py

I'm trying to experiment with the code by running python Code/demo.py but get the following error:

Traceback (most recent call last):
  File "Code/demo.py", line 34, in <module>
    net = Net(extractor, weights, vMF_kappa, occ_likely, mix_models, bool_mixture_bg=bool_mixture_model_bg,compnet_type=compnet_type, num_mixtures=num_mixtures, vc_thresholds=cfg.MODEL.VC_THRESHOLD)
  File "/content/CompositionalNets/Code/model.py", line 48, in __init__
    compnet_type, vMF_kappa)
  File "/content/CompositionalNets/Code/model.py", line 129, in get_clutter_model
    vMF_kappa, max_it=150, tol=1e-10)
  File "/content/CompositionalNets/Code/vMFMM.py", line 45, in fit
    centers_i.append(np.random.choice(rdn_index))
  File "mtrand.pyx", line 1125, in mtrand.RandomState.choice
ValueError: 'a' cannot be empty unless no samples are taken

This happens when trying to fit the 'vmf' clutter model at line 129 in get_clutter_model by passing an empty list of clutter_feats. Any chance I could get help with this?

My environment:
OS: Ubuntu 18.04.3 LTS
Release: 18.04
Python: 3.6.9
Cuda: Release 10.1, V10.1.243
PyTorch: 1.2.0

Error when training from scratch

The model runs into following error if training from scratch and not running train.py:

Total imgs for test of occ_level ZERO and occ_type  4341
Testing
  0%|                                                                                         | 0/4341 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/home/CompositionalNets/Code/test.py", line 138, in <module>
    acc,scores = test(models=nets, test_data=test_imgset, batch_size=1)
  File "/home/CompositionalNets/Code/test.py", line 40, in test
    output, *_ = models[0](input)
  File "/home/toshi/base/lib/python3.9/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/CompositionalNets/Code/model.py", line 62, in forward
    mix_likeli = self.pointwiseinference(vmf_activations)
  File "/home/toshi/base/lib/python3.9/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/CompositionalNets/Code/model.py", line 223, in forward
    foreground = foreground.reshape((-1, self.num_classes,
RuntimeError: shape '[-1, 10, 4, 1, 7, 12]' is invalid for input of size 3612

Steps to recreate -

  1. Add to (top of) vMF_clustering.py, comptSimMat.py, Learn_mix_model_vMF_view.py, Code/test.py files:
import os, sys 
p = os.path.abspath('.') 
sys.path.insert(1, p) 
  1. In comptSimMat.py, change (due to TypeError) -
imgset = Imgset(imgs, masks, labels, imgLoader, bool_square_images=False,bool_cutout=False,bool_pytorch=bool_pytorch) 

To -

imgset = Imgset(imgs, masks, labels, imgLoader, bool_square_images=False) 
  1. mv models/init_vgg/dictionary_vgg/dictionary_pool5_512.pickle models/init_vgg/dictionary_vgg/dictionary_pool5.pickle

Code for detection?

Hi Adam,

Your latest paper in IJCV uses compositional voting for generalizing to object detection also. I am not able to find the template code in this repository for the detection layer.

Can you share that?

question about the mix models

Hello, thanks for your work.
When we download your models,we found that each category has a corresponding model,
such as"mmodel_aeroplane_K4_FEATDIM512_pool4_specific_view.pickle" but we didn't find the method to generate the model in the code
We want to know how to generate such model so that we can add new categories.

torch version

There is a problem with the old version of torch. When I try to install the requirements.txt I get the following error:

Looking in links: https://download.pytorch.org/whl/torch_stable.html
ERROR: Could not find a version that satisfies the requirement torch===1.2.0 (from versions: none)
ERROR: No matching distribution found for torch===1.2.0

Which version do you recommend using?

pretrained_model

Sorry for bother you again!
After you give the initialization code, we try to train with our own data. When we run the demo.py with our own initialization model, we find this question:
size mismatch for mix_model: copying a param with shape torch.Size([48, 512, 41, 91]) from checkpoint, the shape in current model is torch.Size([46, 512, 23, 31]).
size mismatch for pointwiseinference.mix_model: copying a param with shape torch.Size([48, 512, 41, 91]) from checkpoint, the shape in current model is torch.Size([46, 512, 23, 31]).
terminate called without an active exception

We guess the pretrained_model best.pth we download from your github may not mathcing with our model . Can you provide the code to generate this pretrained_model.
Thank you very much!

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.