forked from AxelThevenot/GIF_convolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.py
145 lines (114 loc) · 4.73 KB
/
renderer.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import time
import cv2
import numpy as np
def get_waiting_frame():
# generate a three dot waiting frame in case of a long waiting time
waiting_frame = np.ones((256, 512, 3))
seconds = int(time.time() % 4)
r = 8
for second in range(-1, seconds -1):
dy = r * 5 * second
cv2.circle(waiting_frame, (256 + dy, 128), r, (.2, .6, .6), -1)
return waiting_frame
def get_input_img(inputs, input_shape, G):
# get inputs shape
C_in, H_in, W_in = input_shape
n_by_group_in = int(C_in / G)
# set a padding between the inputs to be visualized
sx = H_in + 1
sy = 0
# compute the image size
img_w = W_in # size of the input
img_w += sy * (n_by_group_in - 1) # number of in -> shifts
img_h = H_in # size of the output
img_h += (sx * (n_by_group_in - 1)) # number of in -> shifts
img_h += 3 * sx * n_by_group_in * (G - 1) // 2 # number of group -> shifts
bg_shape = (img_h, img_w, 3)
# initialize the empty image
img = np.ones(bg_shape)
# set the pixels a their place regargind the shift and coordinate in input
for g, group in enumerate(inputs):
for c, channel in enumerate(group):
y = sy * c
x = 3 * g * sx * n_by_group_in // 2 + (sx * c)
for h, line in enumerate(channel):
for w, pixel in enumerate(line):
img[x+h, y+w] = pixel.color()
pixel.set_coord(x+h, y+w)
return img
def get_output_img(outputs, output_shape, G, shifts=(2, 4)):
# get outputs shape
C_out, H_out, W_out = output_shape
n_by_group_out = int(C_out / G)
# set a padding between the outputs to be visualized
sx = H_out + 1
sy = 0
# compute the image size
img_w = W_out # size of the output
img_w += sy * (n_by_group_out - 1) # number of out -> shifts
img_h = H_out # size of the output
img_h += (sx * (n_by_group_out - 1)) # number of out -> shifts
img_h += 3 * sx * n_by_group_out * (G - 1) // 2 # number of group -> shifts
bg_shape = (img_h, img_w, 3)
# initialize the empty image
img = np.ones(bg_shape)
# set the pixels a their place regargind the shift and coordinate in output
for g, group in enumerate(outputs):
for c, channel in enumerate(group):
y = sy * c
x = 3 * g * sx * n_by_group_out // 2 + (sx * c)
for h, line in enumerate(channel):
for w, pixel in enumerate(line):
img[x+h, y+w] = pixel.color()
pixel.set_coord(x+h, y+w)
return img
def get_kernel_img(kernels, C_in, C_out, K, G):
# get kernels shape
n_by_group_in = int(C_in / G)
n_by_group_out = int(C_out / G)
# set a padding between the kernels to be visualized
sx, sy = K[0] + 1, K[1] + 1
# compute the image size
img_w = K[1] # size of the kernel
img_w += (n_by_group_out - 1) * sy # number of out -> shifts
img_h = K[0] # size of the kernel
img_h += (n_by_group_in - 1) * sx # number of in -> shifts
img_h += sx * ((G - 1) * (n_by_group_in + 1)) # number of group -> shifts
bg_shape = (img_h, img_w, 3)
# initialize the empty image
img = np.ones(bg_shape)
# set the pixels a their place regargind the shift and coordinate in kernels
for g, group in enumerate(kernels):
for c_out, channel_out in enumerate(group):
for c_in, channel in enumerate(channel_out):
y = sy * c_out
x = sx * c_in + g * sx * (n_by_group_in + 1)
for h, line in enumerate(channel):
for w, pixel in enumerate(line):
pcol = np.array(pixel.color())
img[x+h, y+w] = img[x+h, y+w] + pcol
pixel.set_coord(x+h, y+w)
return img
def get_full_img(input_img, kernel_img, output_img, margin=0, pad=(10, 10)):
pad_h, pad_w = pad
# get the max height of the images
H = max(input_img.shape[0], kernel_img.shape[0], output_img.shape[0])
# compute the total width with margin bewteen images
W = input_img.shape[1] + kernel_img.shape[1] + output_img.shape[1]
W = W + 2 * margin
# initialize the full image
full_img = np.ones((H + 2 * pad_h, W + 2 * pad_w, 3))
# place on by one the images by remembering the axis shifts to top left
padding_memory = []
w_shift = pad_w
for img in [input_img, output_img, kernel_img]:
h, w, _ = img.shape # get the shape
# center vertically
top_shift = (H - h) // 2 + pad_h
# place the image
full_img[top_shift:top_shift+h,w_shift:w_shift+w] = img
# actualize
padding_memory.append([top_shift, w_shift])
w_shift += w
w_shift += margin
return full_img, padding_memory