homework 1
programming
1. Figure 1 using convolution kernel, the output feature map
2. Figure 1 using convolution kernel, the output feature map
3. Figure 2 using the convolution kernel, the output feature map
4. Figure 2 using convolution kernel, the output feature map
5. Figure 3 using convolution kernel,
,
, the output feature map
import numpy as np import torch from torch import nn from torch.autograd import Variable from PIL import Image import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # Used to display Chinese labels normally plt.rcParams['axes.unicode_minus'] = False # Used to display the negative sign normally #In the case of Chinese, u' content is required # https://blog.csdn.net/weixin_40123108/article/details/83510592 file_path = 'deer.jpg' im = np.array([[0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255]], dtype='float32') # convert it to a matrix print(im.shape[0], im.shape[1]) plt.imshow(im.astype('uint8'), cmap='gray') # Visualize pictures plt.title('original image') plt.show() im = torch.from_numpy(im.reshape((1, 1, im.shape[0], im.shape[1]))) conv1 = nn.Conv2d(1, 1, (1,2), bias=False) # define convolution sobel_kernel = np.array([[-1,1]], dtype='float32') # Define contour detection operator sobel_kernel = sobel_kernel.reshape((1, 1, 1, 2)) # Fitting the input and output of the convolution conv1.weight.data = torch.from_numpy(sobel_kernel) # assign value to the convolution kernel edge1 = conv1(Variable(im)) # act on the picture x = edge1.data.squeeze().numpy() print(x.shape) # output size plt.imshow(x, cmap='gray') plt.show()
Output result:
1.
2. Implementation code:
import numpy as np import torch from torch import nn from torch.autograd import Variable from PIL import Image import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # Used to display Chinese labels normally plt.rcParams['axes.unicode_minus'] = False # Used to display the negative sign normally #In the case of Chinese, u' content is required # https://blog.csdn.net/weixin_40123108/article/details/83510592 im = np.array([[0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255], [0,0,0,255,255,255]], dtype='float32') # convert it to a matrix print(im.shape[0], im.shape[1]) plt.imshow(im.astype('uint8'), cmap='gray') # Visualize pictures plt.title('original image') plt.show() im = torch.from_numpy(im.reshape((1, 1, im.shape[0], im.shape[1]))) conv1 = nn.Conv2d(1, 1, (1,2), bias=False) # define convolution sobel_kernel = np.array([[-1,1]], dtype='float32') # Define contour detection operator sobel_kernel = sobel_kernel.reshape((1, 1, 2, 1)) # Fitting the input and output of the convolution conv1.weight.data = torch.from_numpy(sobel_kernel) # assign value to the convolution kernel edge1 = conv1(Variable(im)) # act on the picture x = edge1.data.squeeze().numpy() print(x.shape) # output size plt.imshow(x, cmap='gray') plt.show()
Output result:
3.
4.
5. Matrix:
[0,0,0,0,0,0,0,0,0], [0,255,0,0,0,0,0,255,0], [0,0,255,0,0,0,255,0,0], [0,0,0,255,0,255,0,0,0], [0,0,0,0,255,0,0,0,0], [0,0,0,255,0,255,0,0,0], [0,0,255,0,0,0,255,0,0], [0,255,0,0,0,0,0,255,0], [0,0,0,0,0,0,0,0,0],
Output result:
(-1 +1):
(-1 +1)T:
:
Homework 2
1. Concept
convolution:
It is a mathematical operation that generates a third function through two functions f and g. Its essence is a special integral transformation, which represents the integral of the overlapping length of the overlapping part function value product of the flipped and translated functions f and g.
Convolution kernel:
The matrix used in the convolution operation.
Feature map:
What you get when the image pixel values go through the filter is the feature map.
Feature selection:
Feature selection ( Feature Selection ) is also called feature subset selection ( Feature Subset Selection , FSS ), or attribute selection ( Attribute Selection ). It refers to the process of selecting N features from the existing M features to optimize the specific indicators of the system. It is the process of selecting some of the most effective features from the original features to reduce the dimension of the dataset, and improving the performance of the learning algorithm. It is an important method and a key data preprocessing step in pattern recognition. For a learning algorithm, good learning samples are the key to training the model. (Source Baidu, I don't really understand)
Step size:
The step size, as the name suggests, is how many cells the filter moves at a time.
filling:
After convolution, the missing parts are filled.
Feeling field:
In the convolutional neural network, the definition of the receptive field is the size of the area where the pixels on the feature map output by each layer of the convolutional neural network are mapped on the input image. The more popular explanation is that a point on the feature map corresponds to an area on the input map. (source csdn)
2. Explore the role of different convolution kernels
This blurring kernel does not emphasize the difference of adjacent pixel values.
Sobel kernels are used to show the difference in the values of adjacent pixels in a specific direction.
This decorative kernel (similar to the Sobel kernel, and sometimes referred to the same) creates the illusion of depth by emphasizing the differences in pixels in a given direction. In this case, follow a straight line from top left to bottom right.
Meaningless, the image doesn't change.
An overview kernel (also called an "edge" kernel) is used to highlight large differences in pixel values. In the new image, pixels adjacent to adjacent pixels with close to the same intensity will appear black, while pixels adjacent to adjacent pixels that differ greatly will appear white.
This sharp kernel emphasizes differences in adjacent pixel values. This makes the image look more vivid.
The Sobel kernel is used to show only the difference in adjacent pixel values in a specific direction.
3. Programming implementation
1. Realize edge detection, sharpening and blurring of grayscale images. (must do)
Edge detection:
Blur (blur):
Sharpen:
2. Adjust the convolution kernel parameters, test and summarize. (must do)
Test sharpening:
step size 3
step 5
The effect is more pronounced.
The larger the step size, the more obvious the sharpening effect, but the details of the picture will be lost.
3. Use pictures of different sizes, test and summarize.
reduce image size
After comparison, it can be found that the smaller the picture, the more obvious the sharpening effect.
Summarize
This homework is similar to another course - digital image processing, which allows me to understand the concepts of convolution, feature maps and other concepts, and I have benefited a lot. At the same time, I also learned a lot of convolution matrices for image processing. , it may be useful in the future when using software such as photoshop, and the harvest is very large!