def __call__(self, x, test):
if self.sample=="down" or self.sample=="none" or self.sample=='none-9' or self.sample=='none-7' or self.sample=='none-5':
h = self.c(x)
elif self.sample=="up":
h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
h = self.c(h)
else:
print("unknown sample method %s"%self.sample)
if self.bn:
h = self.batchnorm(h, test=test)
if self.noise:
h = add_noise(h, test=test)
if self.dropout:
h = F.dropout(h, train=not test)
if not self.activation is None:
h = self.activation(h)
return h
python类unpooling_2d()的实例源码
def _do_before_cal(self, x):
if self.nn == 'up_unpooling':
x = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
return x
def __call__(self, x, train=True):
b, c, height, width = x.shape
h = self.conv(F.unpooling_2d(x, 2, outsize=(height * 2, width * 2)))
if self.activate:
h = F.relu(self.bn(h, test=not train))
return h
def __call__(self, x):
h = F.reshape(self.l0(x), ((x.shape[0],) + self.embed_shape))
for i in range(self.n_blocks):
for j in range(self.block_size):
h = F.elu(getattr(self, 'c{}'.format(i*j+j))(h))
if i < self.n_blocks - 1:
h = F.unpooling_2d(h, ksize=2, stride=2, cover_all=False)
return self.ln(h)
def __call__(self, x):
return functions.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def check_forward(self, x_data):
x = chainer.Variable(x_data)
y = functions.unpooling_2d(x, self.ksize, outsize=self.outsize,
cover_all=self.cover_all)
self.assertEqual(y.data.dtype, self.dtype)
y_data = cuda.to_cpu(y.data)
self.assertEqual(self.gy.shape, y_data.shape)
for i in six.moves.range(self.N):
for c in six.moves.range(self.n_channels):
outsize = self.outsize or self.expected_outsize
assert y_data.shape[2:] == outsize
if outsize == (5, 2):
expect = numpy.zeros(outsize, dtype=self.dtype)
expect[:2, :] = self.x[i, c, 0, 0]
expect[2:4, :] = self.x[i, c, 1, 0]
elif outsize == (4, 2):
expect = numpy.array([
[self.x[i, c, 0, 0], self.x[i, c, 0, 0]],
[self.x[i, c, 0, 0], self.x[i, c, 0, 0]],
[self.x[i, c, 1, 0], self.x[i, c, 1, 0]],
[self.x[i, c, 1, 0], self.x[i, c, 1, 0]],
])
elif outsize == (3, 1):
expect = numpy.array([
[self.x[i, c, 0, 0]],
[self.x[i, c, 0, 0]],
[self.x[i, c, 1, 0]],
])
else:
raise ValueError('Unsupported outsize: {}'.format(outsize))
gradient_check.assert_allclose(expect, y_data[i, c])
def __call__(self, input_blob, test_mode=False):
# explicit and very flexible DAG!
#################################
data = input_blob[0]
labels = input_blob[1]
if(len(input_blob) >= 3):
weights_classes = input_blob[2]
else:
weights_classes = chainer.Variable(cuda.cupy.ones((self.classes, 1), dtype='float32'))
# ---- CONTRACTION BLOCKS ---- #
blob_b0 = self.bnorm0(data)
(blob_b1, indices_b1, size_b1) = F.max_pooling_2dIndices(self.bnorm1(F.relu(self.conv1(blob_b0)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
(blob_b2, indices_b2, size_b2) = F.max_pooling_2dIndices(self.bnorm2(F.relu(self.conv2(blob_b1)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
(blob_b3, indices_b3, size_b3) = F.max_pooling_2dIndices(self.bnorm3(F.relu(self.conv3(blob_b2)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
(blob_b4, indices_b4, size_b4) = F.max_pooling_2dIndices(self.bnorm4(F.relu(self.conv4(blob_b3)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
# ---- EXPANSION BLOCKS ---- #
blob_b5 = self.bnorm5(F.relu(self.conv5(F.unpooling_2d(blob_b4, indices_b4, size_b4))), test=test_mode)
blob_b6 = self.bnorm6(F.relu(self.conv6(F.unpooling_2d(blob_b5, indices_b3, size_b3))), test=test_mode)
blob_b7 = self.bnorm7(F.relu(self.conv7(F.unpooling_2d(blob_b6, indices_b2, size_b2))), test=test_mode)
blob_b8 = self.bnorm8(F.relu(self.conv8(F.unpooling_2d(blob_b7, indices_b1, size_b1))), test=test_mode)
#ipdb.set_trace()
# ---- SOFTMAX CLASSIFIER ---- #
self.blob_class = self.classi(blob_b8)
self.probs = F.softmax(self.blob_class)
# ---- CROSS-ENTROPY LOSS ---- #
#ipdb.set_trace()
self.loss = F.weighted_cross_entropy(self.probs, labels, weights_classes, normalize=True)
self.output_point = self.probs
return self.loss
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __call__(self, x):
return functions.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)
def __init__(self, ksize, stride=None, pad=0, outsize=None, cover_all=True):
self._function = "unpooling_2d"
self.ksize = ksize
self.stride = stride
self.pad = pad
self.outsize = outsize
self.cover_all = cover_all
def __call__(self, x):
return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)