Commit 9b6ee12d authored by Jesse Heckman's avatar Jesse Heckman

clear temps / added analyses machine learning

parent 057ae718
function [imgs, labels] = readMNIST(imgFile, labelFile, readDigits, offset)
% readMNIST by Siddharth Hegde
%
% Description:
% Read digits and labels from raw MNIST data files
% File format as specified on http://yann.lecun.com/exdb/mnist/
% Note: The 4 pixel padding around the digits will be remove
% Pixel values will be normalised to the [0...1] range
%
% Usage:
% [imgs labels] = readMNIST(imgFile, labelFile, readDigits, offset)
%
% Parameters:
% imgFile = name of the image file
% labelFile = name of the label file
% readDigits = number of digits to be read
% offset = skips the first offset number of digits before reading starts
%
% Returns:
% imgs = 20 x 20 x readDigits sized matrix of digits
% labels = readDigits x 1 matrix containing labels for each digit
% Read digits
fid = fopen(imgFile, 'r', 'b');
header = fread(fid, 1, 'int32');
if header ~= 2051
error('Invalid image file header');
end
count = fread(fid, 1, 'int32');
if count < readDigits+offset
error('Trying to read too many digits');
end
h = fread(fid, 1, 'int32');
w = fread(fid, 1, 'int32');
if offset > 0
fseek(fid, w*h*offset, 'cof');
end
imgs = zeros([h w readDigits]);
for i=1:readDigits
for y=1:h
imgs(y,:,i) = fread(fid, w, 'uint8');
end
end
fclose(fid);
% Read digit labels
fid = fopen(labelFile, 'r', 'b');
header = fread(fid, 1, 'int32');
if header ~= 2049
error('Invalid label file header');
end
count = fread(fid, 1, 'int32');
if count < readDigits+offset
error('Trying to read too many digits');
end
if offset > 0
fseek(fid, offset, 'cof');
end
labels = fread(fid, readDigits, 'uint8');
fclose(fid);
% Calc avg digit and count
imgs = trimDigits(imgs, 4);
imgs = normalizePixValue(imgs);
%[avg num stddev] = getDigitStats(imgs, labels);
end
function digits = trimDigits(digitsIn, border)
dSize = size(digitsIn);
digits = zeros([dSize(1)-(border*2) dSize(2)-(border*2) dSize(3)]);
for i=1:dSize(3)
digits(:,:,i) = digitsIn(border+1:dSize(1)-border, border+1:dSize(2)-border, i);
end
end
function digits = normalizePixValue(digits)
digits = double(digits);
for i=1:size(digits, 3)
digits(:,:,i) = digits(:,:,i)./255.0;
end
end
\ No newline at end of file
%% Exercise Digit Recogniton
% Description:
% The recognition of digits is a supervised learning task where a training
% dataset of 60k images {X1,...,Xn} is matched to a target set of 60k
% labels {T1,...,Tn}. After training, the model can be expressed as a
% function y(x), which can take a new image x and generate and output y.
% Machine learning in steps:
% - Prepare Data
% - Choose Algorithm
% - Fit a Model
% - Choose Validation Method
% - Update
% - Predict using Model
%% Initialize
% Read data from MNIST in matlab arrays (Use readMNIST.m)
% Clean
cfn = pb_clean;
p = [pb_userpath 'machine learning/analysis/clustering/digit recognition/'];
fn = 'data/data_MNIST.mat';
cd(p);
% Globals
sfx_img = '-images-idx3-ubyte';
sfx_lbl = '-labels-idx1-ubyte';
pfx = {'train','t10k'};
sets = {'Training','Test'};
datasz = [60000, 10000];
offset = 0;
if ~exist(fn,'file')==2
% Open or Read data
D = struct('img',[],'lbl',[]);
for iPfx = 1:length(pfx) % Read in data from MNIST
% Set paramaters
imgFile = [pfx{iPfx} sfx_img];
lblFile = [pfx{iPfx} sfx_lbl];
% Read & Store
[img, lbl] = readMNIST(imgFile, lblFile, datasz(iPfx), offset);
D(iPfx).img = img;
D(iPfx).lbl = lbl;
end
save(fn,'D');
else
load(fn);
end
%% Display Example Digits
% Verify reading in the data
% Colormap
def = 9;
col = pb_selectcolor(64,6);
for iSet = 1:2 % Visualize both training and test set
cfn = pb_newfig(cfn);
for iDig = 1:10 % Display example of each digit class from set
% Make axes & visualize
subplot(5,2,iDig);
ind = find(D(iSet).lbl == iDig-1,1);
imagesc(D(iSet).img(:,:,ind))
% Clean
set(gca,'YTick',[])
set(gca,'XTick',[])
axis square;
end
sgtitle([sets{iSet} ' set'],'FontWeight','Bold','FontSize',18)
colormap(col);
end
%% Preprocess data
% Reshape 3D to 2D, set X and Y for model;
% Training set
X = zeros(datasz(1),400);
T = D(1).lbl;
for iLen = 1:datasz(1) % Training
tmp = D(1).img(:,:,iLen);
X(iLen,:) = tmp(:);
end
% Testing set
Xtest = zeros(datasz(2),400);
Ttest = D(2).lbl;
for iLen = 1:datasz(2) % Test
tmp = D(2).img(:,:,iLen);
Xtest(iLen,:) = tmp(:);
end
%% Make adaptive model
% Select models
% % k-nearest neighbour classifier
mdl = fitcknn(X,T);
%% Evaluate model
% Test, and evaluate model performance
% Test data
predictions = mdl.predict(Xtest(:,:));
% Compute model accuracy
match = predictions == Ttest;
accuracy = sum(match)/datasz(2);
misses = find(match == 0);
hits = find(match == 1);
display(['Acurracy of model: ' num2str(accuracy,4)])
% Visualize mistakes
cfn = pb_newfig(cfn);
for iMiss = 1:length(misses) % visualize errors from the test data
clf;
n = misses(iMiss);
sample = num2str(n);
class = num2str(predictions(n));
label = num2str(Ttest(n));
imagesc(D(2).img(:,:,n));
title(['Classified (' class ') - Label (' label ')'],'FontSize',20)
xlabel(['Samplenr (' sample ')'])
% Clean
set(gca,'YTick',[])
set(gca,'XTick',[])
axis square;
colormap(col);
pause(.05);
end
%% Victory!
% Show success model
showsz = 10;
start = 592;
rhits = hits(start+1:start+showsz);
cfn = pb_newfig(cfn);
for iHit = 1:showsz % visualize errors from the test data
clf;
n = rhits(iHit);
sample = num2str(n);
class = num2str(predictions(n));
label = num2str(Ttest(n));
imagesc(D(2).img(:,:,n));
title(['This is a ' class ],'FontSize',20);
% Clean
set(gca,'YTick',[])
set(gca,'XTick',[])
axis square;
colormap(col);
pause(1)
end
%% Initialize
cfn = pb_clean;
pos = [1850, 968, 470, 470];
cd('/Users/jjheckman/Desktop/');
warning('off','images:imshow:magnificationMustBeFitForDockedFigure');
%% Run screen recording
% Connect iPhone
questdlg('Are you ready?','Connect your iPhone to screen','Yes','Cancel','Yes');
p = [pb_userpath 'subtools/webcam/applications/'];
system(['open -a ' p 'runScreenRecording.app']);
% Select Camera
questdlg('Are you ready?','Is your camera selected to iPhone','Yes','Cancel','Yes');
p = [pb_userpath 'subtools/webcam/applications/'];
system(['open -a ' p 'fixPhoneScreen.app']);
%% Read in data
% NN
nnet = alexnet;
% Display
cfn = pb_newfig(cfn);
while true
img = ScreenCapture(pos);
img = imresize(img,[227,227]);
label = classify(nnet,img);
image(img);
axis square;
title(char(label));
drawnow;
pause(inv(48))
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% Part of Programmeer Beer Toolbox (PBToolbox) %
% Written by: Jesse J. Heckman (2020) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%
%% Experiment: C:\DATA\JJH/
%
%% HEADER
ITI 0 0
Blocks 1
Trials 5
Repeats 1
Random 0 % 0=no, 1=per set, 2=all trials
Motor n
Lab 5
% AX SIG AMP DUR
% edg bit Event Time Event Time
% MOD X Y ID INT On On Off Off Event
% edg bit Event Time Event Time
%% BODY
~~~
% Block: 1
==>
HOR 1 20 30 .1
VER 1 0 30 .1
% Trial: 1
==>
TRG0 1 2 0 0 1
LED -45 0 5 0 500 0 750
% Trial: 2
==>
TRG0 1 2 0 0 1
LED -40 0 5 0 500 0 750
% Trial: 3
==>
TRG0 1 2 0 0 1
LED -35 0 5 0 500 0 750
% Trial: 4
==>
TRG0 1 2 0 0 1
LED -30 0 5 0 500 0 750
% Trial: 5
==>
TRG0 1 2 0 0 1
LED -25 0 5 0 500 0 750
%
%% Experiment: C:\DATA\JJH/
%
%% HEADER
ITI 0 0
Blocks 1
Trials 5
Repeats 1
Random 0 % 0=no, 1=per set, 2=all trials
Motor n
Lab 5
% AX SIG AMP DUR
% edg bit Event Time Event Time
% MOD X Y ID INT On On Off Off Event
% edg bit Event Time Event Time
%% BODY
~~~
% Block: 1
==>
HOR 2 15 30 .1
VER 1 25 30 .1
% Trial: 1
==>
TRG0 1 2 0 0 1
LED -45 0 5 0 500 0 750
LED -45 0 5 0 500 0 750
% Trial: 2
==>
TRG0 1 2 0 0 1
LED -40 0 5 0 500 0 750
LED -45 0 5 0 500 0 750
% Trial: 3
==>
TRG0 1 2 0 0 1
LED -35 0 5 0 500 0 750
LED -45 0 5 0 500 0 750
% Trial: 4
==>
TRG0 1 2 0 0 1
LED -30 0 5 0 500 0 750
LED -45 0 5 0 500 0 750
% Trial: 5
==>
TRG0 1 2 0 0 1
LED -25 0 5 0 500 0 750
LED -45 0 5 0 500 0 750
\ No newline at end of file
%
%% Experiment: C:\DATA\JJH/
%
%% HEADER
ITI 0 0
Blocks 1
Trials 40
Repeats 1
Random 0 % 0=no, 1=per set, 2=all trials
Motor n
Lab 5
% AX SIG AMP DUR
% edg bit Event Time Event Time
% MOD X Y ID INT On On Off Off Event
% edg bit Event Time Event Time
%% BODY
~~~
% Block: 1
==>
HOR 2 15 60 .1
VER 1 25 60 .1
% Trial: 1
==>
TRG0 1 2 0 0 1
LED -45 0 5 0 500 0 750
% Trial: 2
==>
TRG0 1 2 0 0 1
LED -40 0 5 0 500 0 750
% Trial: 3
==>
TRG0 1 2 0 0 1
LED -35 0 5 0 500 0 750
% Trial: 4
==>
TRG0 1 2 0 0 1
LED -30 0 5 0 500 0 750
% Trial: 5
==>
TRG0 1 2 0 0 1
LED -25 0 5 0 500 0 750
% Trial: 6
==>
TRG0 1 2 0 0 1
LED -20 0 5 0 500 0 750
% Trial: 7
==>
TRG0 1 2 0 0 1
LED -15 0 5 0 500 0 750
% Trial: 8
==>
TRG0 1 2 0 0 1
LED -10 0 5 0 500 0 750
% Trial: 9
==>
TRG0 1 2 0 0 1
LED -5 0 5 0 500 0 750
% Trial: 10
==>
TRG0 1 2 0 0 1
LED -0 0 5 0 500 0 750
% Trial: 11
==>
TRG0 1 2 0 0 1
LED 5 0 5 0 500 0 750
% Trial: 12
==>
TRG0 1 2 0 0 1
LED 10 0 5 0 500 0 750
% Trial: 13
==>
TRG0 1 2 0 0 1
LED 15 0 5 0 500 0 750
% Trial: 14
==>
TRG0 1 2 0 0 1
LED 20 0 5 0 500 0 750
% Trial: 15
==>
TRG0 1 2 0 0 1
LED 25 0 5 0 500 0 750
% Trial: 16
==>
TRG0 1 2 0 0 1
LED 30 0 5 0 500 0 750
% Trial: 17
==>
TRG0 1 2 0 0 1
LED 35 0 5 0 500 0 750
% Trial: 18
==>
TRG0 1 2 0 0 1
LED 40 0 5 0 500 0 750
% Trial: 19
==>
TRG0 1 2 0 0 1
LED 45 0 5 0 500 0 750
% Trial: 20
==>
TRG0 1 2 0 0 1
LED 0 0 5 0 200 0 800
% Trial: 21
==>
TRG0 1 2 0 0 1
LED -45 0 5 0 500 0 750
% Trial: 22
==>
TRG0 1 2 0 0 1
LED -40 0 5 0 500 0 750
% Trial: 23
==>
TRG0 1 2 0 0 1
LED -35 0 5 0 500 0 750
% Trial: 24
==>
TRG0 1 2 0 0 1
LED -30 0 5 0 500 0 750
% Trial: 25
==>
TRG0 1 2 0 0 1
LED -25 0 5 0 500 0 750
% Trial: 26
==>
TRG0 1 2 0 0 1
LED -20 0 5 0 500 0 750
% Trial: 27
==>
TRG0 1 2 0 0 1
LED -15 0 5 0 500 0 750
% Trial: 28
==>
TRG0 1 2 0 0 1
LED -10 0 5 0 500 0 750
% Trial: 29
==>
TRG0 1 2 0 0 1
LED -5 0 5 0 500 0 750
% Trial: 30
==>
TRG0 1 2 0 0 1
LED -0 0 5 0 500 0 750
% Trial: 31
==>
TRG0 1 2 0 0 1
LED 5 0 5 0 500 0 750
% Trial: 32
==>
TRG0 1 2 0 0 1
LED 10 0 5 0 500 0 750
% Trial: 33
==>
TRG0 1 2 0 0 1
LED 15 0 5 0 500 0 750
% Trial: 34
==>
TRG0 1 2 0 0 1
LED 20 0 5 0 500 0 750
% Trial: 35
==>
TRG0 1 2 0 0 1
LED 25 0 5 0 500 0 750
% Trial: 36
==>
TRG0 1 2 0 0 1
LED 30 0 5 0 500 0 750
% Trial: 37
==>
TRG0 1 2 0 0 1
LED 35 0 5 0 500 0 750
% Trial: 38
==>
TRG0 1 2 0 0 1
LED 40 0 5 0 500 0 750
% Trial: 39
==>
TRG0 1 2 0 0 1
LED 45 0 5 0 500 0 750
% Trial: 40
==>
TRG0 1 2 0 0 1
LED 0 0 5 0 200 0 800
\ No newline at end of file
%
%% Experiment: C:\DATA\JJH/
%
%% HEADER
ITI 0 0
Blocks 1
Trials 5
Repeats 1
Random 0 % 0=no, 1=per set, 2=all trials
Motor n
Lab 5
% AX SIG AMP DUR
% edg bit Event Time Event Time
% MOD X Y ID INT On On Off Off Event
% edg bit Event Time Event Time
%% BODY
~~~
% Block: 1
==>
HOR 1 0 30 .1
VER 1 10 30 .1
% Trial: 1
==>
TRG0 1 2 0 0 1
LED -45 0 5 0 500 0 750
% Trial: 2
==>
TRG0 1 2 0 0 1
LED -40 0 5 0 500 0 750
% Trial: 3
==>
TRG0 1 2 0 0 1
LED -35 0 5 0 500 0 750
% Trial: 4
==>
TRG0 1 2 0 0 1
LED -30 0 5 0 500 0 750
% Trial: 5
==>
TRG0 1 2 0 0 1
LED -25 0 5 0 500 0 750
%
%% Experiment: C:\DATA\DEFAULT
%
%% HEADER
ITI 0 0
Blocks 6
Trials 0
Repeats 1
Random 0 % 0=no, 1=per set, 2=all trials
Motor n
Lab 5
% AX SIG AMP DUR FREQ
%
% MOD X Y ID INT On On Off Off Event
% edg bit Event Time Event Time
%% BODY
~~~
% Block: 1
==>
HOR 3 0 60 0.1
VER 3 10 60 0.1
~~~
% Block: 2
==>
HOR 5 0 60 0.1
VER 5 20 60 0.1
~~~
% Block: 3
==>
HOR 3 0 60 0.1