Hello!
I am working through Activity Recognition from Video and Optical Flow Data Using Deep Learning.
The code is all follows, sith support function at the end.
% Download the HMDB51 Data file Using a supporting function
downloadFolder = fullfile(tempdir, "hmdb51");
downloadHMDB51(downloadFolder);
% Check if the file is in the correct place
allClasses = checkForHMDB51Folder(downloadFolder);
% The data set contains approximately 2GB of video data. In order to reduce
% training, we can set useAllData to false or if you want to use all of the
% Data then set useAllData to true
useAllData = false;
if useAllData == true
classes = allClasses;
else
classes = ["kiss", "laugh", "pick", "pour", "pushup"];
end
dataFolder = fullfile(downloadFolder, "hmdb511_org");
% Now, the data has to be spilt into two different folders, one for
% training and one for validation. 80% of the data will
[labels,files] = folders2labels(fullfile(dataFolder,classes),...
"IncludeSubfolders",true,...
"FileExtensions",'.avi');
indices = splitlabels(labels,0.8,'randomized');
% train_array = indices{1};
% test_array = indices{2};
%
% trainFilenames = files(train_array);
% testFilenames = files(test_array);
trainFilenames = files(indices{1});
testFilenames = files(indices{2});
% Normailze the data
inputStatsFilename = 'inputStatistics.mat';
if ~exist(inputStatsFilename, 'file')
disp("Reading all the training data for input statistics...")
inputStats = inputStatistics(dataFolder);
else
d = load(inputStatsFilename);
inputStats = d.inputStats;
end
% Support functions
function inputStats = inputStatistics(dataFolder)
ds = createDatastore(dataFolder);
ds.ReadFcn = @getMinMax;
tic;
tt = tall(ds);
varnames = {'rgbMax','rgbMin','oflowMax','oflowMin'};
stats1 = groupsummary(tt,[], {'max','min'}, varnames); %ERROR OCCURS HERE (2)
stats = gather(stats1);
inputStats.Filename = gather(tt.Filename);
inputStats.NumFrames = gather(tt.NumFrames);
inputStats.rgbMax = stats.max_rgbMax;
inputStats.rgbMin = stats.min_rgbMin;
inputStats.oflowMax = stats.max_oflowMax;
inputStats.oflowMin = stats.min_oflowMin;
save('inputStatistics.mat','inputStats');
toc;
end
function data = getMinMax(filename)
reader = VideoReader(filename);
opticFlow = opticalFlowFarneback;
data = [];
while hasFrame(reader)
frame = readFrame(reader);
[rgb,oflow] = findMinMax(frame,opticFlow);
data = assignMinMax(data, rgb, oflow);
end
totalFrames = floor(reader.Duration * reader.FrameRate);
totalFrames = min(totalFrames, reader.NumFrames);
[labelName, filename] = getLabelFilename(filename);
data.Filename = fullfile(labelName, filename);
data.NumFrames = totalFrames;
data = struct2table(data,'AsArray',true);
end
function [labelName, filename] = getLabelFilename(filename)
fileNameSplit = split(filename,'/');
labelName = fileNameSplit{end-1};
filename = fileNameSplit{end};
end
function data = assignMinMax(data, rgb, oflow)
if isempty(data)
data.rgbMax = rgb.Max;
data.rgbMin = rgb.Min;
data.oflowMax = oflow.Max;
data.oflowMin = oflow.Min;
return;
end
data.rgbMax = max(data.rgbMax, rgb.Max);
data.rgbMin = min(data.rgbMin, rgb.Min);
data.oflowMax = max(data.oflowMax, oflow.Max);
data.oflowMin = min(data.oflowMin, oflow.Min);
end
function [rgbMinMax,oflowMinMax] = findMinMax(rgb, opticFlow)
rgbMinMax.Max = max(rgb,[],[1,2]);
rgbMinMax.Min = min(rgb,[],[1,2]);
gray = rgb2gray(rgb);
flow = estimateFlow(opticFlow,gray);
oflow = cat(3,flow.Vx,flow.Vy,flow.Magnitude);
oflowMinMax.Max = max(oflow,[],[1,2]);
oflowMinMax.Min = min(oflow,[],[1,2]);
end
function ds = createDatastore(folder)
ds = fileDatastore(folder,...
'IncludeSubfolders', true,...
'FileExtensions', '.avi',...
'UniformRead',false,...
'ReadFcn', @getMinMax, ... % ERROR OCCURS HERE, if the line above 'Uniformread' is chnmaged to true. (1)
'PreviewFcn',@getMinMax);
disp("NumFiles: " + numel(ds.Files));
end
There are more support functions, but i am not having any issues with them as they relate to downlaondg the files and making sure they are in place.
Error (1) (SHown in code up above)
Error (2) (Shown in code above)
The error that appears here is as follows: Error using tall/groupsummary
Grouping variable cannot be specified as a function handle.
I am not sure how to fix it. If anyone can please help i would greatly appreciate it.