Skip to content

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

Merged
merged 17 commits into from
Jun 9, 2023
Merged
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions beginner_source/transfer_learning_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import matplotlib.pyplot as plt
import time
import os
from PIL import Image
from tempfile import TemporaryDirectory

cudnn.benchmark = True
Expand Down Expand Up @@ -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):
Copy link
Contributor

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.

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)
Copy link
Contributor

@kit1980 kit1980 Jun 7, 2023

Choose a reason for hiding this comment

The 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.
And the original model is not even used after this function...

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
# -----------------
Expand Down