I'm training a recurrent neural network, but I want to apply the clipping gradient. I'm using sgd. Can I use the clipping gradient to the sum of the gradients computed for a minibatch?
Clipping the sum of the gradients has no effect. You should instead clip each gradient individually.
Here is a quick code snippet for gradient clipping in Tensorflow:
max = 20
grads = tf.gradients(loss, tf.trainable_variables())
grads, _ = tf.clip_by_global_norm(grads, max) # gradient clipping
grads_and_vars = list(zip(grads, tf.trainable_variables()))
optimizer = tf.train.AdamOptimizer(learning_rate)
train_op = optimizer.apply_gradients(grads_and_vars)
Related
I'm doing some processing with MATLAB on an RGB image.
I had to obtain a circular blurring like in the image below:
obtained through this code:
A = imread('lena .bmp');
I = rgb2gray(A);
[rNum,cNum,~] = size(I);
%center and radius of the circular mask
x1 = 256.5;
y1 = 256.5;
radius = 100;
%circular mask creation
[x,y] = ndgrid((1:rNum)-y1,(1:cNum)-x1);
mask = (x.^2 + y.^2)<radius^2;
h = ones(30,30)/900; %gaussian filter
J = roifilt2(h,I,mask); %apply the filter at the mask
%filtering plane - by - plane in order to apply the circular blurred mask
%to the RGB image
filtered_im = zeros(size(A));
filtered_im(:,:,1) = roifilt2(h, A(:,:,1), mask);
filtered_im(:,:,2) = roifilt2(h, A(:,:,2), mask);
filtered_im(:,:,3) = roifilt2(h, A(:,:,3), mask);
filtered_im = uint8(filtered_im);
figure
imshow(filtered_im)
title('Circular blurring RGB image');
The effect obtained, anyway, is too artificial because the transition between the blurred circular mask and the rest of the image is too sharp. Is there a possibility to make this transition more faded in order to obtain a more natural effect?
You can use a weighted average of both the original image and the modified one, using as weights a mask based on the distance to the center of the circle.
The original one would have more weight in the external part of the circle, and the modified image in the center. This could create a transition in the blurring.
Can someone please explain me why the 'Circularity' in Matlab is calculated by (4*Area*pi)/(Perimeter^2) while in Podczeck Shape it is Area/(Pi/4*sp^2) https://qiftp.tudelft.nl/dipref/FeatureShape.html)? Or is it just simply defined differently?
I tried to write a Podczeck Shape circularity code in Matlab and I assume that ‘MaxFeretDiameter’ is perpendicular to ‘MinFeretDiameter’, am I correct?
Code:
clc;
clear all;
close all;
Pi=pi;
Image = rgb2gray(imread('pillsetc.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);
imshow(BW);
[B,L] = bwboundaries(BW,'noholes');
i=2;
stat = regionprops(BW, 'Area', 'Circularity', 'MaxFeretProperties', 'MinFeretProperties');
OArea = stat(i).Area;
OMaxFeretProperties = stat(i).MaxFeretDiameter;
OMinFeretProperties = stat(i).MinFeretDiameter;
OCircularityPodzeck = OArea/(Pi/4 * (OMaxFeretProperties^2))
OCircularityMatlab = stat(i).Circularity
The 'Circularity' measure in regionprops is defined as
Circularity = (4 Area π)/(Perimeter²)
For a circle, where Area = π r² and Perimeter = 2 π r, this comes out to:
Circularity = (4 π r² π)/((2 π r)²) = (4 π² r²)/(4 π² r²) = 1
For any other shape, the perimeter will be relatively longer (this is a characteristic of the circle!), and so the 'Circularity' measure will be smaller.
Podczeck's Circularity is a different measure. It is defined as
Podczeck Circularity = Area/(π/4 Height²)
In the documentation you link it refers to Height as sp, and defines it as "Feret diameter perpendicular to s", and defines s as "the shortest Feret diameter". Thus, sp is the largest of the two sides of the minimal bounding box.
For a circle, the minimal bounding box has Height equal to the diameter. We substitute again:
Podczeck Circularity = (π r²)/(π/4 (2 r)²) = (π r²)/(π/4 4 r²) = 1
For any other shape, the height will be relatively larger, and so the Podczeck Circularity measure will be smaller.
Do note that the max and min Feret diameters are not necessarily perpendicular. A simple example is a square: the largest diameter is the diagonal of the square; the smallest diameter is the height or width; these two are at 45 degrees from each other. The Podczeck Circularity measure uses the size of the project perpendicular to the smallest projection, which for a square is equal to the smallest projection, and smaller than the largest projection. The smallest projection and its perpendicular projection form the minimal bounding rectangle (typically, though apparently this is not necessarily the case?). However, regionprops has a 'BoundingBox' that is axis-aligned, and therefore not suitable. I don't know how to get the required value out of regionprops.
The approach you would have to follow is to use the 'PixelList' output of regionprops, together with the 'MinFeretAngle'. 'PixelList' is a list of pixel coordinates that belong to the object. You would rotate these coordinates according to 'MinFeretAngle', such that the axis-aligned bounding rectangle now corresponds to the minimal bounding rectangle. You can then determine the size of the box by taking the minimum and maximum values of the rotated coordinates.
I try to remove the white annotations of this image (the numbers and arrows), as well as the black grid, with MATLAB:
I tried to compute, for each pixel, the mode of neighbors, but this process is very slow and I get poor results.
How can I obtain an image like this one?
Thank you for your time.
The general name for such a task is inpainting. If you search for that you will find better methods than what I'm showing here. This is no more than a proof of concept. I'm using DIPimage 3 (because I'm an author and it's easy for me to use).
First we need to create a mask for the regions that we want to remove (inpaint). It is easy to find pixels where all three channels have a high value (white) or a low value (black):
img = readim('https://i.stack.imgur.com/16r9N.png');
% Find a mask for the areas to remove
whitemask = min(img,'tensor') > 50;
blackmask = max(img,'tensor') < 30;
mask = whitemask | blackmask;
This mask doesn't capture all of the black grid, if we increase the threshold we will also remove the dark region of sea off the coast of Spain. And it also captures the white outline of the coasts. We can do a little bit better than this with some additional filtering:
% Find a mask for the areas to remove
whitemask = min(img,'tensor') > 50;
whitemask = whitemask - pathopening(whitemask,50);
blackmask = max(img,'tensor');
blackmask2 = blackmask < 80;
blackmask2 = blackmask2 - areaopening(blackmask2,6);
blackmask = blackmask < 30 | blackmask2;
mask = whitemask | blackmask;
This produces the following mask:
Still far from perfect, but a good start for our proof of concept.
One simple inpainting method uses normalized convolution: using the inverse of the mask we made, convolve the image multiplied by the mask, and convolve the mask separately. The ratio of these two results is a smoothed image that doesn't take the masked pixels into account. Finally, we replace the pixels in the original image under the mask with the values from this normalized convolution:
% Solution 1: normalized convolution
smooth = gaussf(img * ~mask, 2) / gaussf(~mask, 2);
img(mask) = smooth(mask);
An alternative solution applies a closing on the image multiplied by the mask (note that this multiplication makes the pixels we don't want completely black; the closing will spread the surrounding colors over the black areas):
% Solution 2: morphology
smooth = iterate('closing',img * ~mask, 13);
img(mask) = smooth(mask);
I want to apply Temporal Median Filter to a depth map video to ensure temporal consistency and prevent the flickering effect.
Thus, I am trying to apply the filter on all video frames at once by:
First loading all frames,
%%% Read video sequence
numfrm = 5;
infile_name = 'depth_map_1920x1088_80fps.yuv';
width = 1920; %xdim
height = 1088; %ydim
fid_in = fopen(infile_name, 'rb');
[Yd, Ud, Vd] = yuv_import(infile_name,[width, height],numfrm);
fclose(fid_in);
then creating a 3-D depth matrix (height x width x number-of-frames),
%%% Build a stack of images from the video sequence
stack = zeros(height, width, numfrm);
for i=1:numfrm
RGB = yuv2rgb(Yd{i}, Ud{i}, Vd{i});
RGB = RGB(:, :, 1);
stack(:,:,i) = RGB;
end
and finally applying the 1-D median filter along the third direction (time)
temp = medfilt1(stack);
However, for some reason this is not working. When I try to view each frame, I get white images.
frame1 = temp(:,:,1);
imshow(frame1);
Any help would be appreciated!
My guess is that this is actually working but frame1 is of class double and contains values, e.g. between 0 and 255. As imshow represents double images by default on a [0,1] scale, you obtain a white, saturated image.
I would therefore suggest:
caxis auto
after imshow to fix the display problem.
Best,
Hi all I have a stack of images of fluorescent labeled particles that are moving through time. The imagestack is gray scaled.
I computed a maximum intensity projection by taking the maximum of the image stack in the 3rd dimension.
Example:
ImageStack(x,y,N) where N = 31 image frames.
2DProjection = max(ImageStack,[],3)
Now, since the 2D projection image is black and white, I was hoping to assign a color gradient so that I can get a sense of the flow of particles through time. Is there a way that I can overlay this image with color, so that I will know where a particle started, and where it ended up?
Thanks!
You could use the second output of max to get which frame the particular maximum came from. max returns an index matrix which indicates the index of each maximal value, which in your case will be the particular frame in which it occurred. If you use this with the imagesc function, you will be able to plot how the particles move with time. For instance:
ImageStack(x,y,N) where N = 31 image frames.
[2DProjection,FrameInfo] = max(ImageStack,[],3);
imagesc(FrameInfo);
set(gca,'ydir','normal'); % Otherwise the y-axis would be flipped
You can sum up bright pixels of each image with one another after coloring each image. This way you will have mixed colors on overlapped areas which you will miss using max function. Although I like the previous answer more than mine.
hStep = 1/N;
currentH = 0;
resultImage = uint8(zeros(x,y,3));
for i = 1 : N
rgbColor = hsv2rgb(currentH,1,0.5);
resultImage(:,:,1) = resultImage(:,:,1) + im(:,:,i) * rgbColor(1);
resultImage(:,:,2) = resultImage(:,:,2) + im(:,:,i) * rgbColor(2);
resultImage(:,:,3) = resultImage(:,:,3) + im(:,:,i) * rgbColor(3);
currentH = currentH + hStep;
end