Update Batch_Inference.py
Browse files- Batch_Inference.py +111 -118
Batch_Inference.py
CHANGED
|
@@ -1,119 +1,112 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from pathlib import Path
|
| 3 |
-
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
import cv2
|
| 7 |
-
import segmentation_models_pytorch as smp
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
-
from tqdm import tqdm
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
model.
|
| 42 |
-
|
| 43 |
-
model
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
except Exception as e:
|
| 114 |
-
print(f"\nCould not process {filename}. Error: {e}")
|
| 115 |
-
|
| 116 |
-
print(f"\n✅ Done! All generated masks have been saved to the '{OUTPUT_MASK_DIR}' folder.")
|
| 117 |
-
|
| 118 |
-
if __name__ == "__main__":
|
| 119 |
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
import segmentation_models_pytorch as smp
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
HF_USERNAME = "Subh75"
|
| 13 |
+
HF_ORGNAME="LeafNet75"
|
| 14 |
+
MODEL_NAME = "Leaf-Annotate-v2"
|
| 15 |
+
HF_MODEL_REPO_ID = f"{HF_ORGNAME}/{MODEL_NAME}"
|
| 16 |
+
|
| 17 |
+
# --- Point this to your folder of unlabeled leaf images ---
|
| 18 |
+
INPUT_IMAGE_DIR = "toast"
|
| 19 |
+
# --- The script will save the generated masks here ---
|
| 20 |
+
OUTPUT_MASK_DIR = "masks"
|
| 21 |
+
|
| 22 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
IMG_SIZE = 256
|
| 24 |
+
CONFIDENCE_THRESHOLD = 0.5
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def load_model_from_hub(repo_id: str):
|
| 28 |
+
"""Loads the interactive segmentation model from the Hub."""
|
| 29 |
+
print(f"Loading model '{repo_id}' from Hugging Face Hub...")
|
| 30 |
+
|
| 31 |
+
model = smp.Unet(
|
| 32 |
+
encoder_name="mobilenet_v2",
|
| 33 |
+
encoder_weights=None,
|
| 34 |
+
in_channels=4, # RGB + Scribble
|
| 35 |
+
classes=1,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
model_weights_path = hf_hub_download(repo_id=repo_id, filename="best_model.pth")
|
| 39 |
+
model.load_state_dict(torch.load(model_weights_path, map_location=DEVICE))
|
| 40 |
+
model.to(DEVICE)
|
| 41 |
+
model.eval()
|
| 42 |
+
print("Model loaded successfully.")
|
| 43 |
+
return model
|
| 44 |
+
|
| 45 |
+
def predict_scribble(model, pil_image, scribble_mask):
|
| 46 |
+
"""Runs inference using a scribble and returns a binary mask."""
|
| 47 |
+
# Preprocess image and scribble
|
| 48 |
+
img_resized = np.array(pil_image.resize((IMG_SIZE, IMG_SIZE), Image.Resampling.BILINEAR))
|
| 49 |
+
scribble_resized = cv2.resize(scribble_mask, (IMG_SIZE, IMG_SIZE), interpolation=cv2.INTER_NEAREST)
|
| 50 |
+
|
| 51 |
+
img_tensor = torch.from_numpy(img_resized.astype(np.float32)).permute(2, 0, 1) / 255.0
|
| 52 |
+
scribble_tensor = torch.from_numpy(scribble_resized.astype(np.float32)).unsqueeze(0) / 255.0
|
| 53 |
+
|
| 54 |
+
input_tensor = torch.cat([img_tensor, scribble_tensor], dim=0).unsqueeze(0).to(DEVICE)
|
| 55 |
+
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
output = model(input_tensor)
|
| 58 |
+
|
| 59 |
+
probs = torch.sigmoid(output)
|
| 60 |
+
binary_mask_resized = (probs > CONFIDENCE_THRESHOLD).float().squeeze().cpu().numpy()
|
| 61 |
+
|
| 62 |
+
final_mask = cv2.resize(binary_mask_resized, (pil_image.width, pil_image.height), interpolation=cv2.INTER_NEAREST)
|
| 63 |
+
return (final_mask * 255).astype(np.uint8)
|
| 64 |
+
|
| 65 |
+
def main():
|
| 66 |
+
"""
|
| 67 |
+
Main function to run batch inference on a folder of images.
|
| 68 |
+
"""
|
| 69 |
+
if not os.path.isdir(INPUT_IMAGE_DIR):
|
| 70 |
+
print(f"Error: Input directory not found at '{INPUT_IMAGE_DIR}'")
|
| 71 |
+
print("Please update the 'INPUT_IMAGE_DIR' variable in the script.")
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
os.makedirs(OUTPUT_MASK_DIR, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
model = load_model_from_hub(HF_MODEL_REPO_ID)
|
| 77 |
+
|
| 78 |
+
image_files = [f for f in os.listdir(INPUT_IMAGE_DIR) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 79 |
+
|
| 80 |
+
print(f"\nFound {len(image_files)} images to process.")
|
| 81 |
+
|
| 82 |
+
for filename in tqdm(image_files, desc="Generating Masks"):
|
| 83 |
+
image_path = os.path.join(INPUT_IMAGE_DIR, filename)
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
original_image = Image.open(image_path).convert("RGB")
|
| 87 |
+
h, w = original_image.height, original_image.width
|
| 88 |
+
|
| 89 |
+
scribble = np.zeros((h, w), dtype=np.uint8)
|
| 90 |
+
center_x, center_y = w // 2, h // 2
|
| 91 |
+
length = int(min(w, h) * 0.2) # Scribble is 20% of the smallest dimension
|
| 92 |
+
|
| 93 |
+
start_point = (center_x - length // 2, center_y)
|
| 94 |
+
end_point = (center_x + length // 2, center_y)
|
| 95 |
+
cv2.line(scribble, start_point, end_point, 255, thickness=25)
|
| 96 |
+
|
| 97 |
+
predicted_mask = predict_scribble(model, original_image, scribble)
|
| 98 |
+
|
| 99 |
+
mask_image = Image.fromarray(predicted_mask)
|
| 100 |
+
|
| 101 |
+
base_name = Path(filename).stem
|
| 102 |
+
output_path = os.path.join(OUTPUT_MASK_DIR, f"{base_name}_mask.png")
|
| 103 |
+
|
| 104 |
+
mask_image.save(output_path)
|
| 105 |
+
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"\nCould not process {filename}. Error: {e}")
|
| 108 |
+
|
| 109 |
+
print(f"\n Done! All generated masks have been saved to the '{OUTPUT_MASK_DIR}' folder.")
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
main()
|