Skip to content

Instantly share code, notes, and snippets.

@zhreshold
Last active June 26, 2018 01:52
Show Gist options
  • Save zhreshold/1a19b06548f69abca9aea0102a6a020d to your computer and use it in GitHub Desktop.
Save zhreshold/1a19b06548f69abca9aea0102a6a020d to your computer and use it in GitHub Desktop.
GluonCV Darknet 19
class Darknet(gluon.HybridBlock):
def __init__(self, spec, classes=1000, batch_norm=True, **kwargs):
super(Darknet, self).__init__(**kwargs)
filters = spec['filters']
kernels = spec['kernels']
with self.name_scope():
self.features = nn.HybridSequential()
first_stage = True
for sf, sk in zip(filters, kernels):
if not first_stage:
# maxpool
self.features.add(nn.MaxPool2D(pool_size=2, strides=2))
first_stage = False
for f, k in zip(sf, sk):
assert k in [1, 3], "Unexpected kernel size: {}".format(k)
pad = (k - 1) // 2
self.features.add(nn.Conv2D(f, kernel_size=k, padding=pad))
if batch_norm:
self.features.add(nn.BatchNorm())
self.features.add(nn.LeakyReLu(0.1))
# output
self.output = nn.Conv2D(classes, kernel_size=1, padding=0)
def hybrid_forward(self, F, x):
x = self.output(self.features(x))
return F.Pooling(x, kernel=(7, 7), global_pool=True, pool_type='avg')
darknet_spec = {
19: {
'filters': [[32], [64], [128, 64, 128], [256, 128, 256], [512, 256, 512, 256, 512], [1024, 512, 1024, 512, 1024]]
'kernels': [[3], [3], [3, 1, 3], [3, 1, 3], [3, 1, 3, 1, 3], [3, 1, 3, 1, 3]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment