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

If you take a look at the screenshot, I am printing the variable path/n and it is pointing to data/sla/tarp, and path_f is tarp.txt. Is that not correct?

They both need it in this case, as the first path is the destination, the second path should point to exactly where our file is stored.

1 Like

@muellerzr, thank you. I have it running now. Can you please answer one more question: Now that I have a trained model, how can I use it behind a flask server (CPU only) for inference? If you could point me to some resources, please. I can see that there are two files called stage1.pth and stage2.pth, created in the models directory. How do I take them and use them for inference?

We go into that in the next lesson for how to set your code up in general (geared towards Render but it’s applicable JavaScript). Otherwise others may chime in. And the rest of your questions are answered then too. But if you right click the file you can download it

To better understand L class, you may clone the fastcore repo on your local machine and run the nbs\01_foundation.ipynb notebook (it’s the one that generates foundation.py). There are may tests that show L class uses cases. For example:

t = L(range(12))
test_eq(t, list(range(12)))
test_ne(t, list(range(11)))
t.reverse()
test_eq(t[0], 11)
t[3] = "h"
test_eq(t[3], "h")
t[3,5] = ("j","k")
test_eq(t[3,5], ["j","k"])
test_eq(t, L(t))
test_eq(L(L(1,2),[3,4]), ([1,2],[3,4]))
t = L()
test_eq(t, [])
t.append(1)
test_eq(t, [1])
t += [3,2]
test_eq(t, [1,3,2])
t = t + [4]
test_eq(t, [1,3,2,4])
t = 5 + t
test_eq(t, [5,1,3,2,4])
test_eq(L(1,2,3), [1,2,3])
test_eq(L(1,2,3), L(1,2,3))
t = L(1)*5
t = t.map(operator.neg)
test_eq(t,[-1]*5)
test_eq(~L([True,False,False]), L([False,True,True]))
t = L(range(4))
test_eq(zip(t, L(1).cycle()), zip(range(4),(1,1,1,1)))
t = L.range(100)
test_shuffled(t,t.shuffle())

You can also create new cells in the same notebook and run your own experiments. For instance, if you are intrigued by the output of say zip(t, L(1).cycle()) you can create a cell like the one here below and run it, and display the output:

t = L(range(4))
r = zip(t, L(1).cycle())
r

If you want to restore the original file, you can run git restore nbs\01_foundation.ipynb

2 Likes

Hi vahuja4 hope all is well!

If you could point me to some resources, please.

I suggest you read the following threads.

https://forums.fast.ai/t/free-web-deployment/62333m - This shows the challenges of deploying on a platform which you haven’t used before.

Deployment Platform: Render ✅ This is the thread I would recommend if your not an expert in web apps, because it uses starter code that can be found here https://course.fast.ai/deployment_render.html, it can also be set up locally, and there many posts on the render thread discussing issues.

Also in the previous link are four other options such as AWS however they all can be tricky if your not experienced in html, css, javascript, docker, web apps, server side coding etc.

I can see that there are two files called stage1.pth and stage2.pth, created in the models directory.
How do I take them and use them for inference?

I would suggest you complete the production part of the notebook and create a .pkl file for your model save this on your G drive or locally and and run (pip list or pip freeze) and save the contents to a text file as you will need to make sure the library versions of the platform you deploy on are the same versions as the platform you trained on. Save this with your .pth files though I see more deployments with .pkl files than .pth files.

Remember fastai2 is still in development so there are not many deployment resources yet.
All the resources mentioned above have worked for fastai version 1.

Many people find the deploying part way more difficult than running through the notebook and Jeremy recommends building an app as it really shows how much you have understood.

If your a web app genius you can create your own app for inference!

Cheers mrfabulous1 :smiley: :smiley:

2 Likes

Good afternoon everyone hope you are all having a jolly day!

Has anyone managed to deploy lesson one or lesson five as a web app, I have created 10’s of apps in fastai1 but have not been able to complete the hello world of fastai (the lesson 1 image classifier)

.

If some one could share a working repository of an app that they have got working using lesson 1 or lesson 5 that would be great.

On lesson 1 and 5 i am using the https://github.com/render-examples/fastai-v3 as a template

I get the following error when deploying lesson 5 app!

   (fastai2) Mrs-MacBook-Pro:fastai-v3-master fabulous$ python app/server.py serve
Traceback (most recent call last):
  File "app/server.py", line 51, in <module>
    learn = loop.run_until_complete(asyncio.gather(*tasks))[0]
  File "/opt/anaconda3/envs/fastai2/lib/python3.7/asyncio/base_events.py", line 583, in run_until_complete
    return future.result()
  File "app/server.py", line 38, in setup_learner
    learn = torch.load(path/export_file_name, map_location=torch.device('cpu'))
  File "/opt/anaconda3/envs/fastai2/lib/python3.7/site-packages/torch/serialization.py", line 529, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
  File "/opt/anaconda3/envs/fastai2/lib/python3.7/site-packages/torch/serialization.py", line 702, in _legacy_load
    result = unpickler.load()
AttributeError: Can't get attribute 'TransformerNet' on <module '__main__' from 'app/server.py'>
(fastai2) Mrs-MacBook-Pro:fastai-v3-master fabulous$  Can't get attribute 'TransformerNet' on <module '__main__' from 'app/server.py'

Error when deploying lesson 1 app

Successfully installed aiofiles-0.4.0 aiohttp-3.5.4 asyncio-3.4.3 fastai2-0.0.7 h11-0.8.1 httptools-0.0.13 numpy-1.17.5 pillow-6.2.1 python-multipart-0.0.5 starlette-0.12.0 torch-1.3.1 torchvision-0.4.2 uvicorn-0.7.1 uvloop-0.12.2 websockets-7.0
(venv) (base) Mrs-MacBook-Pro:fastai-v3-master fabulous$ python app/server.py serve
INFO: Started server process [12342]
INFO: Waiting for application startup.
INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
INFO: ('127.0.0.1', 51083) - "GET / HTTP/1.1" 200
INFO: ('127.0.0.1', 51083) - "GET /static/style.css HTTP/1.1" 200
INFO: ('127.0.0.1', 51085) - "GET /static/client.js HTTP/1.1" 304
INFO: ('127.0.0.1', 51090) - "POST /analyze HTTP/1.1" 500
ERROR: Exception in ASGI application
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/applications.py", line 133, in __call__
    await self.error_middleware(scope, receive, send)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/middleware/errors.py", line 122, in __call__
    raise exc from None
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/middleware/errors.py", line 100, in __call__
    await self.app(scope, receive, _send)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/middleware/cors.py", line 140, in simple_response
    await self.app(scope, receive, send)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/routing.py", line 585, in __call__
    await route(scope, receive, send)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/routing.py", line 207, in __call__
    await self.app(scope, receive, send)
  File "/opt/anaconda3/lib/python3.7/site-packages/starlette/routing.py", line 40, in app
    response = await func(request)
  File "app/server.py", line 160, in analyze
    pred = learn.predict(BytesIO(img_bytes))[0]
  File "/opt/anaconda3/lib/python3.7/site-packages/fastai2/learner.py", line 325, in predict
    dl = self.dls.test_dl([item], rm_type_tfms=rm_type_tfms)
  File "/opt/anaconda3/lib/python3.7/site-packages/fastai2/data/core.py", line 315, in test_dl
    test_ds = test_set(self.valid_ds, test_items, rm_tfms=rm_type_tfms) if isinstance(self.valid_ds, Datasets) else test_items
  File "/opt/anaconda3/lib/python3.7/site-packages/fastai2/data/core.py", line 305, in test_set
    if rm_tfms is None: rm_tfms = [tl.infer_idx(test_items[0]) for tl in test_tls]
  File "/opt/anaconda3/lib/python3.7/site-packages/fastai2/data/core.py", line 305, in <listcomp>
    if rm_tfms is None: rm_tfms = [tl.infer_idx(test_items[0]) for tl in test_tls]
  File "/opt/anaconda3/lib/python3.7/site-packages/fastai2/data/core.py", line 222, in infer_idx
    assert idx < len(self.types), f"Expected an input of type in \n{pretty_types}\n but got {type(x)}"
AssertionError: Expected an input of type in 
  - <class 'pathlib.PosixPath'>
  - <class 'pathlib.Path'>
  - <class 'str'>
  - <class 'torch.Tensor'>
  - <class 'numpy.ndarray'>
  - <class 'bytes'>
  - <class 'fastai2.vision.core.PILImage'>
 but got <class '_io.BytesIO'>

Any suggestions, Ideas on how to debug, or words of wisdom welcome.

Cheers mrfabulous1 :smiley: :smiley:

@mrfabulous1 to the TransformerNet issue, you need that style_transfer.py file you generated in the app directory so you can find it and use it. To the BytesIO issue, see sgugger’s comment earlier when this was brought up. I don’t have the time to fix the bug myself

Specifically: A walk with fastai2 - Study Group and Online Lectures Megathread

1 Like

Hi muellerzr hope your day is going fabulously.

Thanks for the quick reply, I didn’t reply to you directly as I know your busy. I am happy that sgugger’s dealing with it. I am also happy that it is actually is an issue and not just my lack of understanding. If my coding skills were better I would fix it myself, but I think I am a little way from that yet.

My only concern is that for the new starters to fastai deploying their first ML app, is probably one of the highlights of their learning year, it was for me anyway.

Cheers mrfabulous1 image .image

1 Like

Once things get less hectic on my end I plan on deploying (at least most) of the models we wind up building. I’ve had thoughts on deploying Maybe on GitHub pages (if they do FTP/interactions) and tagging in an s3 bucket request or something (if it can’t be done on pages manually) for doing predictions. But I do plan on eventually getting to this and tackling it. (Not saying it will be soon)

1 Like

That’s my goal also lol!
On any platform that is cheap that I can understand!
:smiley: :smiley:

2 Likes

that is helpful. for some reason, i had it in my head it was like the F in pytorch. thank you.

1 Like

Here is the schedule for tomorrow’s lesson:

  • Keypoint Regression
  • Keypoint with UNet
  • Object Detection
  • Multimodal models and Kaggle (Bengali.AI) (may need to take this down… looking at the rules, I posted it on the Kaggle discussion forum as well so I think we’re okay, if anyone is more familiar with Kaggle tell me if there is anything else I need to do or if it’s not allowed at all)

Those notebooks are all live (minus Unet, will be done soon), I’ll be working on the Lesson 7 Notebooks this week and then that’ll be it for the vision block :slight_smile:

Lesson 7 will consist of:

  • SuperResolution
  • Audio
  • Siamese Dataloader

For those wanting more GAN, there is a Wassertein GAN example in the course subdirectory here (LSUNS)

1 Like

@mrfabulous1, thank you so much for your detailed reply! Two more questions:

  1. In your response, you have mentioned " I would suggest you complete the production part of the notebook and create a .pkl file for your model" . Prodiction part of which notebook?

  2. I am thinking of using fastai v1 for now, but I am having difficulty installing it on a CPU only machine. Can you please take a look at the post here: Fastai v1 install issues thread

Thank you again for your time!

I had assumed the latter part of the “Custom” notebook but upon further inspection… we never exported the model to get the PKL! Oops (my bad!). So to do so, do learn.export('myModel') and it will show up as myModel in the content panel on colab. (this is shown in the beginning of L2 I think if not I know it’s in the style transfer lesson)

However that is shown on the tail end of the lesson2-download notebook on the fastai2 repo:

Here is the part where Jeremy talks about Hooks from v3 part 2:
Starting at 1:18:17 ish
https://course.fast.ai/videos/?lesson=10

1 Like

Question regarding segmentation: looking at the example notebook, the list of codes looks the following:


So “Animal” would be 0, Archway “1”… and so on. Is there a way to change this? i.e. my food categories have other ids so I would like to pass to fastai the correct pairs e.g. corn “1234”, bread “6543”, popcorn “7654”. Is there a way to pass this dictionary?

A workaround would be to go with it and then update the mask to the correct ids but it feels to me as very inefficient.

You may be able to do it if you sort by lowest to highest value (possibly) and build your list via that. If we check the MaskBlock code we trail to https://github.com/fastai/fastai2/blob/master/fastai2/vision/core.py#L121

We can see that it is just a list of codes (though try a list. It may or may not work, not sure!)

@mgloria i wanted to clarify - you are currently training with non continuous codes ?

for some reason i thought the codes needed to be continuous. That’s why i wanted to check thanks :slight_smile:

Regarding your question i feel i’m missing something, you currently have corn : “1234” but you want to change that to corn : “1” ? That means you have to change your mask values ?

in style transfer why are we doing .eval ?
feat_net = vgg19(pretrained=True).features.cuda().eval()

from my understanding .eval is used for changing behaviour of bn and dropout layers, with vgg19, vgg16 we don’t have those layers in the feature extractor. Is this added just as a safety measure when we add an arch that has one of those layers ?