Hello, I am looking for help in loading and running inference with the Siamese model. I created a siamese model as described in this tutorial: fastai - Custom new task - siamese. I moved the SiameseImage
class to a Python file so I could import it from both the training notebook and the inference notebook. Everything worked and looked fine. Then I save the model with the following code:
import dill
learn.export('siam_catsvdogs_tutorial.pkl', pickle_module=dill)
Then I loaded the model and an input image and visualized it with the following code:
from fastai.vision.all import *
import dill
learner = load_learner('siam_catsvdogs_tutorial.pkl', cpu=False, pickle_module=dill)
path = untar_data(URLs.PETS)
files = get_image_files(path/"images")
from siam_utils import SiameseImage
# I moved SiameseImage to a file because if I copied and pasted the code I would get the following error
# AssertionError: Expected an input of type in
# - <class 'pathlib.PosixPath'>
# - <class '__main__.SiameseImage'>
# but got <class '__main__.SiameseImage'>
imgtest = PILImage.create(files[0])
imgval = PILImage.create(files[100])
siamtest = SiameseImage(imgval, imgtest)
siamtest.show();
When I run that everything seems to work and it displays an image like I would expect. Then when I copy the siampredict
patch from the tutorial, I get an error when I run it.
@patch
def siampredict(self:Learner, item, rm_type_tfms=None, with_input=False):
res = self.predict(item, rm_type_tfms=None, with_input=False)
if res[0] == tensor(0):
SiameseImage(item[0], item[1], 'Prediction: Not similar').show()
else:
SiameseImage(item[0], item[1], 'Prediction: Similar').show()
return res
res = learn.siampredict(siamtest)
Here’s the error (note my line numbers might be different, but that’s just due to a formatter):
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/tmp/ipykernel_23558/1905235485.py", line 1, in <module>
res = learner.siampredict(siamtest)
File "/tmp/ipykernel_23558/1808791625.py", line 3, in siampredict
res = self.predict(item, rm_type_tfms=None, with_input=False)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/learner.py", line 450, in predict
inp, preds, _, dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/learner.py", line 434, in get_preds
self._do_epoch_validate(dl=dl)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/learner.py", line 352, in _do_epoch_validate
self._with_events(self.all_batches, "validate", CancelValidException)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/learner.py", line 298, in _with_events
f()
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/learner.py", line 306, in all_batches
for o in enumerate(self.dl):
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/data/load.py", line 129, in __iter__
for b in _loaders[self.fake_l.num_workers==0](self.fake_l):
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 681, in __next__
data = self._next_data()
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 721, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 39, in fetch
data = next(self.dataset_iter)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/data/load.py", line 140, in create_batches
yield from map(self.do_batch, self.chunkify(res))
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastcore/basics.py", line 230, in chunked
res = list(itertools.islice(it, chunk_sz))
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/data/load.py", line 155, in do_item
try: return self.after_item(self.create_item(s))
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastcore/transform.py", line 299, in __call__
return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastcore/transform.py", line 234, in compose_tfms
x = f(x, **kwargs)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/fastai/vision/augment.py", line 48, in __call__
self.before_call(b, split_idx=split_idx)
File "/home/julius/miniconda3/envs/pt/lib/python3.8/site-packages/fastai/vision/augment.py", line -1, in before_call
AttributeError: 'int' object has no attribute 'random'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1997, in showtraceback
stb = self.InteractiveTB.structured_traceback(
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1112, in structured_traceback
return FormattedTB.structured_traceback(
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1006, in structured_traceback
return VerboseTB.structured_traceback(
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/ultratb.py", line 859, in structured_traceback
formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/ultratb.py", line 812, in format_exception_as_a_whole
frames.append(self.format_record(r))
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/IPython/core/ultratb.py", line 730, in format_record
result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/stack_data/core.py", line 734, in lines
pieces = self.included_pieces
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/stack_data/core.py", line 681, in included_pieces
pos = scope_pieces.index(self.executing_piece)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/stack_data/core.py", line 660, in executing_piece
return only(
File "/home/julius/miniconda3/envs/pt2/lib/python3.10/site-packages/executing/executing.py", line 167, in only
raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0
I’m able to run inference in the same notebook that I trained the model, but for some reason I’m not when I load it. Does anyone have any ideas why?