Update README.md
Browse files
README.md
CHANGED
@@ -58,6 +58,111 @@ model.config.id2label[predicted_label]
|
|
58 |
```
|
59 |
|
60 |
<hr>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
### Limitations
|
63 |
- **Specialized Task Fine-Tuning**: While the model is adept at NSFW image classification, its performance may vary when applied to other tasks.
|
|
|
58 |
```
|
59 |
|
60 |
<hr>
|
61 |
+
Run Yolo Version
|
62 |
+
``` markdown
|
63 |
+
|
64 |
+
import os
|
65 |
+
import matplotlib.pyplot as plt
|
66 |
+
from PIL import Image
|
67 |
+
import numpy as np
|
68 |
+
import onnxruntime as ort
|
69 |
+
import json # Added import for json
|
70 |
+
|
71 |
+
# Predict using YOLOv9 model
|
72 |
+
def predict_with_yolov9(image_path, model_path, labels_path, input_size):
|
73 |
+
"""
|
74 |
+
Run inference using the converted YOLOv9 model on a single image.
|
75 |
+
|
76 |
+
Args:
|
77 |
+
image_path (str): Path to the input image file.
|
78 |
+
model_path (str): Path to the ONNX model file.
|
79 |
+
labels_path (str): Path to the JSON file containing class labels.
|
80 |
+
input_size (tuple): The expected input size (height, width) for the model.
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
str: The predicted class label.
|
84 |
+
PIL.Image.Image: The original loaded image.
|
85 |
+
"""
|
86 |
+
def load_json(file_path):
|
87 |
+
with open(file_path, "r") as f:
|
88 |
+
return json.load(f)
|
89 |
+
|
90 |
+
# Load labels
|
91 |
+
labels = load_json(labels_path)
|
92 |
+
|
93 |
+
# Preprocess image
|
94 |
+
original_image = Image.open(image_path).convert("RGB")
|
95 |
+
image_resized = original_image.resize(input_size, Image.Resampling.BILINEAR)
|
96 |
+
image_np = np.array(image_resized, dtype=np.float32) / 255.0
|
97 |
+
image_np = np.transpose(image_np, (2, 0, 1)) # [C, H, W]
|
98 |
+
input_tensor = np.expand_dims(image_np, axis=0).astype(np.float32)
|
99 |
+
|
100 |
+
# Load YOLOv9 model
|
101 |
+
session = ort.InferenceSession(model_path)
|
102 |
+
input_name = session.get_inputs()[0].name
|
103 |
+
output_name = session.get_outputs()[0].name # Assuming classification output
|
104 |
+
|
105 |
+
# Run inference
|
106 |
+
outputs = session.run([output_name], {input_name: input_tensor})
|
107 |
+
predictions = outputs[0]
|
108 |
+
|
109 |
+
# Postprocess predictions (assuming classification output)
|
110 |
+
# Adapt this section if your model output is different (e.g., detection boxes)
|
111 |
+
predicted_index = np.argmax(predictions)
|
112 |
+
predicted_label = labels[str(predicted_index)] # Assumes labels are indexed by string numbers
|
113 |
+
|
114 |
+
return predicted_label, original_image
|
115 |
+
|
116 |
+
# Display prediction for a single image
|
117 |
+
def display_single_prediction(image_path, model_path, labels_path, input_size):
|
118 |
+
"""
|
119 |
+
Predicts the class for a single image and displays the image with its prediction.
|
120 |
+
|
121 |
+
Args:
|
122 |
+
image_path (str): Path to the input image file.
|
123 |
+
model_path (str): Path to the ONNX model file.
|
124 |
+
labels_path (str): Path to the JSON file containing class labels.
|
125 |
+
input_size (tuple): The expected input size (height, width) for the model.
|
126 |
+
"""
|
127 |
+
try:
|
128 |
+
# Run prediction
|
129 |
+
prediction, img = predict_with_yolov9(image_path, model_path, labels_path, input_size)
|
130 |
+
|
131 |
+
# Display image and prediction
|
132 |
+
fig, ax = plt.subplots(1, 1, figsize=(8, 8)) # Create a single plot
|
133 |
+
ax.imshow(img)
|
134 |
+
ax.set_title(f"Prediction: {prediction}", fontsize=14)
|
135 |
+
ax.axis("off") # Hide axes ticks and labels
|
136 |
+
|
137 |
+
plt.tight_layout()
|
138 |
+
plt.show()
|
139 |
+
|
140 |
+
except FileNotFoundError:
|
141 |
+
print(f"Error: Image file not found at {image_path}")
|
142 |
+
except Exception as e:
|
143 |
+
print(f"An error occurred: {e}")
|
144 |
+
|
145 |
+
|
146 |
+
# --- Main Execution ---
|
147 |
+
|
148 |
+
# Paths and parameters - **MODIFY THESE**
|
149 |
+
single_image_path = "path/to/your/single_image.jpg" # <--- Replace with the actual path to your image file
|
150 |
+
model_path = "path/to/your/yolov9_model.onnx" # <--- Replace with the actual path to your ONNX model
|
151 |
+
labels_path = "path/to/your/labels.json" # <--- Replace with the actual path to your labels JSON file
|
152 |
+
input_size = (224, 224) # Standard input size, adjust if your model differs
|
153 |
+
|
154 |
+
# Check if the image file exists before proceeding (optional but recommended)
|
155 |
+
if os.path.exists(single_image_path):
|
156 |
+
# Run prediction and display for the single image
|
157 |
+
display_single_prediction(single_image_path, model_path, labels_path, input_size)
|
158 |
+
else:
|
159 |
+
print(f"Error: The specified image file does not exist: {single_image_path}")
|
160 |
+
|
161 |
+
```
|
162 |
+
|
163 |
+
<hr>
|
164 |
+
|
165 |
+
|
166 |
|
167 |
### Limitations
|
168 |
- **Specialized Task Fine-Tuning**: While the model is adept at NSFW image classification, its performance may vary when applied to other tasks.
|