Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
follow comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dayhaha committed Jan 16, 2017
1 parent 202184b commit ca4f834
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 440 deletions.
459 changes: 199 additions & 260 deletions recognize_digits/README.md

Large diffs are not rendered by default.

71 changes: 0 additions & 71 deletions recognize_digits/cnn_mnist.py

This file was deleted.

4 changes: 1 addition & 3 deletions recognize_digits/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def get_best_pass(filename):

filename = sys.argv[1]
log = get_best_pass(filename)
predict_error = math.sqrt(float(log[0])) / 2
classification_accuracy = (1 - float(log[1])) * 100
print 'Best pass is %s, error is %s, which means predict get error as %f' % (
log[2], log[0], predict_error)
print 'Best pass is %s, testing Avgcost is %s' % (log[2], log[0])
print 'The classification accuracy is %.2f%%' % classification_accuracy
Binary file modified recognize_digits/image/Conv_layer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified recognize_digits/image/cnn_train_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified recognize_digits/image/mlp_train_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified recognize_digits/image/softmax_train_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 11 additions & 18 deletions recognize_digits/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,17 @@


def read_data(path, filename):
imgf = path + filename + "-images-idx3-ubyte"
labelf = path + filename + "-labels-idx1-ubyte"
f = open(imgf, "rb")
l = open(labelf, "rb")

f.read(16)
l.read(8)

# Define number of samples for train/test
n = 60000 if "train" in filename else 10000

rows = 28
cols = 28

images = np.fromfile(
f, 'ubyte',
count=n * rows * cols).reshape(n, rows, cols).astype('float32')
labels = np.fromfile(l, 'ubyte', count=n).astype("int")
with open(path + filename + "-images-idx3-ubyte",
"rb") as f: # open picture file
magic, n, rows, cols = struct.upack(">IIII", f.read(16))
images = np.fromfile(
f, 'ubyte',
count=n * rows * cols).reshape(n, rows, cols).astype('float32')

with open(path + filename + "-labels-idx1-ubyte",
"rb") as l: # open label file
magic, n = struct.upack(">II", l.read(8))
labels = np.fromfile(l, 'ubyte', count=n).astype("int")

return images, labels

Expand Down
54 changes: 47 additions & 7 deletions recognize_digits/mlp_mnist.py → recognize_digits/mnist_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,53 @@
label_size = 10
img = data_layer(name='pixel', size=data_size)

# The first fully-connected layer
hidden1 = fc_layer(input=img, size=128, act=ReluActivation())
# The second fully-connected layer and the according activation function
hidden2 = fc_layer(input=hidden1, size=64, act=ReluActivation())
# The thrid fully-connected layer, note that the hidden size should be 10,
# which is the number of unique digits
predict = fc_layer(input=hidden2, size=10, act=SoftmaxActivation())

def softmax_regression(img):
predict = fc_layer(input=img, size=10, act=SoftmaxActivation())
return predict


def multilayer_perceptron(img):
# The first fully-connected layer
hidden1 = fc_layer(input=img, size=128, act=ReluActivation())
# The second fully-connected layer and the according activation function
hidden2 = fc_layer(input=hidden1, size=64, act=ReluActivation())
# The thrid fully-connected layer, note that the hidden size should be 10,
# which is the number of unique digits
predict = fc_layer(input=hidden2, size=10, act=SoftmaxActivation())
return predict


def convolutional_neural_network(img):
# first conv layer
conv_pool_1 = simple_img_conv_pool(
input=img,
filter_size=5,
num_filters=20,
num_channel=1,
pool_size=2,
pool_stride=2,
act=TanhActivation())
# second conv layer
conv_pool_2 = simple_img_conv_pool(
input=conv_pool_1,
filter_size=5,
num_filters=50,
num_channel=20,
pool_size=2,
pool_stride=2,
act=TanhActivation())
# The first fully-connected layer
fc1 = fc_layer(input=conv_pool_2, size=128, act=TanhActivation())
# The softmax layer, note that the hidden size should be 10,
# which is the number of unique digits
predict = fc_layer(input=fc1, size=10, act=SoftmaxActivation())
return predict


predict = softmax_regression(img)
#predict = multilayer_perceptron(img)
#predict = convolutional_neural_network(img)

if not is_predict:
lbl = data_layer(name="label", size=label_size)
Expand Down
31 changes: 9 additions & 22 deletions recognize_digits/mnist_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,17 @@
# Define a py data provider
@provider(
input_types={'pixel': dense_vector(28 * 28),
'label': integer_value(10)},
cache=CacheType.CACHE_PASS_IN_MEM)
'label': integer_value(10)})
def process(settings, filename): # settings is not used currently.
imgf = filename + "-images-idx3-ubyte"
labelf = filename + "-labels-idx1-ubyte"
f = open(imgf, "rb")
l = open(labelf, "rb")
with open(filename + "-images-idx3-ubyte", "rb") as f: # open picture file
magic, n, rows, cols = struct.upack(">IIII", f.read(16))
images = np.fromfile(
f, 'ubyte',
count=n * rows * cols).reshape(n, rows, cols).astype('float32')

f.read(16)
l.read(8)

# Define number of samples for train/test
if "train" in filename:
n = 60000
else:
n = 10000

images = numpy.fromfile(
f, 'ubyte', count=n * 28 * 28).reshape((n, 28 * 28)).astype('float32')
images = images / 255.0 * 2.0 - 1.0
labels = numpy.fromfile(l, 'ubyte', count=n).astype("int")
with open(filename + "-labels-idx1-ubyte", "rb") as l: # open label file
magic, n = struct.upack(">II", l.read(8))
labels = np.fromfile(l, 'ubyte', count=n).astype("int")

for i in xrange(n):
yield {"pixel": images[i, :], 'label': labels[i]}

f.close()
l.close()
28 changes: 23 additions & 5 deletions recognize_digits/plot_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,33 @@
def plot_log(filename):
with open(filename, 'r') as f:
text = f.read()
pattern = re.compile('Test.*? cost=([0-9]+\.[0-9]+).*?pass-([0-9]+)',
re.S)
pattern = re.compile(
'AvgCost=([0-9]+\.[0-9]+).*?Test.*? cost=([0-9]+\.[0-9]+).*?pass-([0-9]+)',
re.S)
results = re.findall(pattern, text)
cost, pass_ = zip(*results)
cost_float = map(float, cost)
train_cost, test_cost, pass_ = zip(*results)
train_cost_float = map(float, train_cost)
test_cost_float = map(float, test_cost)
pass_int = map(int, pass_)
plt.plot(pass_int, cost_float, 'bo', pass_, cost_float, 'k')
plt.plot(pass_int, train_cost_float, 'red', label='Train Error')
plt.plot(pass_int, test_cost_float, 'green', label='Test Error')
plt.ylabel('AvgCost')
plt.xlabel('epoch')

# Now add the legend with some customizations.
legend = plt.legend(loc='upper center', shadow=True)

# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
frame = legend.get_frame()
frame.set_facecolor('0.90')

# Set the fontsize
for label in legend.get_texts():
label.set_fontsize('large')

for label in legend.get_lines():
label.set_linewidth(1.5) # the legend line width

plt.show()


Expand Down
4 changes: 3 additions & 1 deletion recognize_digits/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ def predict(self, index):
output = self.network.forwardTest(input)
prob = output[0]["value"]
predict = np.argsort(-prob)
print "Predicted probability of each digit:"
print prob
print predict[0][0], self.labels[index]
print "Predict Number: %d" % predict[0][0]
print "Actual Number: %d" % self.labels[index]


def main():
Expand Down
50 changes: 0 additions & 50 deletions recognize_digits/softmax_mnist.py

This file was deleted.

6 changes: 3 additions & 3 deletions recognize_digits/train.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# limitations under the License.
set -e

config=cnn_mnist.py
output=./cnn_mnist_model
log=cnn_train.log
config=mnist_model.py
output=./softmax_mnist_model
log=softmax_train.log



Expand Down

0 comments on commit ca4f834

Please sign in to comment.