A walk with fastai2 - Vision - Study Group and Online Lectures Megathread

ah homework … i get the error below when commented out

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-27-f8c52f244ba9> in <module>()
----> 1 learn_gen = create_gen_learner()

1 frames
/usr/local/lib/python3.6/dist-packages/fastai2/vision/learner.py in unet_learner(dls, arch, loss_func, pretrained, cut, splitter, config, n_in, n_out, normalize, **kwargs)
    189     size = dls.one_batch()[0].shape[-2:]
    190     if n_out is None: n_out = get_c(dls)
--> 191     assert n_out, "`n_out` is not defined, and could not be infered from data, set `dls.c` or pass `n_out`"
    192     if normalize: _add_norm(dls, meta, pretrained)
    193     model = models.unet.DynamicUnet(body, n_out, size, **config)

AssertionError: `n_out` is not defined, and could not be infered from data, set `dls.c` or pass `n_out`

and this when c=4

RuntimeError: The size of tensor a (2097152) must match the size of tensor b (1572864) at non-singleton dimension 0

and in digging in the code, i see that c is passed to the DynamicUnet and used in a ConvLayer. I didn’t completely follow the unet lecture :frowning: and want to revisit, but for now i am ok with understanding that if c is not set correctly, we’ll get a tensor size error. thanks for answering my question.

1 Like

Yes, it’s especially important because it helps declare the number of channels/outputs in our mask, hence why it’s so large.

@hello34 how does your dls.c look? It’s vocab? Does it seem to pick up all the classes? :slight_smile:

I will let you know the result after my Kaggle kernel commits.

hi @hello34 i am just curious why you are using a RnadomSplitter since you are given a validation set you probably want to use GrandParentSplitter.
and get_y i htink should be parent_label, you are trying to tell it how to get the labels.
I haven’t tried it out so there could be errors in what i’m saying :slight_smile:

2 Likes

@muellerzr it didn’t work because I am not passing the right get_y, also. Let my try out the solution Proposed by @barnaci

It resembles this Imagenette tutorial way of DataLoading http://dev.fast.ai/tutorial.imagenette

Also I tried out fastai higher lever API with higerlevel API:

flower_path = '/kaggle/input/104-flowers-garden-of-eden/jpeg-512x512'
data = ImageDataLoaders.from_folder(flower_path, batch_tfms=batch_tfms, 
                                   item_tfms=item_tfms, bs=bs)

which loads the data but on training the model with code:

learn = cnn_learner(data, resnet34, pretrained=True, metrics=error_rate, ).to_fp16()
learn.fit_one_cycle(3)

Returns a training , with no valid_loss and error_rate and can’t be used to interept results as well

Screenshot_2020-03-07 FastAI v2 image classification Kaggle

for the higher level api you should set your validation folder name in ImageDataLoaders.from_folder as an attribute. The default is ‘valid’ and you seem to have the folder name ‘val’

1 Like

Thank you @barnacl. Both of your suggestions worked out. Thank you for helping me out!

GrandparentSplitter naming is quite good in new fastaiv2

1 Like

@muellerzr you told today , fastai.text can be used for only certain problems right? What are they?

Also in the adult notebook, after defining net following Jeremy’s rule of thumb, we have not subsequently used net anywhere. What was the purpose of net in definition?

As I said in the lecture, to show what tabular_learner is doing in the background

Text based problems. We’ll cover those in the NLP block.

1 Like

@muellerzr do you have a megathread for NLP? Have a lovely Sunday :smiling_face_with_three_hearts:

Nope :slight_smile: I’ll make one now :wink:

@mgloria still a month or so out, but here: A walk with fastai2 - Text - Study Group and Online Lectures Megathread

is there an easy way to run the 2018 Object Detection notebook (in terms of installations).
i was not able to find a thread(i might have missed it) any help ?

Hi,

I am trying to implement rgb_transform on fastai2. Here is the code:

@patch
def rgb_randomize(x:TensorImage, channel:int=None, thresh:float=0.3, p=0.5):
    "Randomize one of the channels of the input image"
    if channel is None: channel = np.random.randint(0, x.shape[2])
    x[:,:,channel] = 255*(torch.rand(x[:,:,channel].shape) * np.random.uniform(0, thresh))
    return x
class rgb_transform(RandTransform):
    def __init__(self, channel=None, thresh=0.3, p=0.5, **kwargs):
        super().__init__(p=p)
        self.channel,self.thresh,self.p = channel,thresh,p
    def encodes(self, x:TensorImage): return x.rgb_randomize(channel=self.channel,thresh=self.thresh, p=self.p )
x = TensorImage(img)
_,axs = subplots(2, 4)
for ax in axs.flatten():
    show_image(rgb_transform(channel=0, thresh=0.99)(x, split_idx=0), ctx=ax)

As you can see, the transform works nicely after modifying a little bit the code from v1. However, I am struggling now to applying in a project. If I do:

item_tfms=[RandomResizedCrop(size, min_scale=0.35)]
batch_tfms=[*aug_transforms(flip_vert=True, xtra_tfms=rgb_transform(channel=1, thresh=0.99, p=0.9))]

dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),
                    splitter=GrandparentSplitter(),
                    get_items=get_image_files,
                    get_y=parent_label,
                    item_tfms = item_tfms,
                    batch_tfms=batch_tfms)

dbunch = dblock.dataloaders(path, path=path, bs=bs, num_workers=8)
dbunch.show_batch()

I got images without any rgb_transform. However, if I do:
dblock.summary(path)

I see that the transform is applied:

Setting-up type transforms pipelines
Collecting items from /home/jg/DeepLearning/Datasets/Colon_9_classes/images
Found 107180 items
2 datasets of sizes 100000,7180
Setting up Pipeline: PILBase.create
Setting up Pipeline: parent_label -> Categorize

Building one sample
  Pipeline: PILBase.create
    starting from
      /home/jg/DeepLearning/Datasets/Colon_9_classes/images/train/Background/BACK-WQRMCART.tif
    applying PILBase.create gives
      PILImage mode=RGB size=224x224
  Pipeline: parent_label -> Categorize
    starting from
      /home/jg/DeepLearning/Datasets/Colon_9_classes/images/train/Background/BACK-WQRMCART.tif
    applying parent_label gives
      Background
    applying Categorize gives
      TensorCategory(1)

Final sample: (PILImage mode=RGB size=224x224, TensorCategory(1))


Setting up after_item: Pipeline: RandomResizedCrop -> FlipItem -> ToTensor
Setting up before_batch: Pipeline: 
Setting up after_batch: Pipeline: rgb_transform -> IntToFloatTensor -> AffineCoordTfm -> LightingTfm

Building one batch
Applying item_tfms to the first sample:
  Pipeline: RandomResizedCrop -> FlipItem -> ToTensor
    starting from
      (PILImage mode=RGB size=224x224, TensorCategory(1))
    applying RandomResizedCrop gives
      (PILImage mode=RGB size=224x224, TensorCategory(1))
    applying FlipItem gives
      (PILImage mode=RGB size=224x224, TensorCategory(1))
    applying ToTensor gives
      (TensorImage of size 3x224x224, TensorCategory(1))

Adding the next 3 samples

No before_batch transform to apply

Collating items in a batch

Applying batch_tfms to the batch built
  Pipeline: rgb_transform -> IntToFloatTensor -> AffineCoordTfm -> LightingTfm
    starting from
      (TensorImage of size 4x3x224x224, TensorCategory([1, 1, 1, 1], device='cuda:0'))
    applying rgb_transform gives
      (TensorImage of size 4x3x224x224, TensorCategory([1, 1, 1, 1], device='cuda:0'))
    applying IntToFloatTensor gives
      (TensorImage of size 4x3x224x224, TensorCategory([1, 1, 1, 1], device='cuda:0'))
    applying AffineCoordTfm gives
      (TensorImage of size 4x3x224x224, TensorCategory([1, 1, 1, 1], device='cuda:0'))
    applying LightingTfm gives
      (TensorImage of size 4x3x224x224, TensorCategory([1, 1, 1, 1], device='cuda:0'))

Any idea why is not working?
Thanks

Be careful to set a proper order to your transform. If you look closely at the summary, it is applied before the IntToFloatTensor transform, which means it’s applied on a tensor of ints. This transform should have an order of 10 at the least.

2 Likes

Thanks,
I add order=50 in the rgb_transform class but I am still not able to see the transform in the show_batch().
I notice that like in lighting transforms, I should do x= TensorImage(img) before using the rgb_trasnform. Could be related to this?

Hi! I ran into the same error when trying to run your notebook 05_Inference_Server.ipynb.

I have export the former notebook into a module style-transform. The style-transfer.py is correctly generated and contains the TransformerNet-Class.

Any idea what went wrong? Thanks!

Before loading the learner, you need to import your exported module (the styletransfer.py)

1 Like

Thanks for the fast reply! Hmm I thought I had done this by

from style_transfer import *

Where the folder style_transfer is in the same directory as the inference notebook and contains these files:

  • __init__.py
  • _nbdev.py
  • style-transfer.py

But you are right I cannot create objects from any of the classes contained within the style-transfer.py file.

You have a naming issue that’s why. Notice how one is “-“ and the other is “”. You should export it with “” and it should work (not entirely sure why italics but we’ll roll with it)

1 Like