Hi James,
thank you for your help!
I tried the get_image_files option and this is the error message I get:
Error with get_image_files
> TypeError: expected str, bytes or os.PathLike object, not UploadedFile
> Traceback:
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
> exec(code, module.__dict__)
> File "/Users/joankusuma/Downloads/test-fashion/ctl-dataset/streamlit-app/app.py", line 40, in <module>
> embeddings_list = img2vec(file, model_path)
> ^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 212, in wrapper
> return cached_func(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 241, in __call__
> return self._get_or_create_cached_value(args, kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 267, in _get_or_create_cached_value
> return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 321, in _handle_cache_miss
> computed_value = self._info.func(*func_args, **func_kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Users/joankusuma/Downloads/test-fashion/ctl-dataset/streamlit-app/src/utils.py", line 51, in img2vec
> files = get_image_files(file)
> ^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/data/transforms.py", line 61, in get_image_files
> return get_files(path, extensions=image_extensions, recurse=recurse, folders=folders)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/data/transforms.py", line 32, in get_files
> path = Path(path)
> ^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 871, in __new__
> self = cls._from_parts(args)
> ^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 509, in _from_parts
> drv, root, parts = self._parse_args(args)
> ^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 493, in _parse_args
> a = os.fspath(a)
> ^^^^^^^^^^^^
I also tried returning the hash value of the image like this:
img = Image.open(file).convert("RGB")
img = img.resize((128, 128))
img = io.BytesIO()
img_hash = hash(img.getvalue())
inf_dblock = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=lambda x: [img_hash],
item_tfms=Resize(128),
batch_tfms=[Normalize.from_stats(*imagenet_stats)]
)
and got the following error message:
> RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [1]
> Traceback:
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
> exec(code, module.__dict__)
> File "/Users/joankusuma/Downloads/test-fashion/ctl-dataset/streamlit-app/app.py", line 40, in <module>
> embeddings_list = img2vec(file, model_path)
> ^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 212, in wrapper
> return cached_func(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 241, in __call__
> return self._get_or_create_cached_value(args, kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 267, in _get_or_create_cached_value
> return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 321, in _handle_cache_miss
> computed_value = self._info.func(*func_args, **func_kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Users/joankusuma/Downloads/test-fashion/ctl-dataset/streamlit-app/src/utils.py", line 74, in img2vec
> _ = learner.get_preds(dl=inf_dls)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 308, in get_preds
> self._do_epoch_validate(dl=dl)
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 244, in _do_epoch_validate
> with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 199, in _with_events
> try: self(f'before_{event_type}'); f()
> ^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 205, in all_batches
> for o in enumerate(self.dl): self.one_batch(*o)
> ^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 235, in one_batch
> self._with_events(self._do_one_batch, 'batch', CancelBatchException)
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 199, in _with_events
> try: self(f'before_{event_type}'); f()
> ^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fastai/learner.py", line 216, in _do_one_batch
> self.pred = self.model(*self.xb)
> ^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
> return forward_call(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/container.py", line 217, in forward
> input = module(input)
> ^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
> return forward_call(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/container.py", line 217, in forward
> input = module(input)
> ^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
> return forward_call(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/conv.py", line 463, in forward
> return self._conv_forward(input, self.weight, self.bias)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/nn/modules/conv.py", line 459, in _conv_forward
> return F.conv2d(input, weight, bias, self.stride,
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
At this moment, I think I’ll need to re-train the model and change the data pre-processing method, so the image will be hashable and match the model’s input expectation, but I’m not sure if that’s the best approach.