-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Image prediction using trained model #2392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
fac8a9d
25f629c
63bd066
f51b7c4
ec2f9a7
28155cd
5e9358c
d3c1758
49f310a
b3ca83d
6f2006d
7fc11a1
cefedea
e9fbb22
74ba612
c9dea0f
0650b47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
import matplotlib.pyplot as plt | ||
import time | ||
import os | ||
from PIL import Image | ||
from tempfile import TemporaryDirectory | ||
|
||
cudnn.benchmark = True | ||
|
@@ -339,6 +340,39 @@ def visualize_model(model, num_images=6): | |
plt.ioff() | ||
plt.show() | ||
|
||
###################################################################### | ||
# Save and load the model | ||
# ---------------------- | ||
# | ||
# Here we have saved the trained model and loaded it for inference. We can | ||
# now use our trained model to make predictions on our own images and analyze | ||
# the results. | ||
# | ||
|
||
def save_and_load_model(model, model_name): | ||
torch.save(model.state_dict(), model_name) | ||
model.load_state_dict(torch.load(model_name)) | ||
return model | ||
|
||
def visualize_model_upload_image(model,model_name,img_path): | ||
was_training = model.training | ||
model=save_and_load_model(model,model_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? If the original model is not modified by this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I get your point. I'll make the changes as requested following PEP8 guidelines. |
||
model.eval() | ||
img = Image.open(img_path) | ||
img = data_transforms['val'](img) | ||
img = img.unsqueeze(0) | ||
img = img.to(device) | ||
with torch.no_grad(): | ||
outputs = model(img) | ||
_, preds = torch.max(outputs, 1) | ||
ax = plt.subplot(1, 1, 1) | ||
ax.axis('off') | ||
ax.set_title(f'predicted: {class_names[preds[0]]}') | ||
imshow(img.cpu().data[0]) | ||
model.train(mode=was_training) | ||
|
||
visualize_model_upload_image(model_conv,img_path='data/hymenoptera_data/val/ants/1337725712_2eb53cd742.jpg',model_name='resentconv.pth') | ||
|
||
###################################################################### | ||
# Further Learning | ||
# ----------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a convoluted way to do a deepcopy?
I think in this case using deepcopy is fine if it works.