Python chainer.dataset.convert.to_device() Examples

The following are 10 code examples of chainer.dataset.convert.to_device(). 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.dataset.convert , or try the search function .
Example #1
Source File: utils.py    From contextual_augmentation with MIT License 5 votes vote down vote up
def convert_xt_batch_seq(xt_batch_seq, gpu):
    batchsize = len(xt_batch_seq[0])
    seq_len = len(xt_batch_seq)
    xt_batch_seq = np.array(xt_batch_seq, 'i')
    # (bproplen, batch, 2)
    xt_batch_seq = convert.to_device(gpu, xt_batch_seq)
    xp = cuda.get_array_module(xt_batch_seq)
    x_seq_batch = xp.split(
        xt_batch_seq[:, :, 0].T.reshape(batchsize * seq_len),
        batchsize, axis=0)
    t_seq_batch = xp.split(
        xt_batch_seq[:, :, 1].T.reshape(batchsize * seq_len),
        batchsize, axis=0)
    return x_seq_batch, t_seq_batch 
Example #2
Source File: datasets.py    From chainer with MIT License 5 votes vote down vote up
def converter(batch, device, max_caption_length=None):
    """Optional preprocessing of the batch before forward pass."""
    pad = max_caption_length is not None

    imgs = []
    captions = []
    for img, caption in batch:
        # Preproess the caption by either fixing the length by padding (LSTM)
        # or by simply wrapping each caption in an ndarray (NStepLSTM)
        if pad:
            arr = np.full(max_caption_length, _ignore, dtype=np.int32)

            # Clip to max length if necessary
            arr[:len(caption)] = caption[:max_caption_length]
            caption = arr
        else:
            caption = to_device(device, np.asarray(caption, dtype=np.int32))

        imgs.append(img)
        captions.append(caption)

    if pad:
        captions = to_device(device, np.stack(captions))
    imgs = to_device(device, np.stack(imgs))

    return imgs, captions 
Example #3
Source File: datasets_chainerio.py    From pfio with MIT License 5 votes vote down vote up
def converter(batch, device, max_caption_length=None):
    """Optional preprocessing of the batch before forward pass."""
    pad = max_caption_length is not None

    imgs = []
    captions = []
    for img, caption in batch:
        # Preproess the caption by either fixing the length by padding (LSTM)
        # or by simply wrapping each caption in an ndarray (NStepLSTM)
        if pad:
            arr = np.full(max_caption_length, _ignore, dtype=np.int32)

            # Clip to max length if necessary
            arr[:len(caption)] = caption[:max_caption_length]
            caption = arr
        else:
            caption = to_device(device, np.asarray(caption, dtype=np.int32))

        imgs.append(img)
        captions.append(caption)

    if pad:
        captions = to_device(device, np.stack(captions))
    imgs = to_device(device, np.stack(imgs))

    return imgs, captions 
Example #4
Source File: utils_pretrain.py    From models with MIT License 5 votes vote down vote up
def convert_xt_batch_seq(xt_batch_seq, gpu):
    batchsize = len(xt_batch_seq[0])
    seq_len = len(xt_batch_seq)
    xt_batch_seq = np.array(xt_batch_seq, 'i')
    # (bproplen, batch, 2)
    xt_batch_seq = convert.to_device(gpu, xt_batch_seq)
    xp = cuda.get_array_module(xt_batch_seq)
    x_seq_batch = xp.split(
        xt_batch_seq[:, :, 0].T.reshape(batchsize * seq_len),
        batchsize, axis=0)
    t_seq_batch = xp.split(
        xt_batch_seq[:, :, 1].T.reshape(batchsize * seq_len),
        batchsize, axis=0)
    return x_seq_batch, t_seq_batch 
Example #5
Source File: train_sbd.py    From chainercv with MIT License 5 votes vote down vote up
def concat_examples(batch, device=None):
    # batch: img, mask, label, scale
    if len(batch) == 0:
        raise ValueError('batch is empty')

    first_elem = batch[0]
    result = []
    for i in six.moves.range(len(first_elem)):
        array = _concat_arrays([example[i] for example in batch], None)
        if i == 0:  # img
            result.append(to_device(device, array))
        else:
            result.append(array)
    return tuple(result) 
Example #6
Source File: train_coco_multi.py    From chainercv with MIT License 5 votes vote down vote up
def concat_examples(batch, device=None, padding=None,
                    indices_concat=None, indices_to_device=None):
    if len(batch) == 0:
        raise ValueError('batch is empty')

    first_elem = batch[0]

    elem_size = len(first_elem)
    if indices_concat is None:
        indices_concat = range(elem_size)
    if indices_to_device is None:
        indices_to_device = range(elem_size)

    result = []
    if not isinstance(padding, tuple):
        padding = [padding] * elem_size

    for i in six.moves.range(elem_size):
        res = [example[i] for example in batch]
        if i in indices_concat:
            res = _concat_arrays(res, padding[i])
        if i in indices_to_device:
            if i in indices_concat:
                res = to_device(device, res)
            else:
                res = [to_device(device, r) for r in res]
        result.append(res)

    return tuple(result) 
Example #7
Source File: concat_examples.py    From chainer-mask-rcnn with MIT License 5 votes vote down vote up
def concat_examples(batch, device=None, padding=None,
                    indices_concat=None, indices_to_device=None):
    if len(batch) == 0:
        raise ValueError('batch is empty')

    first_elem = batch[0]

    elem_size = len(first_elem)
    if indices_concat is None:
        indices_concat = range(elem_size)
    if indices_to_device is None:
        indices_to_device = range(elem_size)

    result = []
    if not isinstance(padding, tuple):
        padding = [padding] * elem_size

    for i in six.moves.range(elem_size):
        res = [example[i] for example in batch]
        if i in indices_concat:
            res = _concat_arrays(res, padding[i])
        if i in indices_to_device:
            if i in indices_concat:
                res = to_device(device, res)
            else:
                res = [to_device(device, r) for r in res]
        result.append(res)

    return tuple(result) 
Example #8
Source File: cgcnn_converter.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def cgcnn_converter(batch, device=None, padding=None):
    """CGCNN converter"""
    if len(batch) == 0:
        raise ValueError("batch is empty")

    atom_feat, nbr_feat, nbr_idx = [], [], []
    batch_atom_idx, target = [], []
    current_idx = 0
    xp = device.xp
    for element in batch:
        atom_feat.append(element[0])
        nbr_feat.append(element[1])
        nbr_idx.append(element[2] + current_idx)
        target.append(element[3])
        n_atom = element[0].shape[0]
        atom_idx = numpy.arange(n_atom) + current_idx
        batch_atom_idx.append(atom_idx)
        current_idx += n_atom

    atom_feat = to_device(device, functions.concat(atom_feat, axis=0).data)
    nbr_feat = to_device(device, functions.concat(nbr_feat, axis=0).data)
    # Always use numpy array for batch_atom_index
    # this is list of variable length array
    batch_atom_idx = numpy.array(batch_atom_idx)
    nbr_idx = to_device(device, functions.concat(nbr_idx, axis=0).data)
    target = to_device(device, xp.asarray(target))
    result = (atom_feat, nbr_feat, batch_atom_idx, nbr_idx, target)
    return result 
Example #9
Source File: megnet_converter.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def megnet_converter(batch, device=None, padding=0):
    """MEGNet converter"""
    if len(batch) == 0:
        raise ValueError("batch is empty")

    atom_feat, pair_feat, global_feat, target = [], [], [], []
    atom_idx, pair_idx, start_idx, end_idx = [], [], [], []
    batch_size = len(batch)
    current_atom_idx = 0
    for i in range(batch_size):
        element = batch[i]
        n_atom = element[0].shape[0]
        n_pair = element[1].shape[0]
        atom_feat.extend(element[0])
        pair_feat.extend(element[1])
        global_feat.append(element[2])
        atom_idx.extend([i]*n_atom)
        pair_idx.extend([i]*n_pair)
        start_idx.extend(element[3][0] + current_atom_idx)
        end_idx.extend(element[3][1] + current_atom_idx)
        target.append(element[4])
        current_atom_idx += n_atom

    xp = device.xp
    atom_feat = to_device(device, xp.asarray(atom_feat))
    pair_feat = to_device(device, xp.asarray(pair_feat))
    global_feat = to_device(device, xp.asarray(global_feat))
    atom_idx = to_device(device, xp.asarray(atom_idx))
    pair_idx = to_device(device, xp.asarray(pair_idx))
    start_idx = to_device(device, xp.asarray(start_idx))
    end_idx = to_device(device, xp.asarray(end_idx))
    target = to_device(device, xp.asarray(target))
    result = (atom_feat, pair_feat, global_feat, atom_idx, pair_idx,
              start_idx, end_idx, target)

    return result 
Example #10
Source File: convert.py    From chainer-fcis with MIT License 5 votes vote down vote up
def concat_examples(batch, device=None):
    # batch: img, bboxes, whole_mask, labels, scale
    if len(batch) == 0:
        raise ValueError('batch is empty')

    first_elem = batch[0]

    result = []
    for i in six.moves.range(len(first_elem)):
        array = _concat_arrays([example[i] for example in batch], None)
        if i == 0:  # img
            result.append(to_device(device, array))
        else:
            result.append(array)
    return tuple(result)