file_name='export.pkl' 50 ---> 51 predictor = Predict(file_name)
I'm just trying to recall my model (learner model) of fastai pytorch i've already made, trained and saved in export.pkl now i'm recalling that model in another deployment app(using streamlit) to to classify the uploaded image based on the previously trained model
class Predict:
def __init__(self, filename):
self.learn_inference = load_learner(Path()/filename)
self.img = self.get_image_from_upload()
if self.img is not None:
self.display_output()
self.get_prediction()
@staticmethod
def get_image_from_upload():
uploaded_file = st.file_uploader("Upload Files",type=['png','jpeg', 'jpg'])
if uploaded_file is not None:
return PILImage.create((uploaded_file))
return None
def display_output(self):
st.image(self.img.to_thumb(500,500), caption='Uploaded Image')
def get_prediction(self):
if st.button('Classify'):
pred, pred_idx, probs = self.learn_inference.predict(self.img)
st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
else:
st.write(f'Click the button to classify')
if __name__=='__main__':
file_name='export.pkl'
predictor = Predict(file_name)
and i always get this error, i think it's back to the last 3 lines in the code but i don't understand what's wrong
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2652/833913464.py in <module>
3 file_name='export.pkl'
4
----> 5 predictor = Predict(file_name)
~\AppData\Local\Temp/ipykernel_2652/1038630025.py in __init__(self, filename)
17 def __init__(self, filename):
18
---> 19 self.learn_inference = load_learner(Path()/filename)
20 self.img = self.get_image_from_upload()
21 if self.img is not None:
~\anaconda3\lib\site-packages\fastai\basic_train.py in load_learner(path, file, test, tfm_y, **db_kwargs)
619 "Load a `Learner` object saved with `export_state` in `path/file` with empty data, optionally add `test` and load on `cpu`. `file` can be file-like (file or buffer)"
620 source = Path(path)/file if is_pathlike(file) else file
--> 621 state = torch.load(source, map_location='cpu') if defaults.device == torch.device('cpu') else torch.load(source)
622 model = state.pop('model')
623 src = LabelLists.load_state(path, state.pop('data'))
~\anaconda3\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
592 pickle_load_args['encoding'] = 'utf-8'
593
--> 594 with _open_file_like(f, 'rb') as opened_file:
595 if _is_zipfile(opened_file):
596 # The zipfile reader is going to advance the current file position.
~\anaconda3\lib\site-packages\torch\serialization.py in _open_file_like(name_or_buffer, mode)
228 def _open_file_like(name_or_buffer, mode):
229 if _is_path(name_or_buffer):
--> 230 return _open_file(name_or_buffer, mode)
231 else:
232 if 'w' in mode:
~\anaconda3\lib\site-packages\torch\serialization.py in __init__(self, name, mode)
209 class _open_file(_opener):
210 def __init__(self, name, mode):
--> 211 super(_open_file, self).__init__(open(name, mode))
212
213 def __exit__(self, *args):
FileNotFoundError: [Errno 2] No such file or directory: 'export.pkl\\export.pkl'
How many English words
do you know?
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
how many words do you know
Powered by Examplum