def get_mnist_iterator(): train_set = MNIST(train=True, transform=dummy_transform) train_iter = gluon.data.DataLoader(train_set, args.batch_size, True, num_workers=args.j, last_batch='discard') val_set = MNIST(train=False, transform=dummy_transform) val_iter = gluon.data.DataLoader(val_set, args.batch_size, False, num_workers=args.j) return train_iter, val_iter, len(train_set)
def main(): ctx = mx.cpu() batch_size = 1024 random.seed(47) mnist_train = MNIST(train=True) # 加载训练 tr_data = mnist_train._data.reshape((-1, 28 * 28)) # 数据 tr_label = mnist_train._label # 标签 mnist_test = MNIST(train=False) # 加载测试 te_data = mnist_test._data.reshape((-1, 28 * 28)) # 数据 te_label = mnist_test._label # 标签 def transform(data_, label_): return data_.astype(np.float32) / 255., label_.astype(np.float32) train_data = DataLoader( TripletDataset(rd=tr_data, rl=tr_label, transform=transform), batch_size, shuffle=True) test_data = DataLoader( TripletDataset(rd=te_data, rl=te_label, transform=transform), batch_size, shuffle=True) base_net = Sequential() with base_net.name_scope(): base_net.add(Dense(256, activation='relu')) base_net.add(Dense(128, activation='relu')) base_net.collect_params().initialize(mx.init.Uniform(scale=0.1), ctx=ctx) triplet_loss = gluon.loss.TripletLoss() # TripletLoss损失函数 trainer_triplet = gluon.Trainer(base_net.collect_params(), 'sgd', {'learning_rate': 0.05}) for epoch in range(10): curr_loss = 0.0 for i, (data, _) in enumerate(train_data): data = data.as_in_context(ctx) anc_ins, pos_ins, neg_ins = data[:, 0], data[:, 1], data[:, 2] with autograd.record(): inter1 = base_net(anc_ins) inter2 = base_net(pos_ins) inter3 = base_net(neg_ins) loss = triplet_loss(inter1, inter2, inter3) # Triplet Loss loss.backward() trainer_triplet.step(batch_size) curr_loss = mx.nd.mean(loss).asscalar() # print('Epoch: %s, Batch: %s, Triplet Loss: %s' % (epoch, i, curr_loss)) print('Epoch: %s, Triplet Loss: %s' % (epoch, curr_loss)) evaluate_net(base_net, test_data, ctx=ctx) # 数据可视化 te_data, te_label = transform(te_data, te_label) tb_projector(te_data.asnumpy(), te_label, os.path.join(ROOT_DIR, 'logs', 'origin')) te_res = base_net(te_data) tb_projector(te_res.asnumpy(), te_label, os.path.join(ROOT_DIR, 'logs', 'triplet'))
def getData(): # Download and load the training data trainSet: MNIST = MNIST('~/.mxnet/MNIST_data/', train=True).transform_first(preprocessor) trainLoader: DataLoader = DataLoader(trainSet, batch_size=64, shuffle=False, last_batch='keep') return trainLoader
def load_data(self): """ 加载MNIST数据集 """ def transform(data, label): return data.astype(np.float32) / 255., label.astype(np.float32) # 训练和测试数据 train_data = DataLoader(MNIST(train=True, transform=transform), self.batch_size, shuffle=True) valid_data = DataLoader(MNIST(train=False, transform=transform), self.batch_size, shuffle=False) # mnist = get_mnist() # train_data = NDArrayIter(mnist["train_data"], mnist["train_label"], self.batch_size) # valid_data = NDArrayIter(mnist["test_data"], mnist["test_label"], self.batch_size) return train_data, valid_data
def data_loader(train, batch_size, num_workers): dataset = MNIST(train=train, transform=transform) return DataLoader(dataset, batch_size, shuffle=train, num_workers=num_workers)
def train(): epochs = 100 lr = 0.01 lr_steps = [40, 70, np.inf] momentum = 0.9 wd = 5e-4 plot_period = 5 ctx = [mx.gpu(i) for i in range(2)] batch_size = 64 scale = 10 margin = 0.2 train_set = MNIST(train=True, transform=transform_train) train_data = gluon.data.DataLoader(train_set, batch_size, True, num_workers=4, last_batch='discard') val_set = MNIST(train=False, transform=transform_val) val_data = gluon.data.DataLoader(val_set, batch_size, shuffle=False, num_workers=4) net = LeNetPlus(embedding_size=2, feature_norm=True, weight_norm=True) net.initialize(init=mx.init.MSRAPrelu(), ctx=ctx) # net.load_parameters("./pretrained_mnist.params", ctx=ctx) net.hybridize() loss = CosLoss(classes=10, s=scale, m=margin) train_params = net.collect_params() trainer = gluon.Trainer(train_params, 'sgd', { 'learning_rate': lr, 'momentum': momentum, 'wd': wd }) lr_counter = 0 metric = mtc.Accuracy() num_batch = len(train_data) for epoch in range(epochs + 1): if epoch == lr_steps[lr_counter]: trainer.set_learning_rate(trainer.learning_rate * 0.1) lr_counter += 1 if (epoch % plot_period) == 0: plot = True else: plot = False train_loss = 0 metric.reset() tic = time.time() ebs = [] lbs = [] for batch in train_data: data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0, even_split=False) labels = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0, even_split=False) with ag.record(): ots = [net(X) for X in data] embedds = [ot[0] for ot in ots] outputs = [ot[1] for ot in ots] losses = [loss(yhat, y) for yhat, y in zip(outputs, labels)] for l in losses: ag.backward(l) if plot: for es, ls in zip(embedds, labels): assert len(es) == len(ls) for idx in range(len(es)): ebs.append(es[idx].asnumpy()) lbs.append(ls[idx].asscalar()) trainer.step(batch_size) metric.update(labels, outputs) train_loss += sum([l.mean().asscalar() for l in losses]) / len(losses) _, train_acc = metric.get() train_loss /= num_batch val_acc, val_loss, val_ebs, val_lbs = validate(net, val_data, ctx, loss, plot) if plot: ebs = np.vstack(ebs) lbs = np.hstack(lbs) plot_result( ebs, lbs, os.path.join("../../resources", "cosloss-train-epoch{}.png".format(epoch))) plot_result( val_ebs, val_lbs, os.path.join("../../resources", "cosloss-val-epoch{}.png".format(epoch))) toc = time.time() print('[epoch % 3d] train accuracy: %.6f, train loss: %.6f | ' 'val accuracy: %.6f, val loss: %.6f, time: %.6f' % (epoch, train_acc, train_loss, val_acc, val_loss, toc - tic))
def train(): epochs = 101 lr = 0.1 momentum = 0.9 wd = 5e-4 plot_period = 20 ctx = [mx.gpu(i) for i in range(2)] batch_size = 256 train_set = MNIST(train=True, transform=transform_train) train_data = gluon.data.DataLoader(train_set, batch_size, True, num_workers=4, last_batch='discard') val_set = MNIST(train=False, transform=transform_val) val_data = gluon.data.DataLoader(val_set, batch_size, shuffle=False, num_workers=4) net = MnistNet(embedding_size=2) net.initialize(init=mx.init.MSRAPrelu(), ctx=ctx) net.hybridize() loss = CenterLoss(10, 2, 1) loss.initialize(ctx=ctx) num_batches = len(train_set) // batch_size train_params = net.collect_params() train_params.update(loss.params) lr_scheduler = LRScheduler("cosine", lr, niters=num_batches, nepochs=epochs, targetlr=1e-8, warmup_epochs=10, warmup_lr=0.001) trainer = gluon.Trainer(train_params, 'nag', {'lr_scheduler': lr_scheduler, 'momentum': momentum, 'wd': wd}) metric = mtc.Accuracy() num_batch = len(train_data) for epoch in range(epochs): plot = True if (epoch % plot_period) == 0 else False train_loss = 0 metric.reset() tic = time.time() ebs, lbs = [], [] for i, batch in enumerate(train_data): data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0, even_split=False) labels = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0, even_split=False) with ag.record(): ots = [net(X) for X in data] embedds = [ot[0] for ot in ots] outputs = [ot[1] for ot in ots] losses = [loss(yhat, y, emb) for yhat, y, emb in zip(outputs, labels, embedds)] for l in losses: ag.backward(l) if plot: for es, ls in zip(embedds, labels): assert len(es) == len(ls) for idx in range(len(es)): ebs.append(es[idx].asnumpy()) lbs.append(ls[idx].asscalar()) lr_scheduler.update(i, epoch) trainer.step(batch_size) metric.update(labels, outputs) train_loss += sum([l.mean().asscalar() for l in losses]) / len(losses) _, train_acc = metric.get() train_loss /= num_batch val_acc, val_loss, val_ebs, val_lbs = validate(net, val_data, ctx, loss, plot) toc = time.time() print('[epoch % 3d] train accuracy: %.6f, train loss: %.6f | ' 'val accuracy: %.6f, val loss: %.6f, time: %.6f' % (epoch, train_acc, train_loss, val_acc, val_loss, toc - tic)) if plot: ebs, lbs = np.vstack(ebs), np.hstack(lbs) plot_result(ebs, lbs, os.path.join("../../resources", "center-train-epoch{}.png".format(epoch))) plot_result(val_ebs, val_lbs, os.path.join("../../resources", "center-val-epoch{}.png".format(epoch)))
gluon.data.vision.transforms.Resize(opt.imageSize, keep_ratio=True, interpolation=3), transforms.CenterCrop(opt.imageSize), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) elif opt.dataset == 'cifar10': dataset = CIFAR10(root=opt.dataroot, train=True).transform_first(transforms.Compose([ gluon.data.vision.transforms.Resize(opt.imageSize, keep_ratio=True, interpolation=3), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) ) elif opt.dataset == 'mnist': dataset = MNIST(root=opt.dataroot, train=True).transform_first(transforms.Compose([ gluon.data.vision.transforms.Resize(opt.imageSize, keep_ratio=True, interpolation=3), transforms.ToTensor(), transforms.Normalize((0.5), (0.5)), ])) elif opt.dataset == 'rem_face': dataset, _ = load_rem_face() dataset = dataset.transform_first(transforms.Compose([ gluon.data.vision.transforms.Resize(opt.imageSize, keep_ratio=True, interpolation=3), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])) assert dataset dataloader = DataLoader(dataset, batch_size=opt.batchSize, shuffle=True, num_workers=int(opt.workers)) print('finish init dataloader') ngpu = int(opt.ngpu) nz = int(opt.nz)
import mxnet.gluon as gl from mxnet import autograd, image from mxnet.gluon import nn from mxnet.gluon.data import DataLoader from mxnet.gluon.data.vision import transforms, MNIST from mxnet.gluon.nn import Sequential from mxnet.ndarray import NDArray transform: Sequential = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=0.5, std=0.5) ]) # Download and load the training data trainSet: MNIST = MNIST('~/.mxnet/MNIST_data/', train=True).transform_first(transform) trainLoader: DataLoader = DataLoader(trainSet, batch_size=64, shuffle=True) # Build a feed-forward network model = nn.Sequential() # with model.name_scope(): model.add( nn.Dense(128, activation='relu'), # nn.Activation('relu'), nn.Dense(64, activation='relu'), nn.Dense(10) ) model.initialize() criterion = gl.loss.SoftmaxCrossEntropyLoss() optimizer = gl.Trainer(model.collect_params(), 'sgd', {'learning_rate': 0.01})