Spaces:
Sleeping
Sleeping
Initial commit
Browse filesFiles changed (8)
- .gitattributes+1
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pthAdded+3
- app.pyAdded+67
- examples/2582289.jpgAdded
- examples/3622237.jpgAdded
- examples/592799.jpgAdded
- model.pyAdded+28
- requirements.txtAdded+3
31 unmodified lines32333431 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text31 unmodified lines3233343531 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:12aae0c0d7228af6e83e2c49c47367109baa0e9a7ebdb67afa8cc3fc187a0997size 3131386912345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
### 1. Imports and class names setup ### import gradio as grimport osimport torch
from model import create_effnetb2_modelfrom timeit import default_timer as timerfrom typing import Tuple, Dict
# Setup class namesclass_names = ["pizza", "steak", "sushi"]
# 2. Model generation and weight effnetb2, effnetb2_transforms = create_EffNetB2_model(num_classes=len(class_names))
model.load_state_dict( torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth", map_location=torch.device("cpu"), # load to CPU))
# 3. Prefict function
def predict(img) -> Tuple[Dict, float]: """Transforms and performs a prediction on img and returns prediction and time taken. """ # Start the timer start_time = timer() # Transform the target image and add a batch dimension img = effnetb2_transforms(img).unsqueeze(0) # Put model into evaluation mode and turn on inference mode effnetb2.eval() with torch.inference_mode(): # Pass the transformed image through the model and turn the prediction logits into prediction probabilities pred_probs = torch.softmax(effnetb2(img), dim=1) # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter) pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))} # Calculate the prediction time pred_time = round(timer() - start_time, 5) # Return the prediction dictionary and prediction time return pred_labels_and_probs, pred_time
# 4. Gradio app
# Create title, description and article stringstitle = "FoodVision Mini 🍕🥩🍣"description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
# Create the Gradio demodemo = gr.Interface(fn=predict, # mapping function from input to output inputs=gr.Image(type="pil"), # what are the inputs? outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs? gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs examples=example_list, title=title, description=description, article=article)
# Launch the demo!demo.launch(debug=False, # print errors locally? share=True) # generate a publically shareable URL?12345678910111213141516171819202122232425262728
import gradio as grimport osfrom torch import nn
def create_EffNetB2_model(num_classes:int=3, seed:int = 42): """
"""
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT transform = weights.transforms()
model = torchvision.models.efficientnet_b2(weights=weights)
for parm in model.parameters(): parm.requires_grad = False set_seeds(seed)
model.classifier = nn.Sequential(nn.Dropout(p=0.2, inplace=True), nn.Linear(in_features=1408, out_features=num_classes, bias=True ))
return model, transform123torch==1.12.0torchvision==0.13.0gradio==3.1.4.gitattributes+1
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pthAdded+3
app.pyAdded+67
examples/2582289.jpgAdded

examples/3622237.jpgAdded

examples/592799.jpgAdded

model.pyAdded+28
requirements.txtAdded+3