I am trying to implement Bayesian CNN using Mc Dropout on Pytorch,
the main idea is that by applying dropout at test time and running over many forward passes , you get predictions from a variety of different models.
I’ve found an application of the Mc Dropout and I really did not get how they applied this method and how exactly they did choose the correct prediction from the list of predictions
here is the code
def mcdropout_test(model):
model.train()
test_loss = 0
correct = 0
T = 100
for data, target in test_loader:
if args.cuda:
data, target = data.cuda(), target.cuda()
data, target = Variable(data, volatile=True), Variable(target)
output_list =
for i in xrange(T):
output_list.append(torch.unsqueeze(model(data), 0))
output_mean = torch.cat(output_list, 0).mean(0)
test_loss += F.nll_loss(F.log_softmax(output_mean,dim=1), labels, reduction=‘sum’).data # sum up batch loss
pred = output_mean.data.max(1, keepdim=True)[1] # get the index of the max log-probability
correct += pred.eq(target.data.view_as(pred)).cpu().sum()test_loss /= len(test_loader.dataset) print('\nMC Dropout Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format( test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset)))
train()
mcdropout_test()
I have replaced
data, target = Variable(data, volatile=True), Variable(target)
by adding
with torch.no_grad() at the beginning
:
And this is how I have defined my CNN
class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
self.dropout = nn.Dropout(p=0.3)nn.init.xavier_uniform_(self.conv1.weight) nn.init.constant_(self.conv1.bias, 0.0) nn.init.xavier_uniform_(self.conv2.weight) nn.init.constant_(self.conv2.bias, 0.0) nn.init.xavier_uniform_(self.fc1.weight) nn.init.constant_(self.fc1.bias, 0.0) nn.init.xavier_uniform_(self.fc2.weight) nn.init.constant_(self.fc2.bias, 0.0) nn.init.xavier_uniform_(self.fc3.weight) nn.init.constant_(self.fc3.bias, 0.0) def forward(self, x): x = self.pool(F.relu(self.dropout(self.conv1(x)))) # recommended to add the relu x = self.pool(F.relu(self.dropout(self.conv2(x)))) # recommended to add the relu x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(self.dropout(x))) x = self.fc3(self.dropout(x)) return x
Can anyone help me to get the right implementation of the Monte Carlo Dropout method on CNN?