Error in neural network
2 views (last 30 days)
Show older comments
I'm novice in matlab and in neural network. My problem is to create network that will classificate images of 8x8 on 3 texture classes. 3 arrays are created( training set - P, targets - P, testing set):
imSize = [8 8];
nump = 3; % number of classes
n = 50; % number of images per class
images1 = extract_images('d:\matlab\sharp.jpg');
images2 = extract_images('d:\matlab\smooth.jpg');
images3 = extract_images('d:\matlab\texture.jpg');
images1 = images1(1:n*2);
images2 = images2(1:n*2);
images3 = images3(1:n*2);
trainingImages = [images1(1:n), images2(1:n), images3(1:n)];
testImages = [images1(n+1:end), images2(n+1:end), images3(n+1:end)];
P = zeros(n*nump, prod(imSize));
N = zeros(n*nump, prod(imSize));
% training images reshaped into columns in P
% image size (8x8) reshaped to (1x64)
for i = 1:length(trainingImages)
P(i,:) = trainingImages{i}(:).';
N(i,:) = testImages{i}(:).';
end
P = P';
N = N';
T = zeros(nump, n*nump);
T(1,1:n) = 1;
T(2,n+1:n*2) = 1;
T(3,n*2+1:end) = 1;
Then I create neural network with help nprtool, generated code: inputs = P; targets = T;
% Create a Pattern Recognition Network
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,inputs,targets);
% Test the Network
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)
% View the Network
view(net)
Save net and try to test on one image Q of size 8x8
img = imread('D:\matlab\test1.jpg');
Q = img(:).';
Q = Q';
a = sim( net, Q );
And get errors:
??? Error using ==> minus
Integers can only be combined with integers of the same class, or scalar doubles.
Error in ==> mapminmax>apply at 266
y = (x - xmin(:,copyQ)) .* gain(:,copyQ) + settings.ymin;
Error in ==> forward at 25
xij = pfcns(k).apply(xij,pfcns(k).settings);
Error in ==> pre_inputs at 9
x(i,:) = nnproc.forward(fcns.inputs(i).process,x(i,:));
Error in ==> y at 29
Pc = nnproc.pre_inputs(fcns,Xc);
Error in ==> network.sim at 132
[Y,Xf,Af] = nnsim.y(net,X,Xi,Ai,Q);
Help me, where is mistake in my code?
Sorry for my english
0 Comments
Answers (2)
Walter Roberson
on 5 Jan 2012
imread() usually returns uint8 data. The NN operations including sim() must be passed double precision data and not uint8. This also applies to training.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!