Clear Filters
Clear Filters

I have error in convnet , traindata = trainnetwork

14 views (last 30 days)
Charan sai kumar
Charan sai kumar on 19 Sep 2023
Edited: Shantanu Dixit on 9 Sep 2024 at 13:51
%% BRAIN TUMOR CLASSIFICATION USING CNN BY HOG AND LBP FEATURES
clc
clear all
close all
imds = imageDatastore('C:\Users\new',...
'IncludeSubfolders',true,...
'LabelSource','foldernames');
[Data,testData]= splitEachLabel(imds,0.8,'randomize');
% Training files
[trainData] =Data;
layers = [
imageInputLayer([200 128 3],'Name','input')%SIZE OF IMAGE 200* 128
convolution2dLayer(5,16,'Padding','same','Name','conv_1') % ZERO PADDING
batchNormalizationLayer('Name','BN_1') % BATCH NORMLIZATION LAYER
reluLayer('Name','relu_1')
convolution2dLayer(3,32,'Padding','same','Stride',2,'Name','conv_2')
batchNormalizationLayer('Name','BN_2')
reluLayer('Name','relu_2')
convolution2dLayer(3,32,'Padding','same','Name','conv_3')
batchNormalizationLayer('Name','BN_3')
reluLayer('Name','relu_3')
convolution2dLayer(3,32,'Padding','same','Name','conv_4')
batchNormalizationLayer('Name','BN_4')
reluLayer('Name','relu_4')
%
additionLayer(5,'Name','add')
averagePooling2dLayer(4,'Stride',3,'Name','avpool')
fullyConnectedLayer(4,'Name','fc')
softmaxLayer('Name','softmax')
classificationLayer('Name','classOutput')];
% Create a layer graph from the layer array. layerGraph connects all the layers in layers sequentially. Plot the layer graph.
lgraph = layerGraph(layers);
figure
plot(lgraph)
% Create the 1-by-1 convolutional layer and add it to the layer graph. Specify the number of convolutional filters and the stride so that the activation size matches the activation size of the 'relu_3' layer. This arrangement enables the addition layer to add the outputs of the 'skipConv' and 'relu_3' layers. To check that the layer is in the graph, plot the layer graph.
skipConv = convolution2dLayer(2,32,'Stride',2,'Name','skipConv');
lgraph = addLayers(lgraph,skipConv);
figure
plot(lgraph)
% Create the shortcut connection from the 'relu_1' layer to the 'add' layer. Because you specified two as the number of inputs to the addition layer when you created it, the layer has two inputs named 'in1' and 'in2'. The 'relu_3' layer is already connected to the 'in1' input. Connect the 'relu_1' layer to the 'skipConv' layer and the 'skipConv' layer to the 'in2' input of the 'add' layer. The addition layer now sums the outputs of the 'relu_3' and 'skipConv' layers. To check that the layers are connected correctly, plot the layer graph.
lgraph = connectLayers(lgraph,'relu_1','skipConv');
%lgraph = connectLayers(lgraph,'skipConv','add/in2');
lgraph = connectLayers(lgraph,'relu_2','add/in2');
lgraph = connectLayers(lgraph,'relu_3','add/in3');
lgraph = connectLayers(lgraph,'relu_4','add/in4');
lgraph = connectLayers(lgraph,'skipConv','add/in5');
figure
plot(lgraph);
options = trainingOptions('adam', ...
'MiniBatchSize',128, ...
'MaxEpochs',1, ... %% was 6
'ValidationFrequency',5, ...
'InitialLearnRate',1e-4,'Plots','training-progress');
%% network training
[convnet, traininfo] = trainNetwork(trainData,lgraph,options);
% INPUT IMAGE
inp = input('Enter input :')
I = imread(inp);
figure,imshow(I)
[convnet, traininfo] = trainNetwork(trainData,lgraph,options);
i have found error in trainnetwork pls help me out
  3 Comments
Charan sai kumar
Charan sai kumar on 19 Feb 2024
[convnet,traininfo] = trainNetwork(trainData,lgraph,options);
these is the line which i am getting error

Sign in to comment.

Answers (1)

Shantanu Dixit
Shantanu Dixit on 9 Sep 2024 at 13:50
Edited: Shantanu Dixit on 9 Sep 2024 at 13:51
Hi Charan,
The error encountered at trainNetwork might be due to how the data is being processed since the lgraph seems to be fine. Ensure that the data path is passed correctly and the data points are of the form 'hxwxcxnum_samples' where 'h=200', 'w=128' and 'c=3' and the corresponding labels are 'num_samplesX1'
Below sample code runs the above defined network using dummy data:
numImages = 100; %% sample
numClasses = 4; %% as defined in last fc connection
sampleImages = rand(200, 128, 3, numImages); %% input size
sampleLabels = categorical(randi([1 numClasses], [numImages, 1]));
%% training options as per use-case
options = trainingOptions('sgdm', ...
'MaxEpochs', 100,
'MiniBatchSize', 10,
'InitialLearnRate', 0.01,
'Verbose', false, ...
'Plots', 'training-progress');
[convnet, traininfo] = trainNetwork(sampleImages, sampleLabels, lgraph, options);
disp(traininfo);
Refer to the below MathWorks documentation for preprocessing image data for neural networks:

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!