예제 #1
0
def train():
    net = plate_recognition_net(train=True)

    model = mx.model.FeedForward(symbol=net,
                                 ctx=mx.gpu() if list_gpus() else mx.cpu(),
                                 num_epoch=1,
                                 learning_rate=0.001,
                                 wd=0.00001,
                                 initializer=mx.init.Xavier(factor_type="in",
                                                            magnitude=2.34),
                                 momentum=0.9)

    batch_size = 20

    # data_train = PlateRecognitionIter(2000000, batch_size, 7, 30, 120)
    data_train = PlateRecognitionIter(5000000, batch_size, 7, 30, 120)
    data_test = PlateRecognitionIter(1000, batch_size, 7, 30, 120)

    import logging
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)-15s %(message)s')

    model.fit(X=data_train,
              eval_data=data_test,
              eval_metric=accuracy,
              batch_end_callback=mx.callback.Speedometer(batch_size, 50))
    model.save("models/cnn-ocr")
예제 #2
0
def recognize_one(img_filename):
    img = cv2.imread(img_filename)
    img = cv2.resize(img, (120, 30))
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2)

    batch_size = 1
    _, arg_params, __ = mx.model.load_checkpoint("models/cnn-ocr", 1)
    data_shape = [("data", (batch_size, 3, 30, 120))]
    input_shapes = dict(data_shape)

    sym = plate_recognition_net(train=False)
    executor = sym.simple_bind(ctx=mx.gpu() if list_gpus() else mx.cpu(),
                               **input_shapes)
    for key in executor.arg_dict.keys():
        if key in arg_params:
            arg_params[key].copyto(executor.arg_dict[key])

    executor.forward(is_train=True, data=mx.nd.array([img]))
    probs = executor.outputs[0].asnumpy()

    line = ''
    for i in range(probs.shape[0]):
        if i == 0:
            result = np.argmax(probs[i][0:31])
        elif i == 1:
            result = np.argmax(probs[i][41:65]) + 41
        else:
            result = np.argmax(probs[i][31:65]) + 31

        line += chars[result]

    print('recognized as: ' + line)
예제 #3
0
 def _get_nGPUs(self):
     # caution: if not using all the GPUs, please mannually set nGPUs
     nGPUs = len(test_utils.list_gpus())
     # for CPU
     nGPUs = nGPUs if nGPUs > 0 else 1
     return nGPUs
예제 #4
0
파일: syncbn.py 프로젝트: wfus/gluon-cv
 def _get_num_devices(self):
     # caution: if not using all the GPUs, please mannually set num_devices
     num_devices = len(test_utils.list_gpus())
     # for CPU
     num_devices = num_devices if num_devices > 0 else 1
     return num_devices
예제 #5
0
def try_gpu():
    if len(test_utils.list_gpus()) > 0:
        return mx.gpu()
    else:
        return mx.cpu()
예제 #6
0
 def _get_num_devices(self):
     # caution: if not using all the GPUs, please mannually set num_devices
     num_devices = len(test_utils.list_gpus())
     # for CPU
     num_devices = num_devices if num_devices > 0 else 1
     return num_devices
예제 #7
0
from mydataset import _test_data
from mxnet.gluon.data.vision import transforms
from mxnet.gluon.data import DataLoader
from myNet import resnet18
from mxnet import cpu, gpu
from mxnet import ndarray as nd
from mxnet.test_utils import list_gpus
import pandas as pd

BATCH_SIZE = 1
MODEL_PATH = 'resnet18.params'

if list_gpus():
    CTX = gpu()
else:
    CTX = cpu()

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
])

test_dataloader = DataLoader(_test_data.transform_first(transform_test),
                             batch_size=BATCH_SIZE,
                             shuffle=True,
                             last_batch='keep')

net = resnet18(10)
net.load_parameters(MODEL_PATH, ctx=CTX)
# net.initialize(ctx=CTX)