c3n7 commited on
Commit
8ee3890
·
1 Parent(s): 7f1eb84

First of many

Browse files
.gitattributes CHANGED
@@ -33,3 +33,13 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ EfficientNet_b2.pth filter=lfs diff=lfs merge=lfs -text
37
+ examples/person10_virus_35.jpeg filter=lfs diff=lfs merge=lfs -text
38
+ examples/person100_bacteria_475.jpeg filter=lfs diff=lfs merge=lfs -text
39
+ examples/person101_bacteria_483.jpeg filter=lfs diff=lfs merge=lfs -text
40
+ examples/person103_bacteria_489.jpeg filter=lfs diff=lfs merge=lfs -text
41
+ examples/person1608_virus_2786.jpeg filter=lfs diff=lfs merge=lfs -text
42
+ examples/IM-0001-0001.jpeg filter=lfs diff=lfs merge=lfs -text
43
+ examples/IM-0011-0001-0001.jpeg filter=lfs diff=lfs merge=lfs -text
44
+ examples/IM-0016-0001.jpeg filter=lfs diff=lfs merge=lfs -text
45
+ examples/person1_virus_6.jpeg filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ venv/
2
+ __pycache__/
3
+ flagged/
EfficientNet_b0.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1245266f52ce06f3019f18832199587f9007b848158f6b8f9aea08d469eccaf
3
+ size 16346498
EfficientNet_b2.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f47b49b1538f5c24e37f34b1720aece156cfcb35153d74f0cd8a2af5cdb1269
3
+ size 31278138
LICENSE.txt ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Timothy Karani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class setup
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetb0_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names
11
+ with open("class_names.txt", "r") as f:
12
+ class_names = [class_name.strip() for class_name in f.readlines()]
13
+
14
+ ## 2. Model and transforms preparation
15
+ effnetb0, effnetb0_transforms = create_effnetb0_model(num_classes=3)
16
+
17
+ # Load saved weights
18
+ effnetb0.load_state_dict(
19
+ torch.load(f="EfficientNet_b0.pth", map_location=torch.device("cpu"))
20
+ )
21
+
22
+
23
+ ### 3. Predict function
24
+ def predict(img) -> Tuple[Dict, float]:
25
+ # Start a timer
26
+ start_time = timer()
27
+
28
+ # Transform the input image for use with EffNetB0
29
+ img = effnetb0_transforms(img).unsqueeze(
30
+ 0
31
+ ) # unsqueeze = add batch dimension on 0th index
32
+
33
+ # Put model into eval mode, make prediction
34
+ effnetb0.eval()
35
+ with torch.inference_mode():
36
+ # Pass transformed image through the model and turn the prediction logits into probaiblities
37
+ pred_probs = torch.softmax(effnetb0(img), dim=1)
38
+
39
+ # Create a prediction label and prediction probability dictionary
40
+ pred_labels_and_probs = {
41
+ class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
42
+ }
43
+
44
+ # Calculate pred time
45
+ end_time = timer()
46
+ pred_time = round(end_time - start_time, 4)
47
+
48
+ # Return pred dict and pred time
49
+ return pred_labels_and_probs, pred_time
50
+
51
+
52
+ ### 4. Gradio app ###
53
+ # Create title, description and article
54
+ title = "EffNet Pneumonia, by Timothy Karani"
55
+ description = "An EfficientNetB0 model for multiclass pneumonia detection"
56
+ article = "AN EFFICIENT DEEP LEARNING APPROACH FOR MULTICLASS PNEUMONIA DETECTION IN CHEST X-RAY IMAGES."
57
+
58
+ # Create example list
59
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
60
+
61
+ # Create the Gradio demo
62
+ demo = gr.Interface(
63
+ fn=predict, # maps inputs to outputs
64
+ inputs=gr.Image(type="pil"),
65
+ outputs=[
66
+ gr.Label(num_top_classes=3, label="Predictions"),
67
+ gr.Number(label="Prediction time (s)"),
68
+ ],
69
+ examples=example_list,
70
+ title=title,
71
+ description=description,
72
+ article=article,
73
+ )
74
+
75
+ # Launch the demo!
76
+ demo.launch()
class_names.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ NORMAL
2
+ VIRAL
3
+ BACTERIAL
examples/.gitattributes ADDED
@@ -0,0 +1 @@
 
 
1
+ examples/*.jpeg filter=lfs diff=lfs merge=lfs -text
examples/IM-0001-0001.jpeg ADDED

Git LFS Details

  • SHA256: f17a003185299f171440c2cb408c2e5bacea90376971ed81ecc9812465e209fb
  • Pointer size: 131 Bytes
  • Size of remote file: 253 kB
examples/IM-0011-0001-0001.jpeg ADDED

Git LFS Details

  • SHA256: 559f84bf01cfc3ac75a64175d8d5ab32d8fa97f916b472264e2a5571eb236c4a
  • Pointer size: 131 Bytes
  • Size of remote file: 447 kB
examples/IM-0016-0001.jpeg ADDED

Git LFS Details

  • SHA256: 696eea445b9d885c401f76432a1a57112176dde3f29092ce98cfd520e10848ee
  • Pointer size: 131 Bytes
  • Size of remote file: 542 kB
examples/person100_bacteria_475.jpeg ADDED

Git LFS Details

  • SHA256: 4ba221b94dc77ef2e3663231b816fcf07d411067e8d09b40e72e707bc157c4cd
  • Pointer size: 130 Bytes
  • Size of remote file: 77.4 kB
examples/person101_bacteria_483.jpeg ADDED

Git LFS Details

  • SHA256: 9bf2593745508f3a928790d0960c1839f54162163fca695c8592b2c40f24e184
  • Pointer size: 130 Bytes
  • Size of remote file: 48.1 kB
examples/person103_bacteria_489.jpeg ADDED

Git LFS Details

  • SHA256: 0ba10667f41e446b24910fa53fccd7311ed51716e626216ac05be70b73820445
  • Pointer size: 130 Bytes
  • Size of remote file: 55.7 kB
examples/person10_virus_35.jpeg ADDED

Git LFS Details

  • SHA256: f94ea6ba762ada33f1b9a3f159fa7c9d1f3f2c6cb01fcdae85da4c8974b3de8e
  • Pointer size: 131 Bytes
  • Size of remote file: 143 kB
examples/person1608_virus_2786.jpeg ADDED

Git LFS Details

  • SHA256: 21f658d0d13ec8915d87d7861fa04b1040d22773c99f2b0023524a3ca5e87620
  • Pointer size: 130 Bytes
  • Size of remote file: 69.2 kB
examples/person1_virus_6.jpeg ADDED

Git LFS Details

  • SHA256: d0b01cbe27c7fceb52df8ee4734932d5045c657b2be8450b07aec76c0bf9341b
  • Pointer size: 130 Bytes
  • Size of remote file: 41.2 kB
model.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb0_model(num_classes: int = 3, seed: int = 42):
8
+ """Creates an EfficientNetB0 Model and Transforms"""
9
+ # 1. Setup Weights
10
+ weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
11
+
12
+ # 2. Get transforms
13
+ transforms = weights.transforms()
14
+
15
+ # 3. Setup pretrained model
16
+ model = torchvision.models.efficientnet_b0(weights=weights)
17
+
18
+ # 4 Freeze all layers
19
+ for param in model.parameters():
20
+ param.requires_grad = False
21
+
22
+ # 5. Change classifier head with random seed for reproducability
23
+ torch.manual_seed(seed)
24
+
25
+ model.classifier = nn.Sequential(
26
+ nn.Dropout(p=0.2, inplace=True),
27
+ nn.Linear(in_features=1280, out_features=num_classes, bias=True),
28
+ )
29
+
30
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.37.2
2
+ torch
3
+ torchvision
4
+ numpy<2