Running a Neural Network on the Raspberry Pi

Introduction: Running a Neural Network on the Raspberry Pi

About: I work on electronics and machine learning

In my previous instructable, we installed Keras on our Raspberry Pi and in this instructable, we will be building a neural network using Keras. The neural network will be used to predict a person’s chances of having diabetes. First let's obtain our training data. You can get the training data here, download and name it as data.csv. The data is in the following format, where the list indices indicate the column number of the data in the CSV file.

  1. Number of times pregnant
  2. Plasma glucose concentration a 2 hours in an oral glucose tolerance test
  3. Diastolic blood pressure (mm Hg)
  4. Triceps skin fold thickness (mm)
  5. 2-Hour serum insulin (mu U/ml)
  6. Body mass index (weight in kg/(height in m)^2)
  7. Diabetes pedigree function
  8. Age (years)
  9. Class (1 – Diabetic, 0 – Not Diabetic)

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: The Code

Boot up your Raspberry Pi and go to your Desktop, create a folder and create a file named data.csv. Now copy the contents of the data.csv file in your computer onto the data.csv on your Raspberry Pi. After saving and closing it, create a file and type the code found below.

from keras.models import Sequential
from keras.layers import Dense import numpy as np # 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(12, 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=500, batch_size=10, verbose=2) # calculate predictions # The list below contains patient data for whom we want to carry out the prediction test = np.array([[6,148,72,35,0,33.6,0.627,50]]) predictions = model.predict(test) # predict rounded = [round(x[0]) for x in predictions] print(rounded[0])

Step 2: Running the Network

Save it and then run it. It should take about 3 minutes to complete execution (Training, testing and prediction). I was only able to achieve an accuracy of around 80% so use your knowledge to tweak the code to achieve a higher accuracy.



    • Creative Misuse Contest

      Creative Misuse Contest
    • Tiny Home Contest

      Tiny Home Contest
    • Fix It! Contest

      Fix It! Contest