forked from mcai/end-to-end-for-chinese-plate-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
52 lines (41 loc) · 2.66 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# coding=utf-8
import mxnet as mx
import numpy as np
index = {"京": 0, "沪": 1, "津": 2, "渝": 3, "冀": 4, "晋": 5, "蒙": 6, "辽": 7, "吉": 8, "黑": 9, "苏": 10, "浙": 11, "皖": 12,
"闽": 13, "赣": 14, "鲁": 15, "豫": 16, "鄂": 17, "湘": 18, "粤": 19, "桂": 20, "琼": 21, "川": 22, "贵": 23, "云": 24,
"藏": 25, "陕": 26, "甘": 27, "青": 28, "宁": 29, "新": 30, "0": 31, "1": 32, "2": 33, "3": 34, "4": 35, "5": 36,
"6": 37, "7": 38, "8": 39, "9": 40, "A": 41, "B": 42, "C": 43, "D": 44, "E": 45, "F": 46, "G": 47, "H": 48,
"J": 49, "K": 50, "L": 51, "M": 52, "N": 53, "P": 54, "Q": 55, "R": 56, "S": 57, "T": 58, "U": 59, "V": 60,
"W": 61, "X": 62, "Y": 63, "Z": 64}
chars = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
"琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z"
]
def rand(val):
return int(np.random.random() * val)
def plate_recognition_net(train=False):
data = mx.symbol.Variable('data')
conv1 = mx.symbol.Convolution(data=data, kernel=(5, 5), num_filter=32)
pool1 = mx.symbol.Pooling(data=conv1, pool_type="max", kernel=(2, 2), stride=(1, 1))
relu1 = mx.symbol.Activation(data=pool1, act_type="relu")
conv2 = mx.symbol.Convolution(data=relu1, kernel=(5, 5), num_filter=32)
pool2 = mx.symbol.Pooling(data=conv2, pool_type="avg", kernel=(2, 2), stride=(1, 1))
relu2 = mx.symbol.Activation(data=pool2, act_type="relu")
flatten = mx.symbol.Flatten(data=relu2)
fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=120)
fc21 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc22 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc23 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc24 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc25 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc26 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc27 = mx.symbol.FullyConnected(data=fc1, num_hidden=65)
fc2 = mx.symbol.Concat(*[fc21, fc22, fc23, fc24, fc25, fc26, fc27], dim=0)
if train:
label = mx.symbol.Variable('softmax_label')
label = mx.symbol.transpose(data=label)
label = mx.symbol.Reshape(data=label, target_shape=(0,))
return mx.symbol.SoftmaxOutput(data=fc2, label=label, name="softmax")
else:
return mx.symbol.SoftmaxOutput(data=fc2, name="softmax")