Ejemplo n.º 1
0
    def __init__(self, scales, ratios, fg_threshold, bg_threshold,
                 batch_size_per_image, positive_fraction,
                 pre_nms_top_n_in_train, post_nms_top_n_in_train,
                 pre_nms_top_n_in_test, post_nms_top_n_in_test, nms_thresh,
                 **kwargs):
        super(RegionProposalNetwork, self).__init__(**kwargs)
        self.scales = scales
        self.ratios = ratios
        self.max_num_anchors_per_position = 0
        for k, _scales in scales.items():
            num_anchors_per_position = 0
            _ratios = ratios[k]
            for i, scale in enumerate(_scales):
                num_anchors_per_position += len(_ratios[i])
            if num_anchors_per_position > self.max_num_anchors_per_position:
                self.max_num_anchors_per_position = num_anchors_per_position

        with self.name_scope():
            self.head = nn.Conv2D(256, 3, padding=1, activation="relu")
            self.object_cls = nn.Conv2D(self.max_num_anchors_per_position, 1)
            self.object_reg = nn.Conv2D(self.max_num_anchors_per_position * 4,
                                        1)

        self._anchors = dict()
        self.fg_threshold = fg_threshold
        self.bg_threshold = bg_threshold
        self.batch_size_per_image = batch_size_per_image
        self.positive_fraction = positive_fraction
        self.pre_nms_top_n_in_train = pre_nms_top_n_in_train
        self.post_nms_top_n_in_train = post_nms_top_n_in_train
        self.pre_nms_top_n_in_test = pre_nms_top_n_in_test
        self.post_nms_top_n_in_test = post_nms_top_n_in_test
        self.nms_thresh = nms_thresh
        self.object_cls_loss = gloss.SigmoidBCELoss()
        self.object_reg_loss = gloss.HuberLoss()
    def hybrid_forward(self, F, past_target, future_target):  # add input
        prediction = self.nn(past_target)
        # default is calculate L1 loss with the future_target to learn the median

        # change loss
        # huber loss

        loss_huber = gloss.HuberLoss(rho=0.85)
        hloss = loss_huber(nd.array(prediction), nd.array(future_target))

        return hloss
Ejemplo n.º 3
0
 def __init__(
         self,
         num_classes,
         # rpn
         anchor_scales,
         anchor_ratios,
         rpn_fg_threshold=0.5,
         rpn_bg_threshold=0.3,
         rpn_batch_size_per_image=256,
         rpn_positive_fraction=0.3,
         rpn_pre_nms_top_n_in_train=2000,
         rpn_post_nms_top_n_in_train=1000,
         rpn_pre_nms_top_n_in_test=2000,
         rpn_post_nms_top_n_in_test=1000,
         rpn_nms_thresh=0.7,
         use_fpn=False,
         # head
         fg_threshold=0.5,
         batch_size_per_image=256,
         positive_fraction=0.5,
         max_objs_per_images=100,
         nms_thresh=0.7,
         backbone_pretrained=True,
         ctx=cpu(),
         **kwargs):
     super(FasterRCNNDetector, self).__init__(**kwargs)
     self.backbone = Resnet50Backbone(backbone_pretrained, ctx)
     self.use_fpn = use_fpn
     if use_fpn:
         self.fpn = FeaturePyramidNetwork(prefix='fpn_')
     self.rpn = RegionProposalNetwork(anchor_scales,
                                      anchor_ratios,
                                      rpn_fg_threshold,
                                      rpn_bg_threshold,
                                      rpn_batch_size_per_image,
                                      rpn_positive_fraction,
                                      rpn_pre_nms_top_n_in_train,
                                      rpn_post_nms_top_n_in_train,
                                      rpn_pre_nms_top_n_in_test,
                                      rpn_post_nms_top_n_in_test,
                                      rpn_nms_thresh,
                                      prefix="rpn_")
     if use_fpn:
         self.roi_extractor = RoIExtractor(self.fpn.output_layers[:-1],
                                           use_fpn)
     else:
         self.roi_extractor = RoIExtractor(["c5"])
     self.head = nn.Sequential(prefix="head_")
     with self.head.name_scope():
         self.head.add(nn.Dense(1024, activation='relu'))
         self.head.add(nn.Dense(1024, activation='relu'))
     self.num_classes = num_classes
     self.cls = nn.Dense(num_classes)
     self.reg = nn.Dense(num_classes * 4)
     self.fg_threshold = fg_threshold
     self.batch_size_per_image = batch_size_per_image
     self.positive_fraction = positive_fraction
     self.max_objs_per_images = max_objs_per_images
     self.nms_thresh = nms_thresh
     self.cls_loss = gloss.SoftmaxCrossEntropyLoss()
     self.reg_loss = gloss.HuberLoss()
import mxnet
from mxnet.gluon import nn, loss as gloss

import mxnet as mx
import mxnet.ndarray as nd
from mxnet import nd, autograd, gluon
from mxnet.gluon.data.vision import transforms

# L2 Loss
loss2 = gloss.L2Loss()

# sample data
x = nd.ones((2, ))
y = nd.ones((2, )) * 2
loss2(x, y)

# Huber loss
loss_huber = gloss.HuberLoss(rho=0.85)  # threshold rho

loss = gloss.SoftmaxCrossEntropyLoss()
x = nd.array([[1, 10], [8, 2]])
y = nd.array([0, 1])
loss(x, y)
Ejemplo n.º 5
0
true_w = nd.array([2, -3.4])
true_b = 4.2
features = nd.random.normal(scale=1, shape=(num_examples, num_inputs))
labels = nd.dot(features, true_w) + true_b
labels += nd.random.normal(scale=0.01, shape=labels.shape)

batch_size = 10
# Combine the features and labels of the training data
dataset = gdata.ArrayDataset(features, labels)
# Randomly reading mini-batches
data_iter = gdata.DataLoader(dataset, batch_size, shuffle=True)

net = nn.Sequential()
net.add(nn.Dense(1))
net.initialize(init.Normal(sigma=0.01))
loss = gloss.HuberLoss()  # The squared loss is also known as the L2 norm loss
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.03})

num_epochs = 5
for epoch in range(1, num_epochs + 1):
    for X, y in data_iter:
        with autograd.record():
            l = loss(net(X), y)
        l.backward()
        trainer.step(batch_size)
    l = loss(net(features), labels)
    print('epoch %d, loss: %f' % (epoch, l.mean().asnumpy()))

w = net[0].weight.data()
print(true_w.shape, w.shape)
print('Error in estimating w', true_w.reshape(w.shape) - w)