looking for some solutions? You are welcome.

SOLVED: Executing a Neural Network code in Python from a CPP program

Aniket Sonavane:

There is a Python code of MNIST neural network which we have used to identify Gujarati Characters in a script. For this, we have developed a GUI with the help of the QT Framework in C++ language. At present, the neural network code in executed in python but we faced issues while executing the MNIST model code through C++. I found a GitHub code-named Keras2CPP which has a dump_to_simple_cpp.py file. this file accepts the weights.h5 weight file and model_json file and then it generates a dumped.nnet file. the dump_to_simple_cpp.py file is as follows:

import numpy as np
from keras.models import Sequential, model_from_json
import json
import argparse

parser = argparse.ArgumentParser(description='This is a simple script 
to dump Keras model into simple format suitable for porting into pure 
C++ model')

parser.add_argument('-a', '--architecture', help="JSON with model 
architecture", required=True)
parser.add_argument('-w', '--weights', help="Model weights in HDF5 
format", required=True)
parser.add_argument('-o', '--output', help="Ouput file name", 
parser.add_argument('-v', '--verbose', help="Verbose", required=False)
args = parser.parse_args()

print('Read architecture from', args.architecture)
print('Read weights from', args.weights)
print('Writing to', args.output)

arch = open(args.architecture).read()
model = model_from_json(arch)
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
arch = json.loads(arch)

with open(args.output, 'w') as fout:
    fout.write('layers ' + str(len(model.layers)) + '\n')

layers = []
for ind, l in enumerate(arch["config"]):
    if args.verbose:
        print(ind, l)
    fout.write('layer ' + str(ind) + ' ' + l['class_name'] + '\n') #line number: 33

    if args.verbose:
        print(str(ind), l['class_name'])
    layers += [l['class_name']]

    if l['class_name'] == 'Convolution2D':

        W = model.layers[ind].get_weights()[0]
        if args.verbose:
        fout.write(str(W.shape[0]) + ' ' + str(W.shape[1]) + ' ' + str(W.shape[2]) + ' ' + str(W.shape[3]) + ' ' + l['config']['border_mode'] + '\n')

        for i in range(W.shape[0]):
            for j in range(W.shape[1]):
                for k in range(W.shape[2]):
                    fout.write(str(W[i,j,k]) + '\n')
        fout.write(str(model.layers[ind].get_weights()[1]) + '\n')

    if l['class_name'] == 'Activation':
        fout.write(l['config']['activation'] + '\n')
    if l['class_name'] == 'MaxPooling2D':
        fout.write(str(l['config']['pool_size'][0]) + ' ' + str(l['config']['pool_size'][1]) + '\n')

    if l['class_name'] == 'Dense':
        W = model.layers[ind].get_weights()[0]
        if args.verbose:
        fout.write(str(W.shape[0]) + ' ' + str(W.shape[1]) + '\n')

        for w in W:
            fout.write(str(w) + '\n')
        fout.write(str(model.layers[ind].get_weights()[1]) + '\n')

upon executing the above code with the help of the following command:

python3 dump_to_simple_cpp.py -a model_json -w model.h5 -o dumped.nnet

the following error is thrown:

File "dump_to_simple_cpp.py", line 33, in <module>
fout.write('layer ' + str(ind) + ' ' + l['class_name'] + '\n')
TypeError: string indices must be integers

Can anyone please help me in solving the above error?

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots

No comments: