# XNOR-Net-PyTorch **Repository Path**: shayxu/XNOR-Net-PyTorch ## Basic Information - **Project Name**: XNOR-Net-PyTorch - **Description**: PyTorch Implementation of XNOR-Net - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-03 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # XNOR-Net-Pytorch This a PyTorch implementation of the [XNOR-Net](https://github.com/allenai/XNOR-Net). I implemented Binarized Neural Network (BNN) for: | Dataset | Network                 | Accuracy                   | Accuracy of floating-point | |----------|:-------------------------|:----------------------------|:---------------------------| | MNIST | LeNet-5 | 99.23% | 99.34% | | CIFAR-10 | Network-in-Network (NIN) | 86.28% | 89.67% | | ImageNet | AlexNet | Top-1: 44.87% Top-5: 69.70% | Top-1: 57.1% Top-5: 80.2% | ## MNIST I implemented the LeNet-5 structure for the MNIST dataset. I am using the dataset reader provided by [torchvision](https://github.com/pytorch/vision). To run the training: ```bash $ cd /MNIST/ $ python main.py ``` Pretrained model can be downloaded [here](https://drive.google.com/open?id=0B-7I62GOSnZ8R3Jzd0ozdzlJUk0). To evaluate the pretrained model: ```bash $ cp /MNIST/models/ $ python main.py --pretrained models/LeNet_5.best.pth.tar --evaluate ``` ## CIFAR-10 I implemented the NIN structure for the CIFAR-10 dataset. You can download the training and validation datasets [here](https://drive.google.com/open?id=0B-7I62GOSnZ8Z0ZCVXFtVnFEaTg) and uncompress the .zip file. To run the training: ```bash $ cd /CIFAR_10/ $ ln -s data $ python main.py ``` Pretrained model can be downloaded [here](https://drive.google.com/open?id=0B-7I62GOSnZ8UjJqNnR1V0dMbWs). To evaluate the pretrained model: ```bash $ cp /CIFAR_10/models/ $ python main.py --pretrained models/nin.best.pth.tar --evaluate ``` ## ImageNet I implemented the AlexNet for the ImageNet dataset. You can download the preprocessed dataset [here](https://drive.google.com/uc?export=download&id=0B-7I62GOSnZ8aENhOEtESVFHa2M) and uncompress it. However, to use this dataset, you have to install [Caffe](https://github.com/BVLC/caffe) first. Support with [torchvision](https://github.com/pytorch/vision) data reader will soon be added. If you need the function now, please contact ```jiecaoyu@umich.edu```. To set up the dataset: ```bash $ cd /ImageNet/networks/ $ ln -s data ``` ### AlexNet To train the network: ```bash $ cd /ImageNet/networks/ $ python main.py ``` Pretrained model can be downloaded [here](https://drive.google.com/open?id=0B-7I62GOSnZ8bUtZUXdZLVBtUDQ). To evaluate the pretrained model: ```bash $ cp /ImageNet/networks/ $ python main.py --resume alexnet.baseline.pth.tar --evaluate ``` The training log can be found [here](https://raw.githubusercontent.com/jiecaoyu/XNOR-Net-PyTorch/master/ImageNet/networks/log.baseline). ## Todo - Generate new dataset without caffe support. - NIN for ImageNet. ## Notes ### Gradients of scaled sign function In the paper, the gradient in backward after the scaled sign function is ![equation](http://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20C%7D%7B%5Cpartial%20W_i%7D%3D%5Cfrac%7B%5Cpartial%20C%7D%7B%5Cpartial%20%7B%5Cwidetilde%7BW%7D%7D_i%7D%20%28%5Cfrac%7B1%7D%7Bn%7D+%5Cfrac%7B%5Cpartial%20sign%28W_i%29%7D%7B%5Cpartial%20W_i%7D%5Ccdot%20%5Calpha%20%29) However, this equation is actually inaccurate. The correct backward gradient should be ![equation](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20C%7D%7B%5Cpartial%20W_%7Bi%7D%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%7D%20%5Ccdot%20sign%28W_%7Bi%7D%29%20%5Ccdot%20%5Csum_%7Bj%3D1%7D%5E%7Bn%7D%5B%5Cfrac%7B%5Cpartial%20C%7D%7B%5Cpartial%20%5Cwidetilde%7BW%7D_j%7D%20%5Ccdot%20sign%28W_j%29%5D%20+%20%5Cfrac%7B%5Cpartial%20C%7D%7B%5Cpartial%20%5Cwidetilde%7BW%7D_i%7D%20%5Ccdot%20%5Cfrac%7Bsign%28W_i%29%7D%7BW_i%7D%20%5Ccdot%20%5Calpha) Details about this correction can be found in the [notes](notes/notes.pdf) (section 1).