Pytorch - Streamlit - custom model

username: Stefano_Peirano
cooked: <p>Hello everyone … Online I found this guide that explains how to create a simple webapp for image recognition. In this case it uses the ImageNet model already pretrained …<br>
Here the github site:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/dehaoterryzhang/Image_Classification_App">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/dehaoterryzhang/Image_Classification_App" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/ab8ac6c6ebdf39e0341bf9776ec27e1f00f0d1d818e715d701e2b956a1e8ee55/dehaoterryzhang/Image_Classification_App" class="thumbnail" width="" height=""></div>

<h3><a href="https://github.com/dehaoterryzhang/Image_Classification_App" target="_blank" rel="noopener nofollow ugc">GitHub - dehaoterryzhang/Image_Classification_App: Build a image...</a></h3>

  <p>Build a image classification python web app with Streamlit and PyTorch :cat: :dog: - GitHub - dehaoterryzhang/Image_Classification_App: Build a image classification python web app with Streamlit an...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I would like to use a model that I have already made the train of which I have the .pth file<br>
Where can I edit the clf.py file? For convenience I paste it below:</p>
<blockquote>
<p>Blockquote<br>
from torchvision import models, transforms<br>
import torch<br>
from PIL import Image<br>
def predict(image_path):<br>
resnet = models.resnet101(pretrained=True)</p>
</blockquote>
<pre><code>#https://pytorch.org/docs/stable/torchvision/models.html
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)])

img = Image.open(image_path)
batch_t = torch.unsqueeze(transform(img), 0)

resnet.eval()
out = resnet(batch_t)

with open('imagenet_classes.txt') as f:
    classes = [line.strip() for line in f.readlines()]

prob = torch.nn.functional.softmax(out, dim=1)[0] * 100
_, indices = torch.sort(out, descending=True)
return [(classes[idx], prob[idx].item()) for idx in indices[0][:5]]
</code></pre>

username: ptrblck
cooked: <p>The <code>predict</code> method seems to initialize the complete model, transformation, loads the data, processes the forward pass, and returns the class probabilities for a single input image.</p>
<p>I’m not a webapp expert, but this seems very inefficient. I would recommend to take a look at chapter 15 in <a href="https://pytorch.org/deep-learning-with-pytorch">Deep Learning with PyTorch</a> (which is still free) by <a class="mention" href="/u/elistevens">@elistevens</a>, <a class="mention" href="/u/lantiga">@lantiga</a>, and <a class="mention" href="/u/tom">@tom</a>.</p>

username: Stefano_Peirano
cooked: <p>Thanks a lot! you have been very kind. I’m going to read now …</p>

username: tom
cooked: <p>To echo <a class="mention" href="/u/ptrblck">@ptrblck</a>’s comment,  there is nothing wrong with streamlit if you want the UI, but hopefully the examples will give you some idea about the parts involved, some thoughts on efficiency, when to do what etc.</p>
<p>Best regards</p>
<p>Thomas</p>

username: Stefano_Peirano
cooked: <p>Thank you for your answer<br>
I would like to create a webapp-beta equal to that of the example I have linked above, with the difference I would like to use my .pth model… Nothing different for now…<br>
regars<br>
bye bye</p>

username: tom
cooked: <p>Yes, probably the first step would be to just replace the resnet with your own but also to move everything above Image.open and the reading of the classes out of the predict function.<br>
If you’re looking at multithreading or so, using the JIT is important (it was mainly my theory when I wrote that chapter, but we’ve seen it in the wild in these forums since).</p>