├── LBCNN.py └── README.md /LBCNN.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.nn import Module, Parameter, Conv2d 3 | 4 | class LBCNN(Module): 5 | def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1): 6 | super(LBCNN, self).__init__() 7 | self.nInputPlane = in_channels 8 | self.nOutputPlane = out_channels 9 | self.kW = kernel_size 10 | self.LBCNN = Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, False) 11 | self.LBCNN.weight.requires_grad=False 12 | #init weight 13 | numElements = self.nInputPlane*self.nOutputPlane*self.kW*self.kW 14 | index = torch.randperm(numElements) 15 | self.LBCNN.weight.copy = (torch.Tensor(self.nOutputPlane,self.nInputPlane,self.kW,self.kW).uniform_(0, 1)) 16 | temp = (torch.bernoulli(self.LBCNN.weight.copy)*2-1).view(-1) 17 | for i in range(1,numElements/2): 18 | temp[index[i]] =0; 19 | self.LBCNN.weight.copy = temp.view(self.nOutputPlane,self.nInputPlane,self.kW,self.kW) 20 | self.LBCNN.weight = Parameter(self.LBCNN.weight.copy) 21 | 22 | def forward(self, input): 23 | return self.LBCNN.forward(input) 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pytorch-LBCNN 2 | Local Binary Convolutional Neural Networks by pytorch 3 | 4 | ## 1.Original torch project 5 | 6 | https://github.com/juefeix/lbcnn.torch 7 | 8 | ## 2.This project by pytorch 9 | 10 | Only including the Convolutional layer through Local Binary,it was named LBCNN.py 11 | 12 | ## 3.Usage 13 | 14 | Making Local Binary Convolutional layer instead of the original Convolutional layer 15 | 16 | e.g: 17 | 18 | from LBCNN import LBCNN #module 19 | 20 | .... 21 | 22 | .... 23 | 24 | self.conv = LBCNN(in_channels,out_channels,3,stride,1) #define LBCNN in model file 25 | 26 | ## 4.See blog 27 | 28 | http://blog.csdn.net/yyqq7226741/article/details/78308036 29 | ## If I can help you, please give me a star :star2::star2::star2: 30 | --------------------------------------------------------------------------------