def train_with_data_aug(train_augs, test_augs, lr=0.001): batch_size, ctx, net = 50, try_all_gpus(), d2l.resnet18(10) # 256 net.initialize(ctx=ctx, init=init.Xavier()) trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate':lr}) loss = gloss.SoftmaxCrossEntropyLoss() train_iter = load_cifar10(True, train_augs, batch_size) test_iter = load_cifar10(False, test_augs, batch_size) train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs=10)
def get_net(ctx): num_classes = 10 net = resnet18(num_classes) net.initialize(ctx=ctx, init=init.Xavier()) return net
def evaluate_accuracy(net, data_iter, ctx=[mx.cpu()]): if isinstance(ctx, mx.Context): ctx = [ctx] acc_sum, n = 0, 0 for batch in data_iter: Xs, ys, _ = _get_batch(batch, ctx) for X, y in zip(Xs, ys): acc_sum += (net(X).argmax(axis=1) == y).sum().copyto(mx.cpu()) n += y.size acc_sum.wait_to_read() return acc_sum.asscalar() / n net = d2l.resnet18(10) loss = gloss.SoftmaxCrossEntropyLoss() ctx = try_all_gpus() net.initialize(ctx=ctx, init=init.Xavier()) batch_size = 1000 train_iter = getCifar10(True, batch_size, aug_method) test_iter = getCifar10(False, batch_size, no_aug) lr = 0.01 trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': lr}) def train(net, train_iter, test_iter, num_epoches, ctxes, loss, trainer): train_acc_sum, train_l_sum, acc_count, l_count = 0, 0, 0, 0 for epoch in range(num_epoches): for i, batch in enumerate(train_iter): Xs, ys, batch_size = _get_batch(batch, ctxes)