|
| 1 | +from mxnet.gluon.model_zoo import vision |
| 2 | +from mxnet.gluon import nn |
| 3 | +import mxnet as mx |
| 4 | +from mxnet import gluon |
| 5 | + |
| 6 | + |
| 7 | +def get_fsr(num_classes, ctx, kernel_size): |
| 8 | + net = nn.Sequential() |
| 9 | + with net.name_scope(): |
| 10 | + net.add(nn.Conv2D(channels=256, kernel_size=1)) |
| 11 | + net.add(nn.BatchNorm()) |
| 12 | + net.add(nn.Activation('relu')) |
| 13 | + net.add(nn.Conv2D(channels=512, kernel_size=1)) |
| 14 | + net.add(nn.BatchNorm()) |
| 15 | + net.add(nn.Activation('relu')) |
| 16 | + net.add(nn.Conv2D(channels=1024, kernel_size=kernel_size)) |
| 17 | + net.add(nn.BatchNorm()) |
| 18 | + net.add(nn.Activation('relu')) |
| 19 | + net.add(nn.Dense(num_classes, flatten=True)) |
| 20 | + net.collect_params().initialize(mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2), ctx=ctx) |
| 21 | + |
| 22 | + return net |
| 23 | + |
| 24 | + |
| 25 | +def get_fatt(num_classes, stride, ctx): |
| 26 | + net = nn.Sequential() |
| 27 | + with net.name_scope(): |
| 28 | + net.add(nn.Conv2D(channels=512, kernel_size=1)) |
| 29 | + net.add(nn.BatchNorm()) |
| 30 | + net.add(nn.Activation('relu')) |
| 31 | + net.add(nn.Conv2D(channels=512, kernel_size=3, padding=1)) |
| 32 | + net.add(nn.BatchNorm()) |
| 33 | + net.add(nn.Activation('relu')) |
| 34 | + # net.add(nn.Conv2D(channels=512, kernel_size=3, padding=1)) |
| 35 | + # net.add(nn.BatchNorm()) |
| 36 | + # net.add(nn.Activation('relu')) |
| 37 | + net.add(nn.Conv2D(channels=num_classes, kernel_size=1, strides=stride)) |
| 38 | + net.collect_params().initialize(mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2), ctx=ctx) |
| 39 | + return net |
| 40 | + |
| 41 | + |
| 42 | +def get_conv2D(num_classes, stride, ctx): |
| 43 | + net = nn.Sequential() |
| 44 | + with net.name_scope(): |
| 45 | + net.add(nn.Conv2D(channels=num_classes, kernel_size=1, strides=stride)) |
| 46 | + net.add(nn.Activation('sigmoid')) |
| 47 | + net.collect_params().initialize(mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2), ctx=ctx) |
| 48 | + return net |
| 49 | + |
| 50 | + |
| 51 | +def getResNet(num_classes, ctx, NoTraining=True): |
| 52 | + resnet = vision.resnet101_v1(pretrained=True, ctx=ctx) |
| 53 | + |
| 54 | + net = vision.resnet101_v1(classes=num_classes, prefix='resnetv10_') |
| 55 | + with net.name_scope(): |
| 56 | + net.output = nn.Dense(num_classes, flatten=True, in_units=resnet.output._in_units) |
| 57 | + net.output.collect_params().initialize( |
| 58 | + mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2), ctx=ctx) |
| 59 | + net.features = resnet.features |
| 60 | + |
| 61 | + net.collect_params().reset_ctx(ctx) |
| 62 | + |
| 63 | + inputs = mx.sym.var('data') |
| 64 | + out = net(inputs) |
| 65 | + internals = out.get_internals() |
| 66 | + outputs = [internals['resnetv10_stage3_activation19_output'], internals['resnetv10_stage3_activation22_output'], internals['resnetv10_stage4_activation2_output'], |
| 67 | + internals['resnetv10_dense1_fwd_output']] |
| 68 | + feat_model = gluon.SymbolBlock(outputs, inputs, params=net.collect_params()) |
| 69 | + feat_model._prefix = 'resnetv10_' |
| 70 | + if NoTraining: |
| 71 | + feat_model.collect_params().setattr('grad_req', 'null') |
| 72 | + return feat_model |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +def getDenseNet(num_classes, ctx): |
| 77 | + densenet = vision.densenet201(pretrained=True, ctx=ctx) |
| 78 | + |
| 79 | + net = vision.densenet201(classes=num_classes, prefix='densenet0_') |
| 80 | + with net.name_scope(): |
| 81 | + net.output = nn.Dense(num_classes, flatten=True) |
| 82 | + net.output.collect_params().initialize( |
| 83 | + mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2), ctx=ctx) |
| 84 | + net.features = densenet.features |
| 85 | + |
| 86 | + net.collect_params().reset_ctx(ctx) |
| 87 | + |
| 88 | + inputs = mx.sym.var('data') |
| 89 | + out = net(inputs) |
| 90 | + internals = out.get_internals() |
| 91 | + outputs = [internals['densenet0_conv3_fwd_output'], internals['densenet0_stage4_concat15_output'], |
| 92 | + internals['densenet0_dense1_fwd_output']] |
| 93 | + feat_model = gluon.SymbolBlock(outputs, inputs, params=net.collect_params()) |
| 94 | + feat_model._prefix = 'densenet0_' |
| 95 | + |
| 96 | + return feat_model |
0 commit comments