Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
KentaroAOKI committed Nov 5, 2020
2 parents be78263 + 497ae4f commit 1b2e567
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
Binary file added databases/cnn-alphabet_lowercase.bin
Binary file not shown.
Binary file added databases/cnn-alphabet_number.bin
Binary file not shown.
Binary file added databases/cnn-alphabet_uppercase.bin
Binary file not shown.
18 changes: 9 additions & 9 deletions learning/run.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash

cd ../src
make
make preprocess
cd ../learning
# cd ../src
# make
# make preprocess
# cd ../learning

python make_data.py $1
if [ $? -ne 0 ]
then
exit
fi
# python make_data.py $1
# if [ $? -ne 0 ]
# then
# exit
# fi

python train_cnn.py
if [ $? -ne 0 ]
Expand Down
24 changes: 14 additions & 10 deletions learning/train_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def load_data(input_dirs, nb_dim, pad=3):
for name in os.listdir(input_dir):
if not name.endswith('.png'):
continue
img = cv.imread(input_dir + name, 0)
img = cv.imread(os.path.join(input_dir, name), 0)
if img is None:
continue

# Thresholding
img = cv.threshold(img, 255 * 0.7, 255, cv.THRESH_BINARY_INV)[1]
Expand Down Expand Up @@ -92,7 +94,7 @@ def dump_weights(filename, model, unique_label):
for label in unique_label:
b += struct.pack('i', len(label))
for c in label:
b += struct.pack('c', c)
b += struct.pack('c', bytes(c, 'utf-8'))
for layer in model.layers:
for w in layer.get_weights():
for v in w.astype(np.float32).reshape(-1):
Expand Down Expand Up @@ -148,24 +150,26 @@ def eraser(input_img):
parser.add_argument('--nb_epoch', type=int, default=200)
args = parser.parse_args()

print 'Load data'
print ('Load data')
X, y, unique_label = load_data(args.train_dirs, args.nb_dim)
n = X.shape[0]

print 'Split data into train set and validation set'
print ('Split data into train set and validation set')
idx = np.random.permutation(n)
n_train = int(n * 0.9) / args.batch_size * args.batch_size
n_train = int(int(n * 0.9) / args.batch_size) * args.batch_size
X_train, y_train = X[idx[:n_train]], y[idx[:n_train]]
X_valid, y_valid = X[idx[n_train:]], y[idx[n_train:]]
steps_per_epoch = n_train / args.batch_size

print 'Build model'
print ('Build model')
# set image data format to "channels, conv_dim1, conv_dim2, conv_dim3".
keras.backend.set_image_data_format('channels_first')
model = build_model(args.nb_dim, len(unique_label))
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model.compile(loss='categorical_crossentropy', optimizer=opt,
metrics=['accuracy'])

print 'Fit'
print ('Fit')
earlystopping = EarlyStopping(monitor='val_loss', patience=1000)
checkpointer = ModelCheckpoint(filepath=args.dump_prefix + 'weights.hdf5',
verbose=0, save_best_only=True)
Expand All @@ -184,13 +188,13 @@ def eraser(input_img):
epochs=args.nb_epoch, verbose=1)
model.load_weights(args.dump_prefix + 'weights.hdf5')

print 'Dump results to binary'
print ('Dump results to binary')
dump_weights(args.dump_prefix + 'cnn-result.bin', model, unique_label)

print 'Testing on validation set:', (model.predict_classes(X_valid) == y_valid.argmax(axis=1)).mean()
print ('Testing on validation set:', (model.predict_classes(X_valid) == y_valid.argmax(axis=1)).mean())
for test_dir in args.test_dirs:
X_test, y_test, unique_label_test = load_data([test_dir], args.nb_dim)
y_test = unique_label_test[y_test.argmax(axis=1)]
y_pred = unique_label[model.predict_classes(X_test)]
print 'Testing on {}: {}'.format(test_dir, (y_test == y_pred).mean())
print ('Testing on {}: {}'.format(test_dir, (y_test == y_pred).mean()))

2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ usage()
printf(" kocr\tweights-file target\t\tRecognize characters in target\n");
printf("\n");

printf("\tweights-file: *.txt\n");
printf("\tweights-file: *.bin\n");
#else
printf(" kocr\timage-list\t\tCreates a database file\n");
printf("\tdatabase-file\t\tEvaluates a database file\n");
Expand Down

0 comments on commit 1b2e567

Please sign in to comment.