0

I just got started with MXNet, and I have wrote and trained a CNN with MNIST based on some tutorials. Now I wish to actually send a picture to this CNN and get a result from it, how should I do it?

Here're my code:

def get_lenet():
    data = mx.symbol.Variable('data')
    # first conv
    conv1 = mx.symbol.Convolution(data=data, kernel=(5,5), num_filter=20)
    tanh1 = mx.symbol.Activation(data=conv1, act_type="tanh")
    pool1 = mx.symbol.Pooling(data=tanh1, pool_type="max",
                          kernel=(2,2), stride=(2,2))
    # second conv
    conv2 = mx.symbol.Convolution(data=pool1, kernel=(4,4), num_filter=50)
    tanh2 = mx.symbol.Activation(data=conv2, act_type="tanh")
    pool2 = mx.symbol.Pooling(data=tanh2, pool_type="max",
                          kernel=(2,2), stride=(2,2))

    # first fullc
    flatten = mx.symbol.Flatten(data=pool2)
    fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=500)
    tanh4 = mx.symbol.Activation(data=fc1, act_type="tanh")

    # second fullc
    fc2 = mx.symbol.FullyConnected(data=tanh4, num_hidden=10)
    # loss
    lenet = mx.symbol.SoftmaxOutput(data=fc2, name='softmax')
    return lenet

logging.getLogger().setLevel(logging.DEBUG)

model = mx.model.FeedForward(
    ctx=mx.cpu(),
    symbol=get_lenet(),
    num_epoch=5,
    learning_rate=0.1
)

model.fit(
    X=train_iter,
    eval_data=val_iter,
    batch_end_callback=mx.callback.Speedometer(batch_size, 200)
)

Thanks in advance for any help

Woooooooo
  • 85
  • 1
  • 2
  • 7

1 Answers1

0

You're using MXNet's Model API, which is deprecated in favor of Module API. In your specific example, however, you can run inference with this code:preds = model.predict(infer_iter) where infer_iter is a DataIter for the inference images. If you just want to feed in a single image, you can create an NDArrayIter and setting the data field to a numpy array that contains the image:

# Replace this line with real test data
test_data = np.random.uniform(size=(1, 1, 28, 28))
# Create data iterator
test_iter = mx.io.NDArrayIter(test_data)
# Perform inference
prediction = model.predict(test_iter)
print("prediction: ", np.argmax(prediction, axis=1))

I also recommend that you take a look at the Gluon API, which is significantly easier to understand and debug. Here is a tutorial on convolutional networks in Gluon.

Sina Afrooze
  • 960
  • 6
  • 11