[cv]edge detection: gradients

usually, filter is used to find a specific pattern in the pictures.
If we want to find lines and edges in the picture, firstly we think about gradients.
画图的时候,光画出线条就可以表达很多信息。
不要轻视线条,其中包含了太多的信息。

edges

线条的成因:
1. 物体表面的不连续,形状变化
2. 表面颜色不连续
3. 光照强度不连续
4. depth discontinuity,物体与背景的差异
change-boundary

detection edges

recall images as functions.
each location (x,y) has a value.
imageasfunctions
in the above picture, edges like the steep cliffs.
so, our basic idea: find a neighborhood with strong signs of change.
But, there are two problems:
1. neighborhood size
2. how to detect changes

derivative and edges

derivative and edges
differential operator
differential operator
image gradient
image-gradient

the gradient direction is given by : tan1(fy/fx)
The amount of change is given by the gradient magnitude: f=(fx)2+(fy)2
这里写图片描述

finite difference

fxf(x+1,y)f(x,y)1
it was called right derivative.
finite-difference

differential operator

discrete gradient

sobel operator

sobel-operator
in matlab, there is a function imgradientxy which uses sobel operator but isn’t divided by 8.
So you need add a step to get that output divided by 8.

imgradientxy - Directional gradients of an image
This MATLAB function returns the directional gradients, Gx and Gy, the same size
as the input image I.
[Gx,Gy] = imgradientxy(I)
[Gx,Gy] = imgradientxy(I,method)
[gpuarrayGx,gpuarrayGy] = imgradientxy(gpuarrayI,_)

sobel-operator0
well-known-gradient

filter = fspecial('sobel'); % y direction
res = imfilter(double(img), filter);
imagesc(res);
colormap gray;
imshow(res);

corr-gradient

imfilter function use correlation by default.

% Gradient Direction
function result = select_gdir(gmag, gdir, mag_min, angle_low, angle_high)
    % TODO Find and return pixels that fall within the desired mag, angle range
    result = gmag>= mag_min & gdir >= angle_low & gdir <= angle_high;
endfunction

pkg load image;

%% Load and convert image to double type, range [0, 1] for convenience
img = double(imread('octagon.png')) / 255.; 
imshow(img); % assumes [0, 1] range for double images

%% Compute x, y gradients
[gx gy] = imgradientxy(img, 'sobel'); % Note: gx, gy are not normalized

%% Obtain gradient magnitude and direction
[gmag gdir] = imgradient(gx, gy);
imshow(gmag / (4 * sqrt(2))); % mag = sqrt(gx^2 + gy^2), so [0, (4 * sqrt(2))]
imshow((gdir + 180.0) / 360.0); % angle in degrees [-180, 180]

%% Find pixels with desired gradient direction
my_grad = select_gdir(gmag, gdir, 1, 30, 60); % 45 +/- 15
imshow(my_grad);  % NOTE: enable after you've implemented select_gdir

octagon

octagon-mag

octagon-direct

select

real-world
smooth
2nd-derivative-find-peak

linear-property

### 回答1: 在使用TensorFlow时,有时会出现“tensorflow: gradients do not exist for variables”(变量的梯度不存在)的错误提示,原因是在定义模型时没有正确地设置梯度计算方法。解决这个问题需要以下步骤: 1. 确保定义模型时使用的变量可被优化。通常情况下,变量需要设置trainable=True才可以被优化。 2. 使用tf.GradientTape记录计算图中的操作,以便TensorFlow可以自动计算梯度。需要记录的操作包括损失函数计算、变量更新等。 3. 确保损失函数是可微分的,否则无法计算梯度。通常情况下,使用TensorFlow提供的损失函数可以避免这个问题。 4. 如果出现求导错误的情况,可以先检查损失函数是否正确设置,或者使用tf.debugging.check_numerics检查计算过程中是否存在NaN或inf值。 总之,遵循这些步骤可以避免“tensorflow: gradients do not exist for variables”的错误提示,确保模型训练能够正常进行。 ### 回答2: 在使用 TensorFlow 进行深度学习模型训练的过程中,有时候会出现 “tensorflow:gradients do not exist for variables” 的错误提示。这种问题通常是在神经网络梯度反向传播求导时出现的,可能是因为某些变量没有被正确传递,导致模型在反向传播时找不到相应的梯度。 为了解决这个问题,我们需要对模型中的各个变量进行检查和修正。具体步骤如下: 1. 检查代码 首先,需要确保自己的代码没有错误。可以检查代码中是否有未定义的变量或未声明的函数等。 2. 检查模型结构 模型结构不正确也可能导致梯度不存在的错误。可以检查模型的 input 和 layers 是否正确,卷积神经网络和循环神经网络模型可能需要特殊处理。 3. 检查优化器 优化器可能在传递变量时出现问题。可以检查优化器的参数是否正确,如学习率等。 4. 检查损失函数 损失函数对于模型训练至关重要,如果损失函数计算不正确,可能导致梯度不存在的错误。可以使用 TensorFlow 的计算图检查工具来检查损失函数计算是否正确。 5. 检查数据 数据不完整或格式不正确也可能导致错误。可以检查数据读取的代码是否正确,数据格式是否正确等。 总之,要排除 "tensorflow:gradients do not exist for variables" 错误,需要认真检查代码、模型结构、优化器、损失函数和数据等方面的问题,并逐一解决。平时在写代码的时候,可以多用 TensorFlow 提供的调试工具,如 TensorBoard 等,帮助我们更快地找到出错的位置,从而更快地修复问题。 ### 回答3: 在使用 TensorFlow 进行神经网络训练时,可能会出现 "tensorflow:gradients do not exist for variables" 的错误提示信息。这是因为在计算梯度的过程中,某些变量的梯度无法被计算得到。 为了解决这个问题,我们需要进行以下步骤: 1. 检查代码中是否存在错误。首先,需要检查代码中是否存在语法错误或者逻辑错误,及时排除。 2. 确认是否设置了必要的变量。在使用 TensorFlow 进行计算时,需要设置必要的变量,并对其进行初始化。如果某些变量没有被正确设置或初始化,会导致梯度计算出错。 3. 确认是否合理地设置了损失函数。在训练神经网络时,需要选择合适的损失函数,并正确地设置其参数,否则也会导致梯度计算出错。 4. 利用 tf.clip_by_global_norm 函数进行梯度剪裁。在训练神经网络时,由于梯度可能会非常大,可能会导致数值不稳定。为了避免这种情况,我们可以使用 tf.clip_by_global_norm 函数对梯度进行剪裁,保证其大小在一定范围内。 5. 使用更低的学习率进行训练。如果训练过程中出现了 "tensorflow:gradients do not exist for variables" 的错误提示,可能是因为学习率过高。我们可以尝试降低学习率,再进行训练。 总之,"tensorflow:gradients do not exist for variables" 的错误提示是在神经网络训练过程中常见的错误。我们需要仔细排查代码中可能存在的问题,并采取相应的措施来修复这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值