refactor: rename image preprocessing function for clarity and update HTML output for consensus display
Browse files
app_mcp.py
CHANGED
@@ -107,16 +107,14 @@ CLASS_NAMES = {
|
|
107 |
|
108 |
}
|
109 |
|
110 |
-
def preprocess_resize_256(image):
|
111 |
-
if image.mode != 'RGB':
|
112 |
-
image = image.convert('RGB')
|
113 |
-
return transforms.Resize((256, 256))(image)
|
114 |
-
|
115 |
def preprocess_resize_224(image):
|
116 |
if image.mode != 'RGB':
|
117 |
image = image.convert('RGB')
|
118 |
return transforms.Resize((224, 224))(image)
|
119 |
-
|
|
|
|
|
|
|
120 |
def postprocess_pipeline(prediction, class_names):
|
121 |
# Assumes HuggingFace pipeline output
|
122 |
return {pred['label']: pred['score'] for pred in prediction}
|
@@ -143,7 +141,7 @@ image_processor_1 = AutoImageProcessor.from_pretrained(MODEL_PATHS["model_1"], u
|
|
143 |
model_1 = Swinv2ForImageClassification.from_pretrained(MODEL_PATHS["model_1"]).to(device)
|
144 |
clf_1 = pipeline(model=model_1, task="image-classification", image_processor=image_processor_1, device=device)
|
145 |
register_model_with_metadata(
|
146 |
-
"model_1", clf_1,
|
147 |
display_name="SwinV2 Based", contributor="haywoodsloan", model_path=MODEL_PATHS["model_1"]
|
148 |
)
|
149 |
|
@@ -156,10 +154,7 @@ register_model_with_metadata(
|
|
156 |
# Register remaining models
|
157 |
feature_extractor_3 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_3"], device=device)
|
158 |
model_3 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_3"]).to(device)
|
159 |
-
|
160 |
-
if image.mode != 'RGB':
|
161 |
-
image = image.convert('RGB')
|
162 |
-
return transforms.Resize((256, 256))(image)
|
163 |
def postprocess_logits_model3(outputs, class_names):
|
164 |
logits = outputs.logits.cpu().numpy()[0]
|
165 |
probabilities = softmax(logits)
|
@@ -405,7 +400,7 @@ def predict_with_ensemble(img, confidence_threshold, augment_methods, rotate_deg
|
|
405 |
logger.info(f"Row {i} types: {[type(item) for item in row]}")
|
406 |
|
407 |
# The get_consensus_label function is now replaced by final_prediction_label from weighted consensus
|
408 |
-
consensus_html = f"<b
|
409 |
|
410 |
# Prepare data for logging to Hugging Face dataset
|
411 |
inference_params = {
|
|
|
107 |
|
108 |
}
|
109 |
|
|
|
|
|
|
|
|
|
|
|
110 |
def preprocess_resize_224(image):
|
111 |
if image.mode != 'RGB':
|
112 |
image = image.convert('RGB')
|
113 |
return transforms.Resize((224, 224))(image)
|
114 |
+
def preprocess_256(image):
|
115 |
+
if image.mode != 'RGB':
|
116 |
+
image = image.convert('RGB')
|
117 |
+
return transforms.Resize((256, 256))(image)
|
118 |
def postprocess_pipeline(prediction, class_names):
|
119 |
# Assumes HuggingFace pipeline output
|
120 |
return {pred['label']: pred['score'] for pred in prediction}
|
|
|
141 |
model_1 = Swinv2ForImageClassification.from_pretrained(MODEL_PATHS["model_1"]).to(device)
|
142 |
clf_1 = pipeline(model=model_1, task="image-classification", image_processor=image_processor_1, device=device)
|
143 |
register_model_with_metadata(
|
144 |
+
"model_1", clf_1, preprocess_256, postprocess_pipeline, CLASS_NAMES["model_1"],
|
145 |
display_name="SwinV2 Based", contributor="haywoodsloan", model_path=MODEL_PATHS["model_1"]
|
146 |
)
|
147 |
|
|
|
154 |
# Register remaining models
|
155 |
feature_extractor_3 = AutoFeatureExtractor.from_pretrained(MODEL_PATHS["model_3"], device=device)
|
156 |
model_3 = AutoModelForImageClassification.from_pretrained(MODEL_PATHS["model_3"]).to(device)
|
157 |
+
|
|
|
|
|
|
|
158 |
def postprocess_logits_model3(outputs, class_names):
|
159 |
logits = outputs.logits.cpu().numpy()[0]
|
160 |
probabilities = softmax(logits)
|
|
|
400 |
logger.info(f"Row {i} types: {[type(item) for item in row]}")
|
401 |
|
402 |
# The get_consensus_label function is now replaced by final_prediction_label from weighted consensus
|
403 |
+
consensus_html = f"<div style='display: flex; justify-content: space-between;'><div style='flex: 1;'><b>THIS IMAGE IS LIKELY <span style='color:{'red' if final_prediction_label == 'AI' else ('green' if final_prediction_label == 'REAL' else 'orange')}'>{final_prediction_label}</span></b></div><div style='flex: 1;'><b>CONSENSUS REACHED BY {len(results)} MODELS</b></div></div>"
|
404 |
|
405 |
# Prepare data for logging to Hugging Face dataset
|
406 |
inference_params = {
|
hf_inference_logs/log_20250611031830376635.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|