Skip to content

Some changes on "Optical Character Recognition (OCR)" #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ocr/code/nn.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ocr/code/ocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ var ocrDemo = {
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(msg);
}
}
}
6 changes: 3 additions & 3 deletions ocr/code/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _draw(self, sample):
def train(self, training_data_array):
for data in training_data_array:
# Step 2: Forward propagation
y1 = np.dot(np.mat(self.theta1), np.mat(data['y0']).T)
y1 = np.dot(np.mat(self.theta1), np.mat(data.y0).T)
sum1 = y1 + np.mat(self.input_layer_bias) # Add the bias
y1 = self.sigmoid(sum1)

Expand All @@ -73,12 +73,12 @@ def train(self, training_data_array):

# Step 3: Back propagation
actual_vals = [0] * 10 # actual_vals is a python list for easy initialization and is later turned into an np matrix (2 lines down).
actual_vals[data['label']] = 1
actual_vals[data.label] = 1
output_errors = np.mat(actual_vals).T - np.mat(y2)
hidden_errors = np.multiply(np.dot(np.mat(self.theta2).T, output_errors), self.sigmoid_prime(sum1))

# Step 4: Update weights
self.theta1 += self.LEARNING_RATE * np.dot(np.mat(hidden_errors), np.mat(data['y0']))
self.theta1 += self.LEARNING_RATE * np.dot(np.mat(hidden_errors), np.mat(data.y0))
self.theta2 += self.LEARNING_RATE * np.dot(np.mat(output_errors), np.mat(y1).T)
self.hidden_layer_bias += self.LEARNING_RATE * output_errors
self.input_layer_bias += self.LEARNING_RATE * hidden_errors
Expand Down
10 changes: 5 additions & 5 deletions ocr/code/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BaseHTTPServer
import http.server
import json
from ocr import OCRNeuralNetwork
import numpy as np
Expand All @@ -20,7 +20,7 @@
# for hidden nodes
nn = OCRNeuralNetwork(HIDDEN_NODE_COUNT, data_matrix, data_labels, list(range(5000)));

class JSONHandler(BaseHTTPServer.BaseHTTPRequestHandler):
class JSONHandler(http.server.BaseHTTPRequestHandler):
def do_POST(s):
response_code = 200
response = ""
Expand All @@ -44,18 +44,18 @@ def do_POST(s):
s.send_header("Access-Control-Allow-Origin", "*")
s.end_headers()
if response:
s.wfile.write(json.dumps(response))
s.wfile.write(bytes(json.dumps(response), "utf-8"))
return

if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer;
server_class = http.server.HTTPServer;
httpd = server_class((HOST_NAME, PORT_NUMBER), JSONHandler)

try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
else:
print "Unexpected server exception occurred."
print("Unexpected server exception occurred.")
finally:
httpd.server_close()