반응형

Tensorflow(Keras) Kernel을 만들고 확인해 보자.

 

from keras import layers

conv = layers.Conv2D(filters=4, kernel_size=(3, 3))
print(conv.data_format) # 기본 설정에 따른 결과: channels_last

# Input shape
#4+D tensor with shape: batch_shape + (channels, rows, cols) if data_format='channels_first'
#or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format='channels_last'

input_shape = (5, 28, 28, 1) # (batch_size, rows, cols, channels)
# 5 => batch size (5개의 이미지를 한 번에 사용한다는 의미)
# 28, 28, 1 => 28X28 gray 이미지
# image size, batch size는 kernel 사이즈에 영향이 없다.
# channels는 영향이 있다.
# 결국 kernel_size, channels, filters 인수들에 의해 kernel 사이즈가 결정된다.
# kernel = ((kernel_size), channles, filters)

conv.build(input_shape)
print(conv.kernel)

 

(3, 3, 1, 4) 사이즈의 커널이 만들어진다.

 

간단한 형태의 커널로 바꾸고 직접 커널 값을 세팅해 보자.

 

from keras import layers
import numpy as np

conv = layers.Conv2D(filters=1, kernel_size=(3, 3))
input_shape = (5, 28, 28, 1) # (batch_size, rows, cols, channels)
conv.build(input_shape)

#print(conv.kernel) # 커널만 출력
print(conv.get_weights()) # 커널과 절편 출력

# 커널과 절편 세팅
conv.set_weights([
	np.array([[[[1]], [[2]], [[3]]], [[[4]], [[5]], [[6]]], [[[7]], [[8]], [[9]]]]),
	# 커널 세팅, 4차원(3, 3, 1, 1)
	np.array([7])
	# filters가 1이므로 절편도 1개. 여기서는 절편을 7로 세팅, 1차원(1)
])

#print(conv.kernel)
print(conv.get_weights())

 

 

반응형
Posted by J-sean
: