def __call__(self, chars):
xp = self.xp
if not isinstance(chars, (tuple, list)):
chars = [chars]
lengths = [len(_chars) for _chars in chars]
n_words = len(lengths)
pad_width = self._pad_width
char_ids = F.PadSequence(length=max(lengths) + pad_width,
padding=self._pad_id).forward(chars)[0]
left_pads = xp.full((n_words, pad_width), self._pad_id, xp.int32)
char_ids = xp.concatenate((left_pads, xp.array(char_ids)), axis=1)
"""note: cupy does not have `inf`."""
mask = xp.full(char_ids.shape, np.inf)
for i, length in enumerate(lengths):
mask[i, pad_width:pad_width + length] = 0
mask = xp.expand_dims(mask, axis=2)
xs = self.embed(char_ids)
xs = F.dropout(xs, self._dropout)
C = self.conv(F.expand_dims(xs, axis=1))
C = F.transpose(F.squeeze(C, axis=3), (0, 2, 1))
"""
assert C.shape == (n_words,
pad_width + max(lengths) + pad_width,
self.out_size)
"""
ys = F.max(C - mask, axis=1)
return ys
python类transpose()的实例源码
def __call__(self, x):
r = self.r
out = self.conv(x)
batchsize = out.shape[0]
in_channels = out.shape[1]
out_channels = int(in_channels / (r ** 2))
in_height = out.shape[2]
in_width = out.shape[3]
out_height = in_height * r
out_width = in_width * r
out = F.reshape(out, (batchsize, r, r, out_channels, in_height, in_width))
out = F.transpose(out, (0, 3, 4, 1, 5, 2))
out = F.reshape(out, (batchsize, out_channels, out_height, out_width))
return out
predictive_autoencoder.py 文件源码
项目:Multitask-and-Transfer-Learning
作者: AI-ON
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def predicted_image(self):
# The transpose is because OpenAI gym and chainer have
# different depth conventions
return F.transpose(self._predicted_image[0])
predictive_autoencoder.py 文件源码
项目:Multitask-and-Transfer-Learning
作者: AI-ON
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def error_mask(self):
return F.transpose(self.attention_mask)
def process_image(raw_image):
floated = raw_image.astype('float32') / 255.0
transposed = F.transpose(floated)
expanded = F.expand_dims(transposed, 0) # Make a "batch size" of 1
return expanded
def __call__(self, orig_img, input_size=None):
xp = self.model.xp
orig_input_width, orig_input_height = orig_img.size
if input_size is not None:
img = orig_img.resize(input_size, Image.BILINEAR)
else:
img = utils.reshape_to_yolo_size(orig_img)
input_width, input_height = img.size
img = np.asarray(img, dtype=np.float32) / 255.0
img = img.transpose(2, 0, 1)
# forward
x = xp.asarray(img[np.newaxis, :, :, :])
x, y, w, h, conf, prob = self.model.predict(x)
# parse results
_, _, _, grid_h, grid_w = x.shape
x = F.reshape(x, (self.n_boxes, grid_h, grid_w)).data
y = F.reshape(y, (self.n_boxes, grid_h, grid_w)).data
w = F.reshape(w, (self.n_boxes, grid_h, grid_w)).data
h = F.reshape(h, (self.n_boxes, grid_h, grid_w)).data
conf = F.reshape(conf, (self.n_boxes, grid_h, grid_w)).data
prob = F.transpose(F.reshape(prob, (self.n_boxes, self.n_classes, grid_h, grid_w)), (1, 0, 2, 3)).data
x = cuda.to_cpu(x)
y = cuda.to_cpu(y)
w = cuda.to_cpu(w)
h = cuda.to_cpu(h)
conf = cuda.to_cpu(conf)
prob = cuda.to_cpu(prob)
detected_indices = (conf * prob).max(axis=0) > self.detection_thresh
x = x[detected_indices]
y = y[detected_indices]
w = w[detected_indices]
h = h[detected_indices]
conf = conf[detected_indices]
prob = prob.transpose(1, 2, 3, 0)[detected_indices]
results = []
for i in range(detected_indices.sum()):
class_id = prob[i].argmax()
label = self.labels[class_id]
results.append({
'class_id': class_id,
'label': label,
'probs': prob[i],
'conf' : conf[i],
'objectness': conf[i] * prob[i].max(),
'box' : utils.Box(
x[i] * orig_input_width,
y[i] * orig_input_height,
w[i] * orig_input_width,
h[i] * orig_input_height).crop_region(orig_input_height, orig_input_width)
})
# nms
nms_results = utils.nms(results, self.iou_thresh)
return nms_results
yolov2_predict_caltech.py 文件源码
项目:chainer-object-detection
作者: dsanno
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def __call__(self, orig_img, input_size=None):
xp = self.model.xp
orig_input_width, orig_input_height = orig_img.size
if input_size is not None:
img = orig_img.resize(input_size, Image.BILINEAR)
else:
img = utils.reshape_to_yolo_size(orig_img)
input_width, input_height = img.size
img = np.asarray(img, dtype=np.float32) / 255.0
img = img.transpose(2, 0, 1)
# forward
x = xp.asarray(img[np.newaxis, :, :, :])
x, y, w, h, conf, prob = self.model.predict(x)
# parse results
_, _, _, grid_h, grid_w = x.shape
x = F.reshape(x, (self.n_boxes, grid_h, grid_w)).data
y = F.reshape(y, (self.n_boxes, grid_h, grid_w)).data
w = F.reshape(w, (self.n_boxes, grid_h, grid_w)).data
h = F.reshape(h, (self.n_boxes, grid_h, grid_w)).data
conf = F.reshape(conf, (self.n_boxes, grid_h, grid_w)).data
prob = F.transpose(F.reshape(prob, (self.n_boxes, self.n_classes, grid_h, grid_w)), (1, 0, 2, 3)).data
x = cuda.to_cpu(x)
y = cuda.to_cpu(y)
w = cuda.to_cpu(w)
h = cuda.to_cpu(h)
conf = cuda.to_cpu(conf)
prob = cuda.to_cpu(prob)
detected_indices = (conf * prob).max(axis=0) > self.detection_thresh
x = x[detected_indices]
y = y[detected_indices]
w = w[detected_indices]
h = h[detected_indices]
conf = conf[detected_indices]
prob = prob.transpose(1, 2, 3, 0)[detected_indices]
results = []
for i in range(detected_indices.sum()):
class_id = prob[i].argmax()
label = self.labels[class_id]
results.append({
'class_id': class_id,
'label': label,
'probs': prob[i],
'conf' : conf[i],
'objectness': conf[i] * prob[i].max(),
'box' : utils.Box(
x[i] * orig_input_width,
y[i] * orig_input_height,
w[i] * orig_input_width,
h[i] * orig_input_height).crop_region(orig_input_height, orig_input_width)
})
# nms
print len(results)
nms_results = utils.nms(results, self.iou_thresh)
return nms_results
def forward(self, ws, ss, ps, ls, dep_ts=None):
batchsize, slen = ws.shape
xp = chainer.cuda.get_array_module(ws[0])
wss = self.emb_word(ws)
sss = F.reshape(self.emb_suf(ss), (batchsize, slen, 4 * self.afix_dim))
pss = F.reshape(self.emb_prf(ps), (batchsize, slen, 4 * self.afix_dim))
ins = F.dropout(F.concat([wss, sss, pss], 2), self.dropout_ratio, train=self.train)
xs_f = F.transpose(ins, (1, 0, 2))
xs_b = xs_f[::-1]
cx_f, hx_f, cx_b, hx_b = self._init_state(xp, batchsize)
_, _, hs_f = self.lstm_f(hx_f, cx_f, xs_f, train=self.train)
_, _, hs_b = self.lstm_b(hx_b, cx_b, xs_b, train=self.train)
# (batch, length, hidden_dim)
hs = F.transpose(F.concat([hs_f, hs_b[::-1]], 2), (1, 0, 2))
dep_ys = self.biaffine_arc(
F.elu(F.dropout(self.arc_dep(hs), 0.32, train=self.train)),
F.elu(F.dropout(self.arc_head(hs), 0.32, train=self.train)))
if dep_ts is not None and random.random >= 0.5:
heads = dep_ts
else:
heads = F.flatten(F.argmax(dep_ys, axis=2)) + \
xp.repeat(xp.arange(0, batchsize * slen, slen), slen)
hs = F.reshape(hs, (batchsize * slen, -1))
heads = F.permutate(
F.elu(F.dropout(
self.rel_head(hs), 0.32, train=self.train)), heads)
childs = F.elu(F.dropout(self.rel_dep(hs), 0.32, train=self.train))
cat_ys = self.biaffine_tag(childs, heads)
dep_ys = F.split_axis(dep_ys, batchsize, 0) if batchsize > 1 else [dep_ys]
dep_ys = [F.reshape(v, v.shape[1:])[:l, :l] for v, l in zip(dep_ys, ls)]
cat_ys = F.split_axis(cat_ys, batchsize, 0) if batchsize > 1 else [cat_ys]
cat_ys = [v[:l] for v, l in zip(cat_ys, ls)]
return cat_ys, dep_ys
def __call__(self, xs):
"""
xs [(w,s,p,y), ..., ]
w: word, c: char, l: length, y: label
"""
batchsize = len(xs)
ws, cs, ls, ts = zip(*xs)
# cs: [(sentence length, max word length)]
ws = map(self.emb_word, ws)
# ls: [(sentence length, char dim)]
# cs = map(lambda (c, l): F.sum(self.emb_char(c), 1) / l, zip(cs, ls))
# cs = [F.reshape(F.average_pooling_2d(
# F.expand_dims(self.emb_char(c), 0), (l, 1)), (-1, self.char_dim))
# for c, l in zip(cs, ls)]
# before conv: (sent len, 1, max word len, char_size)
# after conv: (sent len, char_size, max word len, 1)
# after max_pool: (sent len, char_size, 1, 1)
cs = [F.squeeze(
F.max_pooling_2d(
self.conv_char(
F.expand_dims(
self.emb_char(c), 1)), (l, 1)))
for c, l in zip(cs, ls)]
# [(sentence length, (word_dim + char_dim))]
xs_f = [F.dropout(F.concat([w, c]),
self.dropout_ratio, train=self.train) for w, c in zip(ws, cs)]
xs_b = [x[::-1] for x in xs_f]
cx_f, hx_f, cx_b, hx_b = self._init_state(batchsize)
_, _, hs_f = self.lstm_f(hx_f, cx_f, xs_f, train=self.train)
_, _, hs_b = self.lstm_b(hx_b, cx_b, xs_b, train=self.train)
hs_b = [x[::-1] for x in hs_b]
# ys: [(sentence length, number of category)]
ys = [self.linear2(F.relu(self.linear1(F.concat([h_f, h_b]))))
for h_f, h_b in zip(hs_f, hs_b)]
# ys = [self.linear2(F.relu(
# self.linear1(
# F.squeeze(
# F.transpose(
# F.relu(self.conv1(
# F.reshape(
# F.concat([h_f, h_b]),
# (1, 1, -1, 2 * self.hidden_dim))), (0, 3, 2, 1))
# )))))
# for h_f, h_b in zip(hs_f, hs_b)]
loss = reduce(lambda x, y: x + y,
[F.softmax_cross_entropy(y, t) for y, t in zip(ys, ts)])
acc = reduce(lambda x, y: x + y,
[F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(ys, ts)])
acc /= batchsize
chainer.report({
"loss": loss,
"accuracy": acc
}, self)
return loss
def __call__(self, xs):
"""Compute loc and conf from feature maps
This method computes :obj:`mb_locs` and :obj:`mb_confs`
from given feature maps.
Args:
xs (iterable of chainer.Variable): An iterable of feature maps.
The number of feature maps must be same as the number of
:obj:`aspect_ratios`.
Returns:
tuple of chainer.Variable:
This method returns two :obj:`chainer.Variable`: :obj:`mb_locs` and
:obj:`mb_confs`.
* **mb_locs**: A variable of float arrays of shape \
:math:`(B, K, 4)`, \
where :math:`B` is the number of samples in the batch and \
:math:`K` is the number of default bounding boxes.
* **mb_confs**: A variable of float arrays of shape \
:math:`(B, K, n\_fg\_class + 1)`.
"""
mb_locs = list()
mb_confs = list()
for i, x in enumerate(xs):
mb_loc = self.loc[i](x)
mb_loc = F.transpose(mb_loc, (0, 2, 3, 1))
mb_loc = F.reshape(mb_loc, (mb_loc.shape[0], -1, 4))
mb_locs.append(mb_loc)
mb_conf = self.conf[i](x)
mb_conf = F.transpose(mb_conf, (0, 2, 3, 1))
mb_conf = F.reshape(
mb_conf, (mb_conf.shape[0], -1, self.n_class))
mb_confs.append(mb_conf)
mb_locs = F.concat(mb_locs, axis=1)
mb_confs = F.concat(mb_confs, axis=1)
return mb_locs, mb_confs
def __call__(self, x):
batchsize = x.data.shape[0]
input_x_width = x.data.shape[3]
if self.dilation == 1:
# perform normal convolution
padded_x = self.padding_1d(x, self.filter_width - 1)
return super(DilatedConvolution1D, self).__call__(padded_x)
# padding
pad = 0
padded_x_width = input_x_width
## check if we can reshape
mod = padded_x_width % self.dilation
if mod > 0:
pad += self.dilation - mod
padded_x_width = input_x_width + pad
## check if height < filter width
height = padded_x_width / self.dilation
if height < self.filter_width:
pad += (self.filter_width - height) * self.dilation
padded_x_width = input_x_width + pad
if pad > 0:
padded_x = self.padding_1d(x, pad)
else:
padded_x = x
# to skip (dilation - 1) elements
padded_x = F.reshape(padded_x, (batchsize, self.in_channels, -1, self.dilation))
# we can remove transpose operation when residual_conv_filter_width is set to the kernel's height
# padded_x = F.transpose(padded_x, (0, 1, 3, 2))
# convolution
out = super(DilatedConvolution1D, self).__call__(padded_x)
# reshape to the original shape
out = F.reshape(out, (batchsize, self.out_channels, 1, -1))
# remove padded elements / add missing elements
cut = out.data.shape[3] - input_x_width
if cut > 0:
out = self.slice_1d(out, cut)
elif cut < 0:
out = self.padding_1d(out, -cut)
return out
def __call__(self, x):
r = self.r
out = self.conv(x)
batchsize = out.shape[0]
in_channels = out.shape[1]
out_channels = in_channels / (r ** 2)
in_height = out.shape[2]
in_width = out.shape[3]
out_height = in_height * r
out_width = in_width * r
out = F.reshape(out, (batchsize, 1, r * r, out_channels * in_height * in_width, 1))
out = F.transpose(out, (0, 1, 3, 2, 4))
out = F.reshape(out, (batchsize, out_channels, in_height, in_width, r, r))
out = F.transpose(out, (0, 1, 2, 4, 3, 5))
out = F.reshape(out, (batchsize, out_channels, out_height, out_width))
return out
# class BatchRenormalization(link.Link):
# def __init__(self, size, decay=0.9, eps=2e-5, rmax=1, dmax=0, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, use_cudnn=True):
# super(BatchNormalization, self).__init__(size, decay=decay, eps=eps, dtype=dtype, use_gamma=use_gamma, use_beta=use_beta, initial_gamma=initial_gamma, initial_beta=initial_beta, use_cudnn=use_cudnn)
# self.add_persistent("r", numpy.zeros(size, dtype=dtype))
# self.add_persistent("d", numpy.zeros(size, dtype=dtype))
# self.rmax = rmax
# self.dmax = dmax
# def __call__(self, x, test=False, finetune=False):
# if hasattr(self, "gamma"):
# gamma = self.gamma
# else:
# with cuda.get_device(self._device_id):
# gamma = variable.Variable(self.xp.ones(self.avg_mean.shape, dtype=x.dtype), volatile="auto")
# if hasattr(self, "beta"):
# beta = self.beta
# else:
# with cuda.get_device(self._device_id):
# beta = variable.Variable(self.xp.zeros(self.avg_mean.shape, dtype=x.dtype), volatile="auto")
# if not test:
# if finetune:
# self.N += 1
# decay = 1. - 1. / self.N
# else:
# decay = self.decay
# func = batch_normalization.BatchNormalizationFunction(
# self.eps, self.avg_mean, self.avg_var, True, decay,
# self.use_cudnn)
# ret = func(x, gamma, beta)
# self.avg_mean[:] = func.running_mean
# self.avg_var[:] = func.running_var
# else:
# # Use running average statistics or fine-tuned statistics.
# mean = variable.Variable(self.avg_mean, volatile="auto")
# var = variable.Variable(self.avg_var, volatile="auto")
# ret = batch_normalization.fixed_batch_normalization(
# x, gamma, beta, mean, var, self.eps, self.use_cudnn)
# return ret