Pooling Layer

ChangSeong Yooยท2023๋…„ 8์›” 1์ผ
0

Machine Learning

๋ชฉ๋ก ๋ณด๊ธฐ
6/7
post-thumbnail

๐Ÿ“์ด ํฌ์ŠคํŠธ๋Š” Pooling Layer ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.




์ •์˜

Pooling Layer๋ž€ Convolution Layer์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ๋œ Feature map์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋ฉด๋ฉด์„œ๋„ ํŠน์ง•์„ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ด๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ Convolution Layer์€ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ Feature map์˜ non-linearity ์„ฑ์งˆ์„ ์ฃผ๊ธฐ ์œ„ํ•ด ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.

๊ทธ ๋‹ค์Œ Pooling Layer๊ฐ€ ์—ฐ์‚ฐ๋ฉ๋‹ˆ๋‹ค.




๋ชฉ์ 

  1. ํ•ฉ์„ฑ๊ณฑ์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ Feature map ํฌ๊ธฐ ์ค„์—ฌ์„œ ์‹ ๊ฒฝ๋ง์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์—ฐ์‚ฐ๋Ÿ‰์„ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ
  2. ์‹ ๊ฒฝ๋ง์— ๋ฏธ์„ธํ•œ ๋ณ€ํ™”๊ฐ€ ์žˆ์–ด๋„ ๋ฏผ๊ฐํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ (Average Pooling)
  3. local์˜ ์žก์Œ์„ ์ œ๊ฑฐํ•˜๊ณ  ์ค‘์š”ํ•œ ํŠน์ง•์„ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ




์›๋ฆฌ

Max Pooling

  • Pooling Filter ๋‚ด์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ Feature๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ local์—์„œ ๊ฐ€์žฅ ํฐ Feature๋ฅผ ์ถ”์ถœํ•˜๋ฉด์„œ Feature map์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ํฐ Feature๋ฅผ ์ถ”์ถœํ•˜๋ฉด์„œ๋„ ์ถ”์ถœ๋œ ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๋œ ์ค‘์š”ํ•œ ๊ฐ’๋“ค(์žก์Œ)์„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Average Pooling

  • Pooling Filter ๋‚ด์˜ Feature๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ locald์—์„œ ํ‰๊ท ์„ ๊ตฌํžˆ์—ฌ ์ƒˆ๋กœ์šด Feature map์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ํ‰๊ท ์„ ๊ตฌํ•ด low-frequency ๋‚˜ high-frequency ๋“ค์„ ๊ฐ์‡ ์‹œํ‚ค๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.




์ฝ”๋“œ ์ดํ•ด

  • Max Pooling

def max_pooling(input, pooling_size):
    # ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ํ’€๋ง ์˜์—ญ ํฌ๊ธฐ
    input_height, input_width = input.shape
    pool_height, pool_width = pooling_size

    # ํ’€๋ง ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด ์ƒ์„ฑ
    out_height, out_width = input_height // pool_height, input_width // pool_width

    output = np.zeros((out_height, out_width))

    # Max pooling ์ˆ˜ํ–‰
    for i in range(out_height):
        for j in range(out_width):
            # ํ’€๋ง ์˜์—ญ์—์„œ ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ
            local = input[i*pool_height:(i+1)*pool_height, j*pool_width:(j+1)*pool_width]
            output[i, j] = np.max(local)

    return output

์œ„ ์ฝ”๋“œ๋Š” Max Pooling์„ low-level ์—์„œ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ์ด์ œ input ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Max Pooling ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฒฐ์ •ํ•ด์ค๋‹ˆ๋‹ค.

columns, rows = 8, 8

pooling_filter = (2, 2)

input_array = np.zeros(64).reshape(columns, rows)

for i in range(columns):
  for j in range(rows):
    input_array[i][j] = np.random.randint(1, 100)

print(input_array)

๐Ÿ’ป ๊ฒฐ๊ณผ :

[[47. 95. 30. 66. 32. 16. 71. 54.]
 [70. 28. 25. 66. 34. 81. 86. 50.]
 [25. 71. 55. 38. 76. 18. 27. 55.]
 [51. 11. 84. 73. 11. 71. 46. 20.]
 [27. 56.  3. 28. 59. 68. 72. 60.]
 [85. 47. 53. 92. 59. 89. 41. 54.]
 [42.  2. 90. 81.  8.  7. 73. 91.]
 [91. 48. 35.  6. 81. 78. 24.  4.]]

ํ•จ์ˆ˜์— ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ

output_array = max_pooling(input_array, pooling_filter)

๐Ÿ’ป ๊ฒฐ๊ณผ :

[[95. 66. 81. 86.]
 [71. 84. 76. 55.]
 [85. 92. 89. 72.]
 [91. 90. 81. 91.]]

๊ฐ ํ’€๋ง ํ•„ํ„ฐ๊ฐ€ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Average Pooling

def average_pooling(input, pooling_size):
    # ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ํ’€๋ง ์˜์—ญ ํฌ๊ธฐ
    input_height, input_width = input.shape
    pool_height, pool_width = pooling_size

    # ํ’€๋ง ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด ์ƒ์„ฑ
    out_height, out_width = input_height // pool_height, input_width // pool_width

    output = np.zeros((out_height, out_width))

    # Average pooling ์ˆ˜ํ–‰
    for i in range(out_height):
        for j in range(out_width):
            # ํ’€๋ง ์˜์—ญ์—์„œ ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ
            local = input[i*pool_height:(i+1)*pool_height, j*pool_width:(j+1)*pool_width]
            output[i, j] = np.mean(local)

    return output

์œ„ ์ฝ”๋“œ๋Š” Average Pooling์„ low-level ์—์„œ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
input์™€ Pooling ์‚ฌ์ด์ฆˆ๋Š” Max Pooling ๋•Œ ์ผ๋˜ ๋ณ€์ˆ˜ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

output_array = average_pooling(input_array, pooling_filter)

print(output_array)

๐Ÿ’ป ๊ฒฐ๊ณผ :

[[60.   46.75 40.75 65.25]
 [39.5  62.5  44.   37.  ]
 [53.75 44.   68.75 56.75]
 [45.75 53.   43.5  48.  ]]

๊ฐ ํ’€๋ง ํ•„ํ„ฐ๊ฐ€ ์ˆœํšŒํ•˜๋ฉด์„œ ํ•„ํ„ฐ ๋‚ด์˜ ๋ชจ๋“  ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•ด ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

profile
๐’ฅ๐“Š๐“ƒ๐’พโ„ด๐“‡ ๐’Ÿ๐’ถ๐“‰๐’ถ ๐’ฎ๐’ธ๐’พโ„ฏ๐“ƒ๐“‰๐’พ๐“ˆ๐“‰

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 8์›” 1์ผ

์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ž์ฃผ ์˜ฌ๊ฒŒ์š” :)

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ