forked from junyanz/interactive-deep-colorization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaffe_traininglayers.py
196 lines (150 loc) · 6.12 KB
/
caffe_traininglayers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
import warnings
import os
import caffe
from skimage import color
import color_quantization as cq
# ***************************************
# ***** LAYERS FOR GLOBAL HISTOGRAM *****
# ***************************************
class SpatialRepLayer(caffe.Layer):
'''
INPUTS
bottom[0].data NxCx1x1
bottom[1].data NxCxXxY
OUTPUTS
top[0].data NxCxXxY repeat 0th input spatially '''
def setup(self, bottom, top):
if(len(bottom) != 2):
raise Exception("Layer needs 2 inputs")
self.param_str_split = self.param_str.split(' ')
# self.keep_ratio = float(self.param_str_split[0]) # frequency keep whole input
self.N = bottom[0].data.shape[0]
self.C = bottom[0].data.shape[1]
self.X = bottom[0].data.shape[2]
self.Y = bottom[0].data.shape[3]
if(self.X != 1 or self.Y != 1):
raise Exception("bottom[0] should have spatial dimensions 1x1")
# self.Nref = bottom[1].data.shape[0]
# self.Cref = bottom[1].data.shape[1]
self.Xref = bottom[1].data.shape[2]
self.Yref = bottom[1].data.shape[3]
def reshape(self, bottom, top):
top[0].reshape(self.N, self.C, self.Xref, self.Yref) # output shape
def forward(self, bottom, top):
top[0].data[...] = bottom[0].data[:, :, :, :] # will do singleton expansion
def backward(self, top, propagate_down, bottom):
bottom[0].diff[:, :, 0, 0] = np.sum(np.sum(top[0].diff, axis=2), axis=2)
bottom[1].diff[...] = 0
class BGR2HSVLayer(caffe.Layer):
''' Layer converts BGR to HSV
INPUTS
bottom[0] Nx3xXxY
OUTPUTS
top[0].data Nx3xXxY
'''
def setup(self, bottom, top):
warnings.filterwarnings("ignore")
if(len(bottom) != 1):
raise Exception("Layer should a single input")
if(bottom[0].data.shape[1] != 3):
raise Exception("Input should be 3-channel BGR image")
self.N = bottom[0].data.shape[0]
self.X = bottom[0].data.shape[2]
self.Y = bottom[0].data.shape[3]
def reshape(self, bottom, top):
top[0].reshape(self.N, 3, self.X, self.Y)
def forward(self, bottom, top):
for nn in range(self.N):
top[0].data[nn, :, :, :] = color.rgb2hsv(bottom[0].data[nn, ::-1, :, :].astype('uint8').transpose((1, 2, 0))).transpose((2, 0, 1))
def backward(self, top, propagate_down, bottom):
# no back-prop
for i in range(len(bottom)):
if not propagate_down[i]:
continue
# bottom[i].diff[...] = np.zeros_like(bottom[i].data)
class BGR2LabLayer(caffe.Layer):
''' Layer converts BGR to Lab
INPUTS
bottom[0] Nx3xXxY
OUTPUTS
top[0].data Nx3xXxY
'''
def setup(self, bottom, top):
warnings.filterwarnings("ignore")
if(len(bottom) != 1):
raise Exception("Layer should a single input")
if(bottom[0].data.shape[1] != 3):
raise Exception("Input should be 3-channel BGR image")
self.N = bottom[0].data.shape[0]
self.X = bottom[0].data.shape[2]
self.Y = bottom[0].data.shape[3]
def reshape(self, bottom, top):
top[0].reshape(self.N, 3, self.X, self.Y)
def forward(self, bottom, top):
top[0].data[...] = color.rgb2lab(bottom[0].data[:, ::-1, :, :].astype('uint8').transpose((2, 3, 0, 1))).transpose((2, 3, 0, 1))
def backward(self, top, propagate_down, bottom):
# no back-prop
for i in range(len(bottom)):
if not propagate_down[i]:
continue
# bottom[i].diff[...] = np.zeros_like(bottom[i].data)
class ColorGlobalDropoutLayer(caffe.Layer):
'''
Inputs
bottom[0].data NxCx1x1
Outputs
top[0].data Nx(C+1)x1x1 last channel is whether or not to keep input
first C channels are copied from bottom (if kept)
'''
def setup(self, bottom, top):
if(len(bottom) == 0):
raise Exception("Layer needs inputs")
self.param_str_split = self.param_str.split(' ')
self.keep_ratio = float(self.param_str_split[0]) # frequency keep whole input
self.cnt = 0
self.N = bottom[0].data.shape[0]
self.C = bottom[0].data.shape[1]
self.X = bottom[0].data.shape[2]
self.Y = bottom[0].data.shape[3]
def reshape(self, bottom, top):
top[0].reshape(self.N, self.C + 1, self.X, self.Y) # output mask
def forward(self, bottom, top):
top[0].data[...] = 0
# top[0].data[:,:self.C,:,:] = bottom[0].data[...]
# determine which ones are kept
keeps = np.random.binomial(1, self.keep_ratio, size=self.N)
top[0].data[:, -1, :, :] = keeps[:, np.newaxis, np.newaxis]
top[0].data[:, :-1, :, :] = bottom[0].data[...] * keeps[:, np.newaxis, np.newaxis, np.newaxis]
def backward(self, top, propagate_down, bottom):
0 # backward not implemented
class NNEncLayer(caffe.Layer):
''' Layer which encodes ab map into Q colors
INPUTS
bottom[0] Nx2xXxY
OUTPUTS
top[0].data NxQ
'''
def setup(self, bottom, top):
warnings.filterwarnings("ignore")
if len(bottom) == 0:
raise Exception("Layer should have inputs")
# self.NN = 10.
self.NN = 1.
self.sigma = 5.
self.ENC_DIR = './data/color_bins'
self.nnenc = cq.NNEncode(self.NN, self.sigma, km_filepath=os.path.join(self.ENC_DIR, 'pts_in_hull.npy'))
self.N = bottom[0].data.shape[0]
self.X = bottom[0].data.shape[2]
self.Y = bottom[0].data.shape[3]
self.Q = self.nnenc.K
def reshape(self, bottom, top):
top[0].reshape(self.N, self.Q, self.X, self.Y)
def forward(self, bottom, top):
top[0].data[...] = self.nnenc.encode_points_mtx_nd(bottom[0].data[...], axis=1)
def backward(self, top, propagate_down, bottom):
# no back-prop
for i in range(len(bottom)):
if not propagate_down[i]:
continue
bottom[i].diff[...] = np.zeros_like(bottom[i].data)