I'm running faster R-CNN in matlab 2018b on a Windows 10. I face an exception CUDA_ERROR_ILLEGAL_ADDRESS when I increase the number of my training items or when I increase the MaxEpoch.
Below are the information of my gpuDevice
CUDADevice with properties:
Name: 'GeForce GTX 1050'
Index: 1
ComputeCapability: '6.1'
SupportsDouble: 1
DriverVersion: 9.2000
ToolkitVersion: 9.1000
MaxThreadsPerBlock: 1024
MaxShmemPerBlock: 49152
MaxThreadBlockSize: [1024 1024 64]
MaxGridSize: [2.1475e+09 65535 65535]
SIMDWidth: 32
TotalMemory: 4.2950e+09
AvailableMemory: 3.4635e+09
MultiprocessorCount: 5
ClockRateKHz: 1493000
ComputeMode: 'Default'
GPUOverlapsTransfers: 1
KernelExecutionTimeout: 1
CanMapHostMemory: 1
DeviceSupported: 1
DeviceSelected: 1
And this is my code
latest_index =0;
for i=1:6
load (strcat('newDataset', int2str(i), '.mat'));
len =length(vehicleDataset.imageFilename);
for j=1:len
filename = vehicleDataset.imageFilename{j};
latest_index=latest_index+1;
fulldata.imageFilename{latest_index} = filename;
fulldata.vehicle{latest_index} = vehicleDataset.vehicle{j};
end
end
trainingDataTable = table(fulldata.imageFilename', fulldata.vehicle');
trainingDataTable.Properties.VariableNames = {'imageFilename','vehicle'};
data.trainingDataTable = trainingDataTable;
trainingDataTable(1:4,:)
% Split data into a training and test set.
idx = floor(0.6 * height(trainingDataTable));
trainingData = trainingDataTable(1:idx,:);
testData = trainingDataTable(idx:end,:);
% Create image input layer.
inputLayer = imageInputLayer([32 32 3]);
% Define the convolutional layer parameters.
filterSize = [3 3];
numFilters = 64;
% Create the middle layers.
middleLayers = [
convolution2dLayer(filterSize, numFilters, 'Padding', 1)
reluLayer()
convolution2dLayer(filterSize, numFilters, 'Padding', 1)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
];
finalLayers = [
fullyConnectedLayer(128)
% Add a ReLU non-linearity.
reluLayer()
fullyConnectedLayer(width(trainingDataTable))
% Add the softmax loss layer and classification layer.
softmaxLayer()
classificationLayer()
];
layers = [
inputLayer
middleLayers
finalLayers
];
% Options for step 1.
optionsStage1 = trainingOptions('sgdm', ...
'MaxEpochs', 2, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
% Options for step 2.
optionsStage2 = trainingOptions('sgdm', ...
'MaxEpochs', 2, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
% Options for step 3.
optionsStage3 = trainingOptions('sgdm', ...
'MaxEpochs', 2, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
% Options for step 4.
optionsStage4 = trainingOptions('sgdm', ...
'MaxEpochs', 2, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
options = [
optionsStage1
optionsStage2
optionsStage3
optionsStage4
];
doTrainingAndEval = true;
if doTrainingAndEval
% Set random seed to ensure example training reproducibility.
rng(0);
% Train Faster R-CNN detector. Select a BoxPyramidScale of 1.2 to allow
% for finer resolution for multiscale object detection.
detector = trainFasterRCNNObjectDetector(trainingData, layers, options, ...
'NegativeOverlapRange', [0 0.3], ...
'PositiveOverlapRange', [0.6 1], ...
'BoxPyramidScale', 1.2);
data.detector= detector;
else
% Load pretrained detector for the example.
detector = data.detector;
end
save mix_data data
if doTrainingAndEval
% Run detector on each image in the test set and collect results.
resultsStruct = struct([]);
for i = 1:height(testData)
% Read the image.
I = imread(testData.imageFilename{i});
% Run the detector.
[bboxes, scores, labels] = detect(detector, I);
% Collect the results.
resultsStruct(i).Boxes = bboxes;
resultsStruct(i).Scores = scores;
resultsStruct(i).Labels = labels;
end
% Convert the results into a table.
results = struct2table(resultsStruct);
data.results = results;
save mix_data data
else
% Load results from disk.
results = data.results;
end
% Extract expected bounding box locations from test data.
expectedResults = testData(:, 2:end);
% Evaluate the object detector using Average Precision metric.
[ap, recall, precision] = evaluateDetectionPrecision(results, expectedResults);
% Plot precision/recall curve
figure
plot(recall,precision)
xlabel('Recall')
ylabel('Precision')
grid on
title(sprintf('Average Precision = %.2f', ap))
First it prints the warning multiple time and throws the below exception
Warning: An unexpected error occurred during CUDA execution. The CUDA error was:
CUDA_ERROR_ILLEGAL_ADDRESS
In trainFasterRCNNObjectDetector (line 320)
In rcnn_trail (line 184)
Error using -
An unexpected error occurred during CUDA execution. The CUDA error was:
CUDA_ERROR_ILLEGAL_ADDRESS
Error in vision.internal.cnn.layer.SmoothL1Loss/backwardLoss (line 156)
idx = (X > -one) & (X < one);
Error in nnet.internal.cnn.DAGNetwork/computeGradientsForTraining/efficientBackProp (line 585)
dLossdX = thisLayer.backwardLoss( ...
Error in nnet.internal.cnn.DAGNetwork>#()efficientBackProp(i) (line 661)
#() efficientBackProp(i), ...
Error in nnet.internal.cnn.util.executeWithStagedGPUOOMRecovery (line 11)
[ varargout{1:nOutputs} ] = computeFun();
Error in nnet.internal.cnn.DAGNetwork>iExecuteWithStagedGPUOOMRecovery (line 1195)
[varargout{1:nargout}] = nnet.internal.cnn.util.executeWithStagedGPUOOMRecovery(varargin{:});
Error in nnet.internal.cnn.DAGNetwork/computeGradientsForTraining (line 660)
theseGradients = iExecuteWithStagedGPUOOMRecovery( ...
Error in nnet.internal.cnn.Trainer/computeGradients (line 184)
[gradients, predictions, states] = net.computeGradientsForTraining(X, Y,
needsStatefulTraining, propagateState);
Error in nnet.internal.cnn.Trainer/train (line 85)
[gradients, predictions, states] = this.computeGradients(net, X, response,
needsStatefulTraining, propagateState);
Error in vision.internal.cnn.trainNetwork (line 47)
trainedNet = trainer.train(trainedNet, trainingDispatcher);
Error in fastRCNNObjectDetector.train (line 190)
[network, info] = vision.internal.cnn.trainNetwork(ds, lgraph, opts, mapping,
checkpointSaver);
Error in trainFasterRCNNObjectDetector (line 410)
[stage2Detector, fastRCNN, ~, info(2)] = fastRCNNObjectDetector.train(trainingData, fastRCNN,
options(2), iStageTwoParams(params), checkpointSaver);
Error in rcnn_trail (line 184)
detector = trainFasterRCNNObjectDetector(trainingData, layers, options, ...
After talking to Matlab support, apparently my GPU is not the "right" GPU for deep learning and Neural Network.
However, I found that the issue was that Windows changed the GPU during the run, to fix this I went to INVIDIA Control Panel > Programs settings >
1. Select Mathworks Matlab
2. Preferred graphic processor choose your GPU card
Related
I want to optimize my CNN as this example using Bayesian optimization and Matlab 2019a. Instead of showing whole my codes in a single part, I split it to three parts to show which lines causes error. First part is about loading data, dividing for training and validation and defining optimization variables. Second part is where error is. Third part is remaining part of codes. Note that buildLayer is also written by me to make network layers.
My questions are: how can I fix aforementioned error and why it occurred?
First part is (loading data):
folder = 'C:\Users\X';
imds = imageDatastore(folder, ...
"IncludeSubfolders",true, ...
"LabelSource","foldernames");
num = 20;
per = randperm(1000, num);
figure;
for i=1 : num
subplot(4, 5, i);
imshow(imds.Files{per(i)});
end
img = readimage(imds,1);
[trainData, validData] = splitEachLabel(imds,0.8);
% defining optimizable variables
optVars = [
optimizableVariable('depth', [1 5], "Type","integer")
optimizableVariable('initLearningRate', [0.001, 1], "Transform","log")
optimizableVariable('initFilterNum', [8 32], "Type","integer")
optimizableVariable('momentum', [0.7 0.98])
optimizableVariable('l2Reg', [1e-10 1e-2],'Transform','log')]
objFcn = makeObjFcn(trainData,validData);
I get this error:
Array formation and parentheses-style indexing with objects of class
'matlab.io.datastore.ImageDatastore' is not allowed. Use objects of
class 'matlab.io.datastore.ImageDatastore' only as scalars or use a
cell array.
Error in unique>uniqueR2012a (line 191)
a = a(:);
Error in unique (line 103)
[varargout{1:nlhs}] = uniqueR2012a(varargin{:});
Error in main>makeObjFcn/valErrorFun (line 47)
numClasses = numel(unique(vaData));
Error in BayesianOptimization/callObjNormally (line 2560)
[Objective, ConstraintViolations, UserData] = this.ObjectiveFcn(conditionalizeX(this, X));
Error in BayesianOptimization/callObjFcn (line 467)
= callObjNormally(this, X);
Error in BayesianOptimization/runSerial (line 1989)
ObjectiveFcnObjectiveEvaluationTime, ObjectiveNargout] = callObjFcn(this, this.XNext);
Error in BayesianOptimization/run (line 1941)
this = runSerial(this);
Error in BayesianOptimization (line 457)
this = run(this);
Error in bayesopt (line 323) Results = BayesianOptimization(Options);
on second part (the lines cause error):
bayesObj = bayesopt(...
objFcn, optVars, ...
"MaxTime", 1* 3600,...,
"IsObjectiveDeterministic",false,...
"UseParallel",false);
and third part (remaining of codes):
estIdx = bayesObj.IndexOfMinimumTrace(end);
fileName = bayesObj.UserDataTrace{bestIdx};
savedStruct = load(fileName);
valError = savedStruct.valError;
function ObjFcn = makeObjFcn(trData,vaData)
ObjFcn = #valErrorFun;
function [valError,cons,fileName] = valErrorFun(optVars)
% making layers
imageSize = [32 32 3];
numClasses = numel(unique(vaData));
layers = buidLayers(...
imageSize,...
optVars.initFilterNum,...
optVars.depth,...
numClasses);
miniBatchSize = 256;
validationFrequency = floor(numel(vaData)/miniBatchSize);
options = trainingOptions('sgdm', ...
'InitialLearnRate',optVars.initLearningRate, ...
'Momentum',optVars.momentum1, ...
'MaxEpochs',60, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',40, ...
'LearnRateDropFactor',0.1, ...
'MiniBatchSize',miniBatchSize, ...
'L2Regularization',optVars.l2Reg, ...
'Shuffle','every-epoch', ...
'Verbose',false, ...
'Plots','training-progress', ...
'ValidationData',vaData, ...
'ValidationFrequency',validationFrequency);
pixelRange = [-4 4];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
datasource = augmentedImageDatastore(...
imageSize,...
trData,...
'DataAugmentation',imageAugmenter);
trainedNet = trainNetwork(datasource,layers,options);
%close(findall(groot,'Tag','NNET_CNN_TRAININGPLOT_FIGURE'))
YPredicted = classify(trainedNet,XValidation);
valError = 1 - mean(YPredicted == YValidation);
fileName = num2str(valError) + ".mat";
save(fileName,'trainedNet','valError','options')
cons = [];
end
end
I am trying to implement a model that takes an image as the input and gives a vector of 26 numbers. I am using VGG-16 at this time through the following Matlab code:
analyzeNetwork(net);
NUM_OUTPUT = 26;
layers = net.Layers;
%output = fullyConnectedLayer(NUM_OUTPUT, ...
% 'Name','output_layer', ...
% 'WeightLearnRateFactor',10, ...
% 'BiasLearnRateFactor',10);
layers = [
layers(1:38)
fullyConnectedLayer(NUM_OUTPUT)
regressionLayer];
%layers(1:67) = freezeWeights(layers(1:67));
miniBatchSize = 5;
validationFrequency = floor(numel(YTrain)/miniBatchSize);
options = trainingOptions('sgdm',...
'InitialLearnRate',0.001, ...
'ValidationData',{XValidation,YValidation},...
'Plots','training-progress',...
'Verbose',false);
net = trainNetwork(XTrain,YTrain,layers,options);
YPred = predict(net,XValidation);
predictionError = YValidation - YPred;
thr = 10;
numCorrect = sum(abs(predictionError) < thr);
numImagesValidation = numel(YValidation);
accuracy = numCorrect/numImagesValidation;
rmse = sqrt(mean(predictionError.^2));
The shape of XTrain and YTrain are as follows:
XTrain: 224 224 3 140
YTrain: 26 140
By running the code above (it is a part of the code not the whole of it) I get the following error:
Error using trainNetwork (line 170)
Number of observations in X and Y disagree.
I would appreciate it if somebody could help me to figure out what is the problem because as far as I know the number of samples in both are equal and there is no necessity for the rest of the dimensions to be equal.
Transpose YTrain to be 140x26.
Name your new layers, and make them layerGraph
Regression can easly go unstable so decrease learning rate or increase batch size if you get some nans.
net = vgg16 ; % analyzeNetwork(net);
LAYERS_FREEZE_UNTIL=35;
LAYERS_COPY_UNTIL=38;
NUM_TRAIN_SAMPLES = size(YTrain,1);
NUM_OUTPUT = size(YTrain,2);
my_layers =layerGraph([
freezeWeights(net.Layers(1:LAYERS_FREEZE_UNTIL))
net.Layers(LAYERS_FREEZE_UNTIL+1:LAYERS_COPY_UNTIL)
fullyConnectedLayer(NUM_OUTPUT*2,'Name','my_fc1')
fullyConnectedLayer(NUM_OUTPUT,'Name','my_fc2')
regressionLayer('Name','my_regr')
]);
% figure; plot(my_layers), ylim([0.5,6.5])
% analyzeNetwork(my_layers);
MINI_BATCH_SIZE = 16;
options = trainingOptions('sgdm', ...
'MiniBatchSize',MINI_BATCH_SIZE, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',{XValidation,YValidation}, ...
'ValidationFrequency',floor(NUM_TRAIN_SAMPLES/MINI_BATCH_SIZE), ...
'Verbose',true, ...
'Plots','training-progress');
my_net = trainNetwork(XTrain,YTrain,my_layers,options);
I have a Neural Network based on Fully Convolutional Neural Network for semantic segmentation / pixelwise object detection ( FCN) and try to train it using Pascal-Context data. However, instead of 450 classes provided by the dataset, I only want to capture 12 + background.
I prepared a dataset with 7000+ images and label maps, and followed the tutorial on Semantic Segmentation ( Semantic Segmentation), and followed every step similarly to the one presented.
However, when I train my neural network my Mini-batch loss remains constant (checked until end of first 3 epochs, then tried to reduce the amount of images, but still the same value) and doesn't change, even if I change learning rate or other parameters. Is there something wrong with the training process?
So I tried to reduce the number of images to 400, and what I am getting is in picture below. It doesn't improve at all.
( First column represents entry number, second Iteration Number, Third Time Elapsed, Fourth Mini-batch loss, Fifth Accuracy on batch, and finally learning rate).
Below is my code:
% MAIN FUNCTION %
outputFolder= fullfile('imade');
imgDir = fullfile('imade','images');
labelDir = fullfile('imade','labels');
imds = imageDatastore(imgDir);
I = readimage(imds, 1);
% I = histeq(I);
% figure
% imshow(I)
classes = [
"bottle"
"chair"
"diningtable"
"person"
"pottedplant"
"sofa"
"tvmonitor"
"ground"
"wall"
"floor"
"keyboard"
"ceiling"
"background"
];
valueSet = {
[230 25 75]
[60 180 75]
[255 225 25]
[0 130 200]
[245 130 48]
[145 30 180]
[128 128 0]
[210 245 60]
[250 190 190]
[0 128 128]
[170 110 40]
[128 0 0]
[0 0 0]
};
pxds = pixelLabelDatastore(labelDir,classes,valueSet);
% show sample image with overlay
C = readimage(pxds, 1);
cmap = camvidColorMap;
B = labeloverlay(I,C,'ColorMap',cmap);
figure
imshow(B)
pixelLabelColorbar(cmap,classes)
% calculate frequency of class pixels
tbl = countEachLabel(pxds);
% resize images and labels to desired format
imageFolder = fullfile(outputFolder,'imagesReszed',filesep);
imds = resizeCamVidImages(imds,imageFolder);
labelFolder = fullfile(outputFolder,'labelsResized',filesep);
pxds = resizeCamVidPixelLabels(pxds,labelFolder);
% partition dataset into test and train
[imdsTrain, imdsTest, pxdsTrain, pxdsTest] = partitionCamVidData(imds,pxds);
% create FCN net
imageSize = [225 300];
numClasses = numel(classes);
lgraph = fcnLayers(imageSize,numClasses,'type','16s');
st = fullfile('imade','checkPoint');
% adjust based on occurrence of each class
imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount;
classWeights = median(imageFreq) ./ imageFreq;
pxLayer = pixelClassificationLayer('Name','labels','ClassNames', tbl.Name, 'ClassWeights', classWeights);
lgraph = removeLayers(lgraph, 'pixelLabels');
lgraph = addLayers(lgraph, pxLayer);
lgraph = connectLayers(lgraph, 'softmax' ,'labels');
% set training parameters
options = trainingOptions('sgdm', ...
'Momentum', 0.9, ...
'InitialLearnRate', 1e-3, ...
'L2Regularization', 0.0005, ...
'MaxEpochs', 100, ...
'MiniBatchSize', 1, ...
'Shuffle', 'every-epoch', ...
'VerboseFrequency', 2);
datasource = pixelLabelImageSource(imdsTrain,pxdsTrain);
doTraining = true;
if doTraining
[net, info] = trainNetwork(datasource,lgraph,options);
else
data = load (strcat(st,filesep,'convnet_checkpoint__4607__2018_01_27__21_25_03.mat'));
war = data.net;
[sp, info] = trainNetwork(datasource,war.Layers,options);
end
I have trained R-CNN network models on a custom dataset and got the results as expected in the end. But I couldn't find where to set the number of iterations before starting the train process and the training continues without any sign of when it's going to stop. Is there a way to set the number of iterations beforehand, so it would stop after specified steps?
This is the code of training the rcnn:
%%%%%%%%%%%%%%%%%%%%%% Define Inputs
imagePath = 'D:\Thesis\Data\VEDAI\vedai\train_images\';
sampleImage = '00000000.png';
objectClasses = {'car','truck','tractor','campingcar','van','other', 'pickup', 'boat', 'plane'};
imageTable = vedaiTrain;
smallestObjectSize = [32, 32, 3];
%%%%%%%%%%%%%%%%%%%%%% Calculations
numClassesPlusBackground = numel(objectClasses) + 1;
t = num2cell(smallestObjectSize);
[height, width, numChannels] = deal(t{:});
imageSize = [height width numChannels];
%%%%%%%%%%%%%%%%%%%%%% Network Layers
%%%%% inputLayer
inputLayer = imageInputLayer(imageSize);
%%%%% middleLayer
filterSize = [5 5];
numFilters = 32;
middleLayers = [
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride', 2)
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
convolution2dLayer(filterSize, 2 * numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
]
%%%%% finalLayer
finalLayers = [
fullyConnectedLayer(64)
reluLayer
fullyConnectedLayer(numClassesPlusBackground)
softmaxLayer
classificationLayer
]
Layers = [
inputLayer
middleLayers
finalLayers
]
layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);
%%%%%%%%%%%%%%%%%%%%%% training options
options = trainingOptions('sgdm', ...
'Momentum', 0.9, ...
'InitialLearnRate', 0.001, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 8, ...
'L2Regularization', 0.004, ...
'MaxEpochs', 40, ...
'MiniBatchSize', 128, ...
'Verbose', true);
%%%%%%%%%%%%%%%%%%%%%% Train an R-CNN object detector
rcnn = trainRCNNObjectDetector(imageTable,Layers, options, ...
'NegativeOverlapRange', [0 0.3], 'PositiveOverlapRange',[0.5 1]);
It keeps training for iterations until some time, which I don't know how it decides.
In the file train_faster_rcnn_alt_opt.py file, set the max_iters = [80000, 40000, 80000, 40000] parameter to the number of iterations you want at each stage.
Problem Statement
I was using MatConvNet to build a very simple 1D example and small network using the function cnn_train that comes with the examples library. Following their example I built a small CNN example as follows:
clc;clear;clc;clear;
%% prepare Data
M = 32; %batch size
X_train = zeros(1,1,1,M); % (1 1 1 2) = (1 1 1 M)
for m=1:M,
X_train(:,:,:,m) = m; %training example value
end
Y_test = 10*X_train;
split = ones(1,M);
split(floor(M*0.75):end) = 2;
% load image dadabase (imgdb)
imdb.images.data = X_train;
imdb.images.label = Y_test;
imdb.images.set = split;
%% prepare parameters
L1=3;
w1 = randn(1,1,1,L1); %1st layer weights
w2 = randn(1,1,1,L1); %2nd layer weights
b1 = randn(1,1,1,L1); %1st layer biases
b2 = randn(1,1,1,L1); %2nd layer biases
G1 = ones(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN scale, one per dimension
B1 = zeros(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN shift, one per dimension
EPS = 1e-4;
%% make CNN layers: conv, BN, relu, conv, pdist, l2-loss
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
'name', 'conv1', ...
'weights', {{w1, b1}}, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'bnorm', ...
'weights', {{G1, B1}}, ...
'EPSILON', EPS, ...
'learningRate', [1 1 0.05], ...
'weightDecay', [0 0]) ;
net.layers{end+1} = struct('type', 'relu', ...
'name', 'relu1' ) ;
net.layers{end+1} = struct('type', 'conv', ...
'name', 'conv2', ...
'weights', {{w2, b2}}, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'pdist', ...
'name', 'averageing1', ...
'class', 0, ...
'p', 1) ;
%% add L2-loss
fwfun = #l2LossForward;
bwfun = #l2LossBackward;
net = addCustomLossLayer(net, fwfun, bwfun) ;
net.layers{end}.class = Y_test; % its the test set
net = vl_simplenn_tidy(net) ;
res = vl_simplenn(net, X_train);
%% prepare train options
trainOpts.expDir = 'results/' ; %save results/trained cnn
trainOpts.gpus = [] ;
trainOpts.batchSize = 2 ;
trainOpts.learningRate = 0.02 ;
trainOpts.plotDiagnostics = false ;
%trainOpts.plotDiagnostics = true ; % Uncomment to plot diagnostics
trainOpts.numEpochs = 20 ; % number of training epochs
trainOpts.errorFunction = 'none' ;
%% CNN TRAIN
vl_simplenn_display(net) ;
net = cnn_train(net, imdb, #getBatch, trainOpts) ;
I created this according to the example they provided, whenever I run the example I get the error:
Error using vl_nnconv
DATA and DEROUTPUT do not have compatible formats.
Error in vl_simplenn (line 397)
[res(i).dzdx, dzdw{1}, dzdw{2}] = vl_nnconv(res(i).x, l.weights{1},
l.weights{2}, res(i+1).dzdx)
Error in cnn_train>process_epoch (line 323)
res = vl_simplenn(net, im, dzdy, res, ...
Error in cnn_train (line 139)
[net,stats.train,prof] = process_epoch(opts, getBatch, epoch, train, learningRate,
imdb, net) ;
Error in main_1D_1layer_hard_coded_example (line 64)
net = cnn_train(net, imdb, #getBatch, trainOpts) ;
does someone know what is going on? The example is actually suppose to be simple so its confusing me what might be wrong.
SUPPLEMENTARY SECTION OF THINGS I'VE TRIED TO SOLVE THIS.
For more details of things I tried to solve this read ahead.
I went to that line in the file causing the error and printed the inputs to that function to make sure that I was giving arguments that made sense and its seems all is fine with that respect:
case 'conv'
size(res(i).x)
size(res(i+1).dzdx)
size(l.weights{1})
size(l.weights{2})
[res(i).dzdx, dzdw{1}, dzdw{2}] = vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx)
[res(i).dzdx, dzdw{1}, dzdw{2}] = ...
vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx, ...
'pad', l.pad, ...
'stride', l.stride, ...
l.opts{:}, ...
cudnn{:}) ;
prints:
ans =
1 1 3 16
ans =
1 1 3 16
ans =
1 1 1 3
ans =
1 1 1 3
what I expected.
I even went ahead and manually hard coded what the chain of derivatives the network should be computing and that file seems to work fine:
clc;clear;clc;clear;
%% prepare Data
M = 3;
x = zeros(1,1,1,M); % (1 1 1 2) = (1 1 1 M)
for m=1:M,
x(:,:,:,m) = m;
end
Y = 5;
r=Y;
%% parameters
L1 = 3;
w1 = randn(1,1,1,L1); % (1 1 1 L1) = (1 1 1 3)
b1 = ones(1,L1);
w2 = randn(1,1,1,L1); % (1 1 1 L1) = (1 1 1 3)
b2 = ones(1,L1);
G1 = ones(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN scale, one per dimension
B1 = zeros(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN shift, one per dimension
EPS = 1e-4;
%% Forward Pass
z1 = vl_nnconv(x,w1,b1); % (1 1 3 2) = (1 1 L1 M)
%bn1 = z1;
bn1 = vl_nnbnorm(z1,G1,B1,'EPSILON',EPS); % (1 1 3 2) = (1 1 L1 M)
a1 = vl_nnrelu(bn1); % (1 1 3 2) = (1 1 L1 M)
z2 = vl_nnconv(a1,w2,b2);
y1 = vl_nnpdist(z2, 0, 1);
loss_forward = l2LossForward(y1,Y);
%%
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
'name', 'conv1', ...
'weights', {{w1, b1}}, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'bnorm', ...
'weights', {{G1, B1}}, ...
'EPSILON', EPS, ...
'learningRate', [1 1 0.05], ...
'weightDecay', [0 0]) ;
net.layers{end+1} = struct('type', 'relu', ...
'name', 'relu1' ) ;
net.layers{end+1} = struct('type', 'conv', ...
'name', 'conv2', ...
'weights', {{w2, b2}}, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'pdist', ...
'name', 'averageing1', ...
'class', 0, ...
'p', 1) ;
fwfun = #l2LossForward;
bwfun = #l2LossBackward;
net = addCustomLossLayer(net, fwfun, bwfun) ;
net.layers{end}.class = Y;
net = vl_simplenn_tidy(net) ;
res = vl_simplenn(net, x);
%%
loss_forward = squeeze( loss_forward ) % (1 1)
loss_res = squeeze( res(end).x ) % (1 1)
%% Backward Pass
p = 1;
dldx = l2LossBackward(y1,r,p);
dy1dx = vl_nnpdist(z2, 0, 1, dldx);
[dz2dx, dz2dw2] = vl_nnconv(a1, w2, b2, dy1dx);
da1dx = vl_nnrelu(bn1, dz2dx);
[dbn1dx,dbn1dG1,dbn1dB1] = vl_nnbnorm(z1,G1,B1,da1dx);
[dz1dx, dz1dw1] = vl_nnconv(x, w1, b1, dbn1dx);
%%
dzdy = 1;
res = vl_simplenn(net, x, dzdy, res);
%%
% func = #(x) proj(p, forward(x, x0)) ;
% err = checkDerivativeNumerically(f, x, dx)
% %%
dz1dx = squeeze(dz1dx)
dz1dx_vl_simplenn = squeeze(res(1).dzdx)
The derivatives seem to math so I'd assume that everything in that file works. It doesn't throw an error, so the fact that it doesn't even run has me very confused. Anyone know whats going on?
The way I loaded my CNN was based on the example file they provide on that tutorial. I will paste a summary of the important aspects of that file (that runs fine with cnn_train function while mine does not).
setup() ;
% setup('useGpu', true); % Uncomment to initialise with a GPU support
%% Part 3.1: Prepare the data
% Load a database of blurred images to train from
imdb = load('data/text_imdb.mat') ;
%% Part 3.2: Create a network architecture
net = initializeSmallCNN() ;
%net = initializeLargeCNN() ;
% Display network
vl_simplenn_display(net) ;
%% Part 3.3: learn the model
% Add a loss (using a custom layer)
net = addCustomLossLayer(net, #l2LossForward, #l2LossBackward) ;
% Train
trainOpts.expDir = 'data/text-small' ;
trainOpts.gpus = [] ;
% Uncomment for GPU training:
%trainOpts.expDir = 'data/text-small-gpu' ;
%trainOpts.gpus = [1] ;
trainOpts.batchSize = 16 ;
trainOpts.learningRate = 0.02 ;
trainOpts.plotDiagnostics = false ;
%trainOpts.plotDiagnostics = true ; % Uncomment to plot diagnostics
trainOpts.numEpochs = 20 ;
trainOpts.errorFunction = 'none' ;
net = cnn_train(net, imdb, #getBatch, trainOpts) ;
The dimensions of w2 should be 1x1x3x3.
Also usually the biases would be given as 1x3, since they only have one dimension (or 1x1x3xN for the weights and 1xN for the corresponding biases, where N is the number of filters), and the same is true for B1 and G1 (here it is 1xM, where M is the number of filters in the previous layer). But it might work either way.
In your example the dimensions of x are 1x1x3x16 after the first convolution. This means there are 16 elements in one batch where each element has width and height 1 and depth 3. Depth 3 because the first convolution was done with 3 filters (w1 has dimensions 1x1x1x3).
w2 in your example has dimensions 1x1x1x3, which denotes 3 filters of width, height and depth 1. So the depth of the filters doesn't match the depth of the input.
I got the same problem by creating a custom layer. I finally found the solution for it by tracking the matconvnet implementation. Hope it will help other later.
In short, you need to make sure the two data are not empty, not null, and have same device type (GPU or CPU) and same data type (float, single or char).
In my case, both data have to have same 'gpuArray' and 'single'.
====== Details ==================
Firstly, the error
DATA and FILTERS do not have compatible formats
DATA and BIASES do not have compatible formats
DATA and DEROUTPUT do not have compatible formats
exactly say that the two variables do not have compatible formats.
So what Matconvnet mean 'compatible formats'?
It is implemented in vl_nnconv.cu, line 269~278
/* check for GPU/data class consistency */
if (hasFilters && ! vl::areCompatible(data, filters)) {
vlmxError(VLMXE_IllegalArgument, "DATA and FILTERS do not have compatible formats.") ;
}
if (hasBiases && ! vl::areCompatible(data, biases)) {
vlmxError(VLMXE_IllegalArgument, "DATA and BIASES do not have compatible formats.") ;
}
if (backMode && ! vl::areCompatible(data, derOutput)) {
vlmxError(VLMXE_IllegalArgument, "DATA and DEROUTPUT do not have compatible formats.") ;
}
The error come from function vl::areCompatible which is implemented as
inline bool areCompatible(Tensor const & a, Tensor const & b)
{
return
(a.isEmpty() || a.isNull()) ||
(b.isEmpty() || b.isNull()) ||
((a.getDeviceType() == b.getDeviceType()) & (a.getDataType() == b.getDataType())) ;
}
So, basically, it checks whether any input is empty or null and makes sure both inputs have the same data type (double, single, vs char) and device type (GPU, CPU).
/// Type of device: CPU or GPU
enum DeviceType {
VLDT_CPU = 0,
VLDT_GPU
} ;
/// Type of data (char, float, double, ...)
enum DataType {
VLDT_Char,
VLDT_Float,
VLDT_Double
} ;