mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

MNIST example has low accuracy (0.1)

Open sookinoby opened this issue 9 years ago • 9 comments

For bugs or installation issues, please provide the following information. The more information you provide, the more likely people will be able to help you.

Environment info

Operating System: Windows 10, GTX 960.

Compiler: I downloaded the 20160531_win10_x64_gpu..7z. I didnt compile mxnet

Package used (Python/R/Scala/Julia): Python

MXNet version: 20160531_win10_x64_gpu.7z

Or if installed from source:

MXNet commit hash (git rev-parse HEAD): e3e26b5

If you are using python package, please provide

Python version and distribution: Python 3.5.3 :: Continuum Analytics, Inc.

If you are using R package, please provide

R sessionInfo():

Error Message:

Please paste the full error message, including stack trace. There is no stack trace.

Steps to reproduce

I am running the http://mxnet.io/tutorials/python/mnist.html example. The training accuracy is always 0.05 to 0.1 for convolution network. I didn't try MLP

##My Code import numpy as np import os import urllib import gzip import struct import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) def download_data(url, force_download=True): fname = url.split("/")[-1] if force_download or not os.path.exists(fname): urllib.urlretrieve(url, fname) return fname

def read_data(label_url, image_url): with gzip.open(label_url) as flbl: magic, num = struct.unpack(">II", flbl.read(8)) label = np.fromstring(flbl.read(), dtype=np.int8) with gzip.open(image_url, 'rb') as fimg: magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16)) image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols) return (label, image)

path='handwritten-digits/' #(I downloaded the data set and saved it to handwritten-digits/'

(train_lbl, train_img) = read_data( path+'train-labels-idx1-ubyte.gz', path+'train-images-idx3-ubyte.gz') (val_lbl, val_img) = read_data( path+'t10k-labels-idx1-ubyte.gz', path+'t10k-images-idx3-ubyte.gz')

%matplotlib inline import matplotlib.pyplot as plt for i in range(10): plt.subplot(1,10,i+1) plt.imshow(train_img[i], cmap='Greys_r') plt.axis('off') plt.show() print('label: %s' % (train_lbl[0:10],))

import mxnet as mx

def to4d(img): return img.reshape(img.shape[0], 1, 28, 28).astype(np.float32)/255

batch_size = 100 train_iter = mx.io.NDArrayIter(to4d(train_img), train_lbl, batch_size, shuffle=True) val_iter = mx.io.NDArrayIter(to4d(val_img), val_lbl, batch_size)

data = mx.symbol.Variable('data')

first conv layer

conv1 = mx.sym.Convolution(data=data, kernel=(5,5), num_filter=20) tanh1 = mx.sym.Activation(data=conv1, act_type="tanh") pool1 = mx.sym.Pooling(data=tanh1, pool_type="max", kernel=(2,2), stride=(2,2))

second conv layer

conv2 = mx.sym.Convolution(data=pool1, kernel=(5,5), num_filter=50) tanh2 = mx.sym.Activation(data=conv2, act_type="tanh") pool2 = mx.sym.Pooling(data=tanh2, pool_type="max", kernel=(2,2), stride=(2,2))

first fullc layer

flatten = mx.sym.Flatten(data=pool2) fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=500) tanh3 = mx.sym.Activation(data=fc1, act_type="tanh")

second fullc

fc2 = mx.sym.FullyConnected(data=tanh3, num_hidden=10)

softmax loss

lenet = mx.sym.SoftmaxOutput(data=fc2, name='softmax')

@@@ AUTOTEST_OUTPUT_IGNORED_CELL

model = mx.model.FeedForward( ctx = mx.gpu(0), # use GPU 0 for training, others are same as before symbol = lenet,
num_epoch = 10,
learning_rate = 0.1) model.fit( X=train_iter,
eval_data=val_iter, batch_end_callback = mx.callback.Speedometer(batch_size, 200) )

#output logs INFO:root:Start training with [gpu(0)] INFO:root:Epoch[0] Batch [200] Speed: 10550.22 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[0] Batch [400] Speed: 10455.60 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[0] Batch [600] Speed: 10474.79 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[0] Resetting Data Iterator INFO:root:Epoch[0] Time cost=5.730 INFO:root:Epoch[0] Validation-accuracy=0.098000 INFO:root:Epoch[1] Batch [200] Speed: 10510.17 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[1] Batch [400] Speed: 10436.99 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[1] Batch [600] Speed: 10366.46 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[1] Resetting Data Iterator INFO:root:Epoch[1] Time cost=5.756 INFO:root:Epoch[1] Validation-accuracy=0.098000 INFO:root:Epoch[2] Batch [200] Speed: 10485.69 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[2] Batch [400] Speed: 10388.89 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[2] Batch [600] Speed: 10516.51 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[2] Resetting Data Iterator INFO:root:Epoch[2] Time cost=5.742 INFO:root:Epoch[2] Validation-accuracy=0.098000 INFO:root:Epoch[3] Batch [200] Speed: 10514.61 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[3] Batch [400] Speed: 10480.61 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[3] Batch [600] Speed: 10477.18 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[3] Resetting Data Iterator INFO:root:Epoch[3] Time cost=5.727 INFO:root:Epoch[3] Validation-accuracy=0.098000 INFO:root:Epoch[4] Batch [200] Speed: 10380.61 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[4] Batch [400] Speed: 10426.29 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[4] Batch [600] Speed: 10497.75 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[4] Resetting Data Iterator INFO:root:Epoch[4] Time cost=5.758 INFO:root:Epoch[4] Validation-accuracy=0.098000 INFO:root:Epoch[5] Batch [200] Speed: 10544.32 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[5] Batch [400] Speed: 10491.81 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[5] Batch [600] Speed: 10489.34 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[5] Resetting Data Iterator INFO:root:Epoch[5] Time cost=5.718 INFO:root:Epoch[5] Validation-accuracy=0.098000 INFO:root:Epoch[6] Batch [200] Speed: 10512.22 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[6] Batch [400] Speed: 10439.01 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[6] Batch [600] Speed: 10403.41 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[6] Resetting Data Iterator INFO:root:Epoch[6] Time cost=5.749 INFO:root:Epoch[6] Validation-accuracy=0.098000 INFO:root:Epoch[7] Batch [200] Speed: 10485.94 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[7] Batch [400] Speed: 10455.69 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[7] Batch [600] Speed: 10532.22 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[7] Resetting Data Iterator INFO:root:Epoch[7] Time cost=5.728 INFO:root:Epoch[7] Validation-accuracy=0.098000 INFO:root:Epoch[8] Batch [200] Speed: 10525.94 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[8] Batch [400] Speed: 10436.42 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[8] Batch [600] Speed: 10426.20 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[8] Resetting Data Iterator INFO:root:Epoch[8] Time cost=5.743 INFO:root:Epoch[8] Validation-accuracy=0.098000 INFO:root:Epoch[9] Batch [200] Speed: 10522.15 samples/sec Train-accuracy=0.096900 INFO:root:Epoch[9] Batch [400] Speed: 10494.32 samples/sec Train-accuracy=0.099150 INFO:root:Epoch[9] Batch [600] Speed: 10525.46 samples/sec Train-accuracy=0.100100 INFO:root:Epoch[9] Resetting Data Iterator INFO:root:Epoch[9] Time cost=5.715 INFO:root:Epoch[9] Validation-accuracy=0.098000

What have you tried to solve it?

I was working on another project. But the accuracy was very low. So I tried the basic example from website and it still remained the same

sookinoby avatar May 01 '17 18:05 sookinoby

Did you edit the original codes?

ysh329 avatar May 04 '17 09:05 ysh329

@ysh329 Yes. A little bit. Instead of downloading the data set automatically, I downloaded the dataset and placed in a folder. Except that I didn't modify anything.

sookinoby avatar May 04 '17 21:05 sookinoby

@sookinoby Well, it's really strange! @ry

ysh329 avatar May 05 '17 01:05 ysh329

@ysh329 . The example works with CPU only build. I ran the same exact code. I believe there is some mistake with GPU implementation.

sookinoby avatar May 09 '17 22:05 sookinoby

@sookinoby I tried it's okay:

I just edited one line:

    parser.set_defaults(
        # network
        network        = 'mlp',
        # train
        gpus           = '0,1,2', # edit this line
        batch_size     = 64, 
        disp_batches   = 100,
        num_epochs     = 20, 
        lr             = .05,
        lr_step_epochs = '10',
    )
ysh329@linux-W580-G20:~/code/mxnet/example/image-classification$ python train_mnist.py 
INFO:root:start with arguments Namespace(batch_size=64, disp_batches=100, gpus='0,1,2', kv_store='device', load_epoch=None, lr=0.05, lr_factor=0.1, lr_step_epochs='10', model_prefix=None, mom=0.9, monitor=0, network='mlp', num_classe
s=10, num_epochs=20, num_examples=60000, num_layers=None, optimizer='sgd', test_io=0, top_k=0, wd=0.0001)
[09:53:27] src/kvstore/././comm.h:304: only 2 out of 6 GPU pairs are enabled direct access. It may affect the performance. You can set MXNET_ENABLE_GPU_P2P=0 to turn it off
[09:53:27] src/kvstore/././comm.h:313: ...
[09:53:27] src/kvstore/././comm.h:313: ..v
[09:53:27] src/kvstore/././comm.h:313: .v.
INFO:root:Epoch[0] Batch [100]  Speed: 21377.08 samples/sec     Train-accuracy=0.771658
INFO:root:Epoch[0] Batch [200]  Speed: 20085.74 samples/sec     Train-accuracy=0.902500
INFO:root:Epoch[0] Batch [300]  Speed: 21418.29 samples/sec     Train-accuracy=0.926406
INFO:root:Epoch[0] Batch [400]  Speed: 22097.69 samples/sec     Train-accuracy=0.934063
INFO:root:Epoch[0] Batch [500]  Speed: 25782.32 samples/sec     Train-accuracy=0.949063
INFO:root:Epoch[0] Batch [600]  Speed: 26520.81 samples/sec     Train-accuracy=0.947500
INFO:root:Epoch[0] Batch [700]  Speed: 24170.46 samples/sec     Train-accuracy=0.953125
INFO:root:Epoch[0] Batch [800]  Speed: 25383.85 samples/sec     Train-accuracy=0.951094
INFO:root:Epoch[0] Batch [900]  Speed: 25592.02 samples/sec     Train-accuracy=0.955156
INFO:root:Epoch[0] Train-accuracy=0.956503
INFO:root:Epoch[0] Time cost=2.748
INFO:root:Epoch[0] Validation-accuracy=0.962182
INFO:root:Epoch[1] Batch [100]  Speed: 23219.77 samples/sec     Train-accuracy=0.963800
INFO:root:Epoch[1] Batch [200]  Speed: 23090.11 samples/sec     Train-accuracy=0.965313
INFO:root:Epoch[1] Batch [300]  Speed: 23447.34 samples/sec     Train-accuracy=0.963281
INFO:root:Epoch[1] Batch [400]  Speed: 27356.65 samples/sec     Train-accuracy=0.967187
INFO:root:Epoch[1] Batch [500]  Speed: 28517.43 samples/sec     Train-accuracy=0.972500
INFO:root:Epoch[1] Batch [600]  Speed: 27970.42 samples/sec     Train-accuracy=0.965000
INFO:root:Epoch[1] Batch [700]  Speed: 27915.44 samples/sec     Train-accuracy=0.971719
INFO:root:Epoch[1] Batch [800]  Speed: 25521.70 samples/sec     Train-accuracy=0.968437
INFO:root:Epoch[1] Batch [900]  Speed: 21113.54 samples/sec     Train-accuracy=0.972031
INFO:root:Epoch[1] Train-accuracy=0.973818
INFO:root:Epoch[1] Time cost=2.399
INFO:root:Epoch[1] Validation-accuracy=0.966461
INFO:root:Epoch[2] Batch [100]  Speed: 20166.24 samples/sec     Train-accuracy=0.973855
INFO:root:Epoch[2] Batch [200]  Speed: 24421.14 samples/sec     Train-accuracy=0.974688
INFO:root:Epoch[2] Batch [300]  Speed: 21234.51 samples/sec     Train-accuracy=0.976562
INFO:root:Epoch[2] Batch [400]  Speed: 23699.42 samples/sec     Train-accuracy=0.977344
INFO:root:Epoch[2] Batch [500]  Speed: 21244.53 samples/sec     Train-accuracy=0.977187
INFO:root:Epoch[2] Batch [600]  Speed: 23636.56 samples/sec     Train-accuracy=0.973594
INFO:root:Epoch[2] Batch [700]  Speed: 23843.68 samples/sec     Train-accuracy=0.974531
INFO:root:Epoch[2] Batch [800]  Speed: 24169.72 samples/sec     Train-accuracy=0.976250
INFO:root:Epoch[2] Batch [900]  Speed: 24942.50 samples/sec     Train-accuracy=0.980313
INFO:root:Epoch[2] Train-accuracy=0.978463
INFO:root:Epoch[2] Time cost=2.605
INFO:root:Epoch[2] Validation-accuracy=0.971636
INFO:root:Epoch[3] Batch [100]  Speed: 20541.20 samples/sec     Train-accuracy=0.980817
INFO:root:Epoch[3] Batch [200]  Speed: 22404.10 samples/sec     Train-accuracy=0.981406
INFO:root:Epoch[3] Batch [300]  Speed: 23154.68 samples/sec     Train-accuracy=0.981875
INFO:root:Epoch[3] Batch [400]  Speed: 23521.53 samples/sec     Train-accuracy=0.979062
INFO:root:Epoch[3] Batch [500]  Speed: 26749.25 samples/sec     Train-accuracy=0.982812
INFO:root:Epoch[3] Batch [600]  Speed: 29039.70 samples/sec     Train-accuracy=0.980000
INFO:root:Epoch[3] Batch [700]  Speed: 29531.18 samples/sec     Train-accuracy=0.982969

ysh329 avatar May 10 '17 01:05 ysh329

@sookinoby You can ignure this ctx = mx.cpu() or ctx = mx.gpu(0). Just directly edit the parameters of parser.set_defaults.

ysh329 avatar May 10 '17 06:05 ysh329

@ysh329 Sorry for my very late reply. I was out of town and traveling.

I tried Mxnet with Linux on GPU, it works perfectly fine. Accuracy is (0.98). When I use the same exact on windows GPU, the model has a low accuracy (0.02) after 10 epoch.

If use CPU as the context in windows, it works fine.

sookinoby avatar May 19 '17 19:05 sookinoby

@sookinoby That's great! I only use MXNet with Linux. @piiswrong use the same exact on windows GPU, the model has a low accuracy (0.02)

ysh329 avatar May 20 '17 03:05 ysh329

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

yajiedesign avatar Sep 30 '17 17:09 yajiedesign