Python chainer.functions.separate() Examples

The following are 30 code examples of chainer.functions.separate(). 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: text_recognizer.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
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 #2
Source File: svhn_softmax_metrics.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def calc_loss(self, x, t):
        batch_predictions, _, _ = x

        # concat all individual predictions and slice for each time step
        batch_predictions = F.concat([F.expand_dims(p, axis=0) for p in batch_predictions], axis=0)

        self.xp = cuda.get_array_module(batch_predictions[0], t)
        batch_size = t.shape[0]
        t = F.reshape(t, (batch_size, self.num_timesteps, -1))

        losses = []
        for predictions, labels in zip(F.separate(batch_predictions, axis=0), F.separate(t, axis=1)):
            batch_size, num_chars, num_classes = predictions.shape
            predictions = F.reshape(predictions, (batch_size * num_chars, num_classes))
            labels = F.reshape(labels, (-1,))
            losses.append(F.softmax_cross_entropy(predictions, labels))

        return sum(losses) 
Example #3
Source File: fsns.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def attend(self, encoded_features):
        self.out_lstm.reset_state()
        transformed_encoded_features = F.concat([F.expand_dims(self.transform_encoded_features(feature), axis=1) for feature in encoded_features], axis=1)
        concat_encoded_features = F.concat([F.expand_dims(e, axis=1) for e in encoded_features], axis=1)

        lstm_output = self.xp.zeros_like(encoded_features[0])
        outputs = []
        for _ in range(self.num_labels):
            transformed_lstm_output = self.transform_out_lstm_feature(lstm_output)
            attended_feats = []
            for transformed_encoded_feature in F.separate(transformed_encoded_features, axis=1):
                attended_feat = transformed_encoded_feature + transformed_lstm_output
                attended_feat = F.tanh(attended_feat)
                attended_feats.append(self.generate_attended_feat(attended_feat))

            attended_feats = F.concat(attended_feats, axis=1)
            alphas = F.softmax(attended_feats, axis=1)

            lstm_input_feature = F.batch_matmul(alphas, concat_encoded_features, transa=True)
            lstm_input_feature = F.squeeze(lstm_input_feature, axis=1)
            lstm_output = self.out_lstm(lstm_input_feature)
            outputs.append(lstm_output)
        return outputs 
Example #4
Source File: set2set.py    From chainer-chemistry with MIT License 6 votes vote down vote up
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 #5
Source File: textrec_metrics.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def calc_loss(self, x, t):
        batch_predictions, _, grids = x
        self.xp = cuda.get_array_module(batch_predictions, t)

        loss = self.calc_actual_loss(batch_predictions, None, t)

        # reshape grids
        batch_size = t.shape[0]
        grids = grids[-1]
        grid_shape = grids.shape
        grids = F.reshape(grids, (-1, batch_size) + grid_shape[1:])

        grid_losses = []
        for grid in F.separate(grids, axis=0):
            with cuda.get_device_from_array(getattr(grid, 'data', grid[0].data)):
                grid_losses.append(self.calc_direction_loss(grid))

        return loss + (sum(grid_losses) / len(grid_losses)) 
Example #6
Source File: ExtFunctions_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
def test_separate(self):
        class Test():
            def forward(self):
                F.separate(np.zeros((3, 4, 5)), axis=0)

        id2type = generate_id2type_from_forward(Test(), ())

        self.assertEqual(str(id2type[1]), "class Test -> NoneType")	# FunctionDef forward (line 1)
        self.assertEqual(str(id2type[5]), "NoneType")	# Expr
        self.assertEqual(str(id2type[6]), "(Variable(float64, (4, 5)), Variable(float64, (4, 5)), Variable(float64, (4, 5)))")	# Call F.separate(np.zeros((3, 4, 5)), axis=0) (line 2)
        self.assertEqual(str(id2type[11]), "ndarray(float64, (3, 4, 5))")	# Call np.zeros((3, 4, 5)) (line 2)
        self.assertEqual(str(id2type[16]), "(int, int, int)")	# Tuple (3, 4, 5) (line 2)
        self.assertEqual(str(id2type[17]), "int")	# Num 3 (line 2)
        self.assertEqual(str(id2type[18]), "int")	# Num 4 (line 2)
        self.assertEqual(str(id2type[19]), "int")	# Num 5 (line 2)
        self.assertEqual(str(id2type[22]), "int")	# Num 0 (line 2) 
Example #7
Source File: svhn_bbox_plotter.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def decode_predictions(self, predictions):
        # concat all individual predictions and slice for each time step
        predictions = F.concat([F.expand_dims(p, axis=0) for p in predictions], axis=0)

        words = []
        with cuda.get_device_from_array(predictions.data):
            for prediction in F.separate(predictions, axis=0):
                prediction = F.squeeze(prediction, axis=0)
                prediction = F.softmax(prediction, axis=1)
                prediction = self.xp.argmax(prediction.data, axis=1)
                word = self.loss_metrics.strip_prediction(prediction[self.xp.newaxis, ...])[0]
                if len(word) == 1 and word[0] == 0:
                    return ''

                word = "".join(map(self.loss_metrics.label_to_char, word))
                word = word.replace(chr(self.loss_metrics.char_map[str(self.loss_metrics.blank_symbol)]), '')
                words.append(word)

        text = " ".join(words)
        return text 
Example #8
Source File: bbox_plotter.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def draw_bboxes(self, bboxes, image):
        draw = ImageDraw.Draw(image)
        for i, sub_box in enumerate(F.separate(bboxes, axis=1)):
            for bbox, colour in zip(F.separate(sub_box, axis=0), self.colours):
                bbox.data[...] = (bbox.data[...] + 1) / 2
                bbox.data[0, :] *= self.image_size.width
                bbox.data[1, :] *= self.image_size.height

                x = self.xp.clip(bbox.data[0, :].reshape(self.out_size), 0, self.image_size.width) + i * self.image_size.width
                y = self.xp.clip(bbox.data[1, :].reshape(self.out_size), 0, self.image_size.height)

                top_left = (x[0, 0], y[0, 0])
                top_right = (x[0, -1], y[0, -1])
                bottom_left = (x[-1, 0], y[-1, 0])
                bottom_right = (x[-1, -1], y[-1, -1])

                corners = [top_left, top_right, bottom_right, bottom_left]
                next_corners = corners[1:] + [corners[0]]

                for first_corner, next_corner in zip(corners, next_corners):
                    draw.line([first_corner, next_corner], fill=colour, width=3) 
Example #9
Source File: modeling.py    From models with MIT License 6 votes vote down vote up
def __call__(self, input_ids, input_mask, token_type_ids):
        final_hidden = self.bert.get_sequence_output(
            input_ids,
            input_mask,
            token_type_ids)
        batch_size = final_hidden.shape[0]
        seq_length = final_hidden.shape[1]
        hidden_size = final_hidden.shape[2]

        final_hidden_matrix = F.reshape(
            final_hidden, [batch_size * seq_length, hidden_size])

        logits = self.output(final_hidden_matrix)

        logits = F.reshape(logits, [batch_size, seq_length, 2])
        logits = logits - (1 - input_mask[:, :, None]) * 1000.  # ignore pads
        logits = F.transpose(logits, [2, 0, 1])

        unstacked_logits = F.separate(logits, axis=0)

        (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])
        return (start_logits, end_logits) 
Example #10
Source File: bbox_plotter.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def draw_bboxes(self, bboxes, image):
        if len(bboxes) == 0:
            return
        draw = ImageDraw.Draw(image)
        for i, sub_box in enumerate(F.separate(bboxes, axis=1)):
            for bbox, colour in zip(F.separate(sub_box, axis=0), self.colours()):
                bbox.data[...] = (bbox.data[...] + 1) / 2
                bbox.data[0, :] *= self.image_size.width
                bbox.data[1, :] *= self.image_size.height

                x = self.xp.clip(bbox.data[0, :].reshape(self.out_size), 0, self.image_size.width) + i * self.image_size.width
                y = self.xp.clip(bbox.data[1, :].reshape(self.out_size), 0, self.image_size.height)

                top_left = (x[0, 0], y[0, 0])
                top_right = (x[0, -1], y[0, -1])
                bottom_left = (x[-1, 0], y[-1, 0])
                bottom_right = (x[-1, -1], y[-1, -1])

                corners = [top_left, top_right, bottom_right, bottom_left]
                self.draw_bbox(colour, corners, draw) 
Example #11
Source File: text_rec_bbox_plotter.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def draw_bboxes(self, bboxes, image):
        draw = ImageDraw.Draw(image)
        for boxes, colour in zip(F.separate(bboxes, axis=0), self.colours):
            num_boxes = boxes.shape[0]

            for i, bbox in enumerate(F.separate(boxes, axis=0)):
                # render all intermediate results with lower alpha as the others
                fill_colour = colour
                if i < num_boxes - 1:
                    if not self.render_intermediate_bboxes:
                        continue
                    fill_colour += '88'

                bbox.data[...] = (bbox.data[...] + 1) / 2
                bbox.data[0, :] *= self.image_size.width
                bbox.data[1, :] *= self.image_size.height

                x = self.xp.clip(bbox.data[0, :].reshape(self.out_size), 0, self.image_size.width)
                y = self.xp.clip(bbox.data[1, :].reshape(self.out_size), 0, self.image_size.height)

                top_left = (x[0, 0], y[0, 0])
                top_right = (x[0, -1], y[0, -1])
                bottom_left = (x[-1, 0], y[-1, 0])
                bottom_right = (x[-1, -1], y[-1, -1])

                corners = [top_left, top_right, bottom_right, bottom_left]
                next_corners = corners[1:] + [corners[0]]

                for first_corner, next_corner in zip(corners, next_corners):
                    draw.line([first_corner, next_corner], fill=fill_colour, width=3) 
Example #12
Source File: train_mnist.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def mnist_accuracy(x, t):
    xp = cuda.get_array_module(x[0].data, t.data)
    batch_predictions, _, _ = x
    accuracies = []

    for predictions, labels in zip(F.split_axis(batch_predictions, args.timesteps, axis=1), F.separate(t, axis=1)):
        batch_size, _, num_classes = predictions.data.shape
        predictions = F.reshape(F.flatten(predictions), (batch_size, num_classes))
        accuracies.append(F.accuracy(predictions, labels))

    return sum(accuracies) / max(len(accuracies), 1) 
Example #13
Source File: train_mnist.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def mnist_loss(x, t):
    xp = cuda.get_array_module(x[0].data, t.data)
    batch_predictions, _, _ = x
    losses = []

    for predictions, labels in zip(F.split_axis(batch_predictions, args.timesteps, axis=1), F.separate(t, axis=1)):
        batch_size, _, num_classes = predictions.data.shape
        predictions = F.reshape(F.flatten(predictions), (batch_size, num_classes))
        losses.append(F.softmax_cross_entropy(predictions, labels))

    return sum(losses) 
Example #14
Source File: svhn.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, images, localizations):
        points = F.spatial_transformer_grid(localizations, self.target_shape)
        rois = F.spatial_transformer_sampler(images, points)

        # h = self.data_bn(rois)
        h = F.relu(self.bn0(self.conv0(rois)))
        h = F.average_pooling_2d(h, 2, stride=2)

        h = self.rs1(h)
        h = self.rs2(h)
        h = F.max_pooling_2d(h, 2, stride=2)
        h = self.rs3(h)
        self.vis_anchor = h

        h = F.average_pooling_2d(h, 5, stride=1)

        h = F.relu(self.fc1(h))

        # for each timestep of the localization net do the 'classification'
        h = F.reshape(h, (self.num_timesteps * 2 + 1, -1, self.fc1.out_size))
        overall_predictions = []
        for timestep in F.separate(h, axis=0):
            # go 2x num_labels plus 1 timesteps because of ctc loss
            lstm_predictions = []
            self.lstm.reset_state()
            for _ in range(self.num_labels):
                lstm_prediction = self.lstm(timestep)
                classified = self.classifier(lstm_prediction)
                lstm_predictions.append(classified)
            overall_predictions.append(lstm_predictions)

        return overall_predictions, rois, points 
Example #15
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
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 #16
Source File: text_recognizer.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
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 #17
Source File: text_recognizer.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def calc_loss(self, predictions, labels):
        recognition_losses = []
        assert predictions.shape[1] == labels.shape[1], "Number of boxes is not equal in predictions and labels"
        for box, box_labels in zip(F.separate(predictions, axis=1), F.separate(labels, axis=1)):
            assert box.shape[1] == box_labels.shape[1], "Number of predicted chars is not equal to number of chars in label"
            box_losses = [
                F.softmax_cross_entropy(char, char_label, reduce="no")
                for char, char_label in zip(F.separate(box, axis=1), F.separate(box_labels, axis=1))
            ]
            recognition_losses.append(F.stack(box_losses))
        return F.mean(F.stack(recognition_losses)) 
Example #18
Source File: distribution.py    From baselines with MIT License 5 votes vote down vote up
def prob(self, x):
        assert x.shape[1] == len(self.distributions)
        prob_all = 1
        for value, distribution in zip(list(F.separate(x, axis=1)),
                                       self.distributions):
            prob_all *= distribution.prob(value)
        return prob_all 
Example #19
Source File: evaluator.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_accuracy(self, predictions, labels):
        batch_predictions = predictions
        # concat all individual predictions and slice for each time step
        batch_predictions = F.concat([F.expand_dims(p, axis=2) for p in batch_predictions], axis=2)

        t = F.reshape(labels, (1, self.args.timesteps, -1))

        accuracies = []
        with cuda.get_device_from_array(batch_predictions.data):
            for prediction, label in zip(F.separate(batch_predictions, axis=0), F.separate(t, axis=2)):
                classification = F.softmax(prediction, axis=2)
                classification = classification.data
                classification = self.xp.argmax(classification, axis=2)
                # classification = self.xp.transpose(classification, (1, 0))

                words = self.strip_prediction(classification)
                labels = self.strip_prediction(label.data)

                for word, label in zip(words, labels):
                    word = "".join(map(self.label_to_char, word))
                    label = "".join(map(self.label_to_char, label))
                    if word == label:
                        self.num_correct_words += 1
                    self.num_words += 1

        return word, label 
Example #20
Source File: svhn_softmax_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_accuracy(self, x, t):
        batch_predictions, _, _ = x

        # concat all individual predictions and slice for each time step
        batch_predictions = F.concat([F.expand_dims(p, axis=0) for p in batch_predictions], axis=0)

        self.xp = cuda.get_array_module(batch_predictions[0], t)
        batch_size = t.shape[0]
        t = F.reshape(t, (batch_size, self.num_timesteps, -1))

        accuracies = []
        with cuda.get_device_from_array(batch_predictions.data):
            for prediction, label in zip(F.separate(batch_predictions, axis=0), F.separate(t, axis=1)):
                classification = F.softmax(prediction, axis=2)
                classification = classification.data
                classification = self.xp.argmax(classification, axis=2)
                # classification = self.xp.transpose(classification, (1, 0))

                words = self.strip_prediction(classification)
                labels = self.strip_prediction(label.data)

                num_correct_words = 0
                for word, label in zip(words, labels):
                    word = "".join(map(self.label_to_char, word))
                    label = "".join(map(self.label_to_char, label))
                    if word == label:
                        num_correct_words += 1

                accuracy = num_correct_words / len(labels)
                accuracies.append(accuracy)

        overall_accuracy = sum(accuracies) / max(len(accuracies), 1)
        self.scale_area_loss_factor(overall_accuracy)
        return overall_accuracy 
Example #21
Source File: lstm_per_step_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_accuracy(self, x, t):
        batch_predictions, _, _ = x
        self.xp = cuda.get_array_module(batch_predictions[0], t)
        accuracies = []
        for prediction, label in zip(batch_predictions, F.separate(t, axis=1)):
            recognition_accuracy = F.accuracy(prediction, label)
            accuracies.append(recognition_accuracy)
        return sum(accuracies) / len(accuracies) 
Example #22
Source File: models.py    From EEND with MIT License 5 votes vote down vote up
def forward(self, xs, activation=None):
        ilens = [x.shape[0] for x in xs]
        # xs: (B, T, F)
        xs = F.pad_sequence(xs, padding=-1)
        pad_shape = xs.shape
        # emb: (B*T, E)
        emb = self.enc(xs)
        # ys: (B*T, C)
        ys = self.linear(emb)
        if activation:
            ys = activation(ys)
        # ys: [(T, C), ...]
        ys = F.separate(ys.reshape(pad_shape[0], pad_shape[1], -1), axis=0)
        ys = [F.get_item(y, slice(0, ilen)) for y, ilen in zip(ys, ilens)]
        return ys 
Example #23
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x)) 
Example #24
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x, axis=0)) 
Example #25
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x, axis=1)) 
Example #26
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x, axis=2))


# ====================================== 
Example #27
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x)) 
Example #28
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x, axis=0)) 
Example #29
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x, axis=1)) 
Example #30
Source File: Separate.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        return list(F.separate(x, axis=2))


# ======================================