のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

fastai create from ll

    @classmethod
    def create_from_ll(cls,
         lls:LabelLists,
         bs:int=64,
         val_bs:int=None,
         ds_tfms:Optional[TfmList]=None,
         num_workers:int=defaults.cpus,
         dl_tfms:Optional[Collection[Callable]]=None,
         device:torch.device=None,
         test:Optional[PathOrStr]=None,
         collate_fn:Callable=data_collate,
         size:int=None, <=サイズ
         no_check:bool=False,
         resize_method:ResizeMethod=None,
         mult:int=None,
         padding_mode:str='reflection',
         mode:str='bilinear',
         tfm_y:bool=False)->'ImageDataBunch':

        "Create an `ImageDataBunch` from `LabelLists` `lls` with potential `ds_tfms`."

        lls = lls.transform(
            tfms=ds_tfms,
            size=size,   <== サイズ
            resize_method=resize_method,
            mult=mult,
            padding_mode=padding_mode,
            mode=mode,
            tfm_y=tfm_y)

        if test is not None: lls.add_test_folder(test)
        return lls.databunch(
            bs=bs,
            val_bs=val_bs,
            dl_tfms=dl_tfms,
            num_workers=num_workers,
            collate_fn=collate_fn,
            device=device,
            no_check=no_check)

class LabelLists(ItemLists): "A LabelList for each of train and valid (optional test)."

class ItemLists():
    "An `ItemList` for each of `train` and `valid` (optional `test`)."

    def __init__(self, 
        path:PathOrStr, 
        train:ItemList, 
        valid:ItemList):

        self.path,self.train,self.valid,self.test = Path(path),train,valid,None

        if not self.train.ignore_empty and len(self.train.items) == 0:
            warn("Your training set is empty. If this is by design, pass `ignore_empty=True` to remove this warning.")

        if not self.valid.ignore_empty and len(self.valid.items) == 0:
            warn("""Your validation set is empty. If this is by design, use `split_none()`
                 or pass `ignore_empty=True` when labelling to remove this warning.""")

        if isinstance(self.train, LabelList): self.__class__ = LabelLists
    def transform(self, tfms:Optional[Tuple[TfmList,TfmList]]=(None,None), **kwargs):
        "Set `tfms` to be applied to the xs of the train and validation set."
        if not tfms: tfms=(None,None)
        assert is_listy(tfms) and len(tfms) == 2, "Please pass a list of two lists of transforms (train and valid)."
        self.train.transform(tfms[0], **kwargs)
        self.valid.transform(tfms[1], **kwargs)
        if self.test: self.test.transform(tfms[1], **kwargs)
        return self

data_block.py#707

    def transform(self, tfms:TfmList, tfm_y:bool=None, **kwargs):
        "Set the `tfms` and `tfm_y` value to be applied to the inputs and targets."
        _check_kwargs(self.x, tfms, **kwargs)
        if tfm_y is None: tfm_y = self.tfm_y
        if tfm_y: _check_kwargs(self.y, tfms, **kwargs)
        self.tfms,self.tfmargs = tfms,kwargs
        self.tfm_y,self.tfms_y,self.tfmargs_y = tfm_y,tfms,kwargs
        return self
def _check_kwargs(ds:ItemList, tfms:TfmList, **kwargs):
    tfms = listify(tfms)
    if (tfms is None or len(tfms) == 0) and len(kwargs) == 0: return
    if len(ds.items) >= 1:
        x = ds[0]
        try: x.apply_tfms(tfms, **kwargs)
        except Exception as e:
            raise Exception(f"It's not possible to apply those transforms to your dataset:\n {e}")
    def get(self, i):
        fn = super().get(i)
        res = self.open(fn)
        self.sizes[i] = res.size
        return res
    def open(self, fn):
        "Open image in `fn`, subclass and overwrite for custom behavior."
        return open_image(fn, convert_mode=self.convert_mode, after_open=self.after_open)
def open_image(fn:PathOrStr, div:bool=True, convert_mode:str='RGB', cls:type=Image,
        after_open:Callable=None)->Image:
    "Return `Image` object created from image in file `fn`."
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", UserWarning) # EXIF warning from TiffPlugin
        x = PIL.Image.open(fn).convert(convert_mode)
    if after_open: x = after_open(x)
    x = pil2tensor(x,np.float32) <==
    if div: x.div_(255)
    return cls(x)

<PIL.Image.Image image mode=RGB size=64x64 at 0x2080EA6B4A8>

def pil2tensor(image:Union[NPImage,NPArray],dtype:np.dtype)->TensorImage:
    "Convert PIL style `image` array to torch style image tensor."
    a = np.asarray(image)
    if a.ndim==2 : a = np.expand_dims(a,2)
    a = np.transpose(a, (1, 0, 2))
    a = np.transpose(a, (2, 1, 0))
    return torch.from_numpy(a.astype(dtype, copy=False) )

shape(64,64,3) a = np.transpose(a, (1, 0, 2)) a = np.transpose(a, (2, 1, 0)) shape(3,64,64)