i have problem in testing my network , i design cnn network to detect forged frames from videos i used 16 video for training and 4 for testing error: Out of memory on device
1 view (last 30 days)
Show older comments
clc
clear
forged_video1 = VideoReader(['01_forged_enc10.avi']);
original_video1 = VideoReader(['01_original_enc10.avi']);
forged_video2 = VideoReader(['02_forged_enc10.avi']);
original_video2 = VideoReader(['02_original_enc10.avi']);
forged_video3 = VideoReader(['03_forged_enc10.avi']);
original_video3 = VideoReader(['03_original_enc10.avi']);
forged_video4 = VideoReader(['04_forged_enc10.avi']);
original_video4 = VideoReader(['04_original_enc10.avi']);
forged_video5 = VideoReader(['05_forged_enc10.avi']);
original_video5 = VideoReader(['05_original_enc10.avi']);
forged_video6 = VideoReader(['06_forged_enc10.avi']);
original_video6 = VideoReader(['06_original_enc10.avi']);
forged_video7 = VideoReader(['07_forged_enc10.avi']);
original_video7 = VideoReader(['07_original_enc10.avi']);
forged_video8 = VideoReader(['08_forged_enc10.avi']);
original_video8 = VideoReader(['08_original_enc10.avi']);
forged_video9 = VideoReader(['09_forged_enc10.avi']);
original_video9 = VideoReader(['09_original_enc10.avi']);
forged_video10 = VideoReader(['10_forged_enc10.avi']);
original_video10 = VideoReader(['10_original_enc10.avi']);
% Get the frames from the videos
forged_frames1 = read(forged_video1);
original_frames1 = read(original_video1);
forged_frames2 = read(forged_video2);
original_frames2 = read(original_video2);
forged_frames3 = read(forged_video3);
original_frames3 = read(original_video3);
forged_frames4 = read(forged_video4);
original_frames4 = read(original_video4);
forged_frames5 = read(forged_video5);
original_frames5 = read(original_video5);
forged_frames6 = read(forged_video6);
original_frames6 = read(original_video6);
forged_frames7 = read(forged_video7);
original_frames7 = read(original_video7);
forged_frames8 = read(forged_video8);
original_frames8 = read(original_video8);
forged_frames9 = read(forged_video9);
original_frames9 = read(original_video9);
forged_frames10 = read(forged_video10);
original_frames10 = read(original_video10);
% Convert the frames to grayscale
for ii = 1:210
forged_frames_gray1(:,:,ii) = rgb2gray(forged_frames1(:,:,:,ii));
original_frames_gray1(:,:,ii) = rgb2gray(original_frames1(:,:,:,ii));
end
for ii = 1:300
forged_frames_gray2(:,:,ii) = rgb2gray(forged_frames2(:,:,:,ii));
original_frames_gray2(:,:,ii) = rgb2gray(original_frames2(:,:,:,ii));
end
for ii = 1:300
forged_frames_gray3(:,:,ii) = rgb2gray(forged_frames3(:,:,:,ii));
original_frames_gray3(:,:,ii) = rgb2gray(original_frames3(:,:,:,ii));
end
for ii = 1:300
forged_frames_gray4(:,:,ii) = rgb2gray(forged_frames4(:,:,:,ii));
original_frames_gray4(:,:,ii) = rgb2gray(original_frames4(:,:,:,ii));
end
for ii = 1:570
forged_frames_gray5(:,:,ii) = rgb2gray(forged_frames5(:,:,:,ii));
original_frames_gray5(:,:,ii) = rgb2gray(original_frames5(:,:,:,ii));
end
for ii = 1:240
forged_frames_gray6(:,:,ii) = rgb2gray(forged_frames6(:,:,:,ii));
original_frames_gray6(:,:,ii) = rgb2gray(original_frames6(:,:,:,ii));
end
for ii = 1:390
forged_frames_gray7(:,:,ii) = rgb2gray(forged_frames7(:,:,:,ii));
original_frames_gray7(:,:,ii) = rgb2gray(original_frames7(:,:,:,ii));
end
for ii = 1:270
forged_frames_gray8(:,:,ii) = rgb2gray(forged_frames8(:,:,:,ii));
original_frames_gray8(:,:,ii) = rgb2gray(original_frames8(:,:,:,ii));
end
for ii = 1:540
forged_frames_gray9(:,:,ii) = rgb2gray(forged_frames9(:,:,:,ii));
original_frames_gray9(:,:,ii) = rgb2gray(original_frames9(:,:,:,ii));
end
for ii = 1:210
forged_frames_gray10(:,:,ii) = rgb2gray(forged_frames10(:,:,:,ii));
original_frames_gray10(:,:,ii) = rgb2gray(original_frames10(:,:,:,ii));
end
% Reshape the grayscale frames into a 4D array
forged_frames_gray1 = reshape(forged_frames_gray1, [240, 320, 1, 210]);
original_frames_gray1 = reshape(original_frames_gray1, [240, 320, 1, 210]);
forged_frames_gray2 = reshape(forged_frames_gray2, [240, 320, 1, 300]);
original_frames_gray2 = reshape(original_frames_gray2, [240, 320, 1, 300]);
forged_frames_gray3 = reshape(forged_frames_gray3, [240, 320, 1, 300]);
original_frames_gray3 = reshape(original_frames_gray3, [240, 320, 1, 300]);
forged_frames_gray4 = reshape(forged_frames_gray4, [240, 320, 1, 300]);
original_frames_gray4 = reshape(original_frames_gray4, [240, 320, 1, 300]);
forged_frames_gray5 = reshape(forged_frames_gray5, [240, 320, 1, 570]);
original_frames_gray5 = reshape(original_frames_gray5, [240, 320, 1, 570]);
forged_frames_gray6 = reshape(forged_frames_gray6, [240, 320, 1, 240]);
original_frames_gray6 = reshape(original_frames_gray6, [240, 320, 1, 240]);
forged_frames_gray7 = reshape(forged_frames_gray7, [240, 320, 1, 390]);
original_frames_gray7 = reshape(original_frames_gray7, [240, 320, 1, 390]);
forged_frames_gray8 = reshape(forged_frames_gray8, [240, 320, 1, 270]);
original_frames_gray8 = reshape(original_frames_gray8, [240, 320, 1, 270]);
forged_frames_gray9 = reshape(forged_frames_gray9, [240, 320, 1, 540]);
original_frames_gray9 = reshape(original_frames_gray9, [240, 320, 1, 540]);
forged_frames_gray10 = reshape(forged_frames_gray10, [240, 320, 1, 210]);
original_frames_gray10 = reshape(original_frames_gray10, [240, 320, 1, 210]);
% Create the labels
forged_labels = ones(3330,1);
original_labels = zeros(3330,1);
% Concatenate the grayscale frames and labels
X = cat(4,forged_frames_gray1,forged_frames_gray2,forged_frames_gray3,...
forged_frames_gray4,forged_frames_gray5,forged_frames_gray6,forged_frames_gray7,...
forged_frames_gray8,forged_frames_gray9,forged_frames_gray10,...
original_frames_gray1,original_frames_gray2,original_frames_gray3,...
original_frames_gray4,original_frames_gray5,original_frames_gray6,...
original_frames_gray7,original_frames_gray8,original_frames_gray9,original_frames_gray10);
y = cat(1,forged_labels,original_labels);
% Divide the data into training and test sets
rng(0); % For reproducibility
idx = randperm(6660);
X_train = X(:,:,:,idx(1:5170));
y_train = y(idx(1:5170));
X_val = X(:,:,:,idx(5171:end));
y_test = y(idx(5171:end));
y_train = categorical(y_train);
y_val=categorical(y_test);
% Define the CNN architecture
layers = [ imageInputLayer([240 320 1])
convolution2dLayer(3,32,'Padding', 'same')
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,64,'Padding', 'same')
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,128,'Padding', 'same') % Adding a new convolutional layer with 128 filters
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,256,'Padding', 'same') % Adding another convolutional layer with 256 filters
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(512) % Increasing the number of neurons in the fully connected layer to 512
reluLayer
dropoutLayer(0.5)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'ExecutionEnvironment','gpu',...
'MaxEpochs',3, ...
'MiniBatchSize',16, ...
'InitialLearnRate',0.0001, ...
'Shuffle','every-epoch', ...
'ValidationData',{X_val, y_val}, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(X_train,y_train,layers,options);
predLabelsTest = classify(net,X_val);
accuracy = sum(predLabelsTest == y_val) / numel(y_val)
0 Comments
Answers (1)
Joss Knight
on 10 Mar 2023
Try reducing the MiniBatchSize.
6 Comments
Joss Knight
on 13 Mar 2023
Edited: Joss Knight
on 13 Mar 2023
"my gpu is Nvidia Quadro M2000M 2g is that not enough for training and testing?"
Unlikely. We recommend at least 8GB and ideally more than 12GB of memory for deep learning training, certainly for image processing and computer vision.
"I am using an old version 2018a i have not the app deepNetworkDesigner "
Just type analyzeNetwork(layers).
Joss Knight
on 13 Mar 2023
Well, the input to fc_1 is 15x20x256xbatchSize, which wouldn't normally be too bad, but you have very little memory. You could try adding more convolutional and pooling layers or just reducing image size by more than a factor of 2 at each stage (perhaps by having a stride of 2 in the conv layers as well as the pooling layers?), to get the size down and ensure that the spatial dimensions at the input to fc_1 are 1x1.
It doesn't help that you have a mobile chip that probably has a good half of its RAM reserved for video.
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!