· Simple implementation using MXNet Fashionmnist data set classification

  • code
# % %
import sys
import time
from mxnet import gluon as gl
import mxnet as mx
from matplotlib import pyplot as plt
from mxnet import autograd, nd
import numpy as np

mnist_train = gl.data.vision.FashionMNIST(root="fashion-mnist/",train=True)
mnist_test = gl.data.vision.FashionMNIST(root="fashion-mnist/",train=False)
print(len(mnist_train), len(mnist_test))

"" I also have this problem and want to use the data set I downloaded earlier. However, MXNet's MNIST data set reading mechanism will verify the correctness of the data set, so MXNet will still download the data set downloaded by KERAS in the relevant location. Solution, I used MXNet to download the good fashion- MNist data set, uploaded, you can directly download to the corresponding location to decompress. Could also benefit from using a custom location: _mnist_train = gl. Data. Vision. FashionMNIST (root = "fashion - mnist/"," train "= True) _ just in writing code in decompression.." ""

# % %

batch_size = 100
transformer = gl.data.vision.transforms.ToTensor()
if sys.platform.startswith('win'):
    num_workers = 0  # 0 indicates that no additional process is required to speed up the reading of data
else:
    num_workers = 4


train_iter = gl.data.DataLoader(mnist_train.transform_first(transformer),
                              batch_size, shuffle=True,
                              num_workers=num_workers)
test_iter = gl.data.DataLoader(mnist_test.transform_first(transformer),
                             1000, shuffle=False,
                             num_workers=num_workers)



def get_fashion_mnist_labels(labels) :
    text_labels = ['t-shirt'.'trouser'.'pullover'.'dress'.'coat'.'sandal'.'shirt'.'sneaker'.'bag'.'ankle boot']
    return([text_labels[int(i.asnumpy())] for i in labels])

def show_img(x,y) :
    plt.figure("Image") Image window name
    plt.imshow(x.asnumpy()*255)
    plt.title(get_fashion_mnist_labels(y)) # Image title
    plt.show()


"""start = time.time() for X, y in train_iter: break print('%.2f sec' % (time.time() - start))"""

model = gl.nn.Sequential()
model.add(gl.nn.Dense(256,activation="relu"))
model.add()
model.add(gl.nn.Dense(10))
model.initialize(mx.init.Normal(sigma=0.01))

lr = 0.1
loss = gl.loss.SoftmaxCrossEntropyLoss()

trainer = gl.Trainer(model.collect_params(), 'sgd', {'learning_rate': lr})

def accuracy(y_hat, y) :
    return (y_hat.argmax(axis=1) == y.astype('float32')).mean().asscalar()

num_epochs = 10
for epoch in range(1, num_epochs + 1):
    losses = []
    for X, y in train_iter:
        with autograd.record():
            l = loss(model(X), y)
            losses.append(l.asnumpy())
        l.backward()
        trainer.step(batch_size)

    loss_sum = np.mean(losses)
    for Xt, yt in test_iter:
        accy = accuracy(model(Xt),yt)
        break

    print('epoch %d, loss: %f, accy: %f' % (epoch,loss_sum,accy))
Copy the code
  • out
60000 10000
epoch 1, loss: 0.773934, accy: 0.812000
epoch 2, loss: 0.490065, accy: 0.855000
epoch 3, loss: 0.436965, accy: 0.852000
epoch 4, loss: 0.401657, accy: 0.875000
epoch 5, loss: 0.380360, accy: 0.856000
epoch 6, loss: 0.363831, accy: 0.869000
epoch 7, loss: 0.349141, accy: 0.873000
epoch 8, loss: 0.337938, accy: 0.877000
epoch 9, loss: 0.326593, accy: 0.874000
epoch 10, loss: 0.319641, accy: 0.884000
Copy the code