Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, in_channels, channels, norm_layer=nn.BatchNorm, norm_kwargs=None, **kwargs):
super(_FCNHead, self).__init__()
with self.name_scope():
self.block = nn.HybridSequential()
inter_channels = in_channels // 4
with self.block.name_scope():
self.block.add(nn.Conv2D(in_channels=in_channels, channels=inter_channels,
kernel_size=3, padding=1, use_bias=False))
self.block.add(norm_layer(in_channels=inter_channels,
**({} if norm_kwargs is None else norm_kwargs)))
self.block.add(nn.Activation('relu'))
self.block.add(nn.Dropout(0.1))
self.block.add(nn.Conv2D(in_channels=inter_channels, channels=channels,
kernel_size=1))
self.up_res_unit_2.add(BottleneckV2(channels, 1, prefix="up_res2_%d_" % i))
self.up_sample_2 = _UpSampleBlock(out_size)
out_size = out_size // 2
if stage <= 3:
self.down_sample_3 = nn.MaxPool2D(3, 2, 1)
self.down_res_unit_3 = nn.HybridSequential()
for i in range(r):
self.down_res_unit_3.add(BottleneckV2(channels, 1, prefix="down_res3_%d_" % i))
self.up_res_unit_3 = nn.HybridSequential()
for i in range(r):
self.up_res_unit_3.add(BottleneckV2(channels, 1, prefix="up_res3_%d_" % i))
self.up_sample_3 = _UpSampleBlock(out_size)
self.output = nn.HybridSequential()
self.output.add(nn.BatchNorm(),
nn.Activation('relu'),
nn.Conv2D(channels, kernel_size=1, strides=1, use_bias=False),
nn.BatchNorm(),
nn.Activation('relu'),
nn.Conv2D(channels, kernel_size=1, strides=1, use_bias=False),
nn.Activation('sigmoid')
)
stage.add(OctResUnit(
in_channels=in_channels,
out_channels=out_channels,
strides=strides,
oct_alpha=oct_alpha,
oct_mode=oct_mode,
bn_use_global_stats=bn_use_global_stats,
bottleneck=bottleneck,
conv1_stride=conv1_stride))
in_channels = out_channels
self.features.add(stage)
self.features.add(nn.AvgPool2D(
pool_size=7,
strides=1))
self.output = nn.HybridSequential(prefix="")
self.output.add(nn.Flatten())
self.output.add(nn.Dense(
units=classes,
in_units=in_channels))
def _make_transition(num_output_features):
out = nn.HybridSequential(prefix='')
out.add(nn.BatchNorm())
out.add(nn.Activation('relu'))
out.add(nn.Conv2D(num_output_features, kernel_size=1, use_bias=False))
out.add(nn.AvgPool2D(pool_size=2, strides=2))
return out
def resnet50_v2a_mask(pretrained_base, args):
backbone = ResNetV2a(layers=(3, 4, 6, 3), prefix='')
if pretrained_base:
backbone.load_parameters(args.pretrained, allow_missing=True, ignore_extra=True)
features = nn.HybridSequential()
for layer in ['layer0', 'layer1', 'layer2', 'layer3']:
features.add(getattr(backbone, layer))
top_features = nn.HybridSequential()
for layer in ['layer4']:
top_features.add(getattr(backbone, layer))
return MRCNN(
features, top_features, batch_images=args.batch_images, train_patterns='|'.join(['.*rpn', '.*dense', '.*mask', '.*stage(2|3|4)_conv']),
img_short=800, img_max_size=1333, img_means=(0.0, 0.0, 0.0), img_stds=(1.0, 1.0, 1.0), clip=4.42,
rpn_feature_stride=16, rpn_anchor_scales=(2, 4, 8, 16, 32), rpn_anchor_ratios=(0.5, 1, 2),
rpn_channels=1024, rpn_nms_thresh=0.7, rpn_min_size=0,
rpn_train_pre_topk=12000, rpn_train_post_topk=2000, rpn_test_pre_topk=6000, rpn_test_post_topk=1000,
rcnn_feature_stride=16, rcnn_pooled_size=(14, 14), rcnn_roi_mode='align',
mask_channels=256, rcnn_max_dets=1000,
rcnn_num_classes=81, rcnn_batch_rois=512, rcnn_bbox_stds=(0.1, 0.1, 0.2, 0.2),
rcnn_fg_fraction=0.25, rcnn_fg_overlap=0.5, rcnn_nms_thresh=0.5, rcnn_nms_topk=100)
ctx : Context, default CPU
The context in which to load the pretrained weights.
root : str, default '~/.mxnet/models'
Location for keeping the model parameters.
Examples
--------
>>> model = get_cascade_rcnn_resnet50_v2a_voc(pretrained=True)
>>> print(model)
"""
from .resnet50_v2a import resnet50_v2a
from ...data import VOCDetection
classes = VOCDetection.CLASSES
pretrained_base = False if pretrained else pretrained_base
base_network = resnet50_v2a(pretrained=pretrained_base)
features = nn.HybridSequential()
top_features = nn.HybridSequential()
for layer in ['rescale'] + ['layer' + str(i) for i in range(4)]:
features.add(getattr(base_network, layer))
for layer in ['layer4']:
top_features.add(getattr(base_network, layer))
train_patterns = '|'.join(['.*rfcn0_conv', '.*dense', '.*rpn', '.*stage(2|3|4)_conv']) #
print("~~~~~")
# print(features.collect_params())
print(top_features.collect_params())
return get_rfcn(
name='resnet50_v2a', dataset='voc', pretrained=pretrained,
features=features, top_features=top_features,
classes=classes,
short=600, max_size=1000, train_patterns=train_patterns,
nms_thresh=0.3, nms_topk=400, post_nms=100,
roi_mode='pspool', roi_size=(7, 7), stride=16, clip=None,
downsample.add(nn.AvgPool2D(pool_size=strides, strides=strides,
ceil_mode=True, count_include_pad=False))
else:
downsample.add(nn.AvgPool2D(pool_size=1, strides=1,
ceil_mode=True, count_include_pad=False))
downsample.add(nn.Conv2D(channels=planes * block.expansion, kernel_size=1,
strides=1, use_bias=False))
downsample.add(norm_layer(in_channels=planes * block.expansion,
**self.norm_kwargs))
else:
downsample.add(nn.Conv2D(channels=planes * block.expansion,
kernel_size=1, strides=strides, use_bias=False))
downsample.add(norm_layer(in_channels=planes * block.expansion,
**self.norm_kwargs))
layers = nn.HybridSequential(prefix='layers%d_'%stage_index)
with layers.name_scope():
if dilation in (1, 2):
layers.add(block(planes, strides, dilation=1,
downsample=downsample, previous_dilation=dilation,
norm_layer=norm_layer, norm_kwargs=self.norm_kwargs,
last_gamma=last_gamma))
elif dilation == 4:
layers.add(block(planes, strides, dilation=2,
downsample=downsample, previous_dilation=dilation,
norm_layer=norm_layer, norm_kwargs=self.norm_kwargs,
last_gamma=last_gamma))
else:
raise RuntimeError("=> unknown dilation size: {}".format(dilation))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
def ConvBlock(channels, kernel_size, strides, **kwargs):
out = nn.HybridSequential(**kwargs)
with out.name_scope():
out.add(
nn.Conv2D(channels, kernel_size, strides=strides, padding=1, use_bias=False),
nn.BatchNorm(scale=True),
nn.Activation('relu')
)
return out
def _get_embedding(self):
prefix = 'embedding0_'
if self._sparse_weight:
embedding = nn.Sequential(prefix=prefix)
else:
embedding = nn.HybridSequential(prefix=prefix)
with embedding.name_scope():
if self._sparse_weight:
# sparse embedding has both sparse weight and sparse grad
embed = contrib.nn.SparseEmbedding(self._vocab_size, self._embed_size,
prefix=prefix)
else:
embed = nn.Embedding(self._vocab_size, self._embed_size, prefix=prefix,
sparse_grad=self._sparse_grad)
embedding.add(embed)
if self._embed_dropout:
embedding.add(nn.Dropout(self._embed_dropout))
return embedding
def YOLOPyrmaid(pyrmaid_channels, anchors, total_channels_per_anchor, num_sync_bn_devices):
transitions = gluon.nn.HybridSequential()
yolo_blocks = gluon.nn.HybridSequential()
yolo_outputs = gluon.nn.HybridSequential()
# note that anchors and strides should be used in reverse order
for i, channel, anchor in zip(range(len(anchors)), pyrmaid_channels[::-1], anchors[::-1]):
output = YOLOOutput(total_channels_per_anchor, len(anchor))
yolo_outputs.add(output)
block = YOLODetectionBlockV3(channel, num_sync_bn_devices)
yolo_blocks.add(block)
if i > 0:
transitions.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
return transitions, yolo_blocks, yolo_outputs