Exemplo n.º 1
0
    # network
    net = SSD(num_classes=args.num_classes,
              sizes=args.sizes,
              ratios=args.ratios)
    # There are 21 classes, the first class is background. label form 0 to 20.
    # In the succeeding process, num_classes will plus 1.

    # init weight and bias
    net.initialize(ctx=ctx, init=mx.initializer.Xavier(magnitude=2))
    # initialize() define in mxnet/gulon/parameter.py.

    # Loss. Loss will defined in utils.py.

    # training and validating data. Use data.py to load data iter.
    train, val = get_iterators(args)

    # net parameters.
    net.collect_params().reset_ctx(ctx)
    # net.hybridize() # In SSD, only can use NDArray, Symbol is wrong.
    # Before net.hybridize(), F is using NDArray. atfer net.hybridize(), F is using Symbol.
    # Symbol code will not use python, but use C++ to compute!

    # Refer to http://zh.gluon.ai/chapter_computer-vision/kaggle-gluon-cifar10.html, set lr scheduler.
    # The trainer is defined in the main function. The lr scheduler is used in utils.py.
    '''
    In mxnet/gulon/trainer.py, the class Trainer has these method:
    1) learning_rate(), return current learning rate.
    2) set_learning_rate(lr), set the learning rate will be used.
    3) step(batch_size, ignore_stale_grad=False), Makes one step of parameter update based on batch_size data.
    4) save_states(fname), Saves trainer states (e.g. optimizer, momentum) to a file.
Exemplo n.º 2
0
    def __init__(self):
        nn.Module.__init__(self)
        self.sparseModel = scn.SparseVggNet(
            2, 3,
            [['C', 16], ['C', 16], 'MP', ['C', 32], ['C', 32], 'MP', ['C', 48],
             ['C', 48], 'MP', ['C', 64], ['C', 64], 'MP', ['C', 96], ['C', 96]
             ]).add(scn.Convolution(2, 96, 128, 3, 2, False)).add(
                 scn.BatchNormReLU(128)).add(scn.SparseToDense(2, 128))
        self.linear = nn.Linear(128, 3755)

    def forward(self, x):
        x = self.sparseModel(x)
        x = x.view(-1, 128)
        x = self.linear(x)
        return x


model = Model()
spatial_size = model.sparseModel.input_spatial_size(torch.LongTensor([1, 1]))
print('Input spatial size:', spatial_size)
dataset = get_iterators(spatial_size, 63, 3)
scn.ClassificationTrainValidate(
    model, dataset, {
        'n_epochs': 100,
        'initial_lr': 0.1,
        'lr_decay': 0.05,
        'weight_decay': 1e-4,
        'use_gpu': torch.cuda.is_available(),
        'check_point': True,
    })
Exemplo n.º 3
0
                       ['C', 16, 8], 'MP', ['C', 24, 8], ['C', 24, 8], 'MP']),
            scn.Convolution(2, 32, 64, 5, 1, False), scn.BatchNormReLU(64),
            scn.SparseToDense(2, 64))
        self.spatial_size = self.sparseModel.input_spatial_size(
            torch.LongTensor([1, 1]))
        self.inputLayer = scn.InputLayer(2, self.spatial_size, 2)
        self.linear = nn.Linear(64, 183)

    def forward(self, x):
        x = self.inputLayer(x)
        x = self.sparseModel(x)
        x = x.view(-1, 64)
        x = self.linear(x)
        return x


model = Model()
scale = 63
dataset = get_iterators(model.spatial_size, scale)
print('Input spatial size:', model.spatial_size, 'Data scale:', scale)

scn.ClassificationTrainValidate(
    model, dataset, {
        'n_epochs': 100,
        'initial_lr': 0.1,
        'lr_decay': 0.05,
        'weight_decay': 1e-4,
        'use_cuda': torch.cuda.is_available(),
        'check_point': False,
    })
Exemplo n.º 4
0
        return nd.mean(loss, axis=self._batch_axis, exclude=True)


class SmoothL1Loss(gluon.loss.Loss):
    def __init__(self, batch_axis=0, **kwargs):
        super(SmoothL1Loss, self).__init__(None, batch_axis, **kwargs)

    def forward(self, output, label, mask):
        loss = nd.smooth_l1((output - label) * mask, scalar=1.0)
        return nd.mean(loss, self._batch_axis, exclude=True)


if __name__ == '__main__':
    data_shape = 256
    batch_size = 4
    train_data, test_data, class_names, num_class = get_iterators(
        data_shape, batch_size)
    cls_loss = FocalLoss()
    box_loss = SmoothL1Loss()
    cls_metric = mx.metric.Accuracy()
    box_metric = mx.metric.MAE()

    ### Set context for training
    ctx = mx.gpu(0)  # it may takes too long to train using CPU
    try:
        _ = nd.zeros(1, ctx=ctx)
        # pad label for cuda implementation
        train_data.reshape(label_shape=(3, 5))
        train_data = test_data.sync_label_shape(train_data)
    except mx.base.MXNetError as err:
        print('No GPU enabled, fall back to CPU, sit back and be patient...')
        ctx = mx.cpu()