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

How to get or use network from nnp #233

Closed
goofmint opened this issue Sep 4, 2018 · 3 comments
Closed

How to get or use network from nnp #233

goofmint opened this issue Sep 4, 2018 · 3 comments
Assignees

Comments

@goofmint
Copy link

goofmint commented Sep 4, 2018

Hi.

Neural Network Console of Windows provides h5 file and Python code. And Cloud version provides just nnp file. I know the nnp file has h5 and network of protocol buffers. But I don't know how to use network in nnp file.

Usally we can get the python code like below.

import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF

def network(x, y, test=False):
    # Input:x -> 1,28,28
    # MaxPooling -> 1,14,14
    h = F.max_pooling(x, (2,2), (2,2))
    # Affine -> 100
    h = PF.affine(h, (100,), name='Affine')
    # ReLU
    h = F.relu(h, True)
    # Affine_2 -> 1
    h = PF.affine(h, (1,), name='Affine_2')
    # Sigmoid
    h = F.sigmoid(h)
    # BinaryCrossEntropy
    h = F.binary_cross_entropy(h, y)
    return h

And we don't neet BinaryCrossEntropy, so delete it. The nnp file's network has it.

import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF
from nnabla.utils import nnp_graph

nnp = nnp_graph.NnpLoader('./result.nnp')
graph = nnp.get_network('MainValidation')
y = graph.outputs
print(y)

This code outputs below.

{'BinaryCrossEntropy': <Variable((64, 1), need_grad=True) at 0x114f926d8>}

I hope use the nnp in Python like below.

import nnabla as nn

nn.load_parameters('./result.nnp')
graph = nn.get_network('MainValidation')
x = graph.inputs['x']
y = graph.outputs['y']
y.forward(x, test=True)
y.d

How to use network in the nnp file?

I know nnabla_cli can do it. But I don't know how to do it.

Thanks

@goofmint
Copy link
Author

goofmint commented Sep 4, 2018

I can use the nnp file like below.

def image():
     :
    x = nn.Variable((1,1,28,28))
    x.d = np.array(img) * (1.0 / 255.0)
    y = network(x, test=True)
    y.forward()
     :

def network(x, test=False):
    # Input:x -> 1,28,28
    # MaxPooling -> 1,14,14
    h = F.max_pooling(x, (2,2), (2,2))
    # Affine -> 100
    h = PF.affine(h, (100,), name='Affine')
    # ReLU
    h = F.relu(h, True)
    # Affine_2
    h = PF.affine(h, (1,), name='Affine_2')
    # Sigmoid
    h = F.sigmoid(h)
    return h

But I think I don't need to write network method if I'm using nnp file. 'cause it is contained network by protocol buffer.

@TakuyaNarihira TakuyaNarihira self-assigned this Sep 4, 2018
@goofmint
Copy link
Author

goofmint commented Sep 5, 2018

This is NOT completely code.

import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF
from nnabla.utils import nnp_graph

nnp = nnp_graph.NnpLoader('./result.nnp')
print(nnp.get_network_names())
graph = nnp.get_network('MainRuntime', batch_size=1)
x = graph.inputs['Input'][0]
print(x)
y = graph.outputs
print(y)

Output:

# print(nnp.get_network_names())
['Main', 'MainValidation', 'MainRuntime']
# print(x)
<Variable((1, 28, 28), need_grad=False) at 0x11e7a6728>
# print(y)
{'Sigmoid': <Variable((1, 1), need_grad=True) at 0x11e7a6638>}

If I'm using Main or MainValidation network, output is below.

# print(x)
<Variable((1, 28, 28), need_grad=False) at 0x11a08c818>
# print(y)
{'BinaryCrossEntropy': <Variable((1, 1), need_grad=True) at 0x11a08c728>}

I can't find out variable x and y from graph.inputs and graph.outputs looks like sample code. And I don't know what is Nnp method.

# Read a .nnp file.
nnp = Nnp('/path/to/nnp.nnp')
# Assume a graph `graph_a` is in the nnp file.
net = nnp.get_network(network_name, batch_size=1)
# `x` is an input of the graph.
x = net.inputs['x']
# 'y' is an outputs of the graph.
y = net.outputs['y']
# Set random data as input and perform forward prop.
x.d = np.random.randn(x.shape)
y.forward(clear_buffer=True)
print('output:', y.d)

https://nnabla.readthedocs.io/en/latest/_modules/nnabla/utils/nnp_graph.html

I have attached my nnp file.

Thanks.

result.nnp.zip

@goofmint
Copy link
Author

goofmint commented Sep 7, 2018

I did it.

from nnabla.utils import nnp_graph
import numpy as np
from PIL import Image

nnp = nnp_graph.NnpLoader('./result.nnp')
graph = nnp.get_network('MainRuntime', batch_size=1)
# Input variable name
input = list(graph.inputs.keys())[0]
# Output variable name
output = list(graph.outputs.keys())[0]
# Get input and output
x = graph.inputs[input]
y = graph.outputs[output]
# Open image
img = Image.open('001.png')
# Transform
x.d = np.array(img) * (1.0 / 255.0)
# Forward
y.forward(clear_buffer=True)
print(y.d)

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

2 participants