|
a |
|
b/findings_classifier/chexpert_model.py |
|
|
1 |
import torch |
|
|
2 |
from torch import nn |
|
|
3 |
|
|
|
4 |
from biovil_t.pretrained import get_biovil_t_image_encoder |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
class ChexpertClassifier(nn.Module): |
|
|
8 |
def __init__(self, num_classes): |
|
|
9 |
super().__init__() |
|
|
10 |
self.biovil_encoder = get_biovil_t_image_encoder() |
|
|
11 |
|
|
|
12 |
self.fc1 = nn.Linear(128 * 4 * 4, 512) |
|
|
13 |
self.fc2 = nn.Linear(512, num_classes) |
|
|
14 |
|
|
|
15 |
def forward(self, x): |
|
|
16 |
x = self.biovil_encoder(x).projected_patch_embeddings |
|
|
17 |
x = torch.nn.functional.avg_pool2d(x, 4) |
|
|
18 |
x = x.view(x.shape[0], -1) # Flatten the tensor |
|
|
19 |
x = torch.relu(self.fc1(x)) |
|
|
20 |
# x = self.biovil_encoder(x).img_embedding |
|
|
21 |
return self.fc2(x) |