Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,43 @@
|
|
1 |
import torch
|
2 |
from torchvision import transforms
|
3 |
-
from huggingface_hub import
|
4 |
from PIL import Image
|
5 |
|
6 |
-
# Authenticate and download the EfficientNet model from Hugging Face
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
efficientnet_model_content = f.read()
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
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, map_location=torch.device('cpu'))
|
29 |
|
30 |
-
# Load
|
31 |
model.load_state_dict(torch.load(custom_model_file, map_location=torch.device('cpu')))
|
|
|
|
|
32 |
model.eval()
|
33 |
|
34 |
-
# Define
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
transforms.ToTensor()
|
41 |
-
])
|
42 |
-
image = Image.fromarray(image)
|
43 |
-
image = data_transform(image)
|
44 |
-
image = image.unsqueeze(0)
|
45 |
-
|
46 |
-
# Use your custom model for inference
|
47 |
-
with torch.no_grad():
|
48 |
-
outputs = model(image)
|
49 |
-
_, predicted = torch.max(outputs.data, 1)
|
50 |
-
|
51 |
-
# Map the index to a class label
|
52 |
-
labels = ['Healthy', 'Parkinson']
|
53 |
-
predicted_label = labels[predicted.item()]
|
54 |
-
|
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
|
64 |
-
img = img.unsqueeze(0)
|
65 |
|
66 |
# Perform inference
|
67 |
with torch.no_grad():
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
1 |
import torch
|
2 |
from torchvision import transforms
|
3 |
+
from huggingface_hub import HfApi, HfFolder
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Authenticate and download the EfficientNet model from Hugging Face
|
7 |
+
api = HfApi()
|
8 |
+
efficientnet_model_url = api.presigned_url('dhhd255/efficientnet_b3', filename='efficientnet_b3.pt').geturl()
|
9 |
+
efficientnet_model_file = HfFolder.download_file(efficientnet_model_url, cache_dir=HfFolder.cache_dir())
|
|
|
10 |
|
11 |
+
# Authenticate and download your custom model from Hugging Face
|
12 |
+
custom_model_url = api.presigned_url('dhhd255/efficient_net_parkinsons', filename='best_model.pth').geturl()
|
13 |
+
custom_model_file = HfFolder.download_file(custom_model_url, cache_dir=HfFolder.cache_dir())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Load the EfficientNet model onto the CPU
|
16 |
model = torch.load(efficientnet_model_file, map_location=torch.device('cpu'))
|
17 |
|
18 |
+
# Load the saved weights onto the CPU
|
19 |
model.load_state_dict(torch.load(custom_model_file, map_location=torch.device('cpu')))
|
20 |
+
|
21 |
+
# Set the model to evaluation mode
|
22 |
model.eval()
|
23 |
|
24 |
+
# Define the image transform for inference
|
25 |
+
data_transform = transforms.Compose([
|
26 |
+
transforms.Lambda(lambda x: x.convert('RGB')),
|
27 |
+
transforms.Resize((224, 224)),
|
28 |
+
transforms.ToTensor()
|
29 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Load and preprocess the image
|
32 |
img_path = '/content/test_image_healthy.png'
|
33 |
img = Image.open(img_path)
|
34 |
img = data_transform(img)
|
35 |
|
36 |
+
# Add a batch dimension
|
37 |
+
img = img.unsqueeze(0)
|
38 |
|
39 |
# Perform inference
|
40 |
with torch.no_grad():
|
41 |
+
outputs = model(img)
|
42 |
+
_, predicted = torch.max(outputs.data, 1)
|
43 |
+
print(f'Predicted class: {predicted.item()}')
|