Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nChannels not according to paper #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 49 additions & 5 deletions densenet.py
Expand Up @@ -58,14 +58,14 @@ def forward(self, x):


class DenseNet(nn.Module):
def __init__(self, growthRate, depth, reduction, nClasses, bottleneck):
def __init__(self, growthRate, depth, reduction, nClasses, bottleneck, compression):
super(DenseNet, self).__init__()

nDenseBlocks = (depth-4) // 3
if bottleneck:
nDenseBlocks //= 2

nChannels = 2*growthRate
nChannels = 2*growthRate if compression and bottleneck else 16 # They only do this for BC type
self.conv1 = nn.Conv2d(3, nChannels, kernel_size=3, padding=1,
bias=False)
self.dense1 = self._make_dense(nChannels, growthRate, nDenseBlocks, bottleneck)
Expand All @@ -84,6 +84,7 @@ def __init__(self, growthRate, depth, reduction, nClasses, bottleneck):
nChannels += nDenseBlocks*growthRate

self.bn1 = nn.BatchNorm2d(nChannels)
self.avgpool = nn.AvgPool2d(kernel_size=8)
self.fc = nn.Linear(nChannels, nClasses)

for m in self.modules():
Expand Down Expand Up @@ -111,6 +112,49 @@ def forward(self, x):
out = self.trans1(self.dense1(out))
out = self.trans2(self.dense2(out))
out = self.dense3(out)
out = torch.squeeze(F.avg_pool2d(F.relu(self.bn1(out)), 8))
out = F.log_softmax(self.fc(out))
#out = torch.squeeze(F.avg_pool2d(F.relu(self.bn1(out)), 8)) ## Why did you relu and bn again?
out = self.avgpool(8)
Copy link
Owner

@bamos bamos Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, adding self.avgpool is a stylistic change. I prefer keeping the torch.functional verison. Also shouldn't out be input the pooling layer? How is this code working?

# out = F.log_softmax(self.fc(out))
out = self.fc(out) # You can define the softmax within the loss function right?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, this part shouldn't have been changed

return out


def denseNet_40_12():
return DenseNet(12, 40, 1, 10, bottleneck=False, compression=False)

def denseNet_100_12():
return DenseNet(12, 100, 1, 10, bottleneck=False, compression=False)

def denseNet_100_24():
return DenseNet(24, 100, 1, 10, bottleneck=False, compression=False)

def denseNetBC_100_12():
return DenseNet(12, 100, 0.5, 10, bottleneck=True, compression=True)

def denseNetBC_250_24():
return DenseNet(24, 250, 0.5, 10, bottleneck=True, compression=True)

def denseNetBC_190_40():
return DenseNet(40, 190, 0.5, 10, bottleneck=True, compression=True)


''' Did this little check:
DenseNets implemented on the paper <https://arxiv.org/pdf/1608.06993.pdf>

+-------------+-------------+-------+--------------+
| Model | Growth Rate | Depth | M. of Params |
+-------------+-------------+-------+--------------+
| DenseNet | 12 | 40 | 1.02 |
+-------------+-------------+-------+--------------+
| DenseNet | 12 | 100 | 6.98 |
+-------------+-------------+-------+--------------+
| DenseNet | 24 | 100 | 27.249 |
+-------------+-------------+-------+--------------+
| DenseNet-BC | 12 | 100 | 0.769 |
+-------------+-------------+-------+--------------+
| DenseNet-BC | 24 | 250 | 15.324 |
+-------------+-------------+-------+--------------+
| DenseNet-BC | 40 | 190 | 25.624 |
+-------------+-------------+-------+--------------+

''''