Lesson 3/11 and moved eigenfaces folder from 29/9 lesson folder

This commit is contained in:
elvis
2023-11-05 21:28:07 +01:00
parent 17928b1f32
commit 33d89b6bba
183 changed files with 400609 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 MiB

BIN
11-3/cameraman.tif Executable file

Binary file not shown.

View File

@ -0,0 +1,38 @@
function [matched_individual,bestmatchdistance]=eigenfaces_classify(test,training,n);
%classifies using n principal components, closest match
[w, h, nExpressions, nIndividuals]=size(training);
X=reshape(training,[w*h,nIndividuals*nExpressions]);
avg=mean(X,2);
Xd=bsxfun(@minus,X,avg);
[U,S,V]=svd(Xd,0);
Xt=reshape(test,w*h,numel(test)/(w*h));
Xtd=bsxfun(@minus,Xt,avg);
scores=U(:,1:n)'*Xtd;
trainingscores=U(:,1:n)'*Xd;
%normalize scores and samples
%scores=bsxfun(@rdivide,scores,sqrt(sum(abs(scores).^2)));
%trainingscores=bsxfun(@rdivide,trainingscores,sqrt(sum(abs(trainingscores).^2)));
%cosine similarity
%C=scores'*trainingscores;
%[bestmatchdistance bestmatchindex]=max(C,[],2);
%matched_individual=ceil(bestmatchindex/nExpressions);
%Euclidean distance
distanceMatrix=nan(size(scores,2),size(trainingscores,2));
for i=1:size(scores,2)
for j=1:size(trainingscores,2)
distanceMatrix(i,j)=norm(scores(:,i)-trainingscores(:,j));
end
end
[bestmatchdistance bestmatchindex]=min(distanceMatrix,[],2);
matched_individual=ceil(bestmatchindex/nExpressions);
if numel(test)==w*h
subplot(1,2,1);
imagesc(test);
colormap(gray);
subplot(1,2,2);
imagesc(reshape(X(:,bestmatchindex),[w,h]));
colormap(gray);
disp('best match distance=');
disp(bestmatchdistance);
end

View File

@ -0,0 +1,18 @@
function eigenfaces_scatter(images, indices);
[w, h, nExpressions, nIndividuals]=size(images);
X=reshape(images,[w*h,nIndividuals*nExpressions]);
avg=mean(X,2);
Xd=bsxfun(@minus,X,avg);
[U,S,V]=svd(Xd,0);
scores=U(:,indices)'*Xd;
%normalize scores and samples
%scores=bsxfun(@rdivide,scores,sqrt(sum(abs(scores).^2)));
if length(indices) == 3
scatter3(scores(1,:),scores(2,:),scores(3,:),50*ones(size(scores(1,:))),kron(1:nIndividuals,ones(1,nExpressions)));
elseif length(indices) == 2
scatter(scores(1,:),scores(2,:),50*ones(size(scores(1,:))),kron(1:nIndividuals,ones(1,nExpressions)));
else
error('wrong indices size');
end

View File

@ -0,0 +1,29 @@
function interactiverec(F)
% given a 243x320 image F, displays it as sum of components
stdsize=[243,320];
F = F(:);
if not(numel(F) == prod(stdsize))
error('The first argument must be the picture to reconstruct');
end
X=readyalefaces_to_tensor;
X=reshape(X,[prod(stdsize),numel(X)/prod(stdsize)]);
avg=mean(X,2);
Xs=bsxfun(@minus,X,avg);
[U,S,V]=svd(X,0);
colormap(gray);
ncolors=size(gray,1);
h = image(reshape(avg,stdsize)*ncolors);
% Add a slider
uicontrol('Style', 'slider', 'Min', 0, 'Max', size(U,2), ...
'Callback', @callback,'Position',[10 0 300 20]);
function callback(src,evt)
d=round(get(src, 'Value'))
set(h, 'CData', reshape(ncolors*(avg+U(:,1:d)*(U(:,1:d)'*F)),stdsize));
end
end

View File

@ -0,0 +1,34 @@
function [F,descr] = readyalefaces_to_tensor(str)
if not(exist('str','var'))
str='all';
end
switch str
case 'easy'
extensions = {'happy', 'normal', 'sad', 'sleepy', 'surprised', 'wink' };
case 'easy-nowink'
extensions = {'happy', 'normal', 'sad', 'sleepy', 'surprised' };
case 'nowink'
extensions = {'centerlight', 'glasses', 'happy', 'leftlight', 'noglasses', 'normal', 'rightlight', 'sad', 'sleepy', 'surprised' };
case {'hard','all'}
extensions = {'centerlight', 'glasses', 'happy', 'leftlight', 'noglasses', 'normal', 'rightlight', 'sad', 'sleepy', 'surprised', 'wink' };
otherwise
error 'unknown selector';
end
for i = 1 : 15,
basename = 'yalefaces/subject';
if( i < 10 )
basename = [basename, '0', num2str(i)];
else
basename = [basename, num2str(i)];
end;
for j = 1:length(extensions),
fullname = [basename, '.', extensions{j}, '.gif'];
X = imread(fullname);
F(:,:,j,i) = double(X)/255;
end;
end;
descr=extensions;

13
11-3/eigenfaces/showyalefaces.m Executable file
View File

@ -0,0 +1,13 @@
X=readyalefaces_to_tensor;
%X = X(:,:,1:4,[12,9,6,1]); %filters out only some faces
[w h expr ind]=size(X);
clf;
for i=1:expr
for j=1:ind
subplot('position', [(i-1)/expr, (j-1)/ind,1/expr,1/ind]);
% subplot(expr,ind,j+(i-1)*ind);
imagesc(X(:,:,i,j));
colormap(gray);
end
end
set(findobj(gcf, 'type','axes'), 'Visible','off')

7
11-3/eigenfaces/tightsubplot.m Executable file
View File

@ -0,0 +1,7 @@
function tightsubplot(dim, i, data)
row = mod(i-1, dim);
col = floor((i-1) / dim);
subplot('position', [row*(1/dim), (dim-col-1)*(1/dim), 1/dim-.001, 1/dim-0.001 ]);
imagesc(data);
axis off;

400543
11-3/lesson.ipynb generated Normal file

File diff suppressed because one or more lines are too long

BIN
11-3/otherfaces/bart.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
11-3/otherfaces/bart.xcf Executable file

Binary file not shown.

BIN
11-3/otherfaces/bart_unedited.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
11-3/otherfaces/car.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

BIN
11-3/otherfaces/car.xcf Executable file

Binary file not shown.

BIN
11-3/otherfaces/car_unedited.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,39 @@
using Images, GIFImages
function readyalefaces_to_tensor(str::String="all")::Tuple{Array, Array{String}}
# output (F, descr)
nindividuals = 15
if str == "easy"
extensions = ["happy", "normal", "sad", "sleepy", "surprised", "wink"]
elseif str == "easy-nowink"
extensions = ["happy", "normal", "sad", "sleepy", "surprised"]
elseif str == "nowink"
extensions = ["centerlight", "glasses", "happy", "leftlight", "noglasses", "normal", "rightlight", "sad", "sleepy", "surprised"]
elseif str == "hard" || str == "all"
extensions = ["centerlight", "glasses", "happy", "leftlight", "noglasses", "normal", "rightlight", "sad", "sleepy", "surprised", "wink"]
else
throw(ArgumentError("unknown selector"))
end
F = Array{Float32}(undef, (243, 320, length(extensions), nindividuals))
for i 1:nindividuals
basename = "yalefaces/subject"
if i < 10
basename *= "0" * string(i)
else
basename *= string(i)
end
for j = 1:length(extensions)
fullname = basename * "." * extensions[j] * ".gif"
X = Gray.(gif_decode(fullname))
F[:, :, j, i] = X[:,:,1] # just the first frame of the gif (should have only one in total)
end
end
descr = extensions
(F, descr)
end

27
11-3/showyalefaces.jl Normal file
View File

@ -0,0 +1,27 @@
include("readyjalefaces_to_tensor.jl")
using Plots, Plots.PlotMeasures, ColorSchemes
(X, _) = readyalefaces_to_tensor()
# X = X[:,:,1:4,[12,9,6,1]]; # filters out only some faces
(w, h, expr, ind) = size(X)
function showyalefaces()::Plots.Plot
plts = Array{Plots.Plot}(undef, expr, ind)
for i 1:expr
for j 1:ind
plts[i, j] = heatmap(X[:, :, i, j],
leg = false,
ticks = nothing,
yflip=true,
c = cgrad(:dense, rev = true)
)
end
end
l = @layout [
grid(ind, expr)
]
plot(plts..., layout = l, margin = -2mm)
end

29
11-3/yalefaces/README Executable file
View File

@ -0,0 +1,29 @@
The Yale Face Database
----------------------
The database contains 165 GIF images of 15 subjects (subject01,
subject02, etc.). There are 11 images per subject, one for each
of the following facial expressions or configurations: center-light,
w/glasses, happy, left-light, w/no glasses, normal, right-light,
sad, sleepy, surprised, and wink. Note that the image "subject04.sad"
has been corrupted and has been substituted by "subject04.normal".
All the images including this readme file are contained in the file
"yalefaces.tar". It can be unpacked with:
tar xvf yalefaces.tar
A directory called "yalefaces" will be created containing all the images.
These can be viewed using "xv" or ported with any software package that
can understand the GIF format.
You are free to use the Yale Face Database for research purposes.
If experimental results are obtained that use images from within the
database, all publications of these results should acknowledge the use
of the "Yale Face Database." Without permission from Yale, images
from within the database cannot be incorporated into a larger database
which is then publicly distributed.
If you have any trouble or questions please email Prof. David Kriegman
(kriegman@yale.edu) or Prof. Peter Belhumeur (Belhumeur@ledoux.eng.yale.edu).

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
11-3/yalefaces/subject01.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
11-3/yalefaces/subject01.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

BIN
11-3/yalefaces/subject02.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
11-3/yalefaces/subject02.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

BIN
11-3/yalefaces/subject03.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
11-3/yalefaces/subject03.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

BIN
11-3/yalefaces/subject04.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

BIN
11-3/yalefaces/subject04.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

BIN
11-3/yalefaces/subject05.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

BIN
11-3/yalefaces/subject05.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
11-3/yalefaces/subject06.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

BIN
11-3/yalefaces/subject06.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
11-3/yalefaces/subject07.sad.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
11-3/yalefaces/subject07.wink.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Some files were not shown because too many files have changed in this diff Show More