Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,30 @@ from PIL import Image
|
|
| 5 |
|
| 6 |
# Authenticate and download the EfficientNet model from Hugging Face Spaces
|
| 7 |
fs = HfFileSystem()
|
| 8 |
-
|
| 9 |
-
with fs.open(
|
| 10 |
-
|
| 11 |
|
| 12 |
# Save the EfficientNet model file to disk
|
| 13 |
efficientnet_model_file = 'efficientnet_b3.pt'
|
| 14 |
with open(efficientnet_model_file, 'wb') as f:
|
| 15 |
-
f.write(
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
custom_model_file = 'best_model.pth'
|
| 23 |
-
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
model.eval()
|
| 26 |
|
| 27 |
# Define a function that takes an image as input and uses the model for inference
|
|
@@ -48,16 +55,16 @@ def image_classifier(image):
|
|
| 48 |
# Return the result
|
| 49 |
return outputs[0].numpy(), predicted_label
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 5 |
|
| 6 |
# Authenticate and download the EfficientNet model from Hugging Face Spaces
|
| 7 |
fs = HfFileSystem()
|
| 8 |
+
efficientnet_model_path = 'dhhd255/efficientnet_b3/efficientnet_b3.pt'
|
| 9 |
+
with fs.open(efficientnet_model_path, 'rb') as f:
|
| 10 |
+
efficientnet_model_content = f.read()
|
| 11 |
|
| 12 |
# Save the EfficientNet model file to disk
|
| 13 |
efficientnet_model_file = 'efficientnet_b3.pt'
|
| 14 |
with open(efficientnet_model_file, 'wb') as f:
|
| 15 |
+
f.write(efficientnet_model_content)
|
| 16 |
|
| 17 |
+
# Authenticate and download your custom model from Hugging Face Spaces
|
| 18 |
+
custom_model_path = 'dhhd255/efficient_net_parkinsons/best_model.pth'
|
| 19 |
+
with fs.open(custom_model_path, 'rb') as f:
|
| 20 |
+
custom_model_content = f.read()
|
| 21 |
|
| 22 |
+
# Save your custom model file to disk
|
| 23 |
custom_model_file = 'best_model.pth'
|
| 24 |
+
with open(custom_model_file, 'wb') as f:
|
| 25 |
+
f.write(custom_model_content)
|
| 26 |
|
| 27 |
+
# Load the EfficientNet model onto the CPU
|
| 28 |
+
model = torch.load(efficientnet_model_file)
|
| 29 |
+
|
| 30 |
+
# Load your custom model onto the CPU
|
| 31 |
+
model.load_state_dict(torch.load(custom_model_file))
|
| 32 |
model.eval()
|
| 33 |
|
| 34 |
# Define a function that takes an image as input and uses the model for inference
|
|
|
|
| 55 |
# Return the result
|
| 56 |
return outputs[0].numpy(), predicted_label
|
| 57 |
|
| 58 |
+
# Load and preprocess the image
|
| 59 |
+
img_path = '/content/test_image_healthy.png'
|
| 60 |
+
img = Image.open(img_path)
|
| 61 |
+
img = data_transform(img)
|
| 62 |
+
|
| 63 |
+
# Add a batch dimension and move the image to the device
|
| 64 |
+
img = img.unsqueeze(0).to(device)
|
| 65 |
+
|
| 66 |
+
# Perform inference
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
outputs = model(img)
|
| 69 |
+
_, predicted = torch.max(outputs.data, 1)
|
| 70 |
+
print(f'Predicted class: {predicted.item()}')
|