MatConvNet MNIST network for two classes - matlab

Is it possible to make MNIST example in matconvnet to work for two classes instead of 10? I changed the cnn_mnist_init.m file to be the following to generate feature vectors for two classes:
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,500,2, 'single'), zeros(1,2,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
But when I run cnn_train I have the following error:
Error in cnn_train>error_multiclass (line 222)
err(2,1) = sum(sum(sum(min(error(:,:,1:5,:),[],3)))) ;
Error in cnn_train>process_epoch (line 302)
error = sum([error, [...
Error in cnn_train (line 153)
[net, stats.train] = process_epoch(opts, getBatch, epoch, train, learningRate, imdb, net) ;
Error in original_image (line 40)
[net, info] = cnn_train(fold, net, imdb, #getBatch, ...
Error in main_original (line 13)
[imdb, net, info] = original_image(fold);
What I did wrong?

The error you are getting in err(2,1) is probably caused because your error vector has a wrong dimension. err(2,1) is the error of false classificated 5 classes you have only two classes. Check the size of the tensor you feed into softmax it should have the dimension [1,1,2=number of classes, batch size]

Related

Trying to run this code but I keep getting this error

I want to train an Artificial Neural Network on matlab r2020a for an automatic voltage regulator, I already have a simulink file for my output, but whenever I want to run this code:
I = out.input';
T = out.output';
net=newff(minmax(I),[3,5,1],{'logsig','tansig','purelin'},'trainlm');
net = init(net); % Used to initialize the network (weight and biases)
net.trainParam.show =1; % The result of error (mse) is shown at each iteration (epoch)
net.trainParam.epochs = 10000; % Maximum limit of the network training iteration process (epoch)
net.trainParam.goal =1e-12; % Stopping criterion based on error (mse) goal
net=train(net,I,T); % Start training the network
I keep facing this error:
Unrecognized function or variable 'procInfo'.
Error in nnMex.netHints (line 143)
processingInfoArray = [processingInfoArray procInfo];
Error in nncalc.setup1>setupImpl (line 201)
calcHints = calcMode.netHints(net,calcHints);
Error in nncalc.setup1 (line 16)
[calcMode,calcNet,calcData,calcHints,net,resourceText] = setupImpl(calcMode,net,data);
Error in nncalc.setup (line 7)
[calcMode,calcNet,calcData,calcHints,net,resourceText] = nncalc.setup1(calcMode,net,data);
Error in network/train (line 361)
[calcLib,calcNet,net,resourceText] = nncalc.setup(calcMode,net,data);
Error in ann (line 8)
net=train(net,I,T); % Start training the network
I'm not sure where to start to my search for this problem, but I think it might be a compiler issue
I tried searching online, but all I got was just suggestion to download compilers and toolboxes.

`fasterRCNNLayers` for simple custom network give error message

When using fasterRCNNLayers for creating faster rcnn network from custom network, it gives error message for upgrading the network to faster rcnn network. matlab 2022a update 3 is used.
I have traced the error and found the problem is caused by deleting classification layers and adding faster-rcnn layers. What's the restrictions for upgrading network and I can't found related clue from the documents.
My cnn network code is as follows: cup_use_net_h.m
numTrainingFiles = 30;
[imdsTrain,imdsTest] = splitEachLabel(imds,numTrainingFiles,'randomize');
layers = [ ...
imageInputLayer([180 60 1])
convolution2dLayer(5,40)
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(5,10)
reluLayer
maxPooling2dLayer(5,'Stride',5)
fullyConnectedLayer(2,'Name','fc') %% fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',100,...
'InitialLearnRate',1e-2, ...
'Verbose',true, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
save("cup_single_mark_net.mat","net");
My fasterRCNNLayers calling program is as follows: cup_faster_rcnn_c.m
% load("cup_single_mark_net.mat");
% trainingDataTable = objectDetectorTrainingData(gTruth);
load("cup_single_mark_net.mat");
singlenet = net;
inputImageSize = [450,400,1];
featureLayer = 'relu_2';
anchorBoxes = [180,60; 150,50];
numClasses = 2;
lgraph = fasterRCNNLayers(inputImageSize,numClasses,anchorBoxes, ...
singlenet,featureLayer)
analyzeNetwork(lgraph);
error message is as follows:
>> cup_faster_rcnn_c
Error using nnet.cnn.LayerGraph>iValidateLayerName
Layer 'fc' does not exist.
Error in nnet.cnn.LayerGraph>iGetDestinationInformation (line 625)
iValidateLayerName( endLayerName, layerNames );
Error in nnet.cnn.LayerGraph/disconnectLayers (line 372)
iGetDestinationInformation(d, this.PrivateDirectedGraph.Nodes.Layers);
Error in vision.internal.cnn.RCNNLayers/insertOrReplaceROIPooling (line 717)
lgraph = lgraph.disconnectLayers(featureExtractionLayer, outLayers{ii});
Error in vision.internal.cnn.RCNNLayers/fastRCNNForNonSequentialNetworks (line 646)
[lgraph, numFiltersLastConvLayer] = insertOrReplaceROIPooling(this, lgraph, featureExtractionLayer);
Error in vision.internal.cnn.RCNNLayers.createFasterRCNN (line 165)
lgraph = fastRCNNForNonSequentialNetworks(obj, numClasses, lgraph, 'faster-rcnn', anchorBoxes, featureExtractionLayer);
Error in fasterRCNNLayers (line 174)
lgraph = vision.internal.cnn.RCNNLayers.createFasterRCNN(...
Error in cup_faster_rcnn_c (line 12)
lgraph = fasterRCNNLayers(inputImageSize,numClasses,anchorBoxes, ...
The feature_layers parameter should be conv_2 not relu_2.

Matlab feature selection

I am trying to learn relevant features in a 300*299 training matrix by taking a random row from it as my test data and applying sequentialfs on it. I have used the following code:
>> Md1=fitcdiscr(xtrain,ytrain);
>> func = #(xtrain, ytrain, xtest, ytest) sum(ytest ~= predict(Md1,xtest));
>> learnt = sequentialfs(func,xtrain,ytrain)
xtrain and ytrain are 299*299 and 299*1 respectively. Predict will give me the predicted label for xtest(which is some random row from original xtrain).
However, when I run my code, I get the following error:
Error using crossval>evalFun (line 480)
The function '#(xtrain,ytrain,xtest,ytest)sum(ytest~=predict(Md1,xtest))' generated the following error:
X must have 299 columns.
Error in crossval>getFuncVal (line 497)
funResult = evalFun(funorStr,arg(:));
Error in crossval (line 343)
funResult = getFuncVal(1, nData, cvp, data, funorStr, []);
Error in sequentialfs>callfun (line 485)
funResult = crossval(fun,x,other_data{:},...
Error in sequentialfs (line 353)
crit(k) = callfun(fun,x,other_data,cv,mcreps,ParOptions);
Error in new (line 13)
learnt = sequentialfs(func,xtrain,ytrain)
Where did I go wrong?
You should build your classifier inside func, not before.
sequentialfs calls the function each time on different sets, and a classifier must be built specifically for each set, using only the features sequentialfs selected for that iteration.
I'm not sure I managed to be clear, in practice you should move the first line of your code inside the body of func
Source: MathWorks

Errors out of memory running Matlab Autoencoders on 10^5 sparse matrix

I have a 10^5 square sparse matrix called pbAttack. Each element represents if there is connection between node i and node j. If there is connection, pbAttack(i,j) = 1. Otherwise, pbAttack (i,j) = 0. Then I want to use it following this tutorial: Matlab Autoencoders. I use the same code as in the linked Matlab tutorial. I only change it to use my data.
However, I got following errors:
Error using bsxfun
Out of memory. Type HELP MEMORY for your options.
Error in mapminmax.apply (line 8)
y = bsxfun(#plus,y,settings.ymin);
Error in mapminmax.create (line 44)
y = mapminmax.apply(x,settings);
Error in mapminmax (line 51)
[y,settings] = mapminmax.create(x,param);
Error in nnet.internal.configure.input (line 31)
[x,config] = feval(processFcns{j},x,processParams{j});
Error in network/configure (line 234)
net = nnet.internal.configure.input(net,i,X{i});
Error in nntraining.config (line 116)
net = configure(network(net),X,T);
Error in nntraining.setup>setupPerWorker (line 68)
[net,X,Xi,Ai,T,EW,Q,TS,err] = nntraining.config(net,X,Xi,Ai,T,EW,configNetEnable);
Error in nntraining.setup (line 43)
[net,data,tr,err] = setupPerWorker(net,trainFcn,X,Xi,Ai,T,EW,enableConfigure);
Error in network/train (line 335)
[net,data,tr,err] = nntraining.setup(net,net.trainFcn,X,Xi,Ai,T,EW,enableConfigure,isComposite);
Error in Autoencoder.train (line 490)
net = train(net,X,X,'useGPU',iYesOrNo(useGPU));
Error in trainAutoencoder (line 105)
autoenc = Autoencoder.train(X, autonet, paramsStruct.UseGPU);
Error in workflow_autoencoder (line 8)
autoenc1 = trainAutoencoder(pbAttack,hiddenSize,...
Are all these errors caused by the huge size of the matrix? Is there a work around so that it does not need so much memory? Thank you so much.
It just means that you don't have enough memory for this (since your matrix is not so sparse). You can try to repeat your code by reducing the size of the matrix.

MATLAB neural network for image recognition does not work

I am trying to get familiar with the Image category classification using deep learning. I am trying to run the Matlab example available on
http://uk.mathworks.com/help/vision/examples/image-category-classification-using-deep-learning.html
However running this example is giving me the following error
Error using message/getString
Unable to load a message catalog 'nnet_cnn:layer:Layer'. Please check
the file location and format.
Error in nnet.cnn.layer.Layer/getVectorHeader (line 113)
header = getString( message( ...
Error in nnet.cnn.layer.Layer/displayNonScalarObject (line 86)
header = sprintf( ' %s\n', getVectorHeader( layers )
);
Error in cnn (line 150)
convnet.Layers
Moreover if I pass and ignore this error, I will get the following error later for the line
trainingFeatures = activations(convnet, trainingSet, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns')
Undefined variable "nnet" or class "nnet.internal.cnngpu.convolveForward2D".
Error in nnet.internal.cnn.layer.Convolution2D/doForward (line 218)
Z = nnet.internal.cnngpu.convolveForward2D( ...
Error in nnet.internal.cnn.layer.Convolution2D/forwardNormal (line 195)
Z = this.doForward(X,this.Weights.Value,this.Bias.Value);
Error in nnet.internal.cnn.layer.Convolution2D/forward (line 98)
[Z, memory] = this.forwardNormal( X );
Error in nnet.internal.cnn.SeriesNetwork/activations (line 50)
output = this.Layers{currentLayer}.forward( output );
Error in SeriesNetwork/activations (line 269)
YChannelFormat = predictNetwork.activations(X, layerID);
Error in cnn (line 262)
trainingFeatures = activations(convnet, trainingSet, featureLayer, ...
Can please someone tell me the possibilities for getting this error and how to solve it.
Regards