in #U57fa#U7840#U6559#U7a0b/A2-#U795e#U7ecf#U7f51#U7edc#U57fa#U672c#U539f#U7406/#U7b2c8#U6b65 - #U5377#U79ef#U795e#U7ecf#U7f51#U7edc/src/ch17-CNNBasic/Level0_KnowCNN.py [0:0]
def try_filters(file_name):
img = cv2.imread(file_name)
# cv2 format is:G B R, change it to R G B
img1=img[:,:,[2,1,0]]
#plt.imshow(img2)
#plt.show()
img2 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
batch_size = 1
input_channel = 1
(height, width) = img2.shape
FH = 3
FW = 3
print(img2.shape)
data = img2.reshape((1,1,height,width))
hp = HyperParameters_4_2(
0.1, 10, batch_size,
net_type=NetType.MultipleClassifier,
init_method=InitialMethod.Xavier,
optimizer_name=OptimizerName.Momentum)
conv = ConvLayer((1,height,width), (1,FH,FW), (1,1), hp)
conv.initialize("know_cnn", "name")
filters = [
np.array([0,-1,0,
-1,5,-1,
0,-1,0]), # sharpness filter
np.array([0,0,0,
-1,2,-1,
0,0,0]), # vertical edge
np.array([1,1,1,
1,-9,1,
1,1,1]), # surround
np.array([-1,-2,-1,
0,0,0,
1,2,1]), # sobel y
np.array([0,0,0,
0,1,0,
0,0,0]), # nothing
np.array([0,-1,0,
0,2,0,
0,-1,0]), # horizontal edge
np.array([0.11,0.11,0.11,
0.11,0.11,0.11,
0.11,0.11,0.11]), # blur
np.array([-1,0,1,
-2,0,2,
-1,0,1]), # sobel x
np.array([2,0,0,
0,-1,0,
0,0,-1])] # embossing
filters_name = ["sharpness", "vertical edge", "surround", "sobel y", "nothing", "horizontal edge", "blur", "sobel x", "embossing"]
fig, ax = plt.subplots(nrows=3, ncols=3, figsize=(9,9))
for i in range(len(filters)):
filter = np.repeat(filters[i], input_channel).reshape(batch_size, input_channel,FH,FW)
conv.set_filter(filter, None)
z = conv.forward(data)
#z = normalize(z, 255)
ax[i//3, i%3].imshow(z[0,0])
ax[i//3, i%3].set_title(filters_name[i])
ax[i//3, i%3].axis("off")
plt.suptitle("filters")
plt.show()
return z