-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn_basic.py
More file actions
93 lines (71 loc) · 2.62 KB
/
cnn_basic.py
File metadata and controls
93 lines (71 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# cnn_basic.py
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
image = np.array([[[[1],[2],[3]],
[[4],[5],[6]],
[[7],[8],[9]]]], dtype = np.float32)
print(image.shape) # (1, 3, 3, 1)
plt.imshow(image.reshape(3,3), cmap = 'Greys')
plt.show()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# conv2d
# image : (1, 3, 3, 1), Filter : (2, 2, 2, 1), stride : (1, 1)
# number of images : 1, 3 * 3 image, color : 1
# padding : VALID
print("image:\n", image)
print(image.shape) # (1, 3, 3, 1)
weight = tf.constant([[[[1.]], [[1.]]],
[[[1.]], [[1.]]]])
print("weight.shape:", weight.shape)
# weight.shape : (2, 2, 1, 1)
# 2 * 2 image, color : 1, filters : 1
conv2d = tf.nn.conv2d(image, weight, strides=[1,1,1,1], padding='VALID')
conv2d_img = conv2d.eval()
print("conv2d_img.shape :" ,conv2d_img.shape) # (1, 2, 2, 1)
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_image in enumerate(conv2d_img):
print(one_image.reshape(2, 2))
plt.subplot(1, 2, i+1)
plt.imshow(one_image.reshape(2, 2), cmap='Greys')
plt.show()
# conv2d
# image : (1, 3, 3, 1), Filter : (2, 2, 2, 1), stride : (1, 1)
# number of images : 1, 3 * 3 image, color : 1
# padding : VALID
print("image:\n", image)
print(image.shape) # (1, 3, 3, 1)
weight = tf.constant([[[[1.]], [[1.]]],
[[[1.]], [[1.]]]])
print("weight.shape:", weight.shape)
# weight.shape : (2, 2, 1, 1)
# 2 * 2 image, color : 1, filters : 1
conv2d = tf.nn.conv2d(image, weight, strides=[1,1,1,1], padding='SAME')
conv2d_img = conv2d.eval()
print("conv2d_img.shape :" ,conv2d_img.shape) # (1, 2, 2, 1)
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_image in enumerate(conv2d_img):
print(one_image.reshape(3, 3))
plt.subplot(1, 2, i+1)
plt.imshow(one_image.reshape(3, 3), cmap='Greys')
plt.show()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# conv2d
# image : (1, 3, 3, 1) ==> output : (1, 3, 3, 3)
print("image:\n", image)
print(image.shape) # (1, 3, 3, 1)
weight = tf.constant([[[[1.]], [[1.]]],
[[[1.]], [[1.]]]])
print("weight.shape:", weight.shape)
# weight.shape : (2, 2, 1, 1)
# 2 * 2 image, color : 1, filters : 1
conv2d = tf.nn.conv2d(image, weight, strides=[1,1,1,1], padding='SAME')
conv2d_img = conv2d.eval()
print("conv2d_img.shape :" ,conv2d_img.shape) # (1, 2, 2, 1)
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_image in enumerate(conv2d_img):
print(one_image.reshape(3, 3))
plt.subplot(1, 2, i+1)
plt.imshow(one_image.reshape(3, 3), cmap='Greys')
plt.show()