Cross entopy question for semantic segmentation procedure - semantic-segmentation

How to use cross entropy for float images and labels?
I'm studying Semantic segmentation with Cityscapes data.
When I use
torchivision.transfrom.ToTensor()
My Images and labels data are transform the range from [0~255] to [0~1]. Because I used ToTensor().
Then I passed images to a model, I got prediction (Batch X class X W X H) from.
After that, I want to use CrossEntropy to get loss.
The labels is (Batch X W X H), range [0~1]
The prediction is (Batch X Class X W X H), range[-inf ~ +inf (somewhere)]
But I got an error, Those input should be Tensor.long.
If I cast theos labels to Tensor.Long, labels should be all 0.
What shoud I do???
The cityscapes should be treated ignore index.
Cross entropy has Ignore index.
But the labels are [0~1] range.

Solve by myself.
In Segmentation, No use Normalize and toTensor.
Thanks.

Related

How to apply a moving median filter on a time series of 2D scans in Matlab?

I have a huge set of data of a timelapse of 2D laser scans of waves running up and down stairs (see fig.1fig.2fig.3).
There is a lot of noise in the scans, since the water splashes a lot.
Now I want to smoothen the scans.
I have 2 questions:
How do I apply a moving median filter (as recommended by another study dealing with a similar problem)? I can only find instructions for single e.g. (x,y) or (t,y) plots but not for x and y values that vary over time. Maybe an average filter would do it as well, but I do not have a clue on that either.
The scanner is at a fixed point (222m) so all the data spikes point towards that point at the ceiling. Is it possible or necessary to include this into the smoothing process?
This is the part of the code (I hope it's enough to get it):
% Plot data as real time profile
x1=data.x;y1=data.y;
t=data.t;
% add moving median filter here?
h1=plot(x1(1,:),y1(1,:));
axis([210 235 3 9])
ht=title('Scanner data');
for i=1:1:length(t);
set(h1,'XData',x1(i,:),'YData',y1(i,:));set(ht,'String',sprintf('t = %5.2f
s',data.t(i)));pause(.01);end
The data.x values are stored in a (mxn) matrix in which the change in time is arranged vertically and the x values i.e. "laser points" of the scanner are horizontally arranged. The data.y is stored in the same way. The data.t values are stored in a (mx1) matrix.
I hope I explained everything clearly and that somebody can help me. I am already pretty desperate about it... If there is anything missing or confusing, please let me know.
If you're trying to apply a median filter in the x-y plane, then consider using medfilt2 from the Image Processing Toolbox. Note that this function only accepts 2-D inputs, so you'll have to loop over the third dimension.
Also note that medfilt2 assumes that the x and y data are uniformly spaced, so if your x and y data don't fall onto a uniformly spaced grid you may have to manually loop over indices, extract the corresponding patches, and compute the median.
If you can/want to apply an averaging filter instead of a median filter, and if you have uniformly spaced data, then you can use convn to compute a k x k moving average by doing:
y = convn(x, ones(k,k)/(k*k), 'same');
Note that you'll get some bias on the boundaries because you're technically trying to compute an average of k^2 pixels when you have less than that number of values available.
Alternatively, you can use nested calls to movmean since the averaging operation is separable:
y = movmean(movmean(x, k, 2), k, 1);
If your grid is separable, but not uniform, you can still use movmean, just use the SamplePoints name-value pair:
y = movmean(movmean(x, k, 2, 'SamplePoints', yv), k, 1, 'SamplePoints', xv);
You can also control the endpoint handling in movmean with the Endpoints name-value pair.

Can I compute Haar-features using "Coefficients" in a IntegralKernel defined filter instead of integralFilter() function?

I define a Haar-like feature filter H by the IntegralKernel class in Matlab.
H = integralKernel(integralKernel([1,1,im_width,im_height;...
2, 2, w, h;...
2+w, 2, w, h;],...
[0,1,-1]);
im_width and im_height are width and height of the image; w and h are width and height of the bounding box of the filter. The returned filte H is a Matlab struct type variable, which contains the fields like:
BoundingBoxes
Weights
Coefficients - Conventional filter coefficients.
Center
Size
In Matlab document, it uses the function integralFilter(IntegralImage, H), when computing the feature from the given integral image. The code is like:
HaarFeature = integralFilter(integralImage(I), H)
But this takes a long time if the number of images are large (e.g., 10,000 images), because I need to use a for-loop to compute every image. However, I found that is much quicker if I just compute the sum of the given original image I and the coefficients in the filer H. The code is like:
HaarFeature = sum(sum(I .* H.Coefficients));
Both I and H.Coefficients are double type in Matlab.
My question is that if these two ways to compute the Haar Features are equivalent? Thanks!
Do you have to filter each image with multiple kernels?
In that case you should only call integralImage(I) once for each image, and store and re-use the result. Computing the integral image takes some time. However, once you have it, applying an integral kernel to the resulting integral image should be very fast.

Cannonical Correlation Analysis

I have just started working using CCA in Matlab. I have two vectors X and Y of dimension 60x1920 and 60x1536 with the number of samples being 60 and variables in the different set of vectors being 1920 and 1536 respectively. I want to know do CCA for reducing them to the subspace and then do feature matching.
I am using this commands.
%% DO CCA
[A,B,r,U,V] = canoncorr(X,Y);
The output I get is this :
Name Size Bytes Class Attributes
A 1920x58 890880 double
B 1536x58 712704 double
U 60x58 27840 double
V 60x58 27840 double
r 1x58 464 double
Can anyone please tell me what these variables mean. I have gone over the documentation several times and still is unclear about them. As I understand CCA finds two linear projection matrices Wx and Wy such that the projection of X and Y on Wx and Wy are maximally correlated.
1) Could anyone please tell me which of the following matrices are these?
2) Also how can I find the projected vectors in the learned subspace of CCA?
Any help will be appreciated. Thanks in advance.
As I understand it, with X and Y being your original data matrices, A and B are the sets of coefficients that perform a change of basis to maximally correlate your original data. Your data is represented in the new bases as the matrices U and V.
So to answer your questions:
The projection matrices you are looking for would be A and B since they transform X and Y into the new space.
The resulting projections of X and Y into the new space would be U and V, respectively. (The r vector represents the entries of the correlation matrix between U and V, which is a diagonal matrix.)
The The MATLAB documentation says this transformation can be done with the following formulae, where N is the number of observations:
U = (X-repmat(mean(X),N,1))*A
V = (Y-repmat(mean(Y),N,1))*B
This page lays out the process nicely so you can see what each coefficient means in the transformation process.

Matlab's Quiver3 plots only 4 vectors

I want to use the command quiver3(X,Y,Z,M,N,O)
in order to get a vector field, where all matrices are 10x10x10 arrays with real entries. Now I wanted to plot it and got only 4 vectors instead of my expected 1000. Does anybody here know, what I could possibly have done wrong?
If you need further information, I am available for your comments.
Assuming the problem occurs when attempting to display the data (rather than with attempting to display unallowed numerical values such as Inf or NaN), I think the problem is the range of norms of the vectors, such that only 4 show up.
The importance of the norm of the vectors relative to the range of the coordinate system is important, as illustrated by the following example:
Here all 10 random vectors show up:
Na = 10;
[X Y Z M N O] = deal(rand(Na,1),rand(Na,1),rand(Na,1),rand(Na,1),rand(Na,1),rand(Na,1));
quiver3(X,Y,Z,M,N,O)
But if some of the vectors are made 100 x bigger, only the smaller ones show up:
mul = 1e+3;
[X Y Z M N O] = deal(rand(Na,1),rand(Na,1),rand(Na,1),...
[mul*rand(Na/2,1);rand(Na/2,1)],...
[mul*rand(Na/2,1);rand(Na/2,1)],...
[mul*rand(Na/2,1);rand(Na/2,1)]);
quiver3(X,Y,Z,M,N,O)
Similarly if some of the vectors are made too small they don't show up:
mul = 1e-3;
[X Y Z M N O] = deal(rand(Na,1),rand(Na,1),rand(Na,1),...
[mul*rand(Na/2,1);rand(Na/2,1)],...
[mul*rand(Na/2,1);rand(Na/2,1)],...
[mul*rand(Na/2,1);rand(Na/2,1)]);
quiver3(X,Y,Z,M,N,O)
Vectors too large or too small relative to the axis are not displayed at all.
If it is a problem with scaling you may want to inspect your function and see how to narrow the range of x,y,z so that the vectors have a narrower range of values, or change the scale (logarithm?) so that all the data can be displayed in one figure.
edit
As an alternative to the question of how to display your data, you may want to consider using isosurfaces. Here's an example, with each red sphere representing a different isosurface in a spherical potential:

computing PCA matrix for set of sift descriptors

I want to compute a general PCA matrix for a dataset, and I will use it to reduce dimensions of sift descriptors. I have already found some algorithms to compute it, but I couldn't find a way to compute it by using MATLAB.
Can someone help me?
[coeff, score] = princomp(X)
is the right thing to do, but knowing how to use it is a little tricky.
My understanding is that you did something like:
sift_image = sift_fun(img)
which gives you a binary image: sift_feature?
(Even if not binary, this still works.)
Inputs, formulating X:
To use princomp/pca formulate X so that each column is a numel(sift_image) x 1 vector (i.e. sift_image(:))
Do this for all your images and line them up as columns in X. So X will be numel(sift_image) x num_images.
If your images aren't the same size (e.g. pixel dimensions different, more or less of a scene in the images), then you'll need to bring them into some common space, which is a whole different problem.
Unless your stuff is binary, you'll probably want to de-mean/normalize X, both in the column direction (i.e. normalizing each individual image) and row direction (de-meaning the whole dataset).
Outputs
score is the set of eigen vectors: it will be num_pixels * num_images.
To get, say the first eigen vector back into an image shape, do:
first_component = reshape(score(:,1),size(im));
And so on for the rest of the components. There are as many components as input images.
Each row of coeff is the num_images (equal to num_components) set of weights that can be applied to generate each input image. i.e.
input_image_1 = reshape(score * coeff(:,1) , size(original_im));
where input_image_1 is the correct, original shape
coeff(1,:) is a vector (num_images x 1)
score is pixels x num_images
(Disclaimer: I may have the columns/rows mixed up, but the descriptions are correct.)
Does that help?
If you have access to Statistics Toolbox, you can use the command princomp, or in recent versions the command pca.