To simulate a fully functional 2 layer CMOS-Memristor-based neural network for classification of benign and malign cancer.
The circuit-based neural network achieved an accuracy of 94.85% compared to the 96.71% accuracy of the original network. The trained weights were programmed onto the weight blocks of the memristor bridge in this neural network. Training of neural network was done on MATLAB and then weights were extracted and accordingly memristor-bridge weight blocks were configured. In the memristor-bridge circuit, currently resistors were used but they can be replaced with memristors. For activation function, we used ReLu (Rectified Linear Unit) and in between voltage to current converters were used, since the input of each layer is voltage and output is in current form. In the end, Softmax block (implemented using a sub-circuit netlist using a dependent source and inbuilt SPICE mathematical functions) is used which outputs the probability. Below is the final circuit diagram:
Use MATLAB 2017a neural network application and select the dataset to train upon. Train the neural network (with the same layer configuration as you want in hardware implementation) and retrieve the software accuracy. Export the model as MATLAB script. Make changes in the script to export the weight matrix and run it. Program the memristor bridge circuits of both the layers to the required weights (the ones got via software simulation). Simulate the model and retrieve the hardware-based accuracy. Compare the accuracy received by software and hardware simulation.
Tools/Resource to be used:
- MATLAB 2017a (should have neural network toolbox – crack available on torrent)
- LTSpice XV11 (Available freely on the internet)
- As an alternative to LTSpice, one can also use NI MultiSIM 14.0 (Crack available on torrent) – in this tutorial LTSpice is used.
Detailed procedure for setting up the tools:
- Install both the tools, LTSpice XV11 and MATLAB 2017a (Preferable OS: Windows 10).
- Open Neural network toolbox and NN Pattern recognition application, load Breast Cancer dataset from ‘Example Dataset’ and train the Neural Network:
- Export the model as a Matlab function and modify it to export weights. It should look somewhat like this:
- Convert the extracted weights to the memristor bridge’s Resistances values using the script below:
clc; clear all; wt_matrix = [-1.8928/2 1.9249/2 -0.4490 0.2411 -0.4786 -1.8611/2 0 0 0 0]; r1 = ones(1,10); r2 = ones(1,10); r3 = ones(1,10); r4 = ones(1,10); w_test = ones(1,10); for i = 1:1:10 R1 = 81000; R2 = 1000; t = R2/(R1+R2); wt1 = abs(wt_matrix(i)) + t; R4 = 82000*wt1; R3 = 82000 - R4; if(wt_matrix(i) < 0) temp1 = R4; temp2 = R3; R4 = R2; R2 = temp1; R3 = R1; R1 = temp2; end; r1(i) = R1; r2(i) = R2; r3(i) = R3; r4(i) = R4; w_test(i) = (R4/(R4 + R3))-(R2/(R2 + R1)); end;
- Open the neural network circuit in LTSpice XVII and change the resistance values in the memristor bridge within the circuit(In the schematic, by default weights for the Breast Cancer dataset has been set)
- Input the test data via PWL voltage sources at the input. Using a Matlab script, one can vectorize an image and append such images and export the resulting vectors in the form of PWL files. These PWL files can be used as an input to the Neural Network circuit. Below is the MATLAB script to convert images to PWL files:
%Image to voltage signal (PWL) clc; clear; voltage =5; img = imread("testimg5.png"); gray_img = rgb2gray(img); filtersize = [3 3]; imsize = size(gray_img); oconv_size = [ (imsize(1) - filtersize(1) + 1) (imsize(2) - filtersize(2) + 1)]; %Extracting 3x3 segments from the image %Stride 1 for i = 1:1:oconv_size(1) for j = 1:1:oconv_size(2) for k = 1:1:filtersize(1) for l = 1:1:filtersize(2) imgseg(i,j,k,l) = gray_img( i+k-1, j+l-1); end end end end %Signal Generation fname="PWL"; fname1=""; imgseg1 = double(imgseg); imgseg1 = (imgseg1 / 255)*voltage; time = 0:0.001:((oconv_size(1)*oconv_size(2))/1000 - 0.001); time = time'; for i= 1:1:filtersize(1) for j=1:1:filtersize(2) fname1 = strcat(fname,int2str(i),int2str(j)); tseq = [time reshape((imgseg1(:,:,i,j))',,1)]; save('-ascii',fname1,'tseq'); end end %PC convolution : %EDGE FILTER imgseg = double(imgseg); filter = [-1 -1 -1; -1 -8 1; -1 -1 -1]; filter = filter/10; filter = reshape(filter,1,1,3,3); for i=1:1:oconv_size(1) for j=1:1:oconv_size(2) %POSSIBLE RESHAPING ERROR: conv_img(i,j) = sum(sum(imgseg(i,j,:,:).*filter)); end end conv_img = conv_img - min(min(conv_img)); conv_max = max(max(conv_img)); conv_img = conv_img * (255/conv_max); conv_img = uint8(conv_img); imshow(conv_img);
- Run the simulation. And save the softmax output in a CSV file.
- Open the CSV file in MATLAB and use the following function to convert softmax output to the Neural network’s efficiency.
function x = Decoder(fname) %fname = "Smax1.csv"; M = csvread(fname); seq1 = 0:0.001:(699/1000 - 0.001); in1 = interp1(M(:,1)',M(:,2)',seq1); x = in1; end
Results to expect:
Observations and interpretations to be made:
One can observe that there are spikes in the softmax output. These spikes correspond to the output of the Neural Network whether the input image has cancer or not. If it is a benign image, the output is as low as 3% and in the case of a malign image, the output is as high as 97%.