I don't understand how i should format the inputs.
I have this line to train the network:
net = trainNetwork(imgs, true_labels_fix, layers, options);
And imgs and true_labels_fix look like this:
imgs - 1200x1 cell
true_labels_fix - 1200x1 categorical
Where each cell in the imgs is a 255x301 uint8 image.
The error is:
"
Invalid training data. Predictors must be a numeric array, a datastore, or a
table. For networks with sequence input, predictors can also be a cell array of
sequences."
The layers look like this:
input_size = [255, 301];
layers = [
imageInputLayer(input_size)
fullyConnectedLayer(100)
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer
];
I've tried to reformat the inputs but with no success. Also tried looking at examples but nothing have helped.
Related
I try to create an LSTM model. I get following error:
Error using vertcat Dimensions of arrays being concatenated are not
consistent. Error in source (line 9)
sequenceInputLayer(33)
What should be the input of sequenceInputLayer and its size?
Data = csvread('newData.csv');
num_timesteps = size(Data,1)
num_features = size(Data,2)
Data = normalize(Data);
numHiddenUnits = 200;
size(Data)
layers = [
sequenceInputLayer(33)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(num_features),regressionLayer];
maxEpochs = 60;
miniBatchSize = 20;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'InitialLearnRate',0.001, ...
'GradientThreshold',1, ...
'Shuffle','never', ...
'Plots','training-progress',...
'Verbose',0);
% net = trainNetwork(Data,Data,layers,options);
The problem is not in sequenceInputLayer, the problem is in the way you are creating the layers array.
Replace:
layers = [
sequenceInputLayer(33)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(num_features),regressionLayer];
With:
layers = [
sequenceInputLayer(33)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(num_features),
regressionLayer];
Explanation: In an array declaration, when adding elements in new lines (or separating by ;) you are crating a columns vector, when separating by ,, you are crating a row vector. Somehow you mixed them up.
I've created some Matlab code which anyone helping can run and see the problem.
When I run the following code, for each data point on my plot I seem to get all 15 labels instead of only 1 specific label.
So how do I get the Matlab data point labels correct for the following code?
Based on the the suggestions, I did the following:
I replaced these two lines of code:
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
With this line of code as suggested:
labels_cell = strread(num2str(test_vector_label),'%s');
Now there are two follow-up questions:
1) A warning appears stating that I should use textscan instead of strread:
labels_cell = textscan(num2str(test_vector_label),'%s');
Then when I use textscan as in the above line of code above, I get an error?
"Error using text
Cell array of strings may only contain string and numeric
matrices"
"Error in Code_Test (line 46)
text(x_val,y_val,labels_cell,'horizontal','left',
'vertical','bottom')"
2) How do I put a letter in front of the number labels? For example, in the original code I had put letter F followed by a number?
%--------------Randomly select training and testing data.-----------
num_data = 35;
data_idx = 1:35;
train_data_idx_tmp = randsample(num_data,20)
train_dataRand_idx = sort(train_data_idx_tmp)
% Lia = ismember(A,B) returns an array the same size as A, containing 1 (true)
% where the elements of A are found in B, and 0 (false) elsewhere.
test_data_idx_tmp = ismember(data_idx,train_dataRand_idx)
test_dataRand_idx = data_idx(~test_data_idx_tmp)'
% Check to see if training and test data index are exclusive.
check_train_test_idx = ismember(train_dataRand_idx,test_dataRand_idx)
%--------------------------------------------------------------------------
% Testing stage.
test_vector = test_dataRand_idx; %Select randomly obtained testing data.
% Training stage.
train_vector = train_dataRand_idx; %Select randomly obtained training
x_val = [1:15];
y_val = 2*[1:15];
plot(x_val,y_val,'or','MarkerFaceColor','r')
grid on
%Put specific data point labels on plots.
test_vector_label = test_vector';
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
text(x_val,y_val,labels_cell,'horizontal','left', 'vertical','bottom')
Your variable labels_cell is a 1x1 string cell not an array of strings. Replace
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
with
labels_cell = strread(num2str(test_vector_label),'%s');
I have a file with .nii extension. I don't know how to convert a .nii file into 2D format.My question is while converting .nii file into 2D, am I losing some information about the file.Which format is good? dicom or png or bmp.
nii = load_nii('im.nii');
size(nii.img);
returns
ans =
39 305 305
and it is in uint8 format
May I use squeeze or resize?How to apply resize to this image;whether it lose information?
Yes you can manipulate the image/sequence as you would with any array.
Here is a simple example with data available from the NIH here.
The data set is in 4D and is named "filtered_func_data.nii".
Let's load the dataset and access the img field of the resulting structure:
S = load_nii('filtered_func_data.nii')
Here, S is a structure with the following fields:
S =
hdr: [1x1 struct]
filetype: 2
fileprefix: 'filtered_func_data'
machine: 'ieee-be'
img: [4-D int16]
original: [1x1 struct]
And we can access the image data with the field img (you already figured that out):
A = S.img
If we check the size, we get:
size(A)
ans =
64 64 21 180
So the dataset consists in 64x64 images with a depth/number of slices of 21 and a number of frames of 180.
At this point we can manipulate A as we like to reshape, resize or anything.
Here is a simple code (animated gif) to loop through each slice of the 1st timepoint in the 4D array:
NumSlices = size(A,3)
figure(1)
filename = 'MRI_GIF.gif';
for k = 1:NumSlices
imshow(A(:,:,k,1),[])
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
pause(.1)
end
Output:
Which looks pretty nice to me.
So in your case, you get a size of [39 305 305] and you can apply the same manipulations I did to play around with your data set, which is in 3D.
EDIT
With your data it's the same:
S = load_nii('Subject01.nii');
A = S.img;
NumSlices = size(A,3);
And then, if you want a 2D image you need to select a slice in the 3D array.
For example, the first slice is accessed like so:
A(:,:,1)
And so on for the rest.
To save images as png, use imwrite.
Hope that helps!
I am doing a project in character recognition of a local language. I created the dataset. But I am not sure how to feed it using neural network?
In this stage, I can only select one image as input rather than whole set of same character. How to proceed?
Pls help
As far as I know these neurons don't understand 2d input, so you need to make a 1d array from image:
image1flat = image1(:);
image2flat = image2(:);
Then put them into a 2d trainning set array (note that "Samples are:" option on your picture)
%samples are columns now
trainningSet = [image1flat image2flat];
Automated code:
%change this to folder where the files are
cd('/path/to/files');
%change this to your file format
files = dir('*.png');
result = [];
for i = 1:length(files)
A{i} = imread(files(i).name);
Aflat{i} = A{i}(:);
result = [result Aflat{i}];
end
%put the result into nprtool
I would like to add a cell array of images together using imadd, but imadd only takes two arguments. Is there a reduce function in MATLAB which I could use add all of these images together without writing a for loop?
images = {im1, im2, im3};
sum = reduce(#imadd, images);
You could just use an array with an extra dimension. E.g. for 2-d (grayscale images)
images = {im1, im2, im3};
imarr = cat(3, images{:});
imsum = sum(imarr, 3);
Of course there's no need to create the cell array in the first place; you could go straight to
imarr = cat(3, im1, im2, im3);
imsum = sum(imarr, 3);
or even
imsum = sum(cat(ndims(im1)+1, im1, im2, im3),ndims(im1)+1);
which also more generally combines any dimensional matrices.
Since you are using the Image Processing Toolbox, the IMLINCOMB function (linear combination of images) can also be used, just give all coefficients as one:
imsum = imlincomb(1,im1, 1,im2, 1,im3)