Jup*_*Boi 1 python deep-learning keras tensorflow cnn
I know that there are different kinds of Convolutional Filters depending on the job you want to do. ie. Sharpening, blurring, etc. Is there a specific kind we need to use for image classification?
An example CNN used for image classification is provided on the tensorflow website:
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
Run Code Online (Sandbox Code Playgroud)
I realize the convolutional layer is using a 3x3 filter, but how do I know what type of matrix it is? A sharpening one, blurring one, etc? Can any 3X3 be used for image classification?
它不是 Instagram 意义上的“过滤器”。它是一个在输入图像上滑动的矩阵,乘以相应的值,然后将这些值相加。权重是可训练的,因此它们成为特征,即它们成为通过乘法提取最多意义的东西。
如果你初始化一个卷积层,它只是随机值:
import tensorflow as tf
conv = tf.keras.layers.Conv2D(filters=1, kernel_size=3)
conv.build(input_shape=(None, 28, 28, 1))
weights, biases = conv.weights
print(tf.squeeze(weights))
Run Code Online (Sandbox Code Playgroud)
tf.Tensor(
[[ 0.4269786 -0.07291955 0.31100047]
[ 0.05227929 -0.19417122 -0.12701556]
[-0.27614036 -0.12557104 -0.12314937]], shape=(3, 3), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
正如我所说,过滤器是可训练值的矩阵,所以如果你的任务是检测线,你最终可能会得到这样的权重:
tf.eye(3)
Run Code Online (Sandbox Code Playgroud)
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]], dtype=float32)>
Run Code Online (Sandbox Code Playgroud)
下一张图像的输入将是原始图像的扭曲版本,因为它与随机浮点值相乘。
import tensorflow as tf
from skimage import data
import numpy as np
import matplotlib.pyplot as plt
conv = tf.keras.layers.Conv2D(filters=3, kernel_size=3)
image = data.chelsea()
conv.build(input_shape=(None, *image.shape))
plt.imshow(image)
plt.show()
image = np.expand_dims(image, axis=0).astype(np.float32)
for i in range(5):
image = conv(image)
plt.imshow(np.squeeze(np.abs(np.minimum(image, 255))).astype(int))
plt.show()
Run Code Online (Sandbox Code Playgroud)
第二层输入的大小也会稍微小一些,因为图像缩小了(filter_size - 1),除非有填充。
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |