def __init__(self, *args, mask='B', **kwargs):
super(MaskedConvolution2D, self).__init__(
*args, **kwargs
)
Cout, Cin, kh, kw = self.W.shape
pre_mask = self.xp.ones_like(self.W.data).astype('f')
yc, xc = kh // 2, kw // 2
# context masking - subsequent pixels won't hav access to next pixels (spatial dim)
pre_mask[:, :, yc+1:, :] = 0.0
pre_mask[:, :, yc:, xc+1:] = 0.0
# same pixel masking - pixel won't access next color (conv filter dim)
def bmask(i_out, i_in):
cout_idx = np.expand_dims(np.arange(Cout) % 3 == i_out, 1)
cin_idx = np.expand_dims(np.arange(Cin) % 3 == i_in, 0)
a1, a2 = np.broadcast_arrays(cout_idx, cin_idx)
return a1 * a2
for j in range(3):
pre_mask[bmask(j, j), yc, xc] = 0.0 if mask == 'A' else 1.0
pre_mask[bmask(0, 1), yc, xc] = 0.0
pre_mask[bmask(0, 2), yc, xc] = 0.0
pre_mask[bmask(1, 2), yc, xc] = 0.0
self.mask = pre_mask
评论列表
文章目录