Skip to content

Commit

Permalink
regular update
Browse files Browse the repository at this point in the history
  • Loading branch information
hallazie committed Sep 28, 2018
1 parent ee50360 commit 51e778b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
10 changes: 8 additions & 2 deletions py/glu_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mxnet.gluon as gluon
import mxnet as mx

vobsize = 3593
vobsize = 100
batch_size = 2
model_prefix = '../params/ctc'
img_w = 256
Expand Down Expand Up @@ -97,9 +97,15 @@ def train():
loss_ctc = ctc_loss(output, label)
loss_ctc = (label!=-1).sum(axis=1)*loss_ctc
loss_ctc.backward()
loss = loss_ctc.mean()
loss = loss_ctc.mean().asnumpy()
if i % 20 == 0:
print 'epoch %s\t\tbatch %s\t\tloss=%s'%(e, i, loss)

'''
"D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" -vcvars_ver=14.11
cmake -G "Visual Studio 15 2017 Win64" -T cuda=9.1,host=x64 -DUSE_CUDA=1 -DUSE_CUDNN=1 -DUSE_NVRTC=1 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_BLAS=open -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_LIST=Common -DCUDA_TOOLSET=9.1 -DCUDNN_INCLUDE=E:\Env\cuDNN\cuda\include -DCUDNN_LIBRARY=E:\Env\cuDNN\cuda\lib\x64\cudnn.lib "E:\Env\incubator-mxnet"
'''

if __name__ == '__main__':
train()
Binary file modified py/model.pyc
Binary file not shown.
11 changes: 6 additions & 5 deletions py/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import model

vobsize = 3593
batch_size = 2
batch_size = 32
model_prefix = '../params/ctc'
ctx = mx.cpu(0)
ctx = mx.gpu(0)
img_w = 256
img_h = 32
seq_l = 20
Expand Down Expand Up @@ -70,14 +70,14 @@ def train():
dataiter,
optimizer = 'rmsprop',
optimizer_params = {'learning_rate':0.005},
eval_metric = 'loss',
eval_metric = 'acc',
batch_end_callback = mx.callback.Speedometer(batch_size, 5),
epoch_end_callback = mx.callback.do_checkpoint(model_prefix, 1),
num_epoch = 100,
)

def test():
symbol = model.ctc(vobsize, False)
symbol = model.ctc(200, False)
dataiter = mx.io.NDArrayIter(data=mx.nd.normal(loc=0, scale=1, shape=(1, 1, img_w, img_h)), label=mx.nd.normal(loc=0, scale=1, shape=(1, seq_l)), batch_size=1, shuffle=True)
symbol = mx.mod.Module(symbol=symbol, context=mx.cpu(0), data_names=('data',), label_names=('label',))
symbol.bind(for_training=False, data_shapes=dataiter.provide_data)
Expand All @@ -87,4 +87,5 @@ def test():
print out.shape

if __name__ == '__main__':
test()
print 'using mxnet version %s'%mx.__version__
train()
1 change: 1 addition & 0 deletions py/scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def init_data():
fs = fs[:20]
random.shuffle(fs)
idx = 0
iterstop = 0
while True:
try:
data = np.zeros((batch_size, 1, img_w, img_h))
Expand Down
16 changes: 2 additions & 14 deletions py/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,13 @@ def foo():

if __name__ == '__main__':
in_var = mx.sym.Variable('data')

t1 = mx.symbol.reshape(in_var, (6,10))
t2 = mx.symbol.FullyConnected(t1, num_hidden=5, no_bias=True)
t3 = mx.symbol.reshape(t2, (3,2,5))

labels_var = mx.sym.Variable('label')
ctc = mx.sym.contrib.ctc_loss(t3, labels_var)
ctc = mx.sym.contrib.ctc_loss(in_var, labels_var)
loss = mx.symbol.MakeLoss(ctc)

arg_names = loss.list_arguments()
arg_shapes,_,_ = loss.infer_shape(data=(6,2,5), label=(2,3))
for name, shape in zip(arg_names, arg_shapes):
print '%s\t\t\t%s'%(name, shape)

arg_shapes,_,_ = loss.infer_shape(data=(6,2,1000), label=(2,3))
arg_array = [mx.nd.normal(shape=shape, ctx=ctx) for shape in arg_shapes]
exe = loss.bind(ctx=ctx, args=arg_array)
exe.forward(is_train=True)
exe.backward()
outTest = exe.outputs[0]

print '%s'%(outTest.asnumpy())
print '----------'
30 changes: 27 additions & 3 deletions py/val_datagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import random
import chardet
import pygame
# import pygame
import os
import numpy as np

Expand Down Expand Up @@ -86,7 +86,31 @@ def fee(self):
def fum(self):
return self.val

def calc_norm(path):
array_list = []
norm, cnt = 0, 0
for _,_,fs in os.walk(path):
fs = sorted(fs)[:50]
for f in fs:
array = np.array(Image.open(path+f).convert('L'))
# array = 255*(array-np.min(array))/(np.max(array)-np.min(array))
array_list.append(array)
for i in range(len(array_list)):
for j in range(i):
a1 = array_list[i]
a2 = array_list[j]
norm += np.linalg.norm(a1-a2)
cnt += 1
# print '%s finished'%(i)
return norm/(cnt*(640*480))

if __name__ == '__main__':
# init_dataiter()
t = T(3)
print t.foo().fee().fum()
# t = T(3)
# print t.foo().fee().fum()
norm_sal = calc_norm('E:/Paper/feature_validation/data/norm/saliency_512/')
norm_vgg = calc_norm('E:/Paper/feature_validation/data/norm/vgg_feature/')
print 'norm sal: %s'%norm_sal
print 'norm vgg: %s'%norm_vgg
# # norm sal: 9251865169.961721
# # norm vgg: 9803585796.040016

0 comments on commit 51e778b

Please sign in to comment.