Beispiel #1
0
def recognize_char_p():
    label_map = load_label_map()
    model = load_model_nn()

    x = model['x']
    keep_prob = model['keep_prob']
    saver = model['saver']
    prediction = model['prediction']
    graph = model['graph']
    model_ckpt_path, _ = find_model_ckpt()
    # print('load check-point %s'%model_ckpt_path, file=sys.stderr)
    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        saver.restore(session, model_ckpt_path)

        while True:
            sys.stdout.flush()
            captcha_path = input().strip()
            if captcha_path == '$exit':  # for close session
                break
            im = np.reshape(cv2.imread(captcha_path, cv2.IMREAD_GRAYSCALE),
                            IMAGE_SIZE)
            label = prediction.eval(feed_dict={
                x: [im],
                keep_prob: 1.0
            },
                                    session=session)[0]
            sys.stdout.write(label_map[label])
            sys.stdout.write('\n')
Beispiel #2
0
def recognize_p():
    """ 
    captcha_path
    $exit to exit
    """
    # print("recognize_p")

    label_map = load_label_map()
    model = load_model_nn()

    x = model['x']
    keep_prob = model['keep_prob']
    saver = model['saver']
    prediction = model['prediction']
    graph = model['graph']
    model_ckpt_path, _ = find_model_ckpt()
    print('load check-point %s' % model_ckpt_path, file=sys.stderr)
    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        saver.restore(session, model_ckpt_path)

        while True:
            sys.stdout.flush()
            captcha_path = input().strip()
            # print("_recoginze", captcha_path)
            if captcha_path == '$exit':  # for close session
                break

            spliter = Spliter(os.curdir)

            try:
                letters = spliter.split_letters(captcha_path)
                formatted_letters = map(spliter.format_splited_image, letters)
                formatted_letters = [
                    letter.reshape(image_size) for letter in formatted_letters
                ]
            except Exception as ex:
                sys.stdout.write('\n')
                continue

            result = []
            for letter in formatted_letters:
                label = prediction.eval(feed_dict={
                    x: [letter],
                    keep_prob: 1.0
                },
                                        session=session)[0]
                result.append(label_map[label])
                sys.stdout.write(label_map[label])

            sys.stdout.write('\n')
Beispiel #3
0
def recognize_p():
    """ 
    captcha_path
    $exit to exit
    """
    # print("recognize_p")

    label_map = load_label_map()
    model = load_model_nn()

    x = model['x']
    keep_prob = model['keep_prob']
    saver = model['saver']
    prediction = model['prediction']
    graph = model['graph']
    model_ckpt_path, _ = find_model_ckpt()
    print('load check-point %s' % model_ckpt_path, file=sys.stderr)
    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        saver.restore(session, model_ckpt_path)

        while True:
            sys.stdout.flush()
            captcha_path = input().strip()
            # print("_recoginze", captcha_path)
            if captcha_path == '$exit':  # for close session
                break

            spliter = Spliter(os.curdir)

            try:
                letters = spliter.split_letters(captcha_path)
                formatted_letters = map(spliter.format_splited_image,letters)
                formatted_letters = [letter.reshape(image_size) for letter in formatted_letters]
            except Exception as ex:
                sys.stdout.write('\n')
                continue

            result = []
            for letter in formatted_letters:
                label = prediction.eval(feed_dict={x: [letter], keep_prob: 1.0}, session=session)[0]
                result.append(label_map[label])
                sys.stdout.write(label_map[label])

            sys.stdout.write('\n')
Beispiel #4
0
def recognize_char_p():
    label_map = load_label_map()
    model = load_model_nn()

    x = model['x']
    keep_prob = model['keep_prob']
    saver = model['saver']
    prediction = model['prediction']
    graph = model['graph']
    model_ckpt_path, _ = find_model_ckpt()
    # print('load check-point %s'%model_ckpt_path, file=sys.stderr)
    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        saver.restore(session, model_ckpt_path)

        while True:
            sys.stdout.flush()
            captcha_path = input().strip()
            if captcha_path == '$exit':  # for close session
                break
            im = np.reshape(cv2.imread(captcha_path, cv2.IMREAD_GRAYSCALE), IMAGE_SIZE)
            label = prediction.eval(feed_dict={x: [im], keep_prob: 1.0}, session=session)[0]
            sys.stdout.write(label_map[label])
            sys.stdout.write('\n')
Beispiel #5
0
def train(alpha=5e-5):
    print("loading %s..." % formatted_dataset_path)
    with open(formatted_dataset_path, 'rb') as f:
        import sys
        if sys.version_info.major == 3:
            save = pickle.load(f, encoding='latin1')
        else:
            save = pickle.load(f)
        train_dataset = save['train_dataset']
        train_labels = save['train_labels']
        test_dataset = save['test_dataset']
        test_labels = save['test_labels']
        label_map = save['label_map']

    num_labels = len(label_map)

    print("train_dataset:", train_dataset.shape)
    print("train_labels:", train_labels.shape)
    print("test_dataset:", test_dataset.shape)
    print("test_labels:", test_labels.shape)
    print("num_labels:", num_labels)

    model = load_model_nn(alpha)
    x = model['x']
    y = model['y']
    loss = model['loss']
    optimizer = model['optimizer']
    accuracy = model['accuracy']
    keep_prob = model['keep_prob']
    saver = model['saver']
    graph = model['graph']

    save_dir = os.path.join(trainer_dir, '.checkpoint')
    print("Model saved path: ", save_dir)

    batch_size = 64

    def save_model(_step):
        saver.save(session,
                   os.path.join(save_dir, 'weibo.cn-model.ckpt'),
                   global_step=_step)

    with tf.Session(graph=graph) as session:
        tf.summary.scalar('loss', loss)
        tf.summary.scalar('accuracy', accuracy)
        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter(graph_log_dir, session.graph)
        tf.global_variables_initializer().run()

        step = 0
        try:
            model_ckpt_path, global_step = find_model_ckpt(
            )  # try to continue ....
        except FileNotFoundError:
            print("Initialized")
        else:  # try continue to train
            saver.restore(session, model_ckpt_path)
            step = global_step
            print('found %s, step from %d' % (model_ckpt_path, step))

        origin_step = step
        while True:
            offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
            # Generate a minibatch.
            batch_data = train_dataset[offset:(offset + batch_size), :]
            batch_labels = train_labels[offset:(offset + batch_size), :]
            # print(batch_data, batch_labels)
            session.run([optimizer, loss],
                        feed_dict={
                            x: batch_data,
                            y: batch_labels,
                            keep_prob: 0.5
                        })
            step += 1
            if step % 50 == 0:
                train_accuracy = session.run(accuracy,
                                             feed_dict={
                                                 x: batch_data,
                                                 y: batch_labels,
                                                 keep_prob: 1.0
                                             })
                test_accuracy = session.run(accuracy,
                                            feed_dict={
                                                x: test_dataset,
                                                y: test_labels,
                                                keep_prob: 1.0
                                            })

                print(("Step %5d, Training accuracy: %4f, Test accuracy: %4f" %
                       (step, train_accuracy, test_accuracy)))

                if step % 100 == 0:  # save the model every 100 step
                    save_model(step)

                if test_accuracy > 0.999 or step - origin_step > 2000:
                    print('you can re-format dataset and give a smaller alpha '
                          'to continue training')
                    save_model(step)
                    break

        print("Test accuracy: %g" % session.run(accuracy,
                                                feed_dict={
                                                    x: test_dataset,
                                                    y: test_labels,
                                                    keep_prob: 1.0
                                                }))
Beispiel #6
0
def train(alpha=5e-5):
    print("loading %s..." % formatted_dataset_path)
    with open(formatted_dataset_path, 'rb') as f:
        import sys
        if sys.version_info.major == 3:
            save = pickle.load(f, encoding='latin1')
        else:
            save = pickle.load(f)
        train_dataset = save['train_dataset']
        train_labels = save['train_labels']
        test_dataset = save['test_dataset']
        test_labels = save['test_labels']
        label_map = save['label_map']

    num_labels = len(label_map)

    print("train_dataset:", train_dataset.shape)
    print("train_labels:", train_labels.shape)
    print("test_dataset:", test_dataset.shape)
    print("test_labels:", test_labels.shape)
    print("num_labels:", num_labels)

    model = load_model_nn(alpha)
    x = model['x']
    y = model['y']
    loss = model['loss']
    optimizer = model['optimizer']
    accuracy = model['accuracy']
    keep_prob = model['keep_prob']
    saver = model['saver']
    graph = model['graph']

    save_dir = os.path.join(trainer_dir, '.checkpoint')
    print("Model saved path: ", save_dir)

    batch_size = 64

    def save_model(_step):
        saver.save(
            session,
            os.path.join(save_dir, 'weibo.cn-model.ckpt'),
            global_step=_step
        )

    with tf.Session(graph=graph) as session:
        tf.summary.scalar('loss', loss)
        tf.summary.scalar('accuracy', accuracy)
        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter(graph_log_dir, session.graph)
        tf.global_variables_initializer().run()

        step = 0
        try:
            model_ckpt_path, global_step = find_model_ckpt()  # try to continue ....
        except FileNotFoundError:
            print("Initialized")
        else:  # try continue to train
            saver.restore(session, model_ckpt_path)
            step = global_step
            print('found %s, step from %d' % (model_ckpt_path, step))

        origin_step = step
        while True:
            offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
            # Generate a minibatch.
            batch_data = train_dataset[offset:(offset + batch_size), :]
            batch_labels = train_labels[offset:(offset + batch_size), :]
            # print(batch_data, batch_labels)
            session.run(
                [optimizer, loss],
                feed_dict={
                    x: batch_data,
                    y: batch_labels,
                    keep_prob: 0.5
                }
            )
            step += 1
            if step % 50 == 0:
                train_accuracy = session.run(
                    accuracy,
                    feed_dict={
                        x: batch_data,
                        y: batch_labels,
                        keep_prob: 1.0
                    }
                )
                test_accuracy = session.run(
                    accuracy,
                    feed_dict={
                        x: test_dataset,
                        y: test_labels,
                        keep_prob: 1.0
                    }
                )

                print(("Step %5d, Training accuracy: %4f, Test accuracy: %4f" %
                       (step, train_accuracy, test_accuracy)))

                if step % 100 == 0:  # save the model every 100 step
                    save_model(step)

                if test_accuracy > 0.999 or step-origin_step > 2000:
                    print('you can re-format dataset and give a smaller alpha '
                          'to continue training')
                    save_model(step)
                    break

        print("Test accuracy: %g" %
              session.run(
                  accuracy,
                  feed_dict={
                      x: test_dataset,
                      y: test_labels,
                      keep_prob: 1.0
                  })
              )