Exemplo n.º 1
0
def train_vgg11():
    num_outputs = 10
    architecture = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512))
    net = nn.Sequential()
    with net.name_scope():
        net.add(vgg_stack(architecture), nn.Flatten(),
                nn.Dense(4096, activation="relu"), nn.Dropout(.5),
                nn.Dense(4096, activation="relu"), nn.Dropout(.5),
                nn.Dense(num_outputs))

    # 初始化
    ctx = gpu_or_cpu()
    net.initialize(ctx=ctx, init=init.Xavier())
    print('initialize weight on', ctx)

    # 获取数据
    batch_size = 64
    resize = 96
    train_data, test_data = load_data(DataType.FASHION_MNIST,
                                      batch_size,
                                      resize=resize)

    # 训练
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    trainer = gluon.Trainer(net.collect_params(), 'sgd',
                            {'learning_rate': 0.05})
    train(train_data, test_data, net, loss, trainer, ctx, num_epochs=20)
Exemplo n.º 2
0
def train_lenet():
    net = nn.Sequential()
    with net.name_scope():
        net.add(
            nn.Conv2D(channels=20, kernel_size=5, activation='relu'),
            nn.MaxPool2D(pool_size=2, strides=2),
            nn.Conv2D(channels=50, kernel_size=3, activation='relu'),
            nn.MaxPool2D(pool_size=2, strides=2),
            nn.Flatten(),
            nn.Dense(128, activation="relu"),
            nn.Dense(10)
        )

    # 初始化
    ctx = gpu_or_cpu()
    net.initialize(ctx=ctx)
    print('initialize weight on', ctx)

    # 获取数据
    batch_size = 256
    train_data, test_data = load_data(DataType.FASHION_MNIST, batch_size)

    # 训练
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.5})
    train(train_data, test_data, net, loss, trainer, ctx, num_epochs=5)
Exemplo n.º 3
0
def train_nin():
    net = nn.Sequential()
    with net.name_scope():
        net.add(
            mlpconv(96, 11, 0, strides=4),
            mlpconv(256, 5, 2),
            mlpconv(384, 3, 1),
            nn.Dropout(.5),
            mlpconv(10, 3, 1, max_pooling=False),
            nn.GlobalAvgPool2D(),
            nn.Flatten()
        )

    # 初始化
    ctx = gpu_or_cpu()
    net.initialize(ctx=ctx, init=init.Xavier())
    print('initialize weight on', ctx)

    # 获取数据
    batch_size = 64
    resize = 224
    train_data, test_data = load_data(DataType.FASHION_MNIST, batch_size, resize=resize)

    # 训练
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1})
    train(train_data, test_data, net, loss, trainer, ctx, num_epochs=20)
Exemplo n.º 4
0
def train_alexnet():
    net = nn.Sequential()
    with net.name_scope():
        net.add(
            # 第一阶段
            nn.Conv2D(channels=96,
                      kernel_size=11,
                      strides=4,
                      activation='relu'),
            nn.MaxPool2D(pool_size=3, strides=2),
            # 第二阶段
            nn.Conv2D(channels=256,
                      kernel_size=5,
                      padding=2,
                      activation='relu'),
            nn.MaxPool2D(pool_size=3, strides=2),
            # 第三阶段
            nn.Conv2D(channels=384,
                      kernel_size=3,
                      padding=1,
                      activation='relu'),
            nn.Conv2D(channels=384,
                      kernel_size=3,
                      padding=1,
                      activation='relu'),
            nn.Conv2D(channels=256,
                      kernel_size=3,
                      padding=1,
                      activation='relu'),
            nn.MaxPool2D(pool_size=3, strides=2),
            # 第四阶段
            nn.Flatten(),
            nn.Dense(4096, activation="relu"),
            nn.Dropout(.5),
            # 第五阶段
            nn.Dense(4096, activation="relu"),
            nn.Dropout(.5),
            # 第六阶段
            nn.Dense(10))

    # 初始化
    ctx = gpu_or_cpu()
    net.initialize(ctx=ctx, init=init.Xavier())
    print('initialize weight on', ctx)

    # 获取数据
    batch_size = 64
    resize = 224
    train_data, test_data = load_data(DataType.FASHION_MNIST,
                                      batch_size,
                                      resize=resize)

    # 训练
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    trainer = gluon.Trainer(net.collect_params(), 'sgd',
                            {'learning_rate': 0.01})
    train(train_data, test_data, net, loss, trainer, ctx, num_epochs=20)
Exemplo n.º 5
0
def train_resnet():
    # 初始化
    ctx = gpu_or_cpu()
    net = ResNet(10)
    net.initialize(ctx=ctx, init=init.Xavier())
    print('initialize weight on', ctx)

    # 获取数据
    batch_size = 64
    resize = 96
    train_data, test_data = load_data(DataType.FASHION_MNIST,
                                      batch_size,
                                      resize=resize)

    # 训练
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    trainer = gluon.Trainer(net.collect_params(), 'sgd',
                            {'learning_rate': 0.05})
    train(train_data, test_data, net, loss, trainer, ctx, num_epochs=20)
Exemplo n.º 6
0
    train_ds = vision.ImageFolderDataset(os.path.join(input_str, 'train'),
                                         flag=1)
    valid_ds = vision.ImageFolderDataset(os.path.join(input_str, 'valid'),
                                         flag=1)
    test_ds = vision.ImageFolderDataset(os.path.join(input_str, 'test'),
                                        flag=1)

    loader = gluon.data.DataLoader
    train_valid_data = loader(train_valid_ds.transform_first(transform_train),
                              batch_size,
                              shuffle=True,
                              last_batch='keep')
    train_data = loader(train_ds.transform_first(transform_train),
                        batch_size,
                        shuffle=True,
                        last_batch='keep')
    valid_data = loader(valid_ds.transform_first(transform_test),
                        batch_size,
                        shuffle=True,
                        last_batch='keep')
    test_data = loader(test_ds.transform_first(transform_test),
                       batch_size,
                       shuffle=False,
                       last_batch='keep')

    ctx = gpu_or_cpu()
    num_outputs = 10
    filename = "data/mlp.params"
    # train_cifar10(ctx, num_outputs, filename, train_data, valid_data)
    classification(ctx, num_outputs, filename, test_data)