Update README.md
Browse files
README.md
CHANGED
@@ -46,72 +46,4 @@ This model is released under MIT License.
|
|
46 |
|
47 |
## Code Implementation
|
48 |
|
49 |
-
|
50 |
-
import torch
|
51 |
-
from torchvision import transforms, models
|
52 |
-
import torch.nn as nn
|
53 |
-
from PIL import Image
|
54 |
-
import io
|
55 |
-
|
56 |
-
def classify_image_huggingface(image_path, repo_id="mertincesu/property-indoor-or-outdoor-classifier"):
|
57 |
-
|
58 |
-
# Define model structure
|
59 |
-
model = models.mobilenet_v2(weights=None)
|
60 |
-
num_ftrs = model.classifier[1].in_features
|
61 |
-
model.classifier[1] = nn.Linear(num_ftrs, 2)
|
62 |
-
|
63 |
-
# Load model weights directly from Hugging Face
|
64 |
-
model_url = f"https://huggingface.co/{repo_id}/resolve/main/pytorch_model.bin"
|
65 |
-
|
66 |
-
try:
|
67 |
-
# Download the model file
|
68 |
-
print(f"Downloading model from {model_url}")
|
69 |
-
response = requests.get(model_url)
|
70 |
-
response.raise_for_status()
|
71 |
-
|
72 |
-
# Load the model weights
|
73 |
-
model_binary = io.BytesIO(response.content)
|
74 |
-
model.load_state_dict(torch.load(model_binary, map_location=device))
|
75 |
-
model.to(device)
|
76 |
-
model.eval()
|
77 |
-
|
78 |
-
# Define image transformation
|
79 |
-
transform = transforms.Compose([
|
80 |
-
transforms.Resize((160, 160)),
|
81 |
-
transforms.ToTensor(),
|
82 |
-
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
83 |
-
])
|
84 |
-
|
85 |
-
# Load and process the image
|
86 |
-
img = Image.open(image_path).convert('RGB')
|
87 |
-
img_tensor = transform(img).unsqueeze(0).to(device)
|
88 |
-
|
89 |
-
# Make prediction
|
90 |
-
with torch.no_grad():
|
91 |
-
outputs = model(img_tensor)
|
92 |
-
probs = torch.nn.functional.softmax(outputs, dim=1)
|
93 |
-
_, predicted = torch.max(outputs, 1)
|
94 |
-
|
95 |
-
classes = ['indoor', 'outdoor']
|
96 |
-
result = {
|
97 |
-
'class': classes[predicted.item()],
|
98 |
-
'confidence': probs[0][predicted.item()].item() * 100
|
99 |
-
}
|
100 |
-
|
101 |
-
print(f"Class: {result['class']}")
|
102 |
-
print(f"Confidence: {result['confidence']:.2f}%")
|
103 |
-
return result
|
104 |
-
|
105 |
-
except Exception as e:
|
106 |
-
print(f"Error: {e}")
|
107 |
-
return None
|
108 |
-
|
109 |
-
if __name__ == "__main__":
|
110 |
-
import sys
|
111 |
-
|
112 |
-
if len(sys.argv) > 1:
|
113 |
-
image_path = sys.argv[1]
|
114 |
-
else:
|
115 |
-
image_path = input("Enter path to image: ")
|
116 |
-
|
117 |
-
classify_image_huggingface(image_path)
|
|
|
46 |
|
47 |
## Code Implementation
|
48 |
|
49 |
+
Example usage code has been provided in example_usage.py under model files.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|