Update for v2 model
Browse files
app.py
CHANGED
|
@@ -51,17 +51,17 @@ labels = [
|
|
| 51 |
num_labels = len(labels)
|
| 52 |
|
| 53 |
# Toggle between optimized per-class thresholds and a fixed 0.5 cutoff
|
| 54 |
-
USE_OPTIMIZED_THRESHOLD =
|
| 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="
|
| 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="
|
| 65 |
with open(thresholds_path, 'r') as f:
|
| 66 |
optimal_thresholds = json.load(f)
|
| 67 |
|
|
@@ -76,18 +76,20 @@ new_layers = nn.Sequential(
|
|
| 76 |
IMAGE_SIZE = checkpoint['image_size']
|
| 77 |
transform = v2.Compose([
|
| 78 |
v2.ToImage(),
|
| 79 |
-
v2.Resize((IMAGE_SIZE,
|
|
|
|
| 80 |
v2.ToDtype(torch.float32, scale=True),
|
| 81 |
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479])
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
efficientnet = models.efficientnet_v2_l(weights='EfficientNet_V2_L_Weights.DEFAULT')
|
| 93 |
efficientnet.classifier = new_layers
|
|
@@ -100,14 +102,14 @@ def predict(img, use_optimized_threshold):
|
|
| 100 |
img = img.convert('RGB')
|
| 101 |
|
| 102 |
base_input = transform(img).unsqueeze(0).to(device)
|
| 103 |
-
|
| 104 |
|
| 105 |
with torch.no_grad():
|
| 106 |
base_probs = torch.sigmoid(efficientnet(base_input))
|
| 107 |
-
|
| 108 |
|
| 109 |
predicted_probs = base_probs.cpu().flatten().tolist()
|
| 110 |
-
|
| 111 |
|
| 112 |
prediction_dict = {}
|
| 113 |
for i, label in enumerate(labels):
|
|
|
|
| 51 |
num_labels = len(labels)
|
| 52 |
|
| 53 |
# Toggle between optimized per-class thresholds and a fixed 0.5 cutoff
|
| 54 |
+
USE_OPTIMIZED_THRESHOLD = True
|
| 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_v2_epoch26.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_v2.json", token=hf_token)
|
| 65 |
with open(thresholds_path, 'r') as f:
|
| 66 |
optimal_thresholds = json.load(f)
|
| 67 |
|
|
|
|
| 76 |
IMAGE_SIZE = checkpoint['image_size']
|
| 77 |
transform = v2.Compose([
|
| 78 |
v2.ToImage(),
|
| 79 |
+
v2.Resize(int(IMAGE_SIZE * 1.14), antialias=True),
|
| 80 |
+
v2.CenterCrop(IMAGE_SIZE),
|
| 81 |
v2.ToDtype(torch.float32, scale=True),
|
| 82 |
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479])
|
| 83 |
+
])
|
| 84 |
|
| 85 |
+
tta_transform = v2.Compose([
|
| 86 |
+
v2.ToImage(),
|
| 87 |
+
v2.Resize(int(IMAGE_SIZE * 1.14), antialias=True),
|
| 88 |
+
v2.CenterCrop(IMAGE_SIZE),
|
| 89 |
+
v2.RandomHorizontalFlip(p=1.0), # always flip
|
| 90 |
+
v2.ToDtype(torch.float32, scale=True),
|
| 91 |
+
v2.Normalize(mean=[0.6007, 0.5679, 0.5206], std=[0.2411, 0.2392, 0.2479])
|
| 92 |
+
])
|
| 93 |
|
| 94 |
efficientnet = models.efficientnet_v2_l(weights='EfficientNet_V2_L_Weights.DEFAULT')
|
| 95 |
efficientnet.classifier = new_layers
|
|
|
|
| 102 |
img = img.convert('RGB')
|
| 103 |
|
| 104 |
base_input = transform(img).unsqueeze(0).to(device)
|
| 105 |
+
flipped_input = tta_transform(img).unsqueeze(0).to(device)
|
| 106 |
|
| 107 |
with torch.no_grad():
|
| 108 |
base_probs = torch.sigmoid(efficientnet(base_input))
|
| 109 |
+
flipped_probs = torch.sigmoid(efficientnet(flipped_input))
|
| 110 |
|
| 111 |
predicted_probs = base_probs.cpu().flatten().tolist()
|
| 112 |
+
predicted_probs = ((base_probs + flipped_probs) / 2.0).cpu().flatten().tolist()
|
| 113 |
|
| 114 |
prediction_dict = {}
|
| 115 |
for i, label in enumerate(labels):
|