modules/collate.py
def cf(batch):
    r"""Puts each data field into a tensor with outer dimension batch size"""

    elem = batch[0]
    elem_type = type(elem)
    if isinstance(elem, torch.Tensor):
        out = None
        if torch.utils.data.get_worker_info() is not None:
            # If we're in a background process, concatenate directly into a
            # shared memory tensor to avoid an extra copy
            numel = sum([x.numel() for x in batch])
            storage = elem.storage()._new_shared(numel)
            out = elem.new(storage)
        return torch.stack(batch, 0, out=out)
    elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
            and elem_type.__name__ != 'string_':
        if elem_type.__name__ == 'ndarray' or elem_type.__name__ == 'memmap':
            # array of string classes and object
            if np_str_obj_array_pattern.search(elem.dtype.str) is not None:
                raise TypeError(default_collate_err_msg_format.format(elem.dtype))

            # return cf([torch.as_tensor(b) for b in batch])
            stacked = np.stack(batch)
            return torch.as_tensor(stacked)
        elif elem.shape == ():  # scalars
            return torch.as_tensor(batch)
    elif isinstance(elem, collections.abc.Sequence):
        # check to make sure that the elements in batch have consistent size
        it = iter(batch)
        elem_size = len(next(it))
        if not all(len(elem) == elem_size for elem in it):
            raise RuntimeError('each element in list of batch should be of equal size')
        transposed = zip(*batch)
        return [cf(samples) for samples in transposed]

    raise TypeError(default_collate_err_msg_format.format(elem_type))
r"""Puts each data field into a tensor with outer dimension batch size
r"""Puts each data field into a tensor with outer dimension batch size
r " " " Puts each data field into a tensor with outer dimension batch size
def cf(batch):
    elem = batch[0]
    elem_type = type(elem)
    if isinstance(elem, torch.Tensor):
        out = None
        if torch.utils.data.get_worker_info() is not None:
            numel = sum([x.numel() for x in batch])
            storage = elem.storage()._new_shared(numel)
            out = elem.new(storage)
        return torch.stack(batch, 0, out=out)
    elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
            and elem_type.__name__ != 'string_':
        if elem_type.__name__ == 'ndarray' or elem_type.__name__ == 'memmap':
            if np_str_obj_array_pattern.search(elem.dtype.str) is not None:
                raise TypeError(default_collate_err_msg_format.format(elem.dtype))
            stacked = np.stack(batch)
            return torch.as_tensor(stacked)
        elif elem.shape == ():  
            return torch.as_tensor(batch)
    elif isinstance(elem, collections.abc.Sequence):
        it = iter(batch)
        elem_size = len(next(it))
        if not all(len(elem) == elem_size for elem in it):
            raise RuntimeError('each element in list of batch should be of equal size')
        transposed = zip(*batch)
        return [cf(samples) for samples in transposed]
    raise TypeError(default_collate_err_msg_format.format(elem_type))
def cf ( batch ) : elem = batch [ 0 ] elem_type = type ( elem ) if isinstance ( elem , torch . Tensor ) : out = None if torch . utils . data . get_worker_info ( ) is not None : numel = sum ( [ x . numel ( ) for x in batch ] ) storage = elem . storage ( ) . _new_shared ( numel ) out = elem . new ( storage ) return torch . stack ( batch , 0 , out = out ) elif elem_type . __module__ == 'numpy' and elem_type . __name__ != 'str_' and elem_type . __name__ != 'string_' : if elem_type . __name__ == 'ndarray' or elem_type . __name__ == 'memmap' : if np_str_obj_array_pattern . search ( elem . dtype . str ) is not None : raise TypeError ( default_collate_err_msg_format . format ( elem . dtype ) ) stacked = np . stack ( batch ) return torch . as_tensor ( stacked ) elif elem . shape == ( ) : return torch . as_tensor ( batch ) elif isinstance ( elem , collections . abc . Sequence ) : it = iter ( batch ) elem_size = len ( next ( it ) ) if not all ( len ( elem ) == elem_size for elem in it ) : raise RuntimeError ( 'each element in list of batch should be of equal size' ) transposed = zip ( * batch ) return [ cf ( samples ) for samples in transposed ] raise TypeError ( default_collate_err_msg_format . format ( elem_type ) )
r"""Puts each data field into a tensor with outer dimension batch size
r " " " Puts each data field into a tensor with outer dimension batch size
r"""Puts each data field into a tensor with outer dimension batch size""" # If we're in a background process, concatenate directly into a # shared memory tensor to avoid an extra copy # array of string classes and object # return cf([torch.as_tensor(b) for b in batch]) # scalars # check to make sure that the elements in batch have consistent size
batch