Ejemplo n.º 1
0
def test_resnet50_quant():
    set_seed(1)
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    config = config_quant
    print("training configure: {}".format(config))
    epoch_size = config.epoch_size

    # define network
    net = resnet50_quant(class_num=config.class_num)
    net.set_train(True)

    # define loss
    if not config.use_label_smooth:
        config.label_smooth_factor = 0.0
    loss = CrossEntropy(smooth_factor=config.label_smooth_factor,
                        num_classes=config.class_num)
    #loss_scale = FixedLossScaleManager(config.loss_scale, drop_overflow_update=False)

    # define dataset
    dataset = create_dataset(dataset_path=dataset_path,
                             config=config,
                             repeat_num=1,
                             batch_size=config.batch_size)
    step_size = dataset.get_dataset_size()

    # convert fusion network to quantization aware network
    quantizer = QuantizationAwareTraining(bn_fold=True,
                                          per_channel=[True, False],
                                          symmetric=[True, False])
    net = quantizer.quantize(net)

    # get learning rate
    lr = Tensor(
        get_lr(lr_init=config.lr_init,
               lr_end=0.0,
               lr_max=config.lr_max,
               warmup_epochs=config.warmup_epochs,
               total_epochs=config.epoch_size,
               steps_per_epoch=step_size,
               lr_decay_mode='cosine'))

    # define optimization
    opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr,
                   config.momentum, config.weight_decay, config.loss_scale)

    # define model
    #model = Model(net, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale, metrics={'acc'})
    model = Model(net, loss_fn=loss, optimizer=opt)

    print("============== Starting Training ==============")
    monitor = Monitor(lr_init=lr.asnumpy(),
                      step_threshold=config.step_threshold)

    callbacks = [monitor]
    model.train(epoch_size,
                dataset,
                callbacks=callbacks,
                dataset_sink_mode=False)
    print("============== End Training ==============")

    expect_avg_step_loss = 2.40
    avg_step_loss = np.mean(np.array(monitor.losses))

    print("average step loss:{}".format(avg_step_loss))
    assert avg_step_loss < expect_avg_step_loss
Ejemplo n.º 2
0
            get_lr(global_step=0,
                   lr_init=0,
                   lr_end=0,
                   lr_max=config_gpu.lr,
                   warmup_epochs=config_gpu.warmup_epochs,
                   total_epochs=epoch_size,
                   steps_per_epoch=step_size))
        opt = Momentum(filter(lambda x: x.requires_grad,
                              net.get_parameters()), lr, config_gpu.momentum,
                       config_gpu.weight_decay, config_gpu.loss_scale)
        # define model
        model = Model(net,
                      loss_fn=loss,
                      optimizer=opt,
                      loss_scale_manager=loss_scale)

        cb = [Monitor(lr_init=lr.asnumpy())]
        ckpt_save_dir = config_gpu.save_checkpoint_path + "ckpt_" + str(
            get_rank()) + "/"
        if config_gpu.save_checkpoint:
            config_ck = CheckpointConfig(
                save_checkpoint_steps=config_gpu.save_checkpoint_epochs *
                step_size,
                keep_checkpoint_max=config_gpu.keep_checkpoint_max)
            ckpt_cb = ModelCheckpoint(prefix="mobilenetV3",
                                      directory=ckpt_save_dir,
                                      config=config_ck)
            cb += [ckpt_cb]
        # begine train
        model.train(epoch_size, dataset, callbacks=cb)
Ejemplo n.º 3
0
def strided_slice(nptype):
    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')

    x = Tensor(np.arange(0, 2 * 3 * 4 * 5).reshape(2, 3, 4, 5).astype(nptype))
    y = P.StridedSlice()(x, (1, 0, 0, 2), (2, 2, 2, 4), (1, 1, 1, 1))
    expect = np.array([[[[62, 63], [67, 68]], [[82, 83],
                                               [87, 88]]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)

    y = P.StridedSlice()(x, (1, 0, 0, 5), (2, 2, 2, 1), (1, 1, 1, -2))
    expect = np.array([[[[64, 62], [69, 67]], [[84, 82],
                                               [89, 87]]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)

    y = P.StridedSlice()(x, (1, 0, 0, -1), (2, 2, 2, 1), (1, 1, 1, -1))
    expect = np.array([[[[64, 63, 62], [69, 68, 67]],
                        [[84, 83, 82], [89, 88, 87]]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)

    y = P.StridedSlice()(x, (1, 0, -1, -2), (2, 2, 0, -5), (1, 1, -1, -2))
    expect = np.array([[[[78, 76], [73, 71], [68, 66]],
                        [[98, 96], [93, 91], [88, 86]]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)

    # ME Infer fault
    # y = P.StridedSlice(begin_mask=0b1000, end_mask=0b0010)(x, (1, 0, 0, 2), (2, 2, 2, 4), (1, 1, 1, 1))
    # expect = np.array([[[[62, 63],
    #                      [67, 68]],
    #                     [[82, 83],
    #                      [87, 88]],
    #                     [[102, 103],
    #                      [107, 108]]]]).astype(nptype)
    # assert np.allclose(y.asnumpy(), expect)

    op = P.StridedSlice(begin_mask=0b1000,
                        end_mask=0b0010,
                        ellipsis_mask=0b0100)
    y = op(x, (1, 0, 0, 2), (2, 2, 2, 4), (1, 1, 1, 1))
    expect = np.array([[[[60, 61, 62, 63], [65, 66, 67, 68], [70, 71, 72, 73],
                         [75, 76, 77, 78]],
                        [[80, 81, 82, 83], [85, 86, 87, 88], [90, 91, 92, 93],
                         [95, 96, 97, 98]],
                        [[100, 101, 102, 103], [105, 106, 107, 108],
                         [110, 111, 112, 113], [115, 116, 117,
                                                118]]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)

    x = Tensor(np.arange(0, 3 * 4 * 5).reshape(3, 4, 5).astype(nptype))
    y = P.StridedSlice()(x, (1, 0, 0), (2, -3, 3), (1, 1, 3))
    expect = np.array([[[20]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)

    x_np = np.arange(0, 4 * 5).reshape(4, 5).astype(nptype)
    y = Tensor(x_np)[:, ::-1]
    expect = x_np[:, ::-1]
    assert np.allclose(y.asnumpy(), expect)

    x = Tensor(
        np.arange(0, 2 * 3 * 4 * 5 * 4 * 3 * 2).reshape(2, 3, 4, 5, 4, 3,
                                                        2).astype(nptype))
    y = P.StridedSlice()(x, (1, 0, 0, 2, 1, 2, 0), (2, 2, 2, 4, 2, 3, 2),
                         (1, 1, 1, 1, 1, 1, 2))
    expect = np.array([[[[[[[1498.]]], [[[1522.]]]], [[[[1618.]]],
                                                      [[[1642.]]]]],
                        [[[[[1978.]]], [[[2002.]]]],
                         [[[[2098.]]], [[[2122.]]]]]]]).astype(nptype)
    assert np.allclose(y.asnumpy(), expect)
Ejemplo n.º 4
0
def resize_nn_grayscale_integer_ratio(datatype):
    input_tensor = Tensor(np.array(
        [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]]]).astype(datatype))

    # larger h and w
    resize_nn = NetResizeNearestNeighbor((9, 9))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3],
                                         [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3],
                                         [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3],
                                         [0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6],
                                         [0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6],
                                         [0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6],
                                         [0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9],
                                         [0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9],
                                         [0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h and w
    resize_nn = NetResizeNearestNeighbor((1, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[0.1]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, larger w
    resize_nn = NetResizeNearestNeighbor((1, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, smaller w
    resize_nn = NetResizeNearestNeighbor((6, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1], [0.1], [0.4], [0.4], [0.7], [0.7]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, same w
    resize_nn = NetResizeNearestNeighbor((1, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[0.1, 0.2, 0.3]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, same w
    resize_nn = NetResizeNearestNeighbor((6, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[0.1, 0.2, 0.3],
                                         [0.1, 0.2, 0.3],
                                         [0.4, 0.5, 0.6],
                                         [0.4, 0.5, 0.6],
                                         [0.7, 0.8, 0.9],
                                         [0.7, 0.8, 0.9]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, smaller w
    resize_nn = NetResizeNearestNeighbor((3, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1], [0.4], [0.7]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, larger w
    resize_nn = NetResizeNearestNeighbor((3, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3],
                                         [0.4, 0.4, 0.5, 0.5, 0.6, 0.6],
                                         [0.7, 0.7, 0.8, 0.8, 0.9, 0.9]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same w, same h (identity)
    resize_nn = NetResizeNearestNeighbor((3, 3))
    output = resize_nn(input_tensor)
    np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
Ejemplo n.º 5
0
def train_on_ascend():
    config = config_ascend_quant if args_opt.quantization_aware else config_ascend
    print("training args: {}".format(args_opt))
    print("training configure: {}".format(config))
    print("parallel args: rank_id {}, device_id {}, rank_size {}".format(rank_id, device_id, rank_size))
    epoch_size = config.epoch_size

    # distribute init
    if run_distribute:
        context.set_auto_parallel_context(device_num=rank_size,
                                          parallel_mode=ParallelMode.DATA_PARALLEL,
                                          parameter_broadcast=True,
                                          mirror_mean=True)
        init()

    # define network
    network = mobilenetV2(num_classes=config.num_classes)
    # define loss
    if config.label_smooth > 0:
        loss = CrossEntropyWithLabelSmooth(smooth_factor=config.label_smooth, num_classes=config.num_classes)
    else:
        loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction='mean')
    # define dataset
    dataset = create_dataset(dataset_path=args_opt.dataset_path,
                             do_train=True,
                             config=config,
                             device_target=args_opt.device_target,
                             repeat_num=1,
                             batch_size=config.batch_size)
    step_size = dataset.get_dataset_size()
    # load pre trained ckpt
    if args_opt.pre_trained:
        param_dict = load_checkpoint(args_opt.pre_trained)
        load_param_into_net(network, param_dict)

    # convert fusion network to quantization aware network
    if config.quantization_aware:
        network = quant.convert_quant_network(network,
                                              bn_fold=True,
                                              per_channel=[True, False],
                                              symmetric=[True, False])

    # get learning rate
    lr = Tensor(get_lr(global_step=config.start_epoch * step_size,
                       lr_init=0,
                       lr_end=0,
                       lr_max=config.lr,
                       warmup_epochs=config.warmup_epochs,
                       total_epochs=epoch_size + config.start_epoch,
                       steps_per_epoch=step_size))

    # define optimization
    opt = nn.Momentum(filter(lambda x: x.requires_grad, network.get_parameters()), lr, config.momentum,
                      config.weight_decay)
    # define model
    model = Model(network, loss_fn=loss, optimizer=opt)

    print("============== Starting Training ==============")
    callback = None
    if rank_id == 0:
        callback = [Monitor(lr_init=lr.asnumpy())]
        if config.save_checkpoint:
            config_ck = CheckpointConfig(save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
                                         keep_checkpoint_max=config.keep_checkpoint_max)
            ckpt_cb = ModelCheckpoint(prefix="mobilenetV2",
                                      directory=config.save_checkpoint_path,
                                      config=config_ck)
            callback += [ckpt_cb]
    model.train(epoch_size, dataset, callbacks=callback)
    print("============== End Training ==============")
Ejemplo n.º 6
0
def resize_nn_rgb_integer_ratio(datatype):
    input_tensor = Tensor(np.array(
        [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
          [[11, 12, 13], [14, 15, 16], [17, 18, 19]],
          [[111, 112, 113], [114, 115, 116], [117, 118, 119]]]]).astype(datatype))

    # larger h and w
    resize_nn = NetResizeNearestNeighbor((9, 9))
    output = resize_nn(input_tensor)
    expected_output_array = np.array([[[[1, 1, 1, 2, 2, 2, 3, 3, 3],
                                        [1, 1, 1, 2, 2, 2, 3, 3, 3],
                                        [1, 1, 1, 2, 2, 2, 3, 3, 3],
                                        [4, 4, 4, 5, 5, 5, 6, 6, 6],
                                        [4, 4, 4, 5, 5, 5, 6, 6, 6],
                                        [4, 4, 4, 5, 5, 5, 6, 6, 6],
                                        [7, 7, 7, 8, 8, 8, 9, 9, 9],
                                        [7, 7, 7, 8, 8, 8, 9, 9, 9],
                                        [7, 7, 7, 8, 8, 8, 9, 9, 9]],
                                       [[11, 11, 11, 12, 12, 12, 13, 13, 13],
                                        [11, 11, 11, 12, 12, 12, 13, 13, 13],
                                        [11, 11, 11, 12, 12, 12, 13, 13, 13],
                                        [14, 14, 14, 15, 15, 15, 16, 16, 16],
                                        [14, 14, 14, 15, 15, 15, 16, 16, 16],
                                        [14, 14, 14, 15, 15, 15, 16, 16, 16],
                                        [17, 17, 17, 18, 18, 18, 19, 19, 19],
                                        [17, 17, 17, 18, 18, 18, 19, 19, 19],
                                        [17, 17, 17, 18, 18, 18, 19, 19, 19]],
                                       [[111, 111, 111, 112, 112, 112, 113, 113, 113],
                                        [111, 111, 111, 112, 112, 112, 113, 113, 113],
                                        [111, 111, 111, 112, 112, 112, 113, 113, 113],
                                        [114, 114, 114, 115, 115, 115, 116, 116, 116],
                                        [114, 114, 114, 115, 115, 115, 116, 116, 116],
                                        [114, 114, 114, 115, 115, 115, 116, 116, 116],
                                        [117, 117, 117, 118, 118, 118, 119, 119, 119],
                                        [117, 117, 117, 118, 118, 118, 119, 119, 119],
                                        [117, 117, 117, 118, 118, 118, 119, 119, 119]]]])
    expected_output = Tensor(np.array(expected_output_array).astype(datatype))

    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h and w
    resize_nn = NetResizeNearestNeighbor((1, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1]], [[11]], [[111]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, larger w
    resize_nn = NetResizeNearestNeighbor((1, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 1, 2, 2, 3, 3]],
                                        [[11, 11, 12, 12, 13, 13]],
                                        [[111, 111, 112, 112, 113, 113]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, smaller w
    resize_nn = NetResizeNearestNeighbor((6, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1], [1], [4], [4], [7], [7]],
                                        [[11], [11], [14], [14], [17], [17]],
                                        [[111], [111], [114], [114], [117], [117]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, same w
    resize_nn = NetResizeNearestNeighbor((1, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 2, 3]],
                                        [[11, 12, 13]],
                                        [[111, 112, 113]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, same w
    resize_nn = NetResizeNearestNeighbor((6, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 2, 3],
                                         [1, 2, 3],
                                         [4, 5, 6],
                                         [4, 5, 6],
                                         [7, 8, 9],
                                         [7, 8, 9]],
                                        [[11, 12, 13],
                                         [11, 12, 13],
                                         [14, 15, 16],
                                         [14, 15, 16],
                                         [17, 18, 19],
                                         [17, 18, 19]],
                                        [[111, 112, 113],
                                         [111, 112, 113],
                                         [114, 115, 116],
                                         [114, 115, 116],
                                         [117, 118, 119],
                                         [117, 118, 119]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, smaller w
    resize_nn = NetResizeNearestNeighbor((3, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1], [4], [7]],
                                        [[11], [14], [17]],
                                        [[111], [114], [117]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, larger w
    resize_nn = NetResizeNearestNeighbor((3, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 1, 2, 2, 3, 3],
                                         [4, 4, 5, 5, 6, 6],
                                         [7, 7, 8, 8, 9, 9]],
                                        [[11, 11, 12, 12, 13, 13],
                                         [14, 14, 15, 15, 16, 16],
                                         [17, 17, 18, 18, 19, 19]],
                                        [[111, 111, 112, 112, 113, 113],
                                         [114, 114, 115, 115, 116, 116],
                                         [117, 117, 118, 118, 119, 119]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same w, same h (identity)
    resize_nn = NetResizeNearestNeighbor((3, 3))
    output = resize_nn(input_tensor)
    np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
Ejemplo n.º 7
0
def resize_nn_rgb_not_integer_ratio(datatype):
    input_tensor = Tensor(np.array([[[[1, 2, 3, 4],
                                      [5, 6, 7, 8],
                                      [9, 0, 1, 2]],
                                     [[11, 12, 13, 14],
                                      [15, 16, 17, 18],
                                      [19, 10, 11, 12]],
                                     [[111, 112, 113, 114],
                                      [115, 116, 117, 118],
                                      [119, 110, 111, 112]]]]).astype(datatype))

    # larger h and w
    resize_nn = NetResizeNearestNeighbor((7, 7))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 1, 2, 2, 3, 3, 4],
                                         [1, 1, 2, 2, 3, 3, 4],
                                         [1, 1, 2, 2, 3, 3, 4],
                                         [5, 5, 6, 6, 7, 7, 8],
                                         [5, 5, 6, 6, 7, 7, 8],
                                         [9, 9, 0, 0, 1, 1, 2],
                                         [9, 9, 0, 0, 1, 1, 2]],
                                        [[11, 11, 12, 12, 13, 13, 14],
                                         [11, 11, 12, 12, 13, 13, 14],
                                         [11, 11, 12, 12, 13, 13, 14],
                                         [15, 15, 16, 16, 17, 17, 18],
                                         [15, 15, 16, 16, 17, 17, 18],
                                         [19, 19, 10, 10, 11, 11, 12],
                                         [19, 19, 10, 10, 11, 11, 12]],
                                        [[111, 111, 112, 112, 113, 113, 114],
                                         [111, 111, 112, 112, 113, 113, 114],
                                         [111, 111, 112, 112, 113, 113, 114],
                                         [115, 115, 116, 116, 117, 117, 118],
                                         [115, 115, 116, 116, 117, 117, 118],
                                         [119, 119, 110, 110, 111, 111, 112],
                                         [119, 119, 110, 110, 111, 111, 112]]]]).astype(datatype))

    # smaller h and w
    resize_nn = NetResizeNearestNeighbor((2, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 2, 3], [5, 6, 7]],
                                        [[11, 12, 13], [15, 16, 17]],
                                        [[111, 112, 113], [115, 116, 117]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, larger w
    resize_nn = NetResizeNearestNeighbor((2, 7))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 1, 2, 2, 3, 3, 4],
                                         [5, 5, 6, 6, 7, 7, 8]],
                                        [[11, 11, 12, 12, 13, 13, 14],
                                         [15, 15, 16, 16, 17, 17, 18]],
                                        [[111, 111, 112, 112, 113, 113, 114],
                                         [115, 115, 116, 116, 117, 117, 118]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, smaller w
    resize_nn = NetResizeNearestNeighbor((5, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 2, 3],
                                         [1, 2, 3],
                                         [5, 6, 7],
                                         [5, 6, 7],
                                         [9, 0, 1]],
                                        [[11, 12, 13],
                                         [11, 12, 13],
                                         [15, 16, 17],
                                         [15, 16, 17],
                                         [19, 10, 11]],
                                        [[111, 112, 113],
                                         [111, 112, 113],
                                         [115, 116, 117],
                                         [115, 116, 117],
                                         [119, 110, 111]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, same w
    resize_nn = NetResizeNearestNeighbor((2, 4))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 2, 3, 4],
                                         [5, 6, 7, 8]],
                                        [[11, 12, 13, 14],
                                         [15, 16, 17, 18]],
                                        [[111, 112, 113, 114],
                                         [115, 116, 117, 118]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, same w
    resize_nn = NetResizeNearestNeighbor((8, 4))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 2, 3, 4],
                                         [1, 2, 3, 4],
                                         [1, 2, 3, 4],
                                         [5, 6, 7, 8],
                                         [5, 6, 7, 8],
                                         [5, 6, 7, 8],
                                         [9, 0, 1, 2],
                                         [9, 0, 1, 2]],
                                        [[11, 12, 13, 14],
                                         [11, 12, 13, 14],
                                         [11, 12, 13, 14],
                                         [15, 16, 17, 18],
                                         [15, 16, 17, 18],
                                         [15, 16, 17, 18],
                                         [19, 10, 11, 12],
                                         [19, 10, 11, 12]],
                                        [[111, 112, 113, 114],
                                         [111, 112, 113, 114],
                                         [111, 112, 113, 114],
                                         [115, 116, 117, 118],
                                         [115, 116, 117, 118],
                                         [115, 116, 117, 118],
                                         [119, 110, 111, 112],
                                         [119, 110, 111, 112]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, smaller w
    resize_nn = NetResizeNearestNeighbor((3, 2))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 3], [5, 7], [9, 1]],
                                        [[11, 13], [15, 17], [19, 11]],
                                        [[111, 113], [115, 117], [119, 111]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, larger w
    resize_nn = NetResizeNearestNeighbor((3, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(np.array([[[[1, 1, 2, 3, 3, 4],
                                         [5, 5, 6, 7, 7, 8],
                                         [9, 9, 0, 1, 1, 2]],
                                        [[11, 11, 12, 13, 13, 14],
                                         [15, 15, 16, 17, 17, 18],
                                         [19, 19, 10, 11, 11, 12]],
                                        [[111, 111, 112, 113, 113, 114],
                                         [115, 115, 116, 117, 117, 118],
                                         [119, 119, 110, 111, 111, 112]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same w, same h (identity)
    resize_nn = NetResizeNearestNeighbor((3, 4))
    output = resize_nn(input_tensor)
    np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
def test_resize_nn_rgb_not_integer_ratio():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
    input_tensor = Tensor(
        np.array([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2]],
                   [[11, 12, 13, 14], [15, 16, 17, 18], [19, 10, 11, 12]],
                   [[111, 112, 113, 114], [115, 116, 117, 118],
                    [119, 110, 111, 112]]]]).astype(np.int32))

    # larger h and w
    resize_nn = P.ResizeNearestNeighbor((7, 7))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 1, 2, 2, 3, 3, 4], [1, 1, 2, 2, 3, 3, 4],
                    [1, 1, 2, 2, 3, 3, 4], [5, 5, 6, 6, 7, 7, 8],
                    [5, 5, 6, 6, 7, 7, 8], [9, 9, 0, 0, 1, 1, 2],
                    [9, 9, 0, 0, 1, 1, 2]],
                   [[11, 11, 12, 12, 13, 13, 14], [11, 11, 12, 12, 13, 13, 14],
                    [11, 11, 12, 12, 13, 13, 14], [15, 15, 16, 16, 17, 17, 18],
                    [15, 15, 16, 16, 17, 17, 18], [19, 19, 10, 10, 11, 11, 12],
                    [19, 19, 10, 10, 11, 11, 12]],
                   [[111, 111, 112, 112, 113, 113, 114],
                    [111, 111, 112, 112, 113, 113, 114],
                    [111, 111, 112, 112, 113, 113, 114],
                    [115, 115, 116, 116, 117, 117, 118],
                    [115, 115, 116, 116, 117, 117, 118],
                    [119, 119, 110, 110, 111, 111, 112],
                    [119, 119, 110, 110, 111, 111, 112]]]]).astype(np.int32))

    # smaller h and w
    resize_nn = P.ResizeNearestNeighbor((2, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 2, 3], [5, 6, 7]], [[11, 12, 13], [15, 16, 17]],
                   [[111, 112, 113], [115, 116, 117]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, larger w
    resize_nn = P.ResizeNearestNeighbor((2, 7))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 1, 2, 2, 3, 3, 4], [5, 5, 6, 6, 7, 7, 8]],
                   [[11, 11, 12, 12, 13, 13, 14], [15, 15, 16, 16, 17, 17,
                                                   18]],
                   [[111, 111, 112, 112, 113, 113, 114],
                    [115, 115, 116, 116, 117, 117, 118]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, smaller w
    resize_nn = P.ResizeNearestNeighbor((5, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 2, 3], [1, 2, 3], [5, 6, 7], [5, 6, 7], [9, 0, 1]],
                   [[11, 12, 13], [11, 12, 13], [15, 16, 17], [15, 16, 17],
                    [19, 10, 11]],
                   [[111, 112, 113], [111, 112, 113], [115, 116, 117],
                    [115, 116, 117], [119, 110, 111]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, same w
    resize_nn = P.ResizeNearestNeighbor((2, 4))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 2, 3, 4], [5, 6, 7, 8]],
                   [[11, 12, 13, 14], [15, 16, 17, 18]],
                   [[111, 112, 113, 114], [115, 116, 117,
                                           118]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, same w
    resize_nn = P.ResizeNearestNeighbor((8, 4))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [5, 6, 7, 8],
                    [5, 6, 7, 8], [5, 6, 7, 8], [9, 0, 1, 2], [9, 0, 1, 2]],
                   [[11, 12, 13, 14], [11, 12, 13, 14], [11, 12, 13, 14],
                    [15, 16, 17, 18], [15, 16, 17, 18], [15, 16, 17, 18],
                    [19, 10, 11, 12], [19, 10, 11, 12]],
                   [[111, 112, 113, 114], [111, 112, 113, 114],
                    [111, 112, 113, 114], [115, 116, 117, 118],
                    [115, 116, 117, 118], [115, 116, 117, 118],
                    [119, 110, 111, 112], [119, 110, 111,
                                           112]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, smaller w
    resize_nn = P.ResizeNearestNeighbor((3, 2))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 3], [5, 7], [9, 1]], [[11, 13], [15, 17], [19, 11]],
                   [[111, 113], [115, 117], [119, 111]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, larger w
    resize_nn = P.ResizeNearestNeighbor((3, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 1, 2, 3, 3, 4], [5, 5, 6, 7, 7, 8], [9, 9, 0, 1, 1,
                                                             2]],
                   [[11, 11, 12, 13, 13, 14], [15, 15, 16, 17, 17, 18],
                    [19, 19, 10, 11, 11, 12]],
                   [[111, 111, 112, 113, 113, 114],
                    [115, 115, 116, 117, 117, 118],
                    [119, 119, 110, 111, 111, 112]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same w, same h (identity)
    resize_nn = P.ResizeNearestNeighbor((3, 4))
    output = resize_nn(input_tensor)
    np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
Ejemplo n.º 9
0
                                              symmetric=[True, False])

    # get learning rate
    lr = Tensor(get_lr(global_step=config.start_epoch * step_size,
                       lr_init=0,
                       lr_end=0,
                       lr_max=config.lr,
                       warmup_epochs=config.warmup_epochs,
                       total_epochs=epoch_size + config.start_epoch,
                       steps_per_epoch=step_size))

    # define optimization
    opt = nn.Momentum(filter(lambda x: x.requires_grad, network.get_parameters()), lr, config.momentum,
                      config.weight_decay)
    # define model
    model = Model(network, loss_fn=loss, optimizer=opt)

    print("============== Starting Training ==============")
    callback = None
    if rank_id == 0:
        callback = [Monitor(lr_init=lr.asnumpy())]
        if config.save_checkpoint:
            config_ck = CheckpointConfig(save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
                                         keep_checkpoint_max=config.keep_checkpoint_max)
            ckpt_cb = ModelCheckpoint(prefix="mobilenetV2",
                                      directory=config.save_checkpoint_path,
                                      config=config_ck)
            callback += [ckpt_cb]
    model.train(epoch_size, dataset, callbacks=callback)
    print("============== End Training ==============")
def resize_nn_grayscale_not_integer_ratio(datatype):
    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
    input_tensor = Tensor(
        np.array([[[[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8],
                    [0.9, 0.0, 0.1, 0.2]]]]).astype(datatype))

    # larger h and w
    resize_nn = P.ResizeNearestNeighbor((7, 7))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4],
                    [0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4],
                    [0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4],
                    [0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8],
                    [0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8],
                    [0.9, 0.9, 0.0, 0.0, 0.1, 0.1, 0.2],
                    [0.9, 0.9, 0.0, 0.0, 0.1, 0.1, 0.2]]]]).astype(datatype))

    # smaller h and w
    resize_nn = P.ResizeNearestNeighbor((2, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.2, 0.3], [0.5, 0.6, 0.7]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, larger w
    resize_nn = P.ResizeNearestNeighbor((2, 7))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4],
                    [0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, smaller w
    resize_nn = P.ResizeNearestNeighbor((5, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.2, 0.3], [0.1, 0.2, 0.3], [0.5, 0.6, 0.7],
                    [0.5, 0.6, 0.7], [0.9, 0.0, 0.1]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, same w
    resize_nn = P.ResizeNearestNeighbor((2, 4))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7,
                                           0.8]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, same w
    resize_nn = P.ResizeNearestNeighbor((8, 4))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.2, 0.3, 0.4], [0.1, 0.2, 0.3, 0.4],
                    [0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8],
                    [0.5, 0.6, 0.7, 0.8], [0.5, 0.6, 0.7, 0.8],
                    [0.9, 0.0, 0.1, 0.2], [0.9, 0.0, 0.1,
                                           0.2]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, smaller w
    resize_nn = P.ResizeNearestNeighbor((3, 2))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.3], [0.5, 0.7], [0.9, 0.1]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, larger w
    resize_nn = P.ResizeNearestNeighbor((3, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[0.1, 0.1, 0.2, 0.3, 0.3, 0.4],
                    [0.5, 0.5, 0.6, 0.7, 0.7, 0.8],
                    [0.9, 0.9, 0.0, 0.1, 0.1, 0.2]]]]).astype(datatype))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same w, same h (identity)
    resize_nn = P.ResizeNearestNeighbor((3, 4))
    output = resize_nn(input_tensor)
    np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
def test_resize_nn_rgb_integer_ratio():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
    input_tensor = Tensor(
        np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[11, 12, 13], [14, 15, 16], [17, 18, 19]],
                   [[111, 112, 113], [114, 115, 116],
                    [117, 118, 119]]]]).astype(np.int32))

    # larger h and w
    resize_nn = P.ResizeNearestNeighbor((9, 9))
    output = resize_nn(input_tensor)
    expected_output_array = np.array(
        [[[[1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 1, 1, 2, 2, 2, 3, 3, 3],
           [1, 1, 1, 2, 2, 2, 3, 3, 3],
           [4, 4, 4, 5, 5, 5, 6, 6,
            6], [4, 4, 4, 5, 5, 5, 6, 6,
                 6], [4, 4, 4, 5, 5, 5, 6, 6,
                      6], [7, 7, 7, 8, 8, 8, 9, 9,
                           9], [7, 7, 7, 8, 8, 8, 9, 9,
                                9], [7, 7, 7, 8, 8, 8, 9, 9,
                                     9]],
          [[11, 11, 11, 12, 12, 12, 13, 13, 13],
           [11, 11, 11, 12, 12, 12, 13, 13, 13],
           [11, 11, 11, 12, 12, 12, 13, 13, 13],
           [14, 14, 14, 15, 15, 15, 16, 16, 16],
           [14, 14, 14, 15, 15, 15, 16, 16, 16],
           [14, 14, 14, 15, 15, 15, 16, 16, 16],
           [17, 17, 17, 18, 18, 18, 19, 19, 19],
           [17, 17, 17, 18, 18, 18, 19, 19, 19],
           [17, 17, 17, 18, 18, 18, 19, 19, 19]],
          [[111, 111, 111, 112, 112, 112, 113, 113, 113],
           [111, 111, 111, 112, 112, 112, 113, 113, 113],
           [111, 111, 111, 112, 112, 112, 113, 113, 113],
           [114, 114, 114, 115, 115, 115, 116, 116, 116],
           [114, 114, 114, 115, 115, 115, 116, 116, 116],
           [114, 114, 114, 115, 115, 115, 116, 116, 116],
           [117, 117, 117, 118, 118, 118, 119, 119, 119],
           [117, 117, 117, 118, 118, 118, 119, 119, 119],
           [117, 117, 117, 118, 118, 118, 119, 119, 119]]]])
    expected_output = Tensor(np.array(expected_output_array).astype(np.int32))

    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h and w
    resize_nn = P.ResizeNearestNeighbor((1, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1]], [[11]], [[111]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, larger w
    resize_nn = P.ResizeNearestNeighbor((1, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 1, 2, 2, 3, 3]], [[11, 11, 12, 12, 13, 13]],
                   [[111, 111, 112, 112, 113, 113]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, smaller w
    resize_nn = P.ResizeNearestNeighbor((6, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1], [1], [4], [4], [7], [7]],
                   [[11], [11], [14], [14], [17], [17]],
                   [[111], [111], [114], [114], [117],
                    [117]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # smaller h, same w
    resize_nn = P.ResizeNearestNeighbor((1, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 2, 3]], [[11, 12, 13]], [[111, 112,
                                                  113]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # larger h, same w
    resize_nn = P.ResizeNearestNeighbor((6, 3))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9],
                    [7, 8, 9]],
                   [[11, 12, 13], [11, 12, 13], [14, 15, 16], [14, 15, 16],
                    [17, 18, 19], [17, 18, 19]],
                   [[111, 112, 113], [111, 112, 113], [114, 115, 116],
                    [114, 115, 116], [117, 118, 119],
                    [117, 118, 119]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, smaller w
    resize_nn = P.ResizeNearestNeighbor((3, 1))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1], [4], [7]], [[11], [14], [17]],
                   [[111], [114], [117]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same h, larger w
    resize_nn = P.ResizeNearestNeighbor((3, 6))
    output = resize_nn(input_tensor)
    expected_output = Tensor(
        np.array([[[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6], [7, 7, 8, 8, 9,
                                                             9]],
                   [[11, 11, 12, 12, 13, 13], [14, 14, 15, 15, 16, 16],
                    [17, 17, 18, 18, 19, 19]],
                   [[111, 111, 112, 112, 113, 113],
                    [114, 114, 115, 115, 116, 116],
                    [117, 117, 118, 118, 119, 119]]]]).astype(np.int32))
    np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy())

    # same w, same h (identity)
    resize_nn = P.ResizeNearestNeighbor((3, 3))
    output = resize_nn(input_tensor)
    np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
Ejemplo n.º 12
0
def test():
    """The function of eval."""
    start_time = time.time()
    args = parse_args()

    # logger
    args.outputs_dir = os.path.join(args.log_path,
                                    datetime.datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S'))
    rank_id = int(os.environ.get('RANK_ID'))
    args.logger = get_logger(args.outputs_dir, rank_id)

    context.reset_auto_parallel_context()
    parallel_mode = ParallelMode.STAND_ALONE
    context.set_auto_parallel_context(parallel_mode=parallel_mode, mirror_mean=True, device_num=1)

    args.logger.info('Creating Network....')
    network = YOLOV3DarkNet53(is_training=False)

    args.logger.info(args.pretrained)
    if os.path.isfile(args.pretrained):
        param_dict = load_checkpoint(args.pretrained)
        param_dict_new = {}
        for key, values in param_dict.items():
            if key.startswith('moments.'):
                continue
            elif key.startswith('yolo_network.'):
                param_dict_new[key[13:]] = values
            else:
                param_dict_new[key] = values
        load_param_into_net(network, param_dict_new)
        args.logger.info('load_model {} success'.format(args.pretrained))
    else:
        args.logger.info('{} not exists or not a pre-trained file'.format(args.pretrained))
        assert FileNotFoundError('{} not exists or not a pre-trained file'.format(args.pretrained))
        exit(1)

    data_root = args.data_root
    ann_file = args.annFile

    config = ConfigYOLOV3DarkNet53()
    if args.testing_shape:
        config.test_img_shape = conver_testing_shape(args)

    ds, data_size = create_yolo_dataset(data_root, ann_file, is_training=False, batch_size=args.per_batch_size,
                                        max_epoch=1, device_num=1, rank=rank_id, shuffle=False,
                                        config=config)

    args.logger.info('testing shape : {}'.format(config.test_img_shape))
    args.logger.info('totol {} images to eval'.format(data_size))

    network.set_train(False)

    # init detection engine
    detection = DetectionEngine(args)

    input_shape = Tensor(tuple(config.test_img_shape), ms.float32)
    args.logger.info('Start inference....')
    for i, data in enumerate(ds.create_dict_iterator()):
        image = Tensor(data["image"])

        image_shape = Tensor(data["image_shape"])
        image_id = Tensor(data["img_id"])

        prediction = network(image, input_shape)
        output_big, output_me, output_small = prediction
        output_big = output_big.asnumpy()
        output_me = output_me.asnumpy()
        output_small = output_small.asnumpy()
        image_id = image_id.asnumpy()
        image_shape = image_shape.asnumpy()

        detection.detect([output_small, output_me, output_big], args.per_batch_size, image_shape, image_id)
        if i % 1000 == 0:
            args.logger.info('Processing... {:.2f}% '.format(i * args.per_batch_size / data_size * 100))

    args.logger.info('Calculating mAP...')
    detection.do_nms_for_results()
    result_file_path = detection.write_result()
    args.logger.info('result file path: {}'.format(result_file_path))
    eval_result = detection.get_eval_result()

    cost_time = time.time() - start_time
    args.logger.info('\n=============coco eval reulst=========\n' + eval_result)
    args.logger.info('testing cost time {:.2f}h'.format(cost_time / 3600.))
Ejemplo n.º 13
0
def train_on_gpu():
    config = config_gpu_quant
    print("training args: {}".format(args_opt))
    print("training configure: {}".format(config))

    # define network
    network = mobilenetV2(num_classes=config.num_classes)
    # define loss
    if config.label_smooth > 0:
        loss = CrossEntropyWithLabelSmooth(smooth_factor=config.label_smooth,
                                           num_classes=config.num_classes)
    else:
        loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
    # define dataset
    epoch_size = config.epoch_size
    dataset = create_dataset(dataset_path=args_opt.dataset_path,
                             do_train=True,
                             config=config,
                             device_target=args_opt.device_target,
                             repeat_num=1,
                             batch_size=config.batch_size)
    step_size = dataset.get_dataset_size()
    # resume
    if args_opt.pre_trained:
        param_dict = load_checkpoint(args_opt.pre_trained)
        load_nonquant_param_into_quant_net(network, param_dict)

    # convert fusion network to quantization aware network
    quantizer = QuantizationAwareTraining(bn_fold=True,
                                          per_channel=[True, False],
                                          symmetric=[False, False],
                                          freeze_bn=1000000,
                                          quant_delay=step_size * 2)
    network = quantizer.quantize(network)

    # get learning rate
    loss_scale = FixedLossScaleManager(config.loss_scale,
                                       drop_overflow_update=False)
    lr = Tensor(
        get_lr(global_step=config.start_epoch * step_size,
               lr_init=0,
               lr_end=0,
               lr_max=config.lr,
               warmup_epochs=config.warmup_epochs,
               total_epochs=epoch_size + config.start_epoch,
               steps_per_epoch=step_size))

    # define optimization
    opt = nn.Momentum(
        filter(lambda x: x.requires_grad, network.get_parameters()), lr,
        config.momentum, config.weight_decay, config.loss_scale)
    # define model
    model = Model(network,
                  loss_fn=loss,
                  optimizer=opt,
                  loss_scale_manager=loss_scale)

    print("============== Starting Training ==============")
    callback = [Monitor(lr_init=lr.asnumpy())]
    ckpt_save_dir = config.save_checkpoint_path + "ckpt_" + str(
        get_rank()) + "/"
    if config.save_checkpoint:
        config_ck = CheckpointConfig(
            save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
            keep_checkpoint_max=config.keep_checkpoint_max)
        ckpt_cb = ModelCheckpoint(prefix="mobilenetV2",
                                  directory=ckpt_save_dir,
                                  config=config_ck)
        callback += [ckpt_cb]
    model.train(epoch_size, dataset, callbacks=callback)
    print("============== End Training ==============")
Ejemplo n.º 14
0
def test():
    """test method"""

    # init distributed
    if args.is_distributed:
        init()
        args.rank = get_rank()
        args.group_size = get_group_size()

    # logger
    args.outputs_dir = os.path.join(
        args.log_path,
        datetime.datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S'))

    args.logger = get_logger(args.outputs_dir, args.rank)

    context.reset_auto_parallel_context()
    if args.is_distributed:
        parallel_mode = ParallelMode.DATA_PARALLEL
    else:
        parallel_mode = ParallelMode.STAND_ALONE
    context.set_auto_parallel_context(parallel_mode=parallel_mode,
                                      gradients_mean=True,
                                      device_num=1)

    args.logger.info('Creating Network....')
    network = YOLOV4CspDarkNet53(is_training=False)

    args.logger.info(args.pretrained)
    if os.path.isfile(args.pretrained):
        param_dict = load_checkpoint(args.pretrained)
        param_dict_new = {}
        for key, values in param_dict.items():
            if key.startswith('moments.'):
                continue
            elif key.startswith('yolo_network.'):
                param_dict_new[key[13:]] = values
            else:
                param_dict_new[key] = values
        load_param_into_net(network, param_dict_new)
        args.logger.info('load_model {} success'.format(args.pretrained))
    else:
        args.logger.info('{} not exists or not a pre-trained file'.format(
            args.pretrained))
        assert FileNotFoundError(
            '{} not exists or not a pre-trained file'.format(args.pretrained))
        exit(1)

    data_root = args.data_root
    # annFile = args.annFile

    config = ConfigYOLOV4CspDarkNet53()
    if args.testing_shape:
        config.test_img_shape = convert_testing_shape(args)

    data_txt = os.path.join(args.data_dir, 'testdev2017.txt')
    ds, data_size = create_yolo_datasetv2(data_root,
                                          data_txt=data_txt,
                                          batch_size=args.per_batch_size,
                                          max_epoch=1,
                                          device_num=args.group_size,
                                          rank=args.rank,
                                          shuffle=False,
                                          config=config)

    args.logger.info('testing shape : {}'.format(config.test_img_shape))
    args.logger.info('totol {} images to eval'.format(data_size))

    network.set_train(False)

    # init detection engine
    detection = DetectionEngine(args)

    input_shape = Tensor(tuple(config.test_img_shape), ms.float32)
    args.logger.info('Start inference....')
    for i, data in enumerate(ds.create_dict_iterator()):
        image = Tensor(data["image"])

        image_shape = Tensor(data["image_shape"])
        image_id = Tensor(data["img_id"])

        prediction = network(image, input_shape)
        output_big, output_me, output_small = prediction
        output_big = output_big.asnumpy()
        output_me = output_me.asnumpy()
        output_small = output_small.asnumpy()
        image_id = image_id.asnumpy()
        image_shape = image_shape.asnumpy()

        detection.detect([output_small, output_me, output_big],
                         args.per_batch_size, image_shape, image_id)
        if i % 1000 == 0:
            args.logger.info('Processing... {:.2f}% '.format(
                i * args.per_batch_size / data_size * 100))

    args.logger.info('Calculating mAP...')
    detection.do_nms_for_results()
    result_file_path = detection.write_result()
    args.logger.info('result file path: {}'.format(result_file_path))
def test_mobilenetv2_quant():
    set_seed(1)
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    config = config_ascend_quant
    print("training configure: {}".format(config))

    epoch_size = config.epoch_size

    # define network
    network = mobilenetV2(num_classes=config.num_classes)
    # define loss
    if config.label_smooth > 0:
        loss = CrossEntropyWithLabelSmooth(smooth_factor=config.label_smooth,
                                           num_classes=config.num_classes)
    else:
        loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
    # define dataset
    dataset = create_dataset(dataset_path=dataset_path,
                             config=config,
                             repeat_num=1,
                             batch_size=config.batch_size)
    step_size = dataset.get_dataset_size()

    # convert fusion network to quantization aware network
    network = quant.convert_quant_network(network,
                                          bn_fold=True,
                                          per_channel=[True, False],
                                          symmetric=[True, False])

    # get learning rate
    lr = Tensor(
        get_lr(global_step=config.start_epoch * step_size,
               lr_init=0,
               lr_end=0,
               lr_max=config.lr,
               warmup_epochs=config.warmup_epochs,
               total_epochs=epoch_size + config.start_epoch,
               steps_per_epoch=step_size))

    # define optimization
    opt = nn.Momentum(
        filter(lambda x: x.requires_grad, network.get_parameters()), lr,
        config.momentum, config.weight_decay)
    # define model
    model = Model(network, loss_fn=loss, optimizer=opt)

    print("============== Starting Training ==============")
    monitor = Monitor(lr_init=lr.asnumpy(),
                      step_threshold=config.step_threshold)
    callback = [monitor]
    model.train(epoch_size,
                dataset,
                callbacks=callback,
                dataset_sink_mode=False)
    print("============== End Training ==============")

    expect_avg_step_loss = 2.32
    avg_step_loss = np.mean(np.array(monitor.losses))

    print("average step loss:{}".format(avg_step_loss))
    assert avg_step_loss < expect_avg_step_loss
Ejemplo n.º 16
0
def main():
    parser = argparse.ArgumentParser(description="retinanet training")
    parser.add_argument("--only_create_dataset", type=ast.literal_eval, default=False,
                        help="If set it true, only create Mindrecord, default is False.")
    parser.add_argument("--distribute", type=ast.literal_eval, default=False,
                        help="Run distribute, default is False.")
    parser.add_argument("--device_id", type=int, default=0, help="Device id, default is 0.")
    parser.add_argument("--device_num", type=int, default=1, help="Use device nums, default is 1.")
    parser.add_argument("--lr", type=float, default=0.1, help="Learning rate, default is 0.1.")
    parser.add_argument("--mode", type=str, default="sink", help="Run sink mode or not, default is sink.")
    parser.add_argument("--dataset", type=str, default="coco", help="Dataset, default is coco.")
    parser.add_argument("--epoch_size", type=int, default=500, help="Epoch size, default is 500.")
    parser.add_argument("--batch_size", type=int, default=32, help="Batch size, default is 32.")
    parser.add_argument("--pre_trained", type=str, default=None, help="Pretrained Checkpoint file path.")
    parser.add_argument("--pre_trained_epoch_size", type=int, default=0, help="Pretrained epoch size.")
    parser.add_argument("--save_checkpoint_epochs", type=int, default=1, help="Save checkpoint epochs, default is 1.")
    parser.add_argument("--loss_scale", type=int, default=1024, help="Loss scale, default is 1024.")
    parser.add_argument("--filter_weight", type=ast.literal_eval, default=False,
                        help="Filter weight parameters, default is False.")
    parser.add_argument("--run_platform", type=str, default="Ascend", choices=("Ascend"),
                        help="run platform, only support Ascend.")
    args_opt = parser.parse_args()

    if args_opt.run_platform == "Ascend":
        context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
        if args_opt.distribute:
            if os.getenv("DEVICE_ID", "not_set").isdigit():
                context.set_context(device_id=int(os.getenv("DEVICE_ID")))
            init()
            device_num = args_opt.device_num
            rank = get_rank()
            context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, gradients_mean=True,
                                              device_num=device_num)
        else:
            rank = 0
            device_num = 1
            context.set_context(device_id=args_opt.device_id)

    else:
        raise ValueError("Unsupported platform.")

    mindrecord_file = create_mindrecord(args_opt.dataset, "retinanet.mindrecord", True)

    if not args_opt.only_create_dataset:
        loss_scale = float(args_opt.loss_scale)

        # When create MindDataset, using the fitst mindrecord file, such as retinanet.mindrecord0.
        dataset = create_retinanet_dataset(mindrecord_file, repeat_num=1,
                                           batch_size=args_opt.batch_size, device_num=device_num, rank=rank)

        dataset_size = dataset.get_dataset_size()
        print("Create dataset done!")


        backbone = resnet50(config.num_classes)
        retinanet = retinanet50(backbone, config)
        net = retinanetWithLossCell(retinanet, config)
        net.to_float(mindspore.float16)
        init_net_param(net)

        if args_opt.pre_trained:
            if args_opt.pre_trained_epoch_size <= 0:
                raise KeyError("pre_trained_epoch_size must be greater than 0.")
            param_dict = load_checkpoint(args_opt.pre_trained)
            if args_opt.filter_weight:
                filter_checkpoint_parameter(param_dict)
            load_param_into_net(net, param_dict)

        lr = Tensor(get_lr(global_step=config.global_step,
                           lr_init=config.lr_init, lr_end=config.lr_end_rate * args_opt.lr, lr_max=args_opt.lr,
                           warmup_epochs1=config.warmup_epochs1, warmup_epochs2=config.warmup_epochs2,
                           warmup_epochs3=config.warmup_epochs3, warmup_epochs4=config.warmup_epochs4,
                           warmup_epochs5=config.warmup_epochs5, total_epochs=args_opt.epoch_size,
                           steps_per_epoch=dataset_size))
        opt = nn.Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr,
                          config.momentum, config.weight_decay, loss_scale)
        net = TrainingWrapper(net, opt, loss_scale)
        model = Model(net)
        print("Start train retinanet, the first epoch will be slower because of the graph compilation.")
        cb = [TimeMonitor(), LossMonitor()]
        cb += [Monitor(lr_init=lr.asnumpy())]
        config_ck = CheckpointConfig(save_checkpoint_steps=dataset_size * args_opt.save_checkpoint_epochs,
                                     keep_checkpoint_max=config.keep_checkpoint_max)
        ckpt_cb = ModelCheckpoint(prefix="retinanet", directory=config.save_checkpoint_path, config=config_ck)
        if args_opt.distribute:
            if rank == 0:
                cb += [ckpt_cb]
            model.train(args_opt.epoch_size, dataset, callbacks=cb, dataset_sink_mode=True)
        else:
            cb += [ckpt_cb]
            model.train(args_opt.epoch_size, dataset, callbacks=cb, dataset_sink_mode=True)