-
Notifications
You must be signed in to change notification settings - Fork 0
/
test19.py
117 lines (80 loc) · 3.32 KB
/
test19.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
image_apa = "data/O/5a0d5b5b66079.jpg"
img = np.array(Image.open(image_apa).convert('L').resize((120,120)))
# plt.imshow(img)
# plt.show()
def conv2d(input_image, kernel, stride=1, padding=0):
if padding > 0:
input_image = np.pad(input_image, ((padding, padding), (padding, padding)), mode='constant')
input_height, input_width = input_image.shape
kernel_height, kernel_width = kernel.shape
output_height = (input_height - kernel_height) // stride + 1
output_width = (input_width - kernel_width) // stride + 1
output = np.zeros((output_height, output_width))
for y in range(0, output_height):
for x in range(0, output_width):
region = input_image[y*stride:y*stride+kernel_height, x*stride:x*stride+kernel_width]
opa = region * kernel
output[y, x] = np.sum(opa)
return output
def conv2D_drev(input_image, kernel, gradient, stride=1, padding=0, lerning_rate = 0.01):
if padding > 0:
input_image = np.pad(input_image, ((padding, padding), (padding, padding)), mode='constant')
kernel_height, kernel_width = kernel.shape
gradient_height, gradient_width = gradient.shape
filter_gradient = np.zeros((kernel_height, kernel_width))
for y in range(0, gradient_height):
for x in range(0, gradient_width):
region = input_image[y:y+kernel_height, x:x+kernel_width]
filter_gradient += region * gradient[y,x]
new_kernel = kernel - lerning_rate * filter_gradient
return new_kernel
def poolingMax(input_image, steps=2):
input_height, input_width = input_image.shape
output_height = (input_height - steps) // steps + 1
output_width = (input_width - steps) // steps + 1
output_image = np.zeros((output_height, output_width))
for y in range(0, output_height):
for x in range(0, output_width):
region = input_image[y*steps:y*steps+steps, x*steps:x*steps+steps]
opa = np.max(region)
output_image[y, x] = opa
return output_image
def poolimgMax_drev(input_image, steps=2):
input_height, input_width = input_image.shape
find = 1
useg_height = (input_height - steps) // steps + 1
useg_width = (input_width - steps) // steps + 1
output_image = np.zeros((input_height, input_width))
for y in range(0, useg_height):
for x in range(0, useg_width):
region = input_image[y*steps:y*steps+steps, x*steps:x*steps+steps]
maxy = np.max(region)
wer = np.where(region == maxy)
wer_list = list(zip(wer[0], wer[1]))
output_image[y*steps+wer_list[0][0], x*steps+wer_list[0][1]] = find
find += 1
return output_image
def Flatting(img):
return np.array(img).flatten()
def relu(x):
return np.where(x >= 0, x, 0)
input_image = np.array([[1, 2, 3, 0],
[0, 1, 2, 3],
[3, 0, 1, 1],
[2, 1, 0, 2]])
kernel = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
gradient = np.array([[2,2],
[2,2]])
output = conv2d(input_image, kernel, stride=1, padding=1)
print(output)
hopa = relu(output)
print(hopa)
ara = poolingMax(hopa)
print(ara)
flating = Flatting(ara)
print(flating)