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

All the best for your mid-term! I am very much looking forward to the tabular part. Meanwhile, would it be possible to please tell me what all architectures will be covered?

I’ve already spoken on this some, and it’s in the schedule on the first post as well. I may change them depending on model performance. (IE I’m removing NODE)

not sure if this is helpful, but i do see a set_seed https://github.com/fastai/fastai2/blob/9d750d582a692f8c4f011f5c623ba4c7ab50f06d/fastai2/torch_core.py#L119 which looks like it’s setting all the randomization.

1 Like

Thank you @foobar8675 I tried that too but it didn’t help. If I’m not wrong the purpose of set_seed is different. There is another thread that some people have had some success.

fastai doc and here

cool, if you figure it out, can you please let me know. I am curious.

1 Like

sure thing, just posted here

Hey @muellerzr and @mrfabulous1, did you manage to see if predict can be called with BytesIO, and if so, how?

I’m also trying to deploy a fastai2 model, and got an error running pred=learn.predict(BytesIO(img_bytes))[0] (see below)

Thanks!
Sebastien

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 102, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 140, in simple_response
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 550, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "app/server.py", line 67, in analyze
    pred = learn.predict(img_bytes)[0]
  File "/usr/local/lib/python3.7/site-packages/fastai2/learner.py", line 332, in predict
    dl = self.dls.test_dl([item], rm_type_tfms=rm_type_tfms)
  File "/usr/local/lib/python3.7/site-packages/fastai2/data/core.py", line 333, in test_dl
    test_ds = test_set(self.valid_ds, test_items, rm_tfms=rm_type_tfms) if isinstance(self.valid_ds, (Datasets, TfmdLists)) else test_items
  File "/usr/local/lib/python3.7/site-packages/fastai2/data/core.py", line 316, in test_set
    test_tls = [tl._new(test_items, split_idx=1) for tl in dsets.tls[:dsets.n_inp]]
  File "/usr/local/lib/python3.7/site-packages/fastai2/data/core.py", line 316, in <listcomp>
    test_tls = [tl._new(test_items, split_idx=1) for tl in dsets.tls[:dsets.n_inp]]
  File "/usr/local/lib/python3.7/site-packages/fastai2/data/core.py", line 204, in _new
    def _new(self, items, **kwargs): return super()._new(items, tfms=self.tfms, do_setup=False, types=self.types, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastai2/data/core.py", line 172, in _new
    def _new(self, items, **kwargs): return super()._new(items, splits=self.splits, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 315, in _new
    def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 41, in __call__
    res = super().__call__(*((x,) + args), **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastai2/data/core.py", line 198, in __init__
    self.tfms = Pipeline(tfms, as_item=as_item, split_idx=split_idx)
TypeError: __init__() got an unexpected keyword argument 'as_item'

@sebderhy I did not, however how are you installing fastai2?

@muellerzr I’m using a dockerized environment inspired from your repo (thanks btw, it’s super useful!). However, I didn’t manage to make the requirements.txt from your repo work out of the box, so I used the one below. Also, I used fastai2 0.0.10 for compatibility with my training version.

starlette
uvicorn
aiohttp
aiofiles
ipykernel
python-multipart
fastai2==0.0.10

@sebderhy I won’t be able to look at this for awhile, my recommendation is try building a very simple and fast model (like an MNIST) and try the same thing with the most recent version of fastai2 and fastcore. If that breaks I’ll put in an issue (as this example was taken from their examples in the course/ section) but again, wont be able to really look at it as I’m loaded up with the course :slight_smile:

just noticed the error is complaining about as_item. i was getting the same yesterday. do you have the updated fastcore? 0.1.14.
Not sure if that helps. :smiley:
Could you show a little more code - how is img_bytes created ?

I think you want to use fastai2 - 0.0.11 with fastcore-0.1.14(latest)
or fastai2 - 0.0.10 with fastcore-0.1.12 (for 0.0.13 i was getting the as_item error)

2 Likes

Anyone know if there’s documentation for ImageDataLoaders? Nothing seems to come up on the fastai2 docs.

They are defined in vision.data.
In general, it’s best for now to look inside the source notebooks for the documentation of functions as we have no kept the sidebar up to date with all modules and we have done no work to make the docs look nice yet.

1 Like

I’ll also add that I personally prefer them, as if you explore them well enough most everything starts to make sense :slight_smile: (explore = playing around with)

@muellerzr, totally understand, thanks a lot already for your help :slight_smile: !

@barnacl, thanks a lot for your advice! I updated both the training and deployment code to 0.0.11 and I now get a much more understandable error (see below).

The code I use is as follows:

@app.route('/analyze', methods=['POST'])
async def analyze(request):
  img_data = await request.form()
  img_bytes = await (img_data['file'].read())
  img = BytesIO(img_bytes)
  pred = learn.predict(img)[0]
  return JSONResponse({
      'results': str(pred),
  })

And the error is:

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'>

Therefore, I tried to feed img_bytes directly, but this time got a GPU error, although I specificed defaults.device = torch.device('cpu') before:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 102, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 140, in simple_response
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 550, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "app/server.py", line 72, in analyze
    pred = learn.predict(img_bytes)[0]
  File "/usr/local/lib/python3.7/site-packages/fastai2/learner.py", line 215, in predict
    inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
  File "/usr/local/lib/python3.7/site-packages/fastai2/learner.py", line 202, in get_preds
    self(event.begin_epoch if inner else _before_epoch)
  File "/usr/local/lib/python3.7/site-packages/fastai2/learner.py", line 108, in __call__
    def __call__(self, event_name): L(event_name).map(self._call_one)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 362, in map
    return self._new(map(g, self))
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 315, in _new
    def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 41, in __call__
    res = super().__call__(*((x,) + args), **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 306, in __init__
    items = list(items) if use_list else _listify(items)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 242, in _listify
    if is_iter(o): return list(o)
  File "/usr/local/lib/python3.7/site-packages/fastcore/foundation.py", line 208, in __call__
    return self.fn(*fargs, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fastai2/learner.py", line 111, in _call_one
    [cb(event_name) for cb in sort_by_run(self.cbs)]
  File "/usr/local/lib/python3.7/site-packages/fastai2/learner.py", line 111, in <listcomp>
    [cb(event_name) for cb in sort_by_run(self.cbs)]
  File "/usr/local/lib/python3.7/site-packages/fastai2/callback/core.py", line 23, in __call__
    if self.run and _run: getattr(self, event_name, noop)()
  File "/usr/local/lib/python3.7/site-packages/fastai2/callback/core.py", line 43, in begin_fit
    self.model.to(self.dls.device)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 425, in to
    return self._apply(convert)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 201, in _apply
    module._apply(fn)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 201, in _apply
    module._apply(fn)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 223, in _apply
    param_applied = fn(param)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 423, in convert
    return t.to(device, dtype if t.is_floating_point() else None, non_blocking)
  File "/usr/local/lib/python3.7/site-packages/torch/cuda/__init__.py", line 196, in _lazy_init
    _check_driver()
  File "/usr/local/lib/python3.7/site-packages/torch/cuda/__init__.py", line 101, in _check_driver
    http://www.nvidia.com/Download/index.aspx""")
AssertionError:
Found no NVIDIA driver on your system. Please check that you
have an NVIDIA GPU and installed a driver from
http://www.nvidia.com/Download/index.aspx

I feel close, will continue to debug, and keep you posted if I find a solution :).

Thanks,
Sebastien

1 Like

Can you show the code for feeding that in? :slight_smile:

Edit: I could follow through the stack trace there. What you could try to do is first covert the bytes to a PILImage then feed that in. This way we could see if it’s possible, and if so we can put a PR for predict

2 Likes

Sure :slight_smile:

@app.route('/analyze', methods=['POST'])
async def analyze(request):
  img_data = await request.form()
  img_bytes = await (img_data['file'].read())
  print(type(img_bytes))
  img_np = np.array(Image.open(io.BytesIO(img_bytes)))
  print(type(img_np))
  img_io_bytes = BytesIO(img_bytes)
  pred = learn.predict(img_bytes)[0]
  return JSONResponse({
      'results': str(pred),
  })

Btw, I also tried to feed the numpy array img (it worked on my GPU machine), and got the same GPU error. Thanks

Cool, so we know that it works if nothing else :slight_smile: try (manually) setting the model to CPU:

learn.model = learn.model.cpu()

1 Like

No, same error…

BTW, in order to make model loading work within the (CPU) docker, I had to modify a bit the torch.load line. Here’s the function I used:

async def setup_learner():
    # await download_file(export_file_url, path / export_file_name)
    try:
        learn = torch.load(path/export_file_name, map_location=torch.device('cpu'))
        learn.model = learn.model.cpu()
        return learn
    except RuntimeError as e:
        if len(e.args) > 0 and 'CPU-only machine' in e.args[0]:
            print(e)
            message = "\n\nThis model was trained with an old version of fastai and will not work in a CPU environment.\n\nPlease update the fastai library in your training environment and export your model again.\n\nSee instructions for 'Returning to work' at https://course.fast.ai."
            raise RuntimeError(message)
        else:
            raise
1 Like

I’ve got some time this morning, I’ll take a look

1 Like