Update app.py
Browse filesfor new model trained using CC
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import torch
|
| 3 |
from torch import nn
|
| 4 |
from torchvision import models
|
|
@@ -45,14 +46,19 @@ num_labels = len(labels)
|
|
| 45 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 46 |
|
| 47 |
hf_token = os.getenv('HF_token')
|
| 48 |
-
model_path = hf_hub_download(repo_id="samfhy/morphmarket_model", filename="
|
| 49 |
checkpoint = torch.load(model_path, map_location=device)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
new_layers = nn.Sequential(
|
| 52 |
-
nn.LazyLinear(2048),
|
| 53 |
-
nn.BatchNorm1d(2048),
|
| 54 |
-
nn.ReLU(),
|
| 55 |
-
nn.Dropout(0.5),
|
| 56 |
nn.LazyLinear(num_labels)
|
| 57 |
)
|
| 58 |
|
|
@@ -60,24 +66,29 @@ IMAGE_SIZE = checkpoint['image_size']
|
|
| 60 |
transform = v2.Compose([
|
| 61 |
v2.ToImage(),
|
| 62 |
v2.Resize((IMAGE_SIZE, IMAGE_SIZE)),
|
| 63 |
-
v2.ToDtype(torch.float32),
|
| 64 |
-
v2.Normalize(mean=[0.
|
| 65 |
])
|
| 66 |
|
| 67 |
-
efficientnet = models.
|
| 68 |
efficientnet.classifier = new_layers
|
| 69 |
efficientnet.load_state_dict(checkpoint['model_state_dict'])
|
| 70 |
efficientnet.eval()
|
| 71 |
|
| 72 |
-
def predict(img
|
| 73 |
input_img = transform(img)
|
| 74 |
-
input_img = input_img.unsqueeze(0)
|
| 75 |
|
| 76 |
with torch.no_grad():
|
| 77 |
output = efficientnet(input_img)
|
| 78 |
|
| 79 |
-
predicted_probs = torch.sigmoid(output).
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
return prediction_dict
|
| 83 |
|
|
@@ -112,14 +123,11 @@ with gr.Blocks(title='Ball Python Morph Identifier') as demo:
|
|
| 112 |
],
|
| 113 |
inputs=[img_input]
|
| 114 |
)
|
| 115 |
-
confidence = gr.Slider(0, 1, value=0.5, label="Confidence",
|
| 116 |
-
info="Show predictions that are above this confidence level")
|
| 117 |
predict_btn = gr.Button("Identify Morphs", variant="primary")
|
| 118 |
-
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
label_output = gr.Label(label="Predicted Morphs")
|
| 121 |
-
|
| 122 |
-
predict_btn.click(fn=predict, inputs=[img_input, confidence], outputs=label_output)
|
| 123 |
|
| 124 |
-
|
| 125 |
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import torch
|
| 4 |
from torch import nn
|
| 5 |
from torchvision import models
|
|
|
|
| 46 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 47 |
|
| 48 |
hf_token = os.getenv('HF_token')
|
| 49 |
+
model_path = hf_hub_download(repo_id="samfhy/morphmarket_model", filename="model_v14_epoch15.pt", token=hf_token)
|
| 50 |
checkpoint = torch.load(model_path, map_location=device)
|
| 51 |
|
| 52 |
+
# Load per-class optimized thresholds
|
| 53 |
+
thresholds_path = hf_hub_download(repo_id="samfhy/morphmarket_model", filename="optimal_thresholds_v14.json", token=hf_token)
|
| 54 |
+
with open(thresholds_path, 'r') as f:
|
| 55 |
+
optimal_thresholds = json.load(f)
|
| 56 |
+
|
| 57 |
new_layers = nn.Sequential(
|
| 58 |
+
nn.LazyLinear(2048),
|
| 59 |
+
nn.BatchNorm1d(2048),
|
| 60 |
+
nn.ReLU(),
|
| 61 |
+
nn.Dropout(0.5),
|
| 62 |
nn.LazyLinear(num_labels)
|
| 63 |
)
|
| 64 |
|
|
|
|
| 66 |
transform = v2.Compose([
|
| 67 |
v2.ToImage(),
|
| 68 |
v2.Resize((IMAGE_SIZE, IMAGE_SIZE)),
|
| 69 |
+
v2.ToDtype(torch.float32, scale=True),
|
| 70 |
+
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479])
|
| 71 |
])
|
| 72 |
|
| 73 |
+
efficientnet = models.efficientnet_v2_m(weights='EfficientNet_V2_M_Weights.DEFAULT')
|
| 74 |
efficientnet.classifier = new_layers
|
| 75 |
efficientnet.load_state_dict(checkpoint['model_state_dict'])
|
| 76 |
efficientnet.eval()
|
| 77 |
|
| 78 |
+
def predict(img):
|
| 79 |
input_img = transform(img)
|
| 80 |
+
input_img = input_img.unsqueeze(0).to(device)
|
| 81 |
|
| 82 |
with torch.no_grad():
|
| 83 |
output = efficientnet(input_img)
|
| 84 |
|
| 85 |
+
predicted_probs = torch.sigmoid(output).cpu().flatten().tolist()
|
| 86 |
+
|
| 87 |
+
prediction_dict = {}
|
| 88 |
+
for i, label in enumerate(labels):
|
| 89 |
+
thresh = optimal_thresholds.get(label, 0.5)
|
| 90 |
+
if predicted_probs[i] >= thresh:
|
| 91 |
+
prediction_dict[label] = predicted_probs[i]
|
| 92 |
|
| 93 |
return prediction_dict
|
| 94 |
|
|
|
|
| 123 |
],
|
| 124 |
inputs=[img_input]
|
| 125 |
)
|
|
|
|
|
|
|
| 126 |
predict_btn = gr.Button("Identify Morphs", variant="primary")
|
| 127 |
+
|
| 128 |
with gr.Column(scale=1):
|
| 129 |
label_output = gr.Label(label="Predicted Morphs")
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
predict_btn.click(fn=predict, inputs=[img_input], outputs=label_output)
|
| 132 |
|
| 133 |
+
demo.launch()
|