Esempio n. 1
0
def get_iters():
    # check data
    get_mnist_ubyte()

    batch_size = 100
    train_dataiter = mx.io.MNISTIter(
        image="s3://dlami-dataset-test/MXNet/train-images.idx3-ubyte",
        label="s3://dlami-dataset-test/MXNet/train-labels.idx1-ubyte",
        data_shape=(1, 28, 28),
        label_name='sm_label',
        batch_size=batch_size,
        shuffle=True,
        flat=False,
        silent=False,
        seed=10)
    val_dataiter = mx.io.MNISTIter(
        image="s3://dlami-dataset-test/MXNet/t10k-images.idx3-ubyte",
        label="s3://dlami-dataset-test/MXNet/t10k-labels.idx1-ubyte",
        data_shape=(1, 28, 28),
        label_name='sm_label',
        batch_size=batch_size,
        shuffle=True,
        flat=False,
        silent=False)
    return train_dataiter, val_dataiter
Esempio n. 2
0
    def get_iterator_impl_mnist(args, kv):
        """return train and val iterators for mnist"""
        # download data
        get_mnist_ubyte()
        flat = False if len(data_shape) != 1 else True

        train = mx.io.MNISTIter(
            image="data/train-images-idx3-ubyte",
            label="data/train-labels-idx1-ubyte",
            input_shape=data_shape,
            batch_size=args.batch_size,
            shuffle=True,
            flat=flat,
            num_parts=kv.num_workers,
            part_index=kv.rank)

        val = mx.io.MNISTIter(
            image="data/t10k-images-idx3-ubyte",
            label="data/t10k-labels-idx1-ubyte",
            input_shape=data_shape,
            batch_size=args.batch_size,
            flat=flat,
            num_parts=kv.num_workers,
            part_index=kv.rank)

        return (train, val)
Esempio n. 3
0
    def get_iters():
        # check data
        path = str(tmpdir)
        get_mnist_ubyte(path)

        batch_size = 100
        train_dataiter = mx.io.MNISTIter(
            image=os.path.join(path, 'train-images-idx3-ubyte'),
            label=os.path.join(path, 'train-labels-idx1-ubyte'),
            data_shape=(1, 28, 28),
            label_name='sm_label',
            batch_size=batch_size,
            shuffle=True,
            flat=False,
            silent=False,
            seed=10)
        val_dataiter = mx.io.MNISTIter(
            image=os.path.join(path, 't10k-images-idx3-ubyte'),
            label=os.path.join(path, 't10k-labels-idx1-ubyte'),
            data_shape=(1, 28, 28),
            label_name='sm_label',
            batch_size=batch_size,
            shuffle=True,
            flat=False,
            silent=False)
        return train_dataiter, val_dataiter
Esempio n. 4
0
def get_iters():
    # check data
    get_mnist_ubyte()

    batch_size = 100
    train_dataiter = mx.io.MNISTIter(
            image="data/train-images-idx3-ubyte",
            label="data/train-labels-idx1-ubyte",
            data_shape=(1, 28, 28),
            label_name='sm_label',
            batch_size=batch_size, shuffle=True, flat=False, silent=False, seed=10)
    val_dataiter = mx.io.MNISTIter(
            image="data/t10k-images-idx3-ubyte",
            label="data/t10k-labels-idx1-ubyte",
            data_shape=(1, 28, 28),
            label_name='sm_label',
            batch_size=batch_size, shuffle=True, flat=False, silent=False)
    return  train_dataiter, val_dataiter
Esempio n. 5
0
def get_binarized_mnist(batch_size, input_shape):
    """
    Returns training and validation iterators for binarized MNIST dataset
    """
    get_mnist_ubyte()
    flat = False if len(input_shape) == 3 else True

    train_dataiter = BinarizedMNISTIter(
        image=str(DATA_FOLDER_PATH / "train-images-idx3-ubyte"),
        label=str(DATA_FOLDER_PATH / "train-labels-idx1-ubyte"),
        input_shape=input_shape,
        batch_size=batch_size,
        flat=flat)

    val_dataiter = BinarizedMNISTIter(
        image=str(DATA_FOLDER_PATH / "t10k-images-idx3-ubyte"),
        label=str(DATA_FOLDER_PATH / "t10k-labels-idx1-ubyte"),
        input_shape=input_shape,
        batch_size=batch_size,
        flat=flat)

    return train_dataiter, val_dataiter
Esempio n. 6
0
    def get_iterator_impl_mnist(args, kv):
        """return train and val iterators for mnist"""
        # download data
        get_mnist_ubyte()
        flat = False if len(data_shape) != 1 else True

        train = mx.io.MNISTIter(image="data/train-images-idx3-ubyte",
                                label="data/train-labels-idx1-ubyte",
                                input_shape=data_shape,
                                batch_size=args.batch_size,
                                shuffle=True,
                                flat=flat,
                                num_parts=kv.num_workers,
                                part_index=kv.rank)

        val = mx.io.MNISTIter(image="data/t10k-images-idx3-ubyte",
                              label="data/t10k-labels-idx1-ubyte",
                              input_shape=data_shape,
                              batch_size=args.batch_size,
                              flat=flat,
                              num_parts=kv.num_workers,
                              part_index=kv.rank)

        return (train, val)
Esempio n. 7
0
def test_mlp(tmpdir):
    # symbol net
    batch_size = 100
    data = mx.symbol.Variable('data')
    fc1 = mx.symbol.FullyConnected(data, name='fc1', num_hidden=128)
    act1 = mx.symbol.Activation(fc1, name='relu1', act_type="relu")
    fc2 = mx.symbol.FullyConnected(act1, name='fc2', num_hidden=64)
    act2 = mx.symbol.Activation(fc2, name='relu2', act_type="relu")
    fc3 = mx.symbol.FullyConnected(act2, name='fc3', num_hidden=10)
    softmax = mx.symbol.SoftmaxOutput(fc3, name='sm')

    def accuracy(label, pred):
        py = np.argmax(pred, axis=1)
        return np.sum(py == label.astype(py)) / float(label.size)
        # currently mxnet.numpy (which used in gluon.metric) did not support "==" between different types

    num_epoch = 4
    prefix = './mlp'

    #check data
    path = str(tmpdir)
    get_mnist_ubyte(path)

    train_dataiter = mx.io.MNISTIter(
        image=os.path.join(path, 'train-images-idx3-ubyte'),
        label=os.path.join(path, 'train-labels-idx1-ubyte'),
        data_shape=(784, ),
        label_name='sm_label',
        batch_size=batch_size,
        shuffle=True,
        flat=True,
        silent=False,
        seed=10)
    val_dataiter = mx.io.MNISTIter(
        image=os.path.join(path, 't10k-images-idx3-ubyte'),
        label=os.path.join(path, 't10k-labels-idx1-ubyte'),
        data_shape=(784, ),
        label_name='sm_label',
        batch_size=batch_size,
        shuffle=True,
        flat=True,
        silent=False)
    # print logging by default
    logging.basicConfig(level=logging.DEBUG)

    model = mx.model.FeedForward.create(
        softmax,
        X=train_dataiter,
        eval_data=val_dataiter,
        eval_metric=mx.gluon.metric.np(accuracy),
        epoch_end_callback=mx.callback.do_checkpoint(prefix),
        ctx=[mx.cpu(i) for i in range(2)],
        num_epoch=num_epoch,
        learning_rate=0.1,
        wd=0.0004,
        momentum=0.9)

    logging.info('Finish traning...')
    prob = model.predict(val_dataiter)
    logging.info('Finish predict...')
    val_dataiter.reset()
    y = np.concatenate([batch.label[0].asnumpy()
                        for batch in val_dataiter]).astype('int')
    py = np.argmax(prob, axis=1)
    acc1 = float(np.sum(py == y)) / len(y)
    logging.info('final accuracy = %f', acc1)
    assert (acc1 > 0.94)

    # predict internal featuremaps
    internals = softmax.get_internals()
    fc2 = internals['fc2_output']
    mfeat = mx.model.FeedForward(symbol=fc2,
                                 arg_params=model.arg_params,
                                 aux_params=model.aux_params,
                                 allow_extra_params=True)
    feat = mfeat.predict(val_dataiter)
    assert feat.shape == (10000, 64)
    # pickle the model
    smodel = pickle.dumps(model)
    model2 = pickle.loads(smodel)
    prob2 = model2.predict(val_dataiter)
    assert np.sum(np.abs(prob - prob2)) == 0

    # load model from checkpoint
    model3 = mx.model.FeedForward.load(prefix, num_epoch)
    prob3 = model3.predict(val_dataiter)
    assert np.sum(np.abs(prob - prob3)) == 0

    # save model explicitly
    model.save(prefix, 128)
    model4 = mx.model.FeedForward.load(prefix, 128)
    prob4 = model4.predict(val_dataiter)
    assert np.sum(np.abs(prob - prob4)) == 0

    for i in range(num_epoch):
        os.remove('%s-%04d.params' % (prefix, i + 1))
    os.remove('%s-symbol.json' % prefix)
    os.remove('%s-0128.params' % prefix)
Esempio n. 8
0
fc2 = mx.symbol.FullyConnected(act1, name='fc2', num_hidden=64)
act2 = mx.symbol.Activation(fc2, name='relu2', act_type="relu")
fc3 = mx.symbol.FullyConnected(act2, name='fc3', num_hidden=10)
softmax = mx.symbol.SoftmaxOutput(fc3, name='sm')


def accuracy(label, pred):
    py = np.argmax(pred, axis=1)
    return np.sum(py == label) / float(label.size)


num_epoch = 4
prefix = './mlp'

#check data
get_mnist_ubyte()

train_dataiter = mx.io.MNISTIter(image="data/train-images-idx3-ubyte",
                                 label="data/train-labels-idx1-ubyte",
                                 data_shape=(784, ),
                                 label_name='sm_label',
                                 batch_size=batch_size,
                                 shuffle=True,
                                 flat=True,
                                 silent=False,
                                 seed=10)
val_dataiter = mx.io.MNISTIter(image="data/t10k-images-idx3-ubyte",
                               label="data/t10k-labels-idx1-ubyte",
                               data_shape=(784, ),
                               label_name='sm_label',
                               batch_size=batch_size,
def test_autograd(tmpdir):
    # define network
    def get_net():
        net = nn.Sequential()
        net.add(nn.Dense(128, activation='relu', prefix='fc1_'))
        net.add(nn.Dense(64, activation='relu', prefix='fc2_'))
        net.add(nn.Dense(10, prefix='fc3_'))
        return net

    path = str(tmpdir)
    get_mnist_ubyte(path)

    batch_size = 100
    train_data = mx.io.MNISTIter(image=os.path.join(path,
                                                    'train-images-idx3-ubyte'),
                                 label=os.path.join(path,
                                                    'train-labels-idx1-ubyte'),
                                 data_shape=(784, ),
                                 label_name='sm_label',
                                 batch_size=batch_size,
                                 shuffle=True,
                                 flat=True,
                                 silent=False,
                                 seed=10)
    val_data = mx.io.MNISTIter(image=os.path.join(path,
                                                  't10k-images-idx3-ubyte'),
                               label=os.path.join(path,
                                                  't10k-labels-idx1-ubyte'),
                               data_shape=(784, ),
                               label_name='sm_label',
                               batch_size=batch_size,
                               shuffle=True,
                               flat=True,
                               silent=False)

    def score(net, ctx_list):
        metric = mx.metric.Accuracy()
        val_data.reset()
        for batch in val_data:
            datas = gluon.utils.split_and_load(batch.data[0],
                                               ctx_list,
                                               batch_axis=0)
            labels = gluon.utils.split_and_load(batch.label[0],
                                                ctx_list,
                                                batch_axis=0)
            outputs = []
            for x in datas:
                outputs.append(net(x))
            metric.update(labels, outputs)
        return metric.get()[1]

    def train(net, epoch, ctx_list):
        net.collect_params().initialize(mx.init.Xavier(magnitude=2.24),
                                        ctx=ctx_list)
        trainer = gluon.Trainer(net.collect_params(), 'sgd',
                                {'learning_rate': 0.5})
        metric = mx.metric.Accuracy()
        loss = gluon.loss.SoftmaxCrossEntropyLoss()

        for i in range(epoch):
            train_data.reset()
            for batch in train_data:
                datas = gluon.utils.split_and_load(batch.data[0],
                                                   ctx_list,
                                                   batch_axis=0)
                labels = gluon.utils.split_and_load(batch.label[0],
                                                    ctx_list,
                                                    batch_axis=0)
                outputs = []
                with autograd.record():
                    for x, y in zip(datas, labels):
                        z = net(x)
                        L = loss(z, y)
                        L.backward()
                        outputs.append(z)
                trainer.step(batch.data[0].shape[0])
                metric.update(labels, outputs)
            name, acc = metric.get()
            metric.reset()
            print('training acc at epoch %d: %s=%f' % (i, name, acc))

    net1 = get_net()
    train(net1, 5, [mx.cpu(0), mx.cpu(1)])
    acc1 = score(net1, [mx.cpu(0)])
    acc2 = score(net1, [mx.cpu(0), mx.cpu(1)])
    assert acc1 > 0.95
    assert abs(acc1 - acc2) < 0.01
    net1.collect_params().save('mnist.params')

    net2 = get_net()
    net2.collect_params().load('mnist.params', ctx=[mx.cpu(0)])
    acc3 = score(net2, [mx.cpu(0)])
    assert abs(acc3 - acc1) < 0.0001
Esempio n. 10
0
fc1 = mx.symbol.FullyConnected(data, name='fc1', num_hidden=128)
act1 = mx.symbol.Activation(fc1, name='relu1', act_type="relu")
fc2 = mx.symbol.FullyConnected(act1, name = 'fc2', num_hidden = 64)
act2 = mx.symbol.Activation(fc2, name='relu2', act_type="relu")
fc3 = mx.symbol.FullyConnected(act2, name='fc3', num_hidden=10)
softmax = mx.symbol.SoftmaxOutput(fc3, name = 'sm')

def accuracy(label, pred):
    py = np.argmax(pred, axis=1)
    return np.sum(py == label) / float(label.size)

num_epoch = 4
prefix = './mlp'

#check data
get_mnist_ubyte()

train_dataiter = mx.io.MNISTIter(
        image="data/train-images-idx3-ubyte",
        label="data/train-labels-idx1-ubyte",
        data_shape=(784,),
        label_name='sm_label',
        batch_size=batch_size, shuffle=True, flat=True, silent=False, seed=10)
val_dataiter = mx.io.MNISTIter(
        image="data/t10k-images-idx3-ubyte",
        label="data/t10k-labels-idx1-ubyte",
        data_shape=(784,),
        label_name='sm_label',
        batch_size=batch_size, shuffle=True, flat=True, silent=False)

def test_mlp():