Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paddle V4 API - Recognize Digits #10215

Closed
cs2be opened this issue Apr 25, 2018 · 1 comment
Closed

Paddle V4 API - Recognize Digits #10215

cs2be opened this issue Apr 25, 2018 · 1 comment
Assignees

Comments

@cs2be
Copy link
Contributor

cs2be commented Apr 25, 2018

class RecognizeDigits(fluid.Program):
    def _conv_net(img):
        conv_pool_1 = fluid.nets.simple_img_conv_pool(
            input=img,
            filter_size=5,
            num_filters=20,
            pool_size=2,
            pool_stride=2,
            act="relu")
        conv_pool_1 = fluid.layers.batch_norm(conv_pool_1)
        conv_pool_2 = fluid.nets.simple_img_conv_pool(
            input=conv_pool_1,
            filter_size=5,
            num_filters=50,
            pool_size=2,
            pool_stride=2,
            act="relu")
        prediction = fluid.layers.fc(input=conv_pool, size=10, act='softmax')
        return prediction

    @network('img', 'label')
    def train_step():
        img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
        label = fluid.layers.data(name='label', shape=[1], dtype='int64')

        with fluid.var_scope('prediction'):
            prediction = _conv_net(img)

        loss = fluid.layers.cross_entropy(input=prediction, label=label)
        avg_loss = fluid.layers.mean(loss)
        acc = fluid.layers.accuracy(input=prediction, label=label)
        optimizer = fluid.optimizer.Adam(learning_rate=0.001)
        optimizer.minimize(avg_loss)
        return acc, avg_loss

    @network('img', 'label')
    def test_step():
        img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
        label = fluid.layers.data(name='label', shape=[1], dtype='int64')

        with fluid.var_scope('prediction'):
            prediction = _conv_net(img)

        loss = fluid.layers.cross_entropy(input=prediction, label=label)
        avg_loss = fluid.layers.mean(loss)
        acc = fluid.layers.accuracy(input=prediction, label=label)
        return acc, avg_loss

    @network('img')
    def infer():
        img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')

        with fluid.var_scope('prediction'):
            prediction = _conv_net(img)
            return prediction

recognize_digits = RecognizeDigits().Compile()
train_reader = paddle.batch(paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500),
    batch_size=BATCH_SIZE)
test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=BATCH_SIZE)

NUM_EPOCHS = 100
for pass_id in range(NUM_EPOCHS):
    for batch_id, data in enumerate(train_reader()):
        recognize_digits.train_step(data[0], data[1])

        if (batch_id + 1) % 10 == 0:
            acc_set = []
            avg_loss_set = []
            for test_data in test_reader():
                acc_np, avg_loss_np = recognize_digits.test_step(test_data[0], test_data[1])
                acc_set.append(float(acc_np))
                avg_loss_set.append(float(avg_loss_np))

            # get test acc and loss
            acc_val = numpy.array(acc_set).mean()
            if float(acc_val) > 0.2:
                fluid.save_parameters(recognize_digits.infer, "./inference_parameter")
                break

tensor_img = numpy.random.uniform(-1.0, 1.0, [1, 1, 28, 28]).astype("float32")
prediction = recognize_digits.infer(tensor_img)
print prediction
@shanyi15
Copy link
Collaborator

您好,此issue在近一个月内暂无更新,我们将于今天内关闭。若在关闭后您仍需跟进提问,可重新开启此问题,我们将在24小时内回复您。因关闭带来的不便我们深表歉意,请您谅解~感谢您对PaddlePaddle的支持!
Hello, this issue has not been updated in the past month. We will close it today for the sake of other user‘s experience. If you still need to follow up on this question after closing, please feel free to reopen it. In that case, we will get back to you within 24 hours. We apologize for the inconvenience caused by the closure and thank you so much for your support of PaddlePaddle Group!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants