Exemple #1
0
def run_test(output_name, output_nodes, recreate=True):
    batch_size = 1
    source_shapes = {
        ph.name: [
            int(d.value) if d.value is not None else batch_size
            for d in ph.shape.dims
        ]
        for ph in get_placeholders()
    }
    pb_path = os.path.join('out', 'pb', output_name)
    network_name = output_name.rstrip('/').replace('/', '_')
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        save_protobuf(pb_path, output_nodes, sess, recreate)
        sess.close()

    for prefer_nhwc in [True, False]:
        test_activations(filename=os.path.join(pb_path, 'test.pb'),
                         source_shapes=source_shapes,
                         feed_dict={
                             utils.anystr_to_str(k): np.random.random(v)
                             for k, v in six.iteritems(source_shapes)
                         },
                         prefer_nhwc=prefer_nhwc,
                         network_name=network_name,
                         delete_after_each=False,
                         export_io_only=True)

    return nnef.parse_file(
        os.path.join('out', network_name + '_nhwc', 'nnef',
                     network_name + '_nnef', 'graph.nnef'))
Exemple #2
0
def main(args):
    if args.seed is not None:
        np.random.seed(args.seed)

    distributions = {
        'scalar': uniform(0.0, 1.0),
        'integer': integers(0, 100),
        'logical': bernoulli(0.5),
    }

    try:
        random = eval(args.random)
        if isinstance(random, dict):
            distributions.update(
                {key: _ensure_lambda(value)
                 for key, value in random.items()})
        else:
            random = _ensure_lambda(random)
            if args.random.startswith('integers'):
                distributions['integer'] = random
            elif args.random.startswith('bernoulli'):
                distributions['logical'] = random
            else:
                distributions['scalar'] = random
    except Exception as e:
        print("Could not evaluate distribution: " + str(e), file=sys.stderr)
        return -1

    graph = nnef.parse_file(os.path.join(args.model, 'graph.nnef'))

    for op in graph.operations:
        if args.weights and op.name == 'variable':
            label = op.attribs['label']
            shape = op.attribs['shape']
            data = distributions[op.dtype](shape)
            filename = os.path.join(args.model, label + '.dat')

            os.makedirs(os.path.split(filename)[0], exist_ok=True)
            with open(filename, 'wb') as file:
                nnef.write_tensor(file, data)

            if args.verbose:
                print("Generated weight '{}'".format(filename))

        if args.inputs and op.name == 'external':
            name = op.outputs['output']
            shape = op.attribs['shape']
            data = distributions[op.dtype](shape)
            filename = os.path.join(args.model, args.inputs, name + '.dat')

            os.makedirs(os.path.split(filename)[0], exist_ok=True)
            with open(filename, 'wb') as file:
                nnef.write_tensor(file, data)

            if args.verbose:
                print("Generated input '{}'".format(filename))

    return 0
Exemple #3
0
    def run(self):
        # Changing dir to NNEF's graph parent folder.
        network_dir, model_filename = os.path.split(self.input_model)
        if network_dir != '':
            prevdir = os.getcwd()
            os.chdir(network_dir)

        try:
            attr, ops = nnef.parse_file(model_filename)
        except Exception as ex:
            print('WARNING: converter ignoring exception:', ex)
            nnef._register_layer_ops()
            try:
                attr, ops = nnef.parse_file(model_filename)
            except Exception as ex:
                raise Exception('failed to open %s: %s' %
                                (self.input_model, ex))

        self.graph_name = attr['graph'].name
        for operand in ops:
            if hasattr(self, "import_" + operand[0][0]):
                func = getattr(self, "import_" + operand[0][0])
                returned_node = func(operand)
                self.node_pool[returned_node.name] = returned_node
            else:
                self.import_UNKNOWN(operand)

        input_nodes = self.get_input_nodes()
        output_nodes = self.get_output_nodes()

        graph = NNEFGraph(os.path.basename(self.input_model).split('.')[0],
                          input_nodes,
                          output_nodes,
                          node_pool=self.node_pool)

        if prevdir is not None:
            os.chdir(prevdir)
        return graph
Exemple #4
0
    def __init__(self,
                 path,
                 device=None,
                 decomposed=None,
                 custom_operators=None):
        self._nnef_graph = nnef.parse_file(os.path.join(path, 'graph.nnef'),
                                           lowered=decomposed)
        self._init_input_shapes(self._nnef_graph)

        self._nnef_module = NNEFModule(path=path,
                                       custom_operators=custom_operators,
                                       decomposed=decomposed)

        if device is None:
            device = 'cuda' if torch.cuda.is_available() else 'cpu'

        self._nnef_module.to(device)
        self._nnef_module.eval()
        self._device = device
Exemple #5
0
def main(args):
    if args.seed is not None:
        np.random.seed(args.seed)

    try:
        distribution = eval(args.random)
        if not _is_lambda(distribution):
            distribution = distribution()
    except Exception as e:
        print("Could not evaluate distribution: " + str(e), file=sys.stderr)
        return -1

    graph = nnef.parse_file(os.path.join(args.model, 'graph.nnef'))

    for op in graph.operations:
        if args.weights and op.name == 'variable':
            label = op.attribs['label']
            shape = op.attribs['shape']
            data = distribution(shape).astype(_nnef_dtype_to_numpy[op.dtype])
            filename = os.path.join(args.model, label + '.dat')

            os.makedirs(os.path.split(filename)[0], exist_ok=True)
            with open(filename, 'wb') as file:
                nnef.write_tensor(file, data)

            if args.verbose:
                print("Generated weight '{}'".format(filename))

        if args.inputs and op.name == 'external':
            name = op.outputs['output']
            shape = op.attribs['shape']
            data = distribution(shape).astype(_nnef_dtype_to_numpy[op.dtype])
            filename = os.path.join(args.model, args.inputs, name + '.dat')

            os.makedirs(os.path.split(filename)[0], exist_ok=True)
            with open(filename, 'wb') as file:
                nnef.write_tensor(file, data)

            if args.verbose:
                print("Generated input '{}'".format(filename))

    return 0
def parse_file(file_name):
    with default_config:
        return nnef.parse_file(file_name, atomics=AtomicOperations)
Exemple #7
0
 def test_vggnet(self):
     nnef.parse_file("../examples/vgg.txt")
Exemple #8
0
 def test_resnet(self):
     nnef.parse_file("../examples/resnet.txt")
Exemple #9
0
 def test_googlenet(self):
     nnef.parse_file("../examples/googlenet.txt")
Exemple #10
0
 def test_alexnet(self):
     nnef.parse_file("../examples/alexnet.txt")