Code Monkey home page Code Monkey logo

Comments (3)

goofmint avatar goofmint commented on September 2, 2024

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.

from nnabla.

goofmint avatar goofmint commented on September 2, 2024

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

from nnabla.

goofmint avatar goofmint commented on September 2, 2024

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)

from nnabla.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.