Upload 5 files
Browse filesmodel files - updated weights
- best_model.pth +3 -0
- classification_report.txt +11 -0
- confusion_matrix.png +0 -0
- sample_inference_code.py +67 -0
- training_history.png +0 -0
best_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:60b37dbdd4cdd3898f62e8c966c8e4cdaaab7977a19568c07a6b0ee21fd1cebe
|
| 3 |
+
size 9146552
|
classification_report.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Final Model Accuracy: 96.25%
|
| 2 |
+
|
| 3 |
+
Classification Report:
|
| 4 |
+
precision recall f1-score support
|
| 5 |
+
|
| 6 |
+
indoor 0.96 0.96 0.96 200
|
| 7 |
+
outdoor 0.96 0.96 0.96 200
|
| 8 |
+
|
| 9 |
+
accuracy 0.96 400
|
| 10 |
+
macro avg 0.96 0.96 0.96 400
|
| 11 |
+
weighted avg 0.96 0.96 0.96 400
|
confusion_matrix.png
ADDED
|
sample_inference_code.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import transforms, models
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
def classify_image(image_path, model_path="indoor_outdoor_classifier_from_scratch/best_model.pth"):
|
| 8 |
+
# Check if MPS (Apple Silicon GPU) is available
|
| 9 |
+
if torch.backends.mps.is_available():
|
| 10 |
+
device = torch.device("mps")
|
| 11 |
+
else:
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
|
| 14 |
+
# Define the model architecture
|
| 15 |
+
def get_model():
|
| 16 |
+
model = models.mobilenet_v2(weights=None)
|
| 17 |
+
num_ftrs = model.classifier[1].in_features
|
| 18 |
+
model.classifier[1] = nn.Linear(num_ftrs, 2)
|
| 19 |
+
return model
|
| 20 |
+
|
| 21 |
+
# Load the model
|
| 22 |
+
model = get_model()
|
| 23 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 24 |
+
model = model.to(device)
|
| 25 |
+
model.eval()
|
| 26 |
+
|
| 27 |
+
# Prepare image transformation
|
| 28 |
+
transform = transforms.Compose([
|
| 29 |
+
transforms.Resize((160, 160)),
|
| 30 |
+
transforms.ToTensor(),
|
| 31 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 32 |
+
])
|
| 33 |
+
|
| 34 |
+
# Load and preprocess the image
|
| 35 |
+
img = Image.open(image_path).convert('RGB')
|
| 36 |
+
img_tensor = transform(img).unsqueeze(0).to(device)
|
| 37 |
+
|
| 38 |
+
# Make prediction
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
output = model(img_tensor)
|
| 41 |
+
_, predicted = torch.max(output, 1)
|
| 42 |
+
probabilities = torch.nn.functional.softmax(output, dim=1)
|
| 43 |
+
|
| 44 |
+
# Get class label and probability
|
| 45 |
+
class_names = ['indoor', 'outdoor']
|
| 46 |
+
predicted_class = class_names[predicted.item()]
|
| 47 |
+
probability = probabilities[0][predicted.item()].item()
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
'class': predicted_class,
|
| 51 |
+
'probability': probability * 100,
|
| 52 |
+
'all_probabilities': {
|
| 53 |
+
class_names[i]: probabilities[0][i].item() * 100 for i in range(len(class_names))
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Example usage
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
import sys
|
| 60 |
+
|
| 61 |
+
if len(sys.argv) > 1:
|
| 62 |
+
image_path = sys.argv[1]
|
| 63 |
+
else:
|
| 64 |
+
image_path = input("Enter the path to the image: ")
|
| 65 |
+
|
| 66 |
+
result = classify_image(image_path)
|
| 67 |
+
print(f"Class: {result['class']} ({result['probability']:.2f}%)")
|
training_history.png
ADDED
|