Exemplo n.º 1
0
    def _check_context(self, context):
        # infer available context
        gpus = num_gpus()
        available_gpus = [gpu(i) for i in range(gpus)]

        if context:
            # check context values, only accept Context or a list of Context
            if isinstance(context, Context):
                context = [context]
            elif isinstance(context, list) and all([isinstance(c, Context) for c in context]):
                context = context
            else:
                raise ValueError("context must be a Context or a list of Context, "
                                 "for example mx.cpu() or [mx.gpu(0), mx.gpu(1)], "
                                 "refer to mxnet.Context:{}".format(context))
            for ctx in context:
                assert ctx in available_gpus or str(ctx).startswith('cpu'), \
                    "%s is not available, please make sure " \
                    "your context is in one of: mx.cpu(), %s" % \
                    (ctx, ", ".join([str(ctx) for ctx in available_gpus]))
        else:
            # provide default context
            if gpus > 0:
                # only use 1 GPU by default
                if gpus > 1:
                    warnings.warn("You have multiple GPUs, gpu(0) will be used by default."
                                  "To utilize all your GPUs, specify context as a list of gpus, "
                                  "e.g. context=[mx.gpu(0), mx.gpu(1)] ")
                context = [gpu(0)]
            else:
                context = [cpu()]
        return context
Exemplo n.º 2
0
    transforms.Normalize(0.13, 0.31),
])

batch_size = 256
train_data = gluon.data.DataLoader(mnist_train.transform_first(transformer),
                                   batch_size=batch_size,
                                   shuffle=True,
                                   num_workers=2)

mnist_valid = gluon.data.vision.FashionMNIST(train=False)
valid_data = gluon.data.DataLoader(mnist_valid.transform_first(transformer),
                                   batch_size=batch_size,
                                   num_workers=4)

# Set context and start training
ctx = gpu(0) if context.num_gpus() else cpu(0)
print("Using ctx: ", ctx)

softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
net = SimpleNet()
net.initialize(init=init.Xavier(), ctx=ctx)
trainer = gluon.Trainer(net.collect_params(), "sgd", {"learning_rate": 0.1})

for epoch in range(10):
    train_loss = 0
    train_acc = mx.metric.Accuracy()
    val_acc = mx.metric.Accuracy()

    for data, label in train_data:
        data = data.as_in_context(ctx)
        label = label.as_in_context(ctx)
Exemplo n.º 3
0
        cv2.destroyAllWindows()


def parseArgvs():
    parser = ArgumentParser(description='capture service')
    parser.add_argument("--mode", type=str, help="mode", choices=["dev", "prd"], default="dev")
    args = parser.parse_args()
    logger.info(args)
    return args


if __name__ == '__main__':
    import os
    from mxnet import context

    logger.info("num gpu:%s", context.num_gpus())
    os.environ["MXNET_CUDNN_AUTOTUNE_DEFAULT"] = "0"
    args = parseArgvs()
    logger.info('mode %s ', args.mode)
    signal.signal(signal.SIGINT, sigint_handler)
    signal.signal(signal.SIGHUP, sigint_handler)
    signal.signal(signal.SIGTERM, sigint_handler)
    is_sigint_up = False
    app = App(args.mode)
    # given video path, predict and show
    while not is_sigint_up:
        app.detectVideo()
        # app.test()
    logger.info("while end")
    app.clear()
Exemplo n.º 4
0
def try_all_gpus():
    """Return all available GPUs, or [cpu(),] if no GPU exists."""
    ctxes = [context.gpu(i) for i in range(context.num_gpus())]
    return ctxes if ctxes else [context.cpu()]
Exemplo n.º 5
0
def try_gpu(i=0):
    """Return gpu(i) if exists, otherwise return cpu()."""
    return context.gpu(i) if context.num_gpus() >= i else context.cpu()
def _test_consistency(version: str, approx_func, sym: bool):
    """
    test rational activation function from keras package on test_data,
    validating that cuda and cpu results are consistent, i.e. that there is no significant
    difference between cuda and cpu results

    :param sym: use symbolic execution if True, else imperative execution
    :param approx_func: which function to use as initial shape
    :param version: which version of the function to test
    """
    # declare results
    cpu_result = None
    gpu_result = None

    # set cpu context and test
    with mx.Context(cpu(0)):
        # instantiate a tensor for testing
        test_data = mx.nd.array([-2., -1, 0., 1., 2.])

        init_fun_names = {
            LeakyReLU: 'leaky_relu',
            tanh: 'tanh',
            sigmoid: 'sigmoid'
        }

        # instantiate rational activation function under test on cpu
        cpu_fut = Rational(approx_func=init_fun_names.get(approx_func),
                           version=version,
                           cuda=False,
                           trainable=False)

        # create small neural networks and add futs as layers
        cpu_net = mx.gluon.nn.HybridSequential()
        with cpu_net.name_scope():
            cpu_net.add(cpu_fut)
        cpu_net.initialize()

        # trigger symbolic rather than imperative API, if specified
        if sym:
            cpu_net.hybridize()

        # run the function on test data
        cpu_result = cpu_net(test_data)

    # set gpu context and test
    assert num_gpus() > 0, 'tried to run on GPU, but none available.'
    with mx.Context(gpu(0)):
        # instantiate a tensor for testing
        test_data = mx.nd.array([-2., -1, 0., 1., 2.])

        init_fun_names = {
            LeakyReLU: 'leaky_relu',
            tanh: 'tanh',
            sigmoid: 'sigmoid'
        }

        # instantiate rational activation function under test on gpu
        gpu_fut = Rational(approx_func=init_fun_names.get(approx_func),
                           version=version,
                           cuda=True,
                           trainable=False)

        # create small neural networks and add futs as layers
        gpu_net = mx.gluon.nn.HybridSequential()
        with gpu_net.name_scope():
            gpu_net.add(gpu_fut)
        gpu_net.initialize()

        # trigger symbolic rather than imperative API, if specified
        if sym:
            gpu_net.hybridize()

        # run the function on test data
        gpu_result = gpu_net(test_data)

    # check that there is no significant difference between the results
    assert all(isclose(cpu_result.asnumpy(), gpu_result.asnumpy(), atol=1e-06))
Exemplo n.º 7
0
# #### 3.1.1 Test CNN model

# In[9]:

net.hybridize()
net.initialize(force_reinit=True)

X = nd.random.uniform(shape=(1, 3, 32, 32))

net(X)

# ### 3.2 Set device

# In[10]:

ctx = context.gpu(0) if context.num_gpus() else context.cpu()

LOG(INFO, 'Device in Use:', ctx)

# ### 3.3 Define Learning Rate Scheduler

# In[11]:

# learning rate scheduler
iter_per_epochs = math.ceil(len(train_dataset) / BATCH_SIZE)
niterations = cfg.NEPOCHS * iter_per_epochs

lr_scheduler = learning.OneCycleScheduler(start_lr=0.01,
                                          max_lr=0.05,
                                          cycle_length=40 * iter_per_epochs,
                                          cooldown_length=niterations -
Exemplo n.º 8
0
                   type=str2bool,
                   nargs='?',
                   const=True,
                   default=True,
                   help="Use mutation when creating children")
    p.add_argument('--do_crossover',
                   type=str2bool,
                   nargs='?',
                   const=True,
                   default=True,
                   help="Use crossover when creating children")

    # Log parameters
    p.add_argument('--logger_filename',
                   type=str,
                   default="output.json",
                   help="Filename of json output file")

    # Miscellaneous parameters
    p.add_argument(
        '--device',
        type=str,
        default=cuda.gpu(0) if cuda.num_gpus() else cuda.cpu(),
        help=
        "Specifies on which device the neural network of the agent will be run"
    )

    args = p.parse_args()

    main(args)