也可以看看:
在先前的文章中,仅当原始层的每个神经元与先前层的所有神经元都有连接时,才使用神经网络层的一种类型-密集,完全连接。
例如,要处理24x24的黑白图像,我们必须将图像的矩阵表示形式转换为包含24x24 = 576个元素的向量。可以想象,通过这样的变换,我们失去了一个重要的属性-像素在轴的垂直和水平方向上的相对位置,而且,在大多数情况下,位于图像左上角的像素可能几乎对像素不具有逻辑上可解释的影响。右下角。
为了消除这些缺点,将卷积层(CNN)用于图像处理。
CNN的主要目的是从原始图像中提取包含辅助(特征)特征(特征)(例如边缘,轮廓,弧或面)的小部分。在下一个处理级别,可以从这些边缘识别出纹理(圆形,正方形等)的更复杂的可重复片段,然后将其折叠为甚至更复杂的纹理(面部,车轮等的一部分)。
例如,考虑一个经典问题-数字图像识别。每个数字都有自己的一组典型数字(圆圈,线)。同时,每个圆或直线可以由较小的边缘组成(图1)
1. (convolutional layer)
CNN ( ), c () , . – CNN – .
, 2x2 ( K) , 2x2 ( N), :
, .
, (fully-connected, dense layers):
, - , – - , ( ).
2. , , , .
(kernel size) – 3, 5, 7.
(kernel) [kh, kw], [nh, nw], ( 3):
, . , . , .
, – (padding). , . , ph pw , :
, , , :
- . , (stride). – (stride).
, sw, sh, :
, ( – ). (). , (CONV1) 9x9x1 ( – - ), 2 1x1 (stride) (padding) , , . 9x9x2 2 – (. 6). CONV2 , , 2x2, , 2, 2x2x2. (CONV2) 9x9x4, 4 – .
, kw kh , nw x nh x nd, nd - , , kw x kh x nd ( 6, CONV2).
7 , RGB, 3x3. , (3 ), 3x3x3.
TensorFlow.js
, : tf.layers.conv2d, – , :
- filter – number –
- kernelSize – number | number[] – , number, , –
- strides – number | number[] - , [1,1], .
- padding – ‘same’, ‘valid’ – , ‘valid’
.
'same'
, , () (stride) . , - 11 , – 5, 13/5=2.6, – 3 ( 8).
stride=1, ( 9), , ( 8).
'valid'
, strides , 8.
TensorFlow.js
, . :
- :
- :
, , tf.browser.fromPixels. , img canvas .
<img src="./sources/itechart.png" alt="Init image" id="target-image"/>
<canvas id="output-image-01"></canvas>
<script>
const imgSource = document.getElementById('target-image');
const image = tf.browser.fromPixels(imgSource, 1);
</script>
, , , 3x3, “same” ‘relu’:
const model = tf.sequential({
layers: [
tf.layers.conv2d({
inputShape: image.shape,
filters: 1,
kernelSize: 3,
padding: 'same',
activation: 'relu'
})
]
});
[NUM_SAMPLES, WIDTH, HEIGHT,CHANNEL], tf.browser.fromPixel [WIDTH, HEIGHT, CHANNEL], – ( , ):
const input = image.reshape([1].concat(image.shape));
. , setWeights Layer, :
model.getLayer(null, 0).setWeights([
tf.tensor([
1, 1, 1,
0, 0, 0,
-1, -1, -1
], [3, 3, 1, 1]),
tf.tensor([0])
]);
, , 0-255, NUM_SAMPLES:
const output = model.predict(input);
const max = output.max().arraySync();
const min = output.min().arraySync();
const outputImage = output.reshape(image.shape)
.sub(min)
.div(max - min)
.mul(255)
.cast('int32');
canvas, tf.browser.toPixels:
tf.browser.toPixels(outputImage, document.getElementById('output-image-01'));
:
2. (pooling layer)
, ( ), , . , , (pooling layer, subsample layer), . MaxPooling .
, .
. (kernel) , (stride) 1x1, . , (. 10).
, 4x4, 2x2 (stride) , 2x2, .
, ( 11) . , , MaxPooling . (translation invariance). , , 50%. , , MaxPooling .
, .
, , – (stride).
MaxPooling AveragePooling, , , . , MaxPooling. AveragePooling , , MaxPooling .
TensorFlow.js (pooling layer)
tf.layers.maxPooling2d tf.layers.averagePooling2d. – , :
- poolSize -号| number [] -过滤器的尺寸,如果指定了number,则过滤器的尺寸为正方形;如果将其指定为数组,则高度和宽度可能会有所不同
-大步前进-数字| number []是前进步骤,是一个可选参数,默认情况下与指定的poolSize具有相同的尺寸。
-填充- “同”,“有效” -设置零填充,默认为“有效”