Spaces:
Sleeping
Sleeping
luxmorocco
commited on
Commit
•
b3991da
1
Parent(s):
d67965f
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,117 @@ label_names = [
|
|
19 |
"Pulmonary_fibrosis"
|
20 |
]
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def generate_diagnostic_report(predictions, labels, threshold=0.5):
|
23 |
# Initialize an empty report string
|
24 |
report = "Diagnostic Report:\n\n"
|
|
|
19 |
"Pulmonary_fibrosis"
|
20 |
]
|
21 |
|
22 |
+
class VinDetector(pl.LightningModule):
|
23 |
+
def __init__(self, **kwargs):
|
24 |
+
super().__init__()
|
25 |
+
|
26 |
+
self.model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
|
27 |
+
num_classes = 15
|
28 |
+
in_features = self.model.roi_heads.box_predictor.cls_score.in_features
|
29 |
+
self.model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
|
30 |
+
|
31 |
+
self.learning_rate = 1e-3
|
32 |
+
self.batch_size = 4
|
33 |
+
|
34 |
+
def forward(self, x):
|
35 |
+
return self.model(x)
|
36 |
+
|
37 |
+
def prepare_data(self):
|
38 |
+
|
39 |
+
df = pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/train.csv')
|
40 |
+
df = df[df['class_id'] != 14].reset_index(drop=True)
|
41 |
+
self.train_dataset = VBDDataset(df, '../input/vinbigdata-chest-xray-original-png/train', get_train_transform())
|
42 |
+
|
43 |
+
def train_dataloader(self):
|
44 |
+
|
45 |
+
return DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=True, pin_memory=True, num_workers=4, collate_fn=collate_fn)
|
46 |
+
|
47 |
+
def training_step(self, batch, batch_idx):
|
48 |
+
images, targets = batch
|
49 |
+
targets = [{k: v for k, v in t.items()} for t in targets]
|
50 |
+
|
51 |
+
loss_dict = self.model(images, targets)
|
52 |
+
loss = sum(loss for loss in loss_dict.values())
|
53 |
+
self.log('Loss', loss, on_step=True, on_epoch=True, prog_bar=True)
|
54 |
+
return {"loss": loss}
|
55 |
+
|
56 |
+
def configure_optimizers(self):
|
57 |
+
optimizer = torch.optim.SGD(self.model.parameters(), lr=self.learning_rate, momentum=0.95, weight_decay=1e-5, nesterov=True)
|
58 |
+
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=6, eta_min=0, verbose=True)
|
59 |
+
return [optimizer], [scheduler]
|
60 |
+
|
61 |
+
|
62 |
+
#VBDDataset Class
|
63 |
+
class VBDDataset(Dataset):
|
64 |
+
|
65 |
+
def __init__(self, dataframe, image_dir, transforms=None, phase='train'):
|
66 |
+
super().__init__()
|
67 |
+
|
68 |
+
|
69 |
+
self.image_ids = dataframe['image_id'].unique()
|
70 |
+
self.df = dataframe
|
71 |
+
self.image_dir = image_dir
|
72 |
+
self.transforms = transforms
|
73 |
+
self.phase = phase
|
74 |
+
|
75 |
+
def __getitem__(self, idx):
|
76 |
+
|
77 |
+
image_id = self.image_ids[idx]
|
78 |
+
records = self.df[self.df['image_id'] == image_id]
|
79 |
+
|
80 |
+
|
81 |
+
image = cv2.imread(f'{self.image_dir}/{image_id}.png', cv2.IMREAD_COLOR)
|
82 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)
|
83 |
+
image /= 255.0
|
84 |
+
|
85 |
+
|
86 |
+
if self.phase == 'test':
|
87 |
+
if self.transforms:
|
88 |
+
sample = {
|
89 |
+
'image': image,
|
90 |
+
}
|
91 |
+
sample = self.transforms(**sample)
|
92 |
+
image = sample['image']
|
93 |
+
return image, image_id
|
94 |
+
|
95 |
+
|
96 |
+
boxes = records[['x_min', 'y_min', 'x_max', 'y_max']].values
|
97 |
+
|
98 |
+
|
99 |
+
area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
|
100 |
+
area = torch.as_tensor(area, dtype=torch.float32)
|
101 |
+
|
102 |
+
|
103 |
+
labels = torch.squeeze(torch.as_tensor((records.class_id.values+1,), dtype=torch.int64))
|
104 |
+
iscrowd = torch.zeros((records.shape[0],), dtype=torch.int64)
|
105 |
+
|
106 |
+
|
107 |
+
target = {}
|
108 |
+
target['boxes'] = boxes
|
109 |
+
target['labels'] = labels
|
110 |
+
target['area'] = area
|
111 |
+
target['image_id'] = torch.tensor([idx])
|
112 |
+
target['iscrowd'] = iscrowd
|
113 |
+
|
114 |
+
|
115 |
+
if self.transforms:
|
116 |
+
sample = {
|
117 |
+
'image': image,
|
118 |
+
'bboxes': target['boxes'],
|
119 |
+
'labels': labels
|
120 |
+
}
|
121 |
+
sample = self.transforms(**sample)
|
122 |
+
image = sample['image']
|
123 |
+
|
124 |
+
target['boxes'] = torch.as_tensor(sample['bboxes'])
|
125 |
+
|
126 |
+
return image, target
|
127 |
+
|
128 |
+
def __len__(self):
|
129 |
+
|
130 |
+
return self.image_ids.shape[0]
|
131 |
+
|
132 |
+
|
133 |
def generate_diagnostic_report(predictions, labels, threshold=0.5):
|
134 |
# Initialize an empty report string
|
135 |
report = "Diagnostic Report:\n\n"
|