fohy24 commited on
Commit ·
688a048
1
Parent(s): aadf03a
load checkpoint before predict() too
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from torchvision import models
|
|
| 4 |
from torchvision.transforms import v2
|
| 5 |
import os
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
-
|
| 8 |
|
| 9 |
labels = ['Pastel',
|
| 10 |
'Yellow Belly',
|
|
@@ -35,31 +35,28 @@ hf_token = os.getenv('HF_token')
|
|
| 35 |
model_path = hf_hub_download(repo_id="fohy24/morphmarket_model", filename="model_v9_epoch8.pt", token=hf_token)
|
| 36 |
checkpoint = torch.load(model_path, map_location=device)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
efficientnet.load_state_dict(checkpoint['model_state_dict'])
|
| 60 |
-
|
| 61 |
-
efficientnet.eval()
|
| 62 |
|
|
|
|
| 63 |
input_img = transform(img)
|
| 64 |
input_img = input_img.unsqueeze(0)
|
| 65 |
|
|
@@ -71,7 +68,7 @@ def predict(img, confidence):
|
|
| 71 |
|
| 72 |
return prediction_dict
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
gr.Interface(fn=predict,
|
| 77 |
inputs=[gr.Image(type="pil"),
|
|
|
|
| 4 |
from torchvision.transforms import v2
|
| 5 |
import os
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
+
import gradio as gr
|
| 8 |
|
| 9 |
labels = ['Pastel',
|
| 10 |
'Yellow Belly',
|
|
|
|
| 35 |
model_path = hf_hub_download(repo_id="fohy24/morphmarket_model", filename="model_v9_epoch8.pt", token=hf_token)
|
| 36 |
checkpoint = torch.load(model_path, map_location=device)
|
| 37 |
|
| 38 |
+
new_layers = nn.Sequential(
|
| 39 |
+
nn.LazyLinear(1280),
|
| 40 |
+
nn.BatchNorm1d(1280),
|
| 41 |
+
nn.ReLU(),
|
| 42 |
+
nn.Dropout(0.5),
|
| 43 |
+
nn.LazyLinear(num_labels)
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
IMAGE_SIZE = 480
|
| 47 |
+
transform = v2.Compose([
|
| 48 |
+
v2.ToImage(),
|
| 49 |
+
v2.Resize((IMAGE_SIZE, IMAGE_SIZE)),
|
| 50 |
+
v2.ToDtype(torch.float32, scale=True),
|
| 51 |
+
v2.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 52 |
+
])
|
| 53 |
+
|
| 54 |
+
efficientnet = models.efficientnet_v2_l(weights='EfficientNet_V2_L_Weights.DEFAULT')
|
| 55 |
+
efficientnet.classifier = new_layers
|
| 56 |
+
efficientnet.load_state_dict(checkpoint['model_state_dict'])
|
| 57 |
+
efficientnet.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
def predict(img, confidence):
|
| 60 |
input_img = transform(img)
|
| 61 |
input_img = input_img.unsqueeze(0)
|
| 62 |
|
|
|
|
| 68 |
|
| 69 |
return prediction_dict
|
| 70 |
|
| 71 |
+
|
| 72 |
|
| 73 |
gr.Interface(fn=predict,
|
| 74 |
inputs=[gr.Image(type="pil"),
|