我是卷积方面的菜鸟,我正在使用Python。我正在尝试将一维数组与一维高斯进行卷积,我的数组是
B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]。
高斯的 FWHM 是 5。所以我计算出 sigma 现在5/2.385 = ~2.09,我有 2 个选择:
使用高斯标准方程生成高斯核并使用 np.convolve(array, Gaussian) 我使用的高斯方程
使用scipy.ndimage.gaussian_filter1d
由于两者都是卷积任务,理论上两者应该给出相似的输出。但事实并非如此。为什么会这样呢?
我附上了一张图像,其中绘制了该阵列与另一个等距阵列的关系
A = [1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0]。
数组 (B) 相对于等距数组 (A) 绘制
基本上,我想将convolved array和non-convolved数组一起绘制到 与 之间A。我该怎么做?
我正在尝试使用卷积从图像中提取特征img。
img_copy = np.copy(img)
x = img_copy.shape[0]
y = img_copy.shape[1]
matrix = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]] # convolution matrix
weight = 1
def conv(x, y):
val = 0.0
for row,i in enumerate([-1, 0, 1]):
for col,j in enumerate([-1, 0, 1]):
val = val + img[x+j, y+i]*matrix[row, col]
val = val*weight
return val
for i in range(1, x-1):
for j in range(1, y-1):
pixel = conv(i, j)
if(pixel<0):
pixel = 0
if(pixel>255):
pixel = …Run Code Online (Sandbox Code Playgroud) 我正在尝试为信号实现一维卷积。
它应该具有与以下内容相同的输出:
ary1 = np.array([1, 1, 2, 2, 1])
ary2 = np.array([1, 1, 1, 3])
conv_ary = np.convolve(ary2, ary1, 'full')
>>>> [1 2 4 8 8 9 7 3]
Run Code Online (Sandbox Code Playgroud)
我想出了这个方法:
def convolve_1d(signal, kernel):
n_sig = signal.size
n_ker = kernel.size
n_conv = n_sig - n_ker + 1
# by a factor of 3.
rev_kernel = kernel[::-1].copy()
result = np.zeros(n_conv, dtype=np.double)
for i in range(n_conv):
result[i] = np.dot(signal[i: i + n_ker], rev_kernel)
return result
Run Code Online (Sandbox Code Playgroud)
但我的结果是[8,8]我可能必须对数组进行零填充并更改其索引。
有没有更顺利的方法来达到预期的结果?
Matlab/Octave 使用哪种方法conv2函数?
是吗:
我正在寻找最快的方法conv2。我将为它编写 C 代码。
我的主要目标是证明卷积定理有效(只是提醒一下:卷积定理意味着idft(dft(im) .* dft(mask)) = conv(im, mask)).我正在尝试编程.
这是我的代码:
function displayTransform( im )
% This routine displays the Fourier spectrum of an image.
%
% Input: im - a grayscale image (values in [0,255])
%
% Method: Computes the Fourier transform of im and displays its spectrum,
% (if F(u,v) = a+ib, displays sqrt(a^2+b^2)).
% Uses display techniques for visualization: log, and stretch values to full range,
% cyclic shift DC to center (use fftshift).
% Use showImage to display …Run Code Online (Sandbox Code Playgroud) 在Tensorflow文档中,tf.nn.conv2d-operation被描述为:
[filter_height * filter_width * in_channels, output_channels].[batch, out_height, out_width, filter_height * filter_width * in_channels].是否有适用的操作只是第2步?我在API文档中找不到类似的东西.我可能会用错误的关键字搜索.
我在Ubuntu下成功编译了Caffe,并开始研究如何定义和训练自己的网络。但是,我很难理解卷积层如何产生其输出。例如,LeNet MNIST教程(tutorial,lenet.prototxt)的第二个卷积层(conv2 )具有20个输入图像和50个输出图像:
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何O_0, ..., O_49计算输出图像?我的直觉是,它的工作方式如下(I_i输入图像,K_j内核,B_k偏差,*卷积运算符):
O_0 = I_0 * K_0 + ... + I_19 * K_19 + B_0
O_1 = I_0 * K_20 + ... …Run Code Online (Sandbox Code Playgroud) 我想在这里运行CIFAR10图像分类PyTorch教程- http://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py
我做了一个小的更改,并且我使用了另一个数据集。我有Wikiart数据集中要按艺术家分类的图像(标签=艺术家名称)。
这是网络的代码-
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16*5*5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
Run Code Online (Sandbox Code Playgroud)
然后在代码的这一部分中,我开始训练网络。
for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(wiki_train_dataloader, 0):
inputs, labels = data['image'], data['class']
print(inputs.shape) …Run Code Online (Sandbox Code Playgroud) 我指的是在这个链接https://richliao.github.io/supervised/classification/2016/11/26/textclassifier-convolutional上“使用 CNN 进行文本分类”的实现。在“简化的卷积”一节中,他们使用了以下 keras 层:
Conv1D(128, 5, activation='relu')
根据我的理解,没有参数应该是 5*100*128=64,000。但是模型摘要显示了 64,128 个参数。
有人可以帮助我了解我的计算错误在哪里吗?
我正在使用FFT实现二维卷积。这是我的代码:
img = im2single(imread('dog.bmp'));
filter = fspecial('gaussian', 53, 3);
F = fft2(img);
mask = fft2(filter, size(img, 1), size(img, 2));
filtered_img = ifft2(F .* mask);
imshow(real(filtered_img));
Run Code Online (Sandbox Code Playgroud)
这是原始图像:
为什么会这样?我该如何解决?请帮我。非常感谢。