Python chainer.functions.where() Examples

The following are 30 code examples of chainer.functions.where(). 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: net.py    From chainer-partial_convolution_image_inpainting with MIT License 6 votes vote down vote up
def __call__(self, x, mask):
        self.m.W.data = self.xp.array(self.maskW) #mask windows are set by 1
        h = self.c(x*mask) #(B,C,H,W)
        B,C,H,W = h.shape
        b = F.transpose(F.broadcast_to(self.c.b,(B,H,W,C)),(0,3,1,2))
        h = h - b
        mask_sums = self.m(mask)
        mask_new = (self.xp.sign(mask_sums.data-0.5)+1.0)*0.5
        mask_new_b = mask_new.astype("bool")
        
        mask_sums = F.where(mask_new_b,mask_sums,0.01*Variable(self.xp.ones(mask_sums.shape).astype("f")))
        h = h/mask_sums + b
         
        mask_new = Variable(mask_new)
        h = F.where(mask_new_b, h, Variable(self.xp.zeros(h.shape).astype("f"))) 

        if self.bn:
            h = self.batchnorm(h)
        if self.noise:
            h = add_noise(h)
        if self.dropout:
            h = F.dropout(h)
        if not self.activation is None:
            h = self.activation(h)
        return h, mask_new 
Example #2
Source File: shifted_softplus.py    From chainer-chemistry with MIT License 6 votes vote down vote up
def shifted_softplus(x, beta=1, shift=0.5, threshold=20):
    """shifted softplus function, which holds f(0)=0.

     Args:
        x (Variable): Input variable
        beta (float): Parameter :math:`\\beta`.
        shift (float): Shift Parameter
        threshold (float): threshold to avoid overflow

    Returns:
        output (Variable): Output variable whose shape is same with `x`
    """
    xp = chainer.cuda.get_array_module(x)
    cond = chainer.as_variable(x).array > threshold
    x = functions.where(cond, x,
                        functions.softplus(x, beta=beta))
    x += xp.log(shift)
    return x 
Example #3
Source File: utils.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        """
            Calculate a loss based on the expected grid size. Penalize all predicted grids, where the area of the grid
            is smaller than the area of the crop area
        """
        top_left_x, top_right_x, _, _, top_left_y, _, bottom_left_y, _ = self.get_corners(grids, image_size)

        grid_widths = top_right_x - top_left_x
        grid_heights = bottom_left_y - top_left_y
        expected_width = self.xp.full_like(grid_widths.array, grids.shape[-1], dtype=grid_widths.dtype)
        expected_height = self.xp.full_like(grid_heights.array, grids.shape[2], dtype=grid_heights.dtype)

        width_loss = F.maximum(self.xp.zeros_like(grid_widths.array), expected_width - grid_widths)
        height_loss = F.maximum(self.xp.zeros_like(grid_heights.array), expected_height - grid_heights)

        return sum(width_loss) + sum(height_loss) 
Example #4
Source File: net.py    From convolutional_seq2seq with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def attend(self, query, key, value, mask, minfs=None):
        """
        Input shapes:
            q=(b, units, dec_l), k=(b, units, enc_l),
            v=(b, units, dec_l, enc_l), m=(b, dec_l, enc_l)
        """

        # Calculate Attention Scores with Mask for Zero-padded Areas
        pre_a = F.batch_matmul(query, key, transa=True)  # (b, dec_l, enc_l)
        minfs = self.xp.full(pre_a.shape, -np.inf, pre_a.dtype) \
            if minfs is None else minfs
        pre_a = F.where(mask, pre_a, minfs)
        a = F.softmax(pre_a, axis=2)
        # if values in axis=2 are all -inf, they become nan. thus do re-mask.
        a = F.where(self.xp.isnan(a.data),
                    self.xp.zeros(a.shape, dtype=a.dtype), a)
        reshaped_a = a[:, None]  # (b, 1, dec_xl, enc_l)

        # Calculate Weighted Sum
        pre_c = F.broadcast_to(reshaped_a, value.shape) * value
        c = F.sum(pre_c, axis=3, keepdims=True)  # (b, units, dec_xl, 1)
        return c 
Example #5
Source File: light_head_rcnn_resnet101.py    From chainercv with MIT License 6 votes vote down vote up
def __call__(self, x, rois, roi_indices):
        # global context module
        h = self.global_context_module(x)
        # psroi max align
        pool = ps_roi_max_align_2d(
            h, rois, roi_indices,
            (10, self.roi_size, self.roi_size),
            self.spatial_scale, self.roi_size,
            sampling_ratio=2)
        pool = F.where(
            self.xp.isinf(pool.array),
            self.xp.zeros(pool.shape, dtype=pool.dtype), pool)

        # fc
        fc1 = F.relu(self.fc1(pool))
        roi_cls_locs = self.cls_loc(fc1)
        roi_scores = self.score(fc1)
        return roi_cls_locs, roi_scores 
Example #6
Source File: relgcn.py    From graph-nvp with MIT License 5 votes vote down vote up
def rescale_adj(adj):
    xp = cuda.get_array_module(adj)
    num_neighbors = F.sum(adj, axis=(1, 2))
    base = xp.ones(num_neighbors.shape, dtype=xp.float32)
    cond = num_neighbors.data != 0
    num_neighbors_inv = 1 / F.where(cond, num_neighbors, base)
    return adj * F.broadcast_to(num_neighbors_inv[:, None, None, :], adj.shape) 
Example #7
Source File: attenders.py    From lencon with MIT License 5 votes vote down vote up
def _attend(self, p):
        p = self.xh(p)
        p = F.expand_dims(p, 1)
        p = F.broadcast_to(p, self.shape2)

        h = F.tanh(self.h + p)
        shape3 = (self.batchsize * self.src_len, self.dim_hid)
        h_reshaped = F.reshape(h, shape3)
        weight_reshaped = self.hw(h_reshaped)
        weight = F.reshape(weight_reshaped, (self.batchsize, self.src_len, 1))
        weight = F.where(self.mask, weight, self.minf)
        attention = F.softmax(weight)
        return attention 
Example #8
Source File: attenders.py    From lencon with MIT License 5 votes vote down vote up
def _attend(self, p):
        weight = F.batch_matmul(self.source_hiddens, p)
        weight = F.where(self.mask, weight, self.minf)
        attention = F.softmax(weight)
        return attention 
Example #9
Source File: len_emb.py    From lencon with MIT License 5 votes vote down vote up
def post_decode_once(self, output, state, train=True):
        lengths = state['lengths']
        if self.byte:
            itos = self.vocab.itos
            consumed = self.xp.array([[len(itos(oi)) + 1]
                                     for oi in output.tolist()])
            lengths -= consumed
        else:
            lengths -= 1
        flags = chainer.Variable(lengths.data >= 0, volatile=not train)
        lengths = F.where(flags, lengths, self.zeros)
        state['lengths'] = lengths
        return state 
Example #10
Source File: len_emb.py    From lencon with MIT License 5 votes vote down vote up
def post_decode_once(self, output, state, train=True):
        lengths = state['lengths']
        if self.byte:
            itos = self.vocab.itos
            consumed = self.xp.array([[len(itos(oi)) + 1]
                                      for oi in output.tolist()])
            lengths -= consumed
        else:
            lengths -= 1
        flags = chainer.Variable(lengths.data >= 0, volatile=not train)
        lengths = F.where(flags, lengths, self.zeros)
        state['lengths'] = lengths
        return state 
Example #11
Source File: nfp_update.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def __call__(self, h, adj, deg_conds):
        # h: (minibatch, atom, ch)
        # h encodes each atom's info in ch axis of size hidden_dim
        # adjs: (minibatch, atom, atom)

        # --- Message part ---
        # Take sum along adjacent atoms

        # fv: (minibatch, atom, ch)
        fv = chainer_chemistry.functions.matmul(adj, h)

        # --- Update part ---
        # TODO(nakago): self.xp is chainerx
        if self.xp is numpy:
            zero_array = numpy.zeros(fv.shape, dtype=numpy.float32)
        else:
            zero_array = self.xp.zeros_like(fv.array)

        fvds = [functions.where(cond, fv, zero_array) for cond in deg_conds]

        out_h = 0
        for graph_linear, fvd in zip(self.graph_linears, fvds):
            out_h = out_h + graph_linear(fvd)

        # out_h shape (minibatch, max_num_atoms, hidden_dim)
        out_h = functions.sigmoid(out_h)
        return out_h 
Example #12
Source File: attention.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def attention_implementation(self, query, key, value, mask=None, dropout_ratio=None):
        scores = F.matmul(query, F.transpose(key, (0, 1, 3, 2))) / math.sqrt(self.key_dimensionality)
        if mask is not None:
            batch_size, num_heads, _, _ = scores.shape
            mask = self.xp.array(mask)
            mask = self.xp.broadcast_to(mask, (batch_size, num_heads) + mask.shape[2:])
            mask = mask[:, :, :scores.shape[2], :scores.shape[3]]
            scores = F.where(mask, scores, self.xp.full_like(scores.array, -1e9))

        attention_probabilities = F.softmax(scores, axis=3)
        if dropout_ratio is not None:
            attention_probabilities = F.dropout(attention_probabilities, ratio=dropout_ratio)

        return F.matmul(attention_probabilities, value), attention_probabilities 
Example #13
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def smallest_area(self, box1, box2):
        area_box_1 = (box1[:, 2] - box1[:, 0]) * (box1[:, 3] - box1[:, 1])
        area_box_2 = (box2[:, 2] - box2[:, 0]) * (box1[:, 3] - box1[:, 1])
        smallest_area = F.where(area_box_1.data < area_box_2.data, area_box_1, area_box_2)
        return smallest_area 
Example #14
Source File: transformer_text_updater.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def clear_bboxes(self, bboxes, num_word_indicators, xp):
        condition = xp.ones_like(bboxes.array)
        for i in range(len(bboxes)):
            condition[i, num_word_indicators[i]:] = 0

        return F.where(condition.astype(xp.bool), bboxes, xp.zeros_like(bboxes.array)) 
Example #15
Source File: attention.py    From espnet with Apache License 2.0 5 votes vote down vote up
def forward(self, e_var, s_var=None, mask=None, batch=1):
        """Core function of the Multi-head attention layer.

        Args:
            e_var (chainer.Variable): Variable of input array.
            s_var (chainer.Variable): Variable of source array from encoder.
            mask (chainer.Variable): Attention mask.
            batch (int): Batch size.

        Returns:
            chainer.Variable: Outout of multi-head attention layer.

        """
        xp = self.xp
        if s_var is None:
            # batch, head, time1/2, d_k)
            Q = self.linear_q(e_var).reshape(batch, -1, self.h, self.d_k)
            K = self.linear_k(e_var).reshape(batch, -1, self.h, self.d_k)
            V = self.linear_v(e_var).reshape(batch, -1, self.h, self.d_k)
        else:
            Q = self.linear_q(e_var).reshape(batch, -1, self.h, self.d_k)
            K = self.linear_k(s_var).reshape(batch, -1, self.h, self.d_k)
            V = self.linear_v(s_var).reshape(batch, -1, self.h, self.d_k)
        scores = F.matmul(F.swapaxes(Q, 1, 2), K.transpose(0, 2, 3, 1)) / np.sqrt(
            self.d_k
        )
        if mask is not None:
            mask = xp.stack([mask] * self.h, axis=1)
            scores = F.where(mask, scores, xp.full(scores.shape, MIN_VALUE, "f"))
        self.attn = F.softmax(scores, axis=-1)
        p_attn = F.dropout(self.attn, self.dropout)
        x = F.matmul(p_attn, F.swapaxes(V, 1, 2))
        x = F.swapaxes(x, 1, 2).reshape(-1, self.h * self.d_k)
        return self.linear_out(x) 
Example #16
Source File: test_ps_roi_max_align.py    From chainercv with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, roi_index_data, y_grad_data):
        def f(x, rois, roi_indices):
            y = functions.ps_roi_max_align_2d(
                x, rois, roi_indices, self.outsize,
                self.spatial_scale, self.group_size,
                sampling_ratio=self.sampling_ratio)
            xp = cuda.get_array_module(y)
            y = F.where(
                xp.isinf(y.array), xp.zeros(y.shape, dtype=y.dtype), y)
            return y

        gradient_check.check_backward(
            f, (x_data, roi_data, roi_index_data), y_grad_data,
            no_grads=[False, True, True], **self.check_backward_options) 
Example #17
Source File: test_ps_roi_max_pooling.py    From chainercv with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, roi_index_data, y_grad_data):
        def f(x, rois, roi_indices):
            y = functions.ps_roi_max_pooling_2d(
                x, rois, roi_indices, self.outsize,
                self.spatial_scale, self.group_size)
            xp = cuda.get_array_module(y)
            y = F.where(
                xp.isinf(y.array), xp.zeros(y.shape, dtype=y.dtype), y)
            return y
        gradient_check.check_backward(
            f, (x_data, roi_data, roi_index_data), y_grad_data,
            no_grads=[False, True, True], **self.check_backward_options) 
Example #18
Source File: test_arrays.py    From onnx-chainer with MIT License 5 votes vote down vote up
def test_output(self):
        model = chainer.Sequential(
            F.where
        )
        cond = np.array([[1, 0, 0], [0, 1, 0]], dtype=np.bool)
        x = input_generator.increasing(2, 3)
        y = np.zeros((2, 3), np.float32)
        self.expect(model, (cond, x, y), skip_opset_version=[7, 8]) 
Example #19
Source File: attention.py    From models with MIT License 5 votes vote down vote up
def attention_implementation(self, query, key, value, mask=None, dropout_ratio=None):
        scores = F.matmul(query, F.transpose(key, (0, 1, 3, 2))) / math.sqrt(self.key_dimensionality)
        if mask is not None:
            batch_size, num_heads, _, _ = scores.shape
            mask = self.xp.array(mask)
            mask = self.xp.broadcast_to(mask, (batch_size, num_heads) + mask.shape[2:])
            mask = mask[:, :, :scores.shape[2], :scores.shape[3]]
            scores = F.where(mask, scores, self.xp.full_like(scores.array, -1e9))

        attention_probabilities = F.softmax(scores, axis=3)
        if dropout_ratio is not None:
            attention_probabilities = F.dropout(attention_probabilities, ratio=dropout_ratio)

        return F.matmul(attention_probabilities, value), attention_probabilities 
Example #20
Source File: net.py    From convolutional_seq2seq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __call__(self, x, mask=None):
        x = F.dropout(x, ratio=self.dropout)
        out, pregate = F.split_axis(self.conv(x), 2, axis=1)
        out = out * F.sigmoid(pregate)
        if mask is not None:
            out *= mask
        return out

# TODO: For layers whose output is not directly fed to a gated linear
# unit, we initialize weights from N (0, p 1/nl) where nl is the number of
# input connections for each neuron. 
Example #21
Source File: test_roi_max_align_2d.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, roi_index_data, y_grad):
        def f(x, rois, roi_indices):
            y = functions.roi_max_align_2d(
                x, rois, roi_indices, outsize=self.outsize,
                spatial_scale=self.spatial_scale,
                sampling_ratio=self.sampling_ratio)
            xp = chainer.backend.get_array_module(y)
            y = functions.where(
                xp.isinf(y.array), xp.zeros(y.shape, dtype=y.dtype), y)
            return y

        gradient_check.check_backward(
            f, (x_data, roi_data, roi_index_data), y_grad,
            no_grads=[False, True, True], **self.check_backward_options) 
Example #22
Source File: test_roi_max_pooling_2d.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, roi_index_data, y_grad):
        def f(x, rois, roi_indices):
            y = functions.roi_max_pooling_2d(
                x, rois, roi_indices, outsize=self.outsize,
                spatial_scale=self.spatial_scale)
            xp = cuda.get_array_module(y)
            # replace -inf with zero for gradient_check
            y = functions.where(
                xp.isinf(y.array), xp.zeros(y.shape, dtype=y.dtype), y)
            return y

        gradient_check.check_backward(
            f, (x_data, roi_data, roi_index_data), y_grad,
            no_grads=[False, True, True], **self.check_backward_options) 
Example #23
Source File: test_where.py    From chainer with MIT License 5 votes vote down vote up
def check_forward_raises(self, c_data, x_data, y_data):
        c = chainer.Variable(c_data)
        x = chainer.Variable(x_data)
        y = chainer.Variable(y_data)
        with pytest.raises(type_check.InvalidType):
            functions.where(c, x, y) 
Example #24
Source File: test_where.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, devices):
        c, x, y = inputs
        z = functions.where(c, x, y)
        return z, 
Example #25
Source File: test_where.py    From chainer with MIT License 5 votes vote down vote up
def forward_expected(self, inputs):
        c, x, y = inputs
        z_expected = numpy.where(c, x, y)
        return z_expected, 
Example #26
Source File: net_pre-trained.py    From chainer-partial_convolution_image_inpainting with MIT License 5 votes vote down vote up
def __call__(self, x, mask):
        #h = self.c(x) - self.b
        self.m.W.data = self.xp.array(self.maskW) #mask windows are set by 1
        h = self.c(x*mask) #(B,C,H,W)
        B,C,H,W = h.shape
        #b = F.transpose(F.broadcast_to(self.c.b,(B,H,W,C)),(0,3,1,2))
        #h = h - b
        mask_sums = self.m(mask)
        mask_new = (self.xp.sign(mask_sums.data-0.5)+1.0)*0.5
        mask_new_b = mask_new.astype("bool")
        
        mask_sums = F.where(mask_new_b,mask_sums,0.01*Variable(self.xp.ones(mask_sums.shape).astype("f")))
        h = h/mask_sums 
        #h = h/mask_sums + b
         
        mask_new = Variable(mask_new)
        h = F.where(mask_new_b, h, Variable(self.xp.zeros(h.shape).astype("f"))) 

        #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)
        if self.noise:
            h = add_noise(h)
        if self.dropout:
            h = F.dropout(h)
        if not self.activation is None:
            h = self.activation(h)
        return h, mask_new 
Example #27
Source File: evaluation.py    From chainer-partial_convolution_image_inpainting with MIT License 5 votes vote down vote up
def evaluation(model, test_image_folder, image_size=256):
    @chainer.training.make_extension()
    def _eval(trainer, it):
        xp = model.xp
        batch = it.next()
        batchsize = len(batch)

        x = xp.zeros((batchsize, 3, image_size, image_size)).astype("f")
        m = xp.zeros((batchsize, 3, image_size, image_size)).astype("f")
        for i in range(batchsize):
            x[i, :] = xp.asarray(batch[i][0])
            m[i, :] = xp.asarray(batch[i][1])
        mask_b = xp.array(m.astype("bool"))

        I_gt = Variable(x)
        M = Variable(m)
        M_b = Variable(mask_b)
        
        I_out = model(x, m)
        I_comp = F.where(M_b,I_gt,I_out)

        img = I_comp.data.get()

        img = batch_postprocess_images(img, int(batchsize/2), 2)
        Image.fromarray(img).save(test_image_folder+"/iter_"+str(trainer.updater.iteration)+"_Icomp.jpg")

        img = I_out.data.get()

        img = batch_postprocess_images(img, int(batchsize/2), 2)
        Image.fromarray(img).save(test_image_folder+"/iter_"+str(trainer.updater.iteration)+"_Iout.jpg")

        img = M.data.get()

        img = batch_postprocess_images(img, int(batchsize/2), 2)
        Image.fromarray(img).save(test_image_folder+"/iter_"+str(trainer.updater.iteration)+"_mask.jpg")

    def evaluation(trainer):
        it = trainer.updater.get_iterator('test')
        _eval(trainer, it)

    return evaluation 
Example #28
Source File: utils.py    From kiss with GNU General Public License v3.0 4 votes vote down vote up
def calc_loss(self, image_size, predicted_grids, gt_bbox_points, objectness_scores, normalize=True):
        predicted_bbox_points = self.get_corners(predicted_grids, image_size, scale_to_image_size=False)

        # 1. transform box coordinates to aabb coordinates for determination of iou
        predicted_bbox_points = predicted_bbox_points[0], predicted_bbox_points[4], predicted_bbox_points[3], predicted_bbox_points[7]
        predicted_bbox_points = F.stack(predicted_bbox_points, axis=1)

        # 2. find best prediction area for each gt bbox
        gt_bboxes_to_use_for_loss = []
        positive_anchor_indices = self.xp.empty((0,), dtype=self.xp.int32)
        not_contributing_anchors = self.xp.empty((0,), dtype=self.xp.int32)
        for index, gt_bbox in enumerate(gt_bbox_points):
            # determine which bboxes are positive boxes as they have high iou with gt and also which bboxes are negative
            # this is also used to train objectness classification
            gt_bbox = self.xp.tile(gt_bbox[None, ...], (len(predicted_bbox_points), 1))

            ious = bbox_iou(gt_bbox, predicted_bbox_points.data)
            positive_boxes = self.xp.where((ious[0] >= 0.7))
            not_contributing_boxes = self.xp.where(self.xp.logical_and(0.3 < ious[0], ious[0] < 0.7))
            if len(positive_boxes[0]) == 0:
                best_iou_index = ious[0, :].argmax()
                positive_anchor_indices = self.xp.concatenate((positive_anchor_indices, best_iou_index[None, ...]), axis=0)
                gt_bboxes_to_use_for_loss.append(gt_bbox[0])
            else:
                positive_anchor_indices = self.xp.concatenate((positive_anchor_indices, positive_boxes[0]), axis=0)
                gt_bboxes_to_use_for_loss.extend(gt_bbox[:len(positive_boxes[0])])
            not_contributing_anchors = self.xp.concatenate((not_contributing_anchors, not_contributing_boxes[0]), axis=0)

        if len(gt_bboxes_to_use_for_loss) == 0:
            return Variable(self.xp.array(0, dtype=predicted_grids.dtype))

        gt_bboxes_to_use_for_loss = F.stack(gt_bboxes_to_use_for_loss)

        # filter predicted bboxes and only keep bboxes from those regions that actually contain a bbox
        predicted_bbox_points = F.get_item(predicted_bbox_points, positive_anchor_indices)

        # 3. calculate L1 loss for bbox regression
        loss = F.huber_loss(
            predicted_bbox_points,
            gt_bboxes_to_use_for_loss,
            1
        )

        # 4. calculate objectness loss
        objectness_labels = self.xp.zeros(len(objectness_scores), dtype=self.xp.int32)
        objectness_labels[not_contributing_anchors] = -1
        objectness_labels[positive_anchor_indices] = 1

        objectness_loss = F.softmax_cross_entropy(
            objectness_scores,
            objectness_labels,
            ignore_label=-1,
        )

        return F.mean(loss), objectness_loss 
Example #29
Source File: nets.py    From contextual_augmentation with MIT License 4 votes vote down vote up
def predict_embed(self,
                      xs, embedW,
                      labels=None,
                      dropout=0.,
                      mode='sampling',
                      temp=1.,
                      word_lower_bound=0.,
                      gold_lower_bound=0.,
                      gumbel=True,
                      residual=0.,
                      wordwise=True,
                      add_original=0.,
                      augment_ratio=0.25):
        x_len = [len(x) for x in xs]
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            t_out_concat = self.encode(xs, labels=labels)
            prob_concat = self.output.output(t_out_concat).data
            prob_concat /= temp
            prob_concat += self.xp.random.gumbel(
                size=prob_concat.shape).astype('f')
            prob_concat = F.softmax(prob_concat).data

        out_concat = F.embed_id(
            self.xp.argmax(prob_concat, axis=1).astype(np.int32), embedW)

        # insert eos
        eos = embedW[0][None]
        new_out = []
        count = 0
        for i, x in enumerate(xs):
            new_out.append(eos)
            new_out.append(out_concat[count:count + len(x) - 2])
            new_out.append(eos)
            count += len(x) - 2
        out_concat = F.concat(new_out, axis=0)

        def embed_func(x): return F.embed_id(x, embedW, ignore_label=-1)
        raw_concat = F.concat(
            sequence_embed(embed_func, xs, self.dropout), axis=0)
        b, u = raw_concat.shape

        mask = self.xp.broadcast_to(
            (self.xp.random.rand(b, 1) < augment_ratio),
            raw_concat.shape)
        out_concat = F.where(mask, out_concat, raw_concat)

        x_len = [len(x) for x in xs]
        x_section = np.cumsum(x_len[:-1])
        out_concat = F.dropout(out_concat, dropout)
        exs = F.split_axis(out_concat, x_section, 0)
        return exs 
Example #30
Source File: updater.py    From chainer-partial_convolution_image_inpainting with MIT License 4 votes vote down vote up
def update_core(self):
        xp = self.model.xp
        self._iter += 1
        batch = self.get_iterator('main').next() #img_processed (B,4,H,W), origin (B,3,H,W), mask (B,1,H,W)
        batchsize = len(batch)

        w_in = self._image_size

        zero_f = Variable(xp.zeros((batchsize, 3, w_in, w_in)).astype("f"))
        
        x_train = np.zeros((batchsize, 3, w_in, w_in)).astype("f")
        mask_train = np.zeros((batchsize, 3, w_in, w_in)).astype("f")
         
        for i in range(batchsize):
            x_train[i, :] = batch[i][0] #original image
            mask_train[i, :] = batch[i][1] #0-1 mask of c 
        
        x_train = xp.array(x_train)
        mask_train = xp.array(mask_train)
        mask_b = xp.array(mask_train.astype("bool"))
        
        I_gt = Variable(x_train)
        M = Variable(mask_train)
        M_b = Variable(mask_b)

        I_out = self.model(I_gt,M)
        I_comp = F.where(M_b,I_gt,I_out) #if an element of Mc_b is True, return the corresponded element of I_gt, otherwise return that of I_out)

        fs_I_gt = vgg_extract(self.vgg,I_gt) #feature dict
        fs_I_out = vgg_extract(self.vgg,I_out) #feature dict
        fs_I_comp = vgg_extract(self.vgg,I_comp) #feature dict

        opt_model = self.get_optimizer('model')

        L_valid = F.mean_absolute_error(M*I_out,M*I_gt)
        L_hole = F.mean_absolute_error((1-M)*I_out,(1-M)*I_gt) 
        
        L_perceptual = calc_loss_perceptual(fs_I_gt,fs_I_out,fs_I_comp)
        
        L_style = calc_loss_style(fs_I_out,fs_I_comp,fs_I_gt) #Loss style out and comp 
        L_tv = calc_loss_tv(I_comp, M, xp=xp)

        L_total = L_valid + self._lambda1 * L_hole + self._lambda2 * L_perceptual + \
                  self._lambda3 * L_style + self._lambda4 * L_tv
        
        self.vgg.cleargrads()
        self.model.cleargrads()
        L_total.backward()
        opt_model.update()

        chainer.report({'L_valid': L_valid})
        chainer.report({'L_hole': L_hole})
        chainer.report({'L_perceptual': L_perceptual})
        chainer.report({'L_style': L_style})
        chainer.report({'L_tv': L_tv})