Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
@@ -15,22 +16,20 @@ transform = transforms.Compose([
|
|
15 |
|
16 |
# Load the trained ResNet50 model
|
17 |
model = FineTunedResNet(num_classes=3)
|
18 |
-
model.load_state_dict(torch.load('
|
19 |
-
map_location=torch.device('cpu')))
|
20 |
model.eval()
|
21 |
|
22 |
-
|
23 |
# Define a function to make predictions
|
24 |
def predict(image):
|
25 |
start_time = time.time() # Start the timer
|
26 |
image = transform(image).unsqueeze(0) # Transform and add batch dimension
|
27 |
-
|
28 |
with torch.no_grad():
|
29 |
output = model(image)
|
30 |
probabilities = F.softmax(output, dim=1)[0]
|
31 |
top_prob, top_class = torch.topk(probabilities, 3)
|
32 |
classes = ['🦠 COVID', '🫁 Normal', '🦠 Pneumonia'] # Adjust based on the classes in your model
|
33 |
-
|
34 |
end_time = time.time() # End the timer
|
35 |
prediction_time = end_time - start_time # Calculate the prediction time
|
36 |
|
@@ -39,10 +38,9 @@ def predict(image):
|
|
39 |
for i in range(top_prob.size(0)):
|
40 |
result += f"{classes[top_class[i]]}: {top_prob[i].item() * 100:.2f}%\\n"
|
41 |
result += f"Prediction Time: {prediction_time:.2f} seconds"
|
42 |
-
|
43 |
return result
|
44 |
|
45 |
-
|
46 |
# Example images with labels
|
47 |
examples = [
|
48 |
['examples/Pneumonia/02009view1_frontal.jpg', '🦠 Pneumonia'],
|
@@ -67,4 +65,4 @@ interface = gr.Interface(
|
|
67 |
)
|
68 |
|
69 |
# Launch the interface
|
70 |
-
interface.launch()
|
|
|
1 |
+
%%writefile /content/lung_disease_detection/app.py
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import torch.nn.functional as F
|
|
|
16 |
|
17 |
# Load the trained ResNet50 model
|
18 |
model = FineTunedResNet(num_classes=3)
|
19 |
+
model.load_state_dict(torch.load('models/final_fine_tuned_resnet50.pth', map_location=torch.device('cpu')))
|
|
|
20 |
model.eval()
|
21 |
|
|
|
22 |
# Define a function to make predictions
|
23 |
def predict(image):
|
24 |
start_time = time.time() # Start the timer
|
25 |
image = transform(image).unsqueeze(0) # Transform and add batch dimension
|
26 |
+
|
27 |
with torch.no_grad():
|
28 |
output = model(image)
|
29 |
probabilities = F.softmax(output, dim=1)[0]
|
30 |
top_prob, top_class = torch.topk(probabilities, 3)
|
31 |
classes = ['🦠 COVID', '🫁 Normal', '🦠 Pneumonia'] # Adjust based on the classes in your model
|
32 |
+
|
33 |
end_time = time.time() # End the timer
|
34 |
prediction_time = end_time - start_time # Calculate the prediction time
|
35 |
|
|
|
38 |
for i in range(top_prob.size(0)):
|
39 |
result += f"{classes[top_class[i]]}: {top_prob[i].item() * 100:.2f}%\\n"
|
40 |
result += f"Prediction Time: {prediction_time:.2f} seconds"
|
41 |
+
|
42 |
return result
|
43 |
|
|
|
44 |
# Example images with labels
|
45 |
examples = [
|
46 |
['examples/Pneumonia/02009view1_frontal.jpg', '🦠 Pneumonia'],
|
|
|
65 |
)
|
66 |
|
67 |
# Launch the interface
|
68 |
+
interface.launch()
|