Update app.py
Browse files
app.py
CHANGED
|
@@ -9,48 +9,59 @@ import gradio as gr
|
|
| 9 |
|
| 10 |
labels = [
|
| 11 |
'Pastel',
|
|
|
|
| 12 |
'Yellow Belly',
|
| 13 |
'Enchi',
|
| 14 |
-
'Clown',
|
| 15 |
-
'Leopard',
|
| 16 |
'Piebald',
|
|
|
|
| 17 |
'Orange Dream',
|
| 18 |
'Fire',
|
| 19 |
'Mojave',
|
| 20 |
-
'
|
| 21 |
'Banana',
|
| 22 |
-
'
|
| 23 |
'Black Pastel',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
'Lesser',
|
| 25 |
-
'Spotnose',
|
| 26 |
'Cinnamon',
|
| 27 |
-
'
|
| 28 |
-
'Hypo',
|
| 29 |
-
'Spider',
|
| 30 |
-
'Super Pastel',
|
| 31 |
-
'Desert Ghost',
|
| 32 |
'Black Head',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
'Vanilla',
|
| 34 |
-
'Red Stripe',
|
| 35 |
-
'Asphalt',
|
| 36 |
'Gravel',
|
| 37 |
'Butter',
|
|
|
|
|
|
|
| 38 |
'Calico',
|
| 39 |
-
'
|
| 40 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
]
|
| 42 |
|
|
|
|
| 43 |
num_labels = len(labels)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
# If using GPU
|
| 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="
|
| 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="
|
| 54 |
with open(thresholds_path, 'r') as f:
|
| 55 |
optimal_thresholds = json.load(f)
|
| 56 |
|
|
@@ -70,23 +81,36 @@ transform = v2.Compose([
|
|
| 70 |
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479])
|
| 71 |
])
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
efficientnet = models.efficientnet_v2_l(weights='EfficientNet_V2_L_Weights.DEFAULT')
|
| 74 |
efficientnet.classifier = new_layers
|
| 75 |
efficientnet.load_state_dict(checkpoint['model_state_dict'])
|
| 76 |
efficientnet.to(device).eval()
|
| 77 |
|
| 78 |
-
def predict(img):
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
with torch.no_grad():
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
predicted_probs =
|
| 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 |
|
|
|
|
| 9 |
|
| 10 |
labels = [
|
| 11 |
'Pastel',
|
| 12 |
+
'Clown',
|
| 13 |
'Yellow Belly',
|
| 14 |
'Enchi',
|
|
|
|
|
|
|
| 15 |
'Piebald',
|
| 16 |
+
'Leopard',
|
| 17 |
'Orange Dream',
|
| 18 |
'Fire',
|
| 19 |
'Mojave',
|
| 20 |
+
'Spotnose',
|
| 21 |
'Banana',
|
| 22 |
+
'Desert Ghost',
|
| 23 |
'Black Pastel',
|
| 24 |
+
'Hypo',
|
| 25 |
+
'Normal',
|
| 26 |
+
'Pinstripe',
|
| 27 |
+
'GHI',
|
| 28 |
'Lesser',
|
|
|
|
| 29 |
'Cinnamon',
|
| 30 |
+
'Red Stripe',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
'Black Head',
|
| 32 |
+
'Super Pastel',
|
| 33 |
+
'Chocolate',
|
| 34 |
+
'Axanthic (VPI)',
|
| 35 |
+
'Cypress',
|
| 36 |
'Vanilla',
|
|
|
|
|
|
|
| 37 |
'Gravel',
|
| 38 |
'Butter',
|
| 39 |
+
'Ultramel',
|
| 40 |
+
'Asphalt',
|
| 41 |
'Calico',
|
| 42 |
+
'Stranger',
|
| 43 |
+
'Spider',
|
| 44 |
+
'Lavender Albino',
|
| 45 |
+
'Hurricane',
|
| 46 |
+
'Mahogany',
|
| 47 |
+
'Albino'
|
| 48 |
]
|
| 49 |
|
| 50 |
+
|
| 51 |
num_labels = len(labels)
|
| 52 |
|
| 53 |
+
# Toggle between optimized per-class thresholds and a fixed 0.5 cutoff
|
| 54 |
+
USE_OPTIMIZED_THRESHOLD = False
|
| 55 |
+
|
| 56 |
# If using GPU
|
| 57 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 58 |
|
| 59 |
hf_token = os.getenv('HF_token')
|
| 60 |
+
model_path = hf_hub_download(repo_id="samfhy/morphmarket_model", filename="model_v1_epoch20.pt", token=hf_token)
|
| 61 |
checkpoint = torch.load(model_path, map_location=device)
|
| 62 |
|
| 63 |
# Load per-class optimized thresholds
|
| 64 |
+
thresholds_path = hf_hub_download(repo_id="samfhy/morphmarket_model", filename="optimal_thresholds_model2_v1.json", token=hf_token)
|
| 65 |
with open(thresholds_path, 'r') as f:
|
| 66 |
optimal_thresholds = json.load(f)
|
| 67 |
|
|
|
|
| 81 |
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479])
|
| 82 |
])
|
| 83 |
|
| 84 |
+
tta_transform = v2.Compose([
|
| 85 |
+
v2.ToImage(),
|
| 86 |
+
v2.Resize((IMAGE_SIZE, IMAGE_SIZE)),
|
| 87 |
+
v2.RandomHorizontalFlip(p=1.0),
|
| 88 |
+
v2.ToDtype(torch.float32, scale=True),
|
| 89 |
+
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479]),
|
| 90 |
+
])
|
| 91 |
+
|
| 92 |
efficientnet = models.efficientnet_v2_l(weights='EfficientNet_V2_L_Weights.DEFAULT')
|
| 93 |
efficientnet.classifier = new_layers
|
| 94 |
efficientnet.load_state_dict(checkpoint['model_state_dict'])
|
| 95 |
efficientnet.to(device).eval()
|
| 96 |
|
| 97 |
+
def predict(img, use_optimized_threshold):
|
| 98 |
+
if img is None:
|
| 99 |
+
return {}
|
| 100 |
+
img = img.convert('RGB')
|
| 101 |
+
|
| 102 |
+
base_input = transform(img).unsqueeze(0).to(device)
|
| 103 |
+
flipped_input = tta_transform(img).unsqueeze(0).to(device)
|
| 104 |
|
| 105 |
with torch.no_grad():
|
| 106 |
+
base_probs = torch.sigmoid(efficientnet(base_input))
|
| 107 |
+
flipped_probs = torch.sigmoid(efficientnet(flipped_input))
|
| 108 |
|
| 109 |
+
predicted_probs = ((base_probs + flipped_probs) / 2.0).cpu().flatten().tolist()
|
| 110 |
|
| 111 |
prediction_dict = {}
|
| 112 |
for i, label in enumerate(labels):
|
| 113 |
+
thresh = optimal_thresholds.get(label, 0.5) if use_optimized_threshold else 0.5
|
| 114 |
if predicted_probs[i] >= thresh:
|
| 115 |
prediction_dict[label] = predicted_probs[i]
|
| 116 |
|