for i, num_layers in enumerate(block_config):
block = _DenseBlock(num_layers=num_layers, num_input_features=num_features, bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate)
self.features.add_module('denseblock%d' % (i + 1), block)
I want to apply a classifier for the block ( likes deep supervision) . How can I implement it? Thanks
You can load the pretrained model and then take apart the network and add your classifiers and have these outputs returned in your forward pass. You can take apart the network by iterating through named_children or children.
I haven’t tested this, but this is the general idea.
import torch.nn as nn
import torchvision.models as model_zoo
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.dnet = model_zoo.densenet121(pretrained=True)
def forward(self, input):
dblock_outs = []
x = input
for name, module in self.dnet["features"].named_children():
if "denseblock" in name:
x = module(x)
dblock_outs.append(x)
else:
x = module(x)
x = self.dnet["classifier"](x)
return x, dblock_outs
model = Net()
out, dblock_outs_for_classifiers = model(minibatch)