Update README.md
Browse files
README.md
CHANGED
|
@@ -46,23 +46,39 @@ from transformers import AutoModel
|
|
| 46 |
# Load model
|
| 47 |
model = AutoModel.from_pretrained("your-huggingface-username/model-name")
|
| 48 |
model.eval()
|
|
|
|
| 49 |
|
| 50 |
-
#
|
|
|
|
| 51 |
transform = transforms.Compose([
|
| 52 |
-
transforms.Resize((224, 224)),
|
| 53 |
-
transforms.ToTensor(),
|
| 54 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 55 |
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
print(f"Predicted DR severity: {prediction}")
|
| 66 |
```
|
| 67 |
|
| 68 |
## License
|
|
|
|
| 46 |
# Load model
|
| 47 |
model = AutoModel.from_pretrained("your-huggingface-username/model-name")
|
| 48 |
model.eval()
|
| 49 |
+
```
|
| 50 |
|
| 51 |
+
### Transformer Application
|
| 52 |
+
```python
|
| 53 |
transform = transforms.Compose([
|
| 54 |
+
transforms.Resize((224, 224)), # Resize image to match input size
|
| 55 |
+
transforms.ToTensor(), # Convert image to tensor
|
| 56 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Normalize using ImageNet stats
|
| 57 |
])
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### Function to preprocess image and get predictions
|
| 61 |
+
```python
|
| 62 |
+
def predict(image_path):
|
| 63 |
+
# Load and preprocess the input image
|
| 64 |
+
image = Image.open(image_path).convert('RGB') # Ensure RGB format
|
| 65 |
+
input_tensor = transform(image).unsqueeze(0).to(device) # Add batch dimension
|
| 66 |
+
|
| 67 |
+
# Perform inference
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
outputs = model(input_tensor) # Forward pass
|
| 70 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1) # Get class probabilities
|
| 71 |
+
|
| 72 |
+
return probabilities.cpu().numpy()[0] # Return probabilities as a NumPy array
|
| 73 |
|
| 74 |
+
# Test with an example image
|
| 75 |
+
image_path = "your_image_path" # Replace with your test image path
|
| 76 |
+
class_probs = predict(image_path)
|
| 77 |
|
| 78 |
+
# Print results
|
| 79 |
+
print(f"Class probabilities: {class_probs}")
|
| 80 |
+
predicted_class = np.argmax(class_probs) # Get the class with highest probability
|
| 81 |
+
print(f"Predicted class: {predicted_class}")
|
|
|
|
| 82 |
```
|
| 83 |
|
| 84 |
## License
|