Error in nnet.cnn.L​ayerGraph/​connectLay​ers (line 280) iValidateE​ndLayerInp​utIsNotOcc​upied( ... Error in lstm_cnn_14 (line 133) lgraph = connectLayers(lgraph,

6 次查看(过去 30 天)
% Load your video data and labels
% Assume you have a cell array 'videoData' containing video sequences and
% a corresponding categorical array 'labels' containing class labels.
% Set the input and output paths
inputPath = "E:\PHB\presentation back up\datasets\violence detection";
outputPath = "E:\PHB\presentation back up\new action\action5\frames";
% Set the number of frames per clip
numFramesPerClip = 64; % Adjust as needed
% Set the subfolders as the categories
categories = dir(inputPath);
categories = {categories(3:end).name};
numClasses = length(categories);
% Create output folder if it doesn't exist
if ~exist(outputPath, 'dir')
mkdir(outputPath);
end
% Loop over categories
for i = 1:numel(categories)
category = categories{i};
categoryPath = fullfile(inputPath, category);
% Create subfolder in the output path for the current category
outputCategoryPath = fullfile(outputPath, category);
if ~exist(outputCategoryPath, 'dir')
mkdir(outputCategoryPath);
end
videos = dir(fullfile(categoryPath, '*.mp4'));
% Set the label for the current category
ll = i * ones(numel(videos), 1);
% Loop over the videos in the current category
for j = 1:numel(videos)
videoPath = fullfile(categoryPath, videos(j).name);
[~, videoName, ~] = fileparts(videos(j).name); % Extract video name
disp(['Processing video: ', videoPath]);
v = VideoReader(videoPath);
% Extract frames
frameIdx = 1;
while hasFrame(v) && frameIdx <= numFramesPerClip
frame = readFrame(v);
% Resize frame
resizedFrame = imresize(frame, [224, 224]);
% Save the frame with a unique name
frameFileName = sprintf('%s_frame_%04d.jpg', videoName, frameIdx);
imwrite(resizedFrame, fullfile(outputCategoryPath, frameFileName));
frameIdx = frameIdx + 1;
end
end
end
% Define the input size based on your frame size
inputSize = [224 224 3];
% Define the DCNN architecture
layers = [
imageInputLayer(inputSize, 'Name', 'input', 'Normalization', 'none')
convolution2dLayer(3, 32, 'Padding', 'same', 'Name', 'conv1')
reluLayer('Name', 'relu1')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'pool1')
convolution2dLayer(3, 64, 'Padding', 'same', 'Name', 'conv2')
reluLayer('Name', 'relu2')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'pool2')
fullyConnectedLayer(256, 'Name', 'fc1')
reluLayer('Name', 'relu3')
dropoutLayer(0.5, 'Name', 'drop1')
fullyConnectedLayer(2, 'Name', 'output')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'classoutput')
];
% Define LSTM layer
hiddenUnits = 100; % Adjust based on your requirements
lstmLayerObj = lstmLayer(hiddenUnits, 'OutputMode', 'last', 'Name', 'lstm_layer');
% Define the rest of the layers for combining CNN and LSTM
combinedLayers = [
fullyConnectedLayer(100, 'Name', 'fc_final')
softmaxLayer('Name', 'softmax_final')
classificationLayer('Name', 'classOutput')
];
% Set training options
options = trainingOptions('sgdm', ...
'MaxEpochs', 10, ...
'MiniBatchSize', 32, ...
'Shuffle', 'every-epoch', ...
'Verbose', true, ...
'Plots', 'training-progress');
% Convert video data to sequences of image data
sequences = cell(numClasses, 1);
for i = 1:numClasses
category = categories{i};
categoryPath = fullfile(outputPath, category);
frames = dir(fullfile(categoryPath, '*.jpg'));
numFrames = min(numFramesPerClip, numel(frames));
currentSequence = zeros(inputSize(1), inputSize(2), inputSize(3), numFrames);
for k = 1:numFrames
frame = imread(fullfile(categoryPath, frames(k).name));
currentSequence(:, :, :, k) = im2double(frame);
end
sequences{i} = currentSequence;
end
% Display the layer names
layerNames = {lgraph.Layers.Name}
% Create a layer graph and add layers to it
lgraph = layerGraph();
lgraph = addLayers(lgraph, layers);
lgraph = addLayers(lgraph, lstmLayerObj);
lgraph = addLayers(lgraph, combinedLayers);
% Connect layers
lgraph = connectLayers(lgraph, 'fc_final', 'softmax');
lgraph = connectLayers(lgraph, 'softmax', 'classOutput');
% Train the model
net = trainNetwork(sequences, categorical(ll), lgraph, options);

回答(1 个)

Shubham
Shubham 2024-5-24
Hey Prachi,
It seems you are facing issue while connecting layers in your network architecture when you're trying to connect layers within your layer graph (lgraph). Without the exact error message, I'm assuming the issue might be with how the LSTM layer is connected to the rest of the network or possibly a misunderstanding in the connections of the final layers.
I would suggest you to use network analyzer to understand the network architecture and debug any problems before training: https://www.mathworks.com/help/deeplearning/ref/analyzenetwork.html
I hope this would resolve your issue.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by