Tweaks and Tricks: Running a Neural Network on the Raspberry Pi


Introduction: Tweaks and Tricks: Running a Neural Network on the Raspberry Pi

About: I work on electronics and machine learning

In my previous instructable, I pointed out that I was only able to achieve an accuracy of about 80% and having an accuracy score of 80% percent for such a well-known data set is not decent. However, after tweaking the code I was able to achieve an astonishing 99.4% accuracy.The trick was to have two hidden layers instead of having one and increasing the number of hidden nodes from 12 to 392 (this was purely through trial and error). Training the neural network for a longer period of time also helped.

This project was done by me, Nikhil Raghavendra, a Diploma in Computer Engineering student from Singapore Polytechnic, School of Electrical and Electronic Engineering, under the guidance of my mentor Mr Teo Shin Jen.

Step 1: Tweaking and Optimizing the Original Code

The graph above shows the accuracy level of the model that was run for 700 epochs. Even though the accuracy was fluctuating quite a bit as the network was trained and tested, there was a net increase in the accuracy of the prediction up till the 600th epoch. After the 600th epoch, the accuracy was just fluctuating to near 100% and back to about 80% (this observation was consistent with 5 executions of the model). We can see the accuracy is the highest around the 680th epoch (99.xx%), so training the neural network till the 680th epoch will help us achieve an accuracy closer to 100%.

We should also note that every time we make a prediction, we are essentially training first before making the prediction and training the network till the 680th epoch takes almost 5 minutes and that’s a lot of time being wasted, so to save time, we can save the weights and the model itself into an hd5 file and a JSON file respectively and run a script based on that model using the optimized weights. This will make the execution faster. Tweak your code as found below.

from keras.models import Sequential
from keras.layers import Dense from keras.models import model_from_json import numpy as np import time import os #Time start_time = time.time() # load dataset np.random.seed(7) dataset = np.loadtxt("data.csv", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:, 0:8] Y = dataset[:, 8] # create model model = Sequential() model.add(Dense(8, activation="relu", input_dim=8, kernel_initializer="normal")) model.add(Dense(392, activation="relu", kernel_initializer="normal")) model.add(Dense(392, activation="relu", kernel_initializer="normal")) model.add(Dense(1, activation="sigmoid", kernel_initializer="normal")) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model, Y, epochs=680, batch_size=10, verbose=2) # Evaluate and save the weights to the file scores = model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) # serialize model to JSON model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights("model.h5") print("Saved model to disk") print("Execution took {} seconds".format(time.time() - start_time))

Then to load the model and run it (Ideally in a separate script for faster execution),

from keras.models import Sequential
from keras.layers import Dense from keras.models import model_from_json import numpy as np import time import os # Load JSON start_time = time.time() json_file = open('model.json', 'r') loaded_model_json = json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("model.h5") print("Loaded model and weights from disk") # evaluate loaded model on test data loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # calculate predictions test = np.array([[6,148,72,35,0,33.6,0.627,50]]) score = loaded_model.predict(test) # round predictions rounded = [round(x[0]) for x in score] print(rounded[0]) print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[0]*100)) print("Execution took {} seconds".format(time.time() - start_time))

Step 2: The Results

Running the code above gave me an accuracy of 99.46% and the execution only took 0.31 seconds (the execution should ideally be executed using a separate script, it’s lightning fast!



    • Planter Challenge

      Planter Challenge
    • Clocks Contest

      Clocks Contest
    • Woodworking Contest

      Woodworking Contest

    We have a be nice policy.
    Please be positive and constructive.