0

I am new to AI and this code is about mnist digits recognition according to the tutorial from this website: tensorflow tutorial. For some reason, I cant use the mnist data directly like what it is in tutorial. My method is to download the data first, then decompress it into a folder. Then read the data as binary data and decode it into numpy. I dont know why , the accuracy is merely 0.098, which is far from the supposed value 0.92. My code is here :

 import numpy as np
 import struct
 import matplotlib.pyplot as plt


class MnistData():

    def printImg(self,img):
        fig = plt.figure()
        plotwindow = fig.add_subplot(111)
        plt.imshow(img , cmap = 'gray')
        plt.show()
        return

    def getBinFile(self,filename):
        binfile = open(filename , 'rb')  
        buf = binfile.read()
        return buf


    def decodeLabelsFromBin(self,binFile):
        offset = 0
        fmt_header = '>ii'
        magic_number, num_images = struct.unpack_from(fmt_header, binFile, offset)
        offset += struct.calcsize(fmt_header)
        fmt_image = '>B'
        labels = np.empty(num_images)
        for i in range(num_images):
            labels[i] = struct.unpack_from(fmt_image, binFile, offset)[0]
            offset += struct.calcsize(fmt_image)
        return labels


    def decodeImagesFromBin(self,buf):    
        #读取头四个32bit的interger:
        index = 0
        magic, numImages, numRows, numColumns = struct.unpack_from('>IIII' , buf , index)
        index += struct.calcsize('>IIII')
        imageSize = numRows * numColumns
        fmtImage = '>' + str(imageSize) + 'B'
        images = np.empty((numImages, numRows, numColumns))
        for i in range(numImages):   
            images[i] = np.array(struct.unpack_from(fmtImage, buf, index)).reshape((numRows, numColumns))
            index += struct.calcsize(fmtImage)
        return images


    def __init__(self):
        trainImgPath = 'E:/AI projects/mnistPJ/mnist/train-images.idx3-ubyte'
        trainLabelPath = 'E:/AI projects/mnistPJ/mnist/train-labels.idx1-ubyte'
        testImgPath = 'E:/AI projects/mnistPJ/mnist/t10k-images.idx3-ubyte'
        testLabelPath = 'E:/AI projects/mnistPJ/mnist/t10k-labels.idx1-ubyte'

        trainImgBinFile = self.getBinFile(trainImgPath)
        self.trainImgs = self.decodeImagesFromBin(trainImgBinFile)

        trainLabelBinFile = self.getBinFile(trainLabelPath)
        self.trainLabels = self.decodeLabelsFromBin(trainLabelBinFile)

        testImgBinFile = self.getBinFile(testImgPath)
        self.testImgs = self.decodeImagesFromBin(testImgBinFile)

        testLabelBinFile = self.getBinFile(testLabelPath)
        self.testLabels = self.decodeLabelsFromBin(testLabelBinFile)
        return 

This class is used to deal with minist data.

My main.py is as follows:

import tensorflow as tf
import mnistDecode
import random;
import numpy as np

sess = tf.InteractiveSession()
mdata = mnistDecode.MnistData()
x = tf.placeholder(tf.float32, shape=[None, 784])  
y_ = tf.placeholder(tf.float32, shape=[None, 10])  
W = tf.Variable(tf.zeros([784,10])) 
b = tf.Variable(tf.zeros(10)) 
y = tf.nn.softmax(tf.matmul(x,W)+b) 
cross_entrophy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),
                                               reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entrophy)
tf.global_variables_initializer().run()
len = mdata.trainLabels.shape[0]  
COUNT = 100;   
turns = 1000;   

for j in range(turns):
    batch_xs = np.empty((COUNT, 784))
    batch_ys = np.zeros((COUNT, 10)) 
    index = random.sample(range(len),COUNT); 
    imgs = mdata.trainImgs[index]
    lbs = mdata.trainLabels[index]
    for i in range(COUNT):
        batch_xs[i] = imgs[i].reshape(1,784)  
        batch_ys[i,int(lbs[i])] = 1
    train_step.run({x:batch_xs,y_:batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy= tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

testLen = mdata.testLabels.shape[0]
timgs = np.empty((testLen, 784))
tlabels = np.zeros((testLen, 10))

for i in range(testLen):
    timgs[i] = mdata.testImgs[i].reshape(1,784)  
    tlabels[i,int(mdata.testLabels[i])] = 1

print(accuracy.eval({x:timgs,y_:tlabels}))
sess.close()
aqjs
  • 1
  • 1
  • Please see mnist example provided in tf repo – brown.2179 Nov 29 '17 at 19:05
  • this is according to that tutorial. but i dont know why i get so poor accuracy – aqjs Nov 30 '17 at 01:54
  • The tutorial you linked to uses `cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))`. Why did you change this? Also, if you can fix whatever is preventing you from using the mnist data directly that might help, since it's possible your decoding code is messing something up. – Stephen Nov 30 '17 at 20:54

0 Answers0