Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,63 @@
|
|
|
|
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 |
-
|
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 |
-
#
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
#
|
|
|
|
|
22 |
model.eval()
|
23 |
|
24 |
-
# Define
|
25 |
-
|
26 |
-
|
27 |
-
transforms.
|
28 |
-
|
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 |
-
|
42 |
-
|
43 |
-
|
|
|
1 |
+
import io
|
2 |
import torch
|
3 |
from torchvision import transforms
|
4 |
+
from huggingface_hub import HfFileSystem
|
5 |
from PIL import Image
|
6 |
|
7 |
# Authenticate and download the EfficientNet model from Hugging Face
|
8 |
+
fs = HfFileSystem()
|
9 |
+
efficientnet_model_path = 'dhhd255/efficientnet_b3/efficientnet_b3.pt'
|
10 |
+
with fs.open(efficientnet_model_path, 'rb') as f:
|
11 |
+
efficientnet_model_content = f.read()
|
|
|
|
|
|
|
12 |
|
13 |
# Load the EfficientNet model onto the CPU
|
14 |
+
efficientnet_model_file = io.BytesIO(efficientnet_model_content)
|
15 |
model = torch.load(efficientnet_model_file, map_location=torch.device('cpu'))
|
16 |
|
17 |
+
# Authenticate and download your custom model from Hugging Face
|
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 |
+
# Load your custom model onto the CPU
|
23 |
+
custom_model_file = io.BytesIO(custom_model_content)
|
24 |
+
model.load_state_dict(torch.load(custom_model_file, map_location=torch.device('cpu')))
|
25 |
model.eval()
|
26 |
|
27 |
+
# Define a function that takes an image as input and uses the model for inference
|
28 |
+
def image_classifier(image):
|
29 |
+
# Preprocess the input image
|
30 |
+
data_transform = transforms.Compose([
|
31 |
+
transforms.Lambda(lambda x: x.convert('RGB')),
|
32 |
+
transforms.Resize((224, 224)),
|
33 |
+
transforms.ToTensor()
|
34 |
+
])
|
35 |
+
image = Image.fromarray(image)
|
36 |
+
image = data_transform(image)
|
37 |
+
image = image.unsqueeze(0)
|
38 |
+
|
39 |
+
# Use your custom model for inference
|
40 |
+
with torch.no_grad():
|
41 |
+
outputs = model(image)
|
42 |
+
_, predicted = torch.max(outputs.data, 1)
|
43 |
+
|
44 |
+
# Map the index to a class label
|
45 |
+
labels = ['Healthy', 'Parkinson']
|
46 |
+
predicted_label = labels[predicted.item()]
|
47 |
+
|
48 |
+
# Return the result
|
49 |
+
return outputs[0].numpy(), predicted_label
|
50 |
|
51 |
# Load and preprocess the image
|
52 |
img_path = '/content/test_image_healthy.png'
|
53 |
img = Image.open(img_path)
|
54 |
img = data_transform(img)
|
55 |
|
56 |
+
# Add a batch dimension and move the image to the device
|
57 |
img = img.unsqueeze(0)
|
58 |
|
59 |
# Perform inference
|
60 |
with torch.no_grad():
|
61 |
+
outputs = model(img)
|
62 |
+
_, predicted = torch.max(outputs.data, 1)
|
63 |
+
print(f'Predicted class: {predicted.item()}')
|