Python chainer.functions.stack() Examples
The following are 30
code examples of chainer.functions.stack().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer.functions
, or try the search function
.
Example #1
Source File: test_inout.py From chainer with MIT License | 6 votes |
def test_hook_for_funcnode(self, test_type): class Model(chainer.Chain): def forward(self, x): if test_type in ['variable', 'array']: x = [chainer.as_variable(x)] elif test_type == 'dict': x = list(x.values()) x.append(chainer.Variable(np.array(7, np.float32))) return F.stack(x) model = Model() x = self.get_x(test_type) with RetainInputHook() as h: model(x) expected_count = 1 if test_type == 'array': # input is ndarray and not checked in forward_preprocess expected_count += 1 assert len(h.retain_inputs) == expected_count
Example #2
Source File: set2set.py From chainer-chemistry with MIT License | 6 votes |
def __call__(self, h): # type: (chainer.Variable) -> chainer.Variable xp = cuda.get_array_module(h) mb, node, ch = h.shape # type: int, int, int if self.q_star is None: self.q_star = [ xp.zeros((1, self.in_channels * 2)).astype('f') for _ in range(mb) ] self.hx, self.cx, q = self.lstm_layer(self.hx, self.cx, self.q_star) # self.hx: (mb, mb, ch) # self.cx: (mb, mb, ch) # q: List[(1, ch) * mb] q = functions.stack(q) # q: (mb, 1, ch) q_ = functions.transpose(q, axes=(0, 2, 1)) # q_: (mb, ch, 1) e = functions.matmul(h, q_) # e: (mb, node, 1) a = functions.softmax(e) # a: (mb, node, 1) a = functions.broadcast_to(a, h.shape) # a: (mb, node, ch) r = functions.sum((a * h), axis=1, keepdims=True) # r: (mb, 1, ch) q_star_ = functions.concat((q, r), axis=2) # q_star_: (mb, 1, ch*2) self.q_star = functions.separate(q_star_) return functions.reshape(q_star_, (mb, ch * 2))
Example #3
Source File: network.py From Looking-to-Listen with MIT License | 6 votes |
def loadData(self, num): # ===== Initialize variables ===== # N = self.N M = self.M # ===== Load data ===== # audio_spec = [xp.load(os.path.join(DATA_DIR_SPEC, "{}.npz".format(i)))["mix"].T[xp.newaxis,:,:] for i in num] N_index = xp.random.randint(0,audio_spec[0].shape[1]-N) audio_spec = xp.stack([i[:,N_index:(N_index+N),:] for i in audio_spec]) M_index = int(N_index/4) face1 = [ (xp.array(pd.read_csv(os.path.join(DATA_DIR_VISUAL, "{}/speech1.csv".format(i)), header=None)).T[:,:,xp.newaxis].astype(xp.float32) / 255.) for i in num] face1 = xp.stack([i[:,M_index:(M_index+M),:] for i in face1]) face2 = [ (xp.array(pd.read_csv(os.path.join(DATA_DIR_VISUAL, "{}/speech2.csv".format(i)), header=None)).T[:,:,xp.newaxis].astype(xp.float32) / 255.) for i in num] face2 = xp.stack([i[:,M_index:(M_index+M),:] for i in face2]) true_spec = xp.stack( [xp.load(os.path.join(DATA_DIR_SPEC, "{}.npz".format(i)))["true"].T[N_index:(N_index+N),:] \ for i in num]) true_spec /= ( xp.max(true_spec) * 1.1 ) return audio_spec, face1, face2, true_spec
Example #4
Source File: utils.py From kiss with GNU General Public License v3.0 | 6 votes |
def calc_bboxes(self, predicted_bboxes, image_size, out_size): predicted_bboxes = (predicted_bboxes + 1) / 2 x_points = predicted_bboxes[:, 0, ...] * image_size.width y_points = predicted_bboxes[:, 1, ...] * image_size.height top_left_x = F.get_item(x_points, [..., 0, 0]) top_left_y = F.get_item(y_points, [..., 0, 0]) bottom_right_x = F.get_item(x_points, [..., out_size.height - 1, out_size.width - 1]) bottom_right_y = F.get_item(y_points, [..., out_size.height - 1, out_size.width - 1]) bboxes = F.stack( [ F.minimum(top_left_x, bottom_right_x), F.minimum(top_left_y, bottom_right_y), F.maximum(top_left_x, bottom_right_x), F.maximum(top_left_y, bottom_right_y), ], axis=1 ) return bboxes
Example #5
Source File: test_inout.py From onnx-chainer with MIT License | 6 votes |
def test_hook_for_funcnode(self, test_type): class Model(chainer.Chain): def forward(self, x): if test_type in ['variable', 'array']: x = [chainer.as_variable(x)] elif test_type == 'dict': x = list(x.values()) x.append(chainer.Variable(np.array(7, np.float32))) return F.stack(x) model = Model() x = self.get_x(test_type) with RetainInputHook() as h: model(x) expected_count = 1 if test_type == 'array': # input is ndarray and not checked in forward_preprocess expected_count += 1 assert len(h.retain_inputs) == expected_count
Example #6
Source File: models.py From EEND with MIT License | 6 votes |
def batch_pit_loss(ys, ts, label_delay=0): """ PIT loss over mini-batch. Args: ys: B-length list of predictions ts: B-length list of labels Returns: loss: (1,)-shape mean cross entropy over mini-batch labels: B-length list of permuted labels """ loss_w_labels = [pit_loss(y, t) for (y, t) in zip(ys, ts)] losses, labels = zip(*loss_w_labels) loss = F.sum(F.stack(losses)) n_frames = np.sum([t.shape[0] for t in ts]) loss = loss / n_frames return loss, labels
Example #7
Source File: models.py From EEND with MIT License | 6 votes |
def __call__(self, xs, ts): _, ys, ems = self.forward(xs) # PIT loss loss, labels = batch_pit_loss(ys, ts) reporter.report({'loss_pit': loss}, self) report_diarization_error(ys, labels, self) # DPCL loss loss_dc = F.sum( F.stack([dc_loss(em, t) for (em, t) in zip(ems, ts)])) n_frames = np.sum([t.shape[0] for t in ts]) loss_dc = loss_dc / (n_frames ** 2) reporter.report({'loss_dc': loss_dc}, self) # Multi-objective loss = (1 - self.dc_loss_ratio) * loss + self.dc_loss_ratio * loss_dc reporter.report({'loss': loss}, self) return loss
Example #8
Source File: text_recognizer.py From kiss with GNU General Public License v3.0 | 6 votes |
def __call__(self, rois): batch_size, num_bboxes, num_channels, height, width = rois.shape rois = F.reshape(rois, (-1, num_channels, height, width)) # if not chainer.config.user_text_recognition_grayscale_input: # # convert data to grayscale # assert rois.shape[1] == 3, "rois are not in RGB, can not convert them to grayscale" # r, g, b = F.separate(rois, axis=1) # grey = 0.299 * r + 0.587 * g + 0.114 * b # rois = F.stack([grey, grey, grey], axis=1) h = self.feature_extractor(rois) _, num_channels, feature_height, feature_width = h.shape h = F.average_pooling_2d(h, (feature_height, feature_width)) h = F.reshape(h, (batch_size, num_bboxes, num_channels, -1)) all_predictions = [] for box in F.separate(h, axis=1): # box_predictions = [self.classifier(self.lstm(box)) for _ in range(self.num_chars)] box_predictions = [self.classifier(box) for _ in range(self.num_chars)] all_predictions.append(F.stack(box_predictions, axis=1)) # return shape: batch_size, num_bboxes, num_chars, num_classes return F.stack(all_predictions, axis=2)
Example #9
Source File: irevnet.py From imgclsmob with MIT License | 6 votes |
def inverse(self, y): scale_sqr = self.scale * self.scale batch, y_channels, y_height, y_width = y.shape assert (y_channels % scale_sqr == 0) x_channels = y_channels // scale_sqr x_height = y_height * self.scale x_width = y_width * self.scale x = F.transpose(y, axes=(0, 2, 3, 1)) x = x.reshape(batch, y_height, y_width, scale_sqr, x_channels) d3_split_seq = F.split_axis(x, indices_or_sections=(x.shape[3] // self.scale), axis=3) d3_split_seq = [t.reshape(batch, y_height, x_width, x_channels) for t in d3_split_seq] x = F.stack(d3_split_seq, axis=0) x = F.transpose(F.swapaxes(x, axis1=0, axis2=1), axes=(0, 2, 1, 3, 4)).reshape( batch, x_height, x_width, x_channels) x = F.transpose(x, axes=(0, 3, 1, 2)) return x
Example #10
Source File: text_localizer.py From kiss with GNU General Public License v3.0 | 6 votes |
def predict(self, images, return_visual_backprop=False): with chainer.using_device(self.device): if isinstance(images, list): images = [self.xp.array(image) for image in images] images = self.xp.stack(images, axis=0) visual_backprop = None with chainer.using_config('train', False): roi, bbox = self(images) rois = [roi] bboxes = [bbox] if return_visual_backprop: if not hasattr(self, 'visual_backprop'): self.visual_backprop = VisualBackprop() visual_backprop = self.visual_backprop.perform_visual_backprop(self.visual_backprop_anchors[0]) bboxes = F.stack(bboxes, axis=1) bboxes = F.reshape(bboxes, (-1,) + bboxes.shape[2:]) rois = F.stack(rois, axis=1) rois = F.reshape(rois, (-1,) + rois.shape[2:]) return rois, bboxes, visual_backprop
Example #11
Source File: lstm_text_localizer.py From kiss with GNU General Public License v3.0 | 5 votes |
def get_transform_params(self, features): h = self.pre_transform_params(features) slices = F.split_axis(h, self.num_bboxes_to_localize, axis=1) lstm_predictions = [self.lstm(slice) for slice in slices] lstm_predictions = F.stack(lstm_predictions, axis=1) batch_size, num_boxes, _ = lstm_predictions.shape lstm_predictions = F.reshape(lstm_predictions, (-1,) + lstm_predictions.shape[2:]) params = self.param_predictor(lstm_predictions) transform_params = rotation_dropout(F.reshape(params, (-1, 2, 3)), ratio=self.dropout_ratio) return transform_params
Example #12
Source File: test_inout.py From chainer with MIT License | 5 votes |
def test_hook_for_childlink(self, test_type): # TODO(disktnk): test_type='variable' is failed class ChildModel(chainer.Chain): def forward(self, x, h): if test_type in ['variable', 'array']: h = [chainer.as_variable(h)] elif test_type == 'dict': h = list(h.values()) h.append(x) return F.stack(h) class ParentModel(chainer.Chain): def __init__(self, get_x): super().__init__() self.get_x = get_x with self.init_scope(): self.m = ChildModel() def forward(self, x): h = self.get_x(test_type) return self.m(x, h) model = ParentModel(self.get_x) x = self.get_x('variable') with RetainInputHook() as h: model(x) assert len(h.retain_inputs) == 1
Example #13
Source File: model.py From models with MIT License | 5 votes |
def __call__(self, x): h = F.relu(self.l1(x)) h = F.relu(self.l2(h)) h = F.relu(self.l3(h)) h = F.relu(self.l4(h)) outs = [] for i in range(self.n_task): l = getattr(self, 'task_{}'.format(i)) outs.append(l(h)) return F.stack(outs, axis=1)
Example #14
Source File: test_inout.py From onnx-chainer with MIT License | 5 votes |
def test_hook_for_childlink(self, test_type): # TODO(disktnk): test_type='variable' is failed class ChildModel(chainer.Chain): def forward(self, x, h): if test_type in ['variable', 'array']: h = [chainer.as_variable(h)] elif test_type == 'dict': h = list(h.values()) h.append(x) return F.stack(h) class ParentModel(chainer.Chain): def __init__(self, get_x): super().__init__() self.get_x = get_x with self.init_scope(): self.m = ChildModel() def forward(self, x): h = self.get_x(test_type) return self.m(x, h) model = ParentModel(self.get_x) x = self.get_x('variable') with RetainInputHook() as h: model(x) assert len(h.retain_inputs) == 1
Example #15
Source File: nets.py From dynamic_routing_between_capsules with MIT License | 5 votes |
def output(self, x): batchsize = x.shape[0] n_iters = self.n_iterations gg = self.n_grids * self.n_grids # h1 = F.relu(self.conv1(x)) h1 = F.leaky_relu(self.conv1(x), 0.05) pr_caps = F.split_axis(self.conv2(h1), 32, axis=1) # shapes if MNIST. -> if MultiMNIST # x (batchsize, 1, 28, 28) -> (:, :, 36, 36) # h1 (batchsize, 256, 20, 20) -> (:, :, 28, 28) # pr_cap (batchsize, 8, 6, 6) -> (:, :, 10, 10) Preds = [] for i in range(32): pred = self.Ws[i](pr_caps[i]) Pred = pred.reshape((batchsize, 16, 10, gg)) Preds.append(Pred) Preds = F.stack(Preds, axis=3) assert(Preds.shape == (batchsize, 16, 10, 32, gg)) bs = self.xp.zeros((batchsize, 10, 32, gg), dtype='f') for i_iter in range(n_iters): cs = F.softmax(bs, axis=1) Cs = F.broadcast_to(cs[:, None], Preds.shape) assert(Cs.shape == (batchsize, 16, 10, 32, gg)) ss = F.sum(Cs * Preds, axis=(3, 4)) vs = squash(ss) assert(vs.shape == (batchsize, 16, 10)) if i_iter != n_iters - 1: Vs = F.broadcast_to(vs[:, :, :, None, None], Preds.shape) assert(Vs.shape == (batchsize, 16, 10, 32, gg)) bs = bs + F.sum(Vs * Preds, axis=1) assert(bs.shape == (batchsize, 10, 32, gg)) vs_norm = get_norm(vs) return vs_norm, vs
Example #16
Source File: text_localizer.py From kiss with GNU General Public License v3.0 | 5 votes |
def virtual_box_number_increase(self, boxes, image_shape): image_shape = Size(*image_shape) offset_boxes = [] box_offset_bounds = self.box_offset_side_length // 2 x_box_shifts = self.xp.random.randint(1, 20, size=(self.box_offset_side_length, self.box_offset_side_length)) y_box_shifts = self.xp.random.randint(1, 20, size=(self.box_offset_side_length, self.box_offset_side_length)) for i in range(box_offset_bounds, box_offset_bounds + 1): for j in range(box_offset_bounds, box_offset_bounds + 1): x_shift = boxes[:, 0, :, :] + j * (x_box_shifts[i, j] / image_shape.width) y_shift = boxes[:, 1, :, :] + i * (y_box_shifts[i, j] / image_shape.height) offset_boxes.append(F.stack([x_shift, y_shift], axis=1)) return F.stack(offset_boxes, axis=1)
Example #17
Source File: lstm_text_localizer.py From kiss with GNU General Public License v3.0 | 5 votes |
def get_transform_params(self, features): h = _global_average_pooling_2d(features) lstm_predictions = [self.lstm(h) for _ in range(self.num_bboxes_to_localize)] lstm_predictions = F.stack(lstm_predictions, axis=1) batch_size, num_boxes, _ = lstm_predictions.shape lstm_predictions = F.reshape(lstm_predictions, (-1,) + lstm_predictions.shape[2:]) params = self.param_predictor(lstm_predictions) transform_params = rotation_dropout(F.reshape(params, (-1, 2, 3)), ratio=self.dropout_ratio) return transform_params
Example #18
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y), axis=0) return y1
Example #19
Source File: text_recognizer.py From kiss with GNU General Public License v3.0 | 5 votes |
def decode_prediction(self, prediction): words = [] for box in F.separate(prediction, axis=1): word = [F.argmax(F.softmax(character), axis=1) for character in F.separate(box, axis=1)] words.append(F.stack(word, axis=1)) return F.stack(words, axis=1)
Example #20
Source File: text_recognizer.py From kiss with GNU General Public License v3.0 | 5 votes |
def predict(self, images, return_visual_backprop=False): if isinstance(images, list): images = [self.xp.asarray(image) for image in images] images = self.xp.stack(images, axis=0) with chainer.using_config('train', False): text_recognition_result = self(images) prediction = self.decode_prediction(text_recognition_result) return prediction
Example #21
Source File: utils.py From kiss with GNU General Public License v3.0 | 5 votes |
def calc_loss(self, grids, image_size, **kwargs): num_grids = kwargs.pop('num_grids') corners = self.get_corners(grids, image_size, scale_to_image_size=False) top_left_x, top_right_x, bottom_left_x, bottom_right_x, top_left_y, top_right_y, bottom_left_y, bottom_right_y = corners corner_coordinates = F.stack( [ top_left_x, top_left_y, top_right_x, top_right_y, bottom_left_x, bottom_left_y, bottom_right_x, bottom_right_y ], axis=1 ) corner_coordinates = F.reshape(corner_coordinates, (-1, num_grids, 4, 2)) grids = F.separate(corner_coordinates, axis=1) intersection_areas = [] for box1, box2 in zip(grids, grids[1:]): intersection_areas.append(self.intersection(box1, box2)) loss = F.sum(F.stack(intersection_areas, axis=1)) return loss
Example #22
Source File: model.py From TSNetVocoder with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _loss(self, Y, T, A): _, T = self._norm(None, T) loss = lossfn(F.stack(Y), F.stack(T), F.stack(A), self.fl, self.fftl) return loss
Example #23
Source File: test_function_n_step_lstm.py From chainer with MIT License | 5 votes |
def _stack_weight(ws): # TODO(unno): Input of the current LSTM implementation is shuffled w = F.stack(ws, axis=1) shape = w.shape return F.reshape(w, (shape[0] * shape[1],) + shape[2:])
Example #24
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y)) return y1
Example #25
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y), axis=1) return y1
Example #26
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y), axis=2) return y1 # ======================================
Example #27
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y)) y2 = np.stack([x, y]) return y1, y2
Example #28
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y), axis=1) y2 = np.stack([x, y], axis=1) return y1, y2
Example #29
Source File: Stack.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x, y): y1 = F.stack((x, y), axis=2) y2 = np.stack([x, y], axis=2) return y1, y2 # ======================================
Example #30
Source File: models.py From EEND with MIT License | 5 votes |
def pit_loss(pred, label, label_delay=0): """ Permutation-invariant training (PIT) cross entropy loss function. Args: pred: (T,C)-shaped pre-activation values label: (T,C)-shaped labels in {0,1} label_delay: if label_delay == 5: pred: 0 1 2 3 4 | 5 6 ... 99 100 | label: x x x x x | 0 1 ... 94 95 | 96 97 98 99 100 calculated area: | <------------> | Returns: min_loss: (1,)-shape mean cross entropy label_perms[min_index]: permutated labels """ # label permutations along the speaker axis label_perms = [label[..., list(p)] for p in permutations(range(label.shape[-1]))] losses = F.stack( [F.sigmoid_cross_entropy( pred[label_delay:, ...], l[:len(l) - label_delay, ...]) for l in label_perms]) xp = cuda.get_array_module(losses) min_loss = F.min(losses) * (len(label) - label_delay) min_index = cuda.to_cpu(xp.argmin(losses.data)) return min_loss, label_perms[min_index]