Spaces:
Sleeping
Sleeping
File size: 1,973 Bytes
8ccabdf 127734b 8ccabdf 127734b 8ccabdf 127734b 8ccabdf 127734b 8ccabdf 127734b 8ccabdf 194eedd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """
models/level1_gatekeeper.py
Level 1 Binary Gatekeeper — ResNet-50 pretrained on ImageNet.
"""
import logging
from typing import Dict, List
import torch
import torch.nn as nn
from torchvision import models
logger = logging.getLogger(__name__)
class GatekeeperModel(nn.Module):
def __init__(
self,
num_classes: int = 2,
dropout_rate: float = 0.3,
pretrained: bool = True,
freeze_backbone: bool = True,
) -> None:
super().__init__()
# Load ResNet-50
weights = models.ResNet50_Weights.IMAGENET1K_V1 if pretrained else None
backbone = models.resnet50(weights=weights)
# Extract features (everything except avgpool and fc)
self.features = nn.Sequential(*list(backbone.children())[:-2])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
in_features = 2048
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Dropout(p=dropout_rate),
nn.Linear(in_features, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=dropout_rate / 2),
nn.Linear(512, num_classes),
)
if freeze_backbone:
self.freeze_backbone()
def freeze_backbone(self) -> None:
for param in self.features.parameters():
param.requires_grad = False
def unfreeze_backbone(self) -> None:
for param in self.features.parameters():
param.requires_grad = True
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.features(x)
x = self.avgpool(x)
x = self.classifier(x)
return x
def build_gatekeeper(
num_classes: int = 2,
dropout_rate: float = 0.3,
pretrained: bool = True,
freeze_backbone: bool = True,
) -> GatekeeperModel:
return GatekeeperModel(
num_classes=num_classes,
dropout_rate=dropout_rate,
pretrained=pretrained,
freeze_backbone=freeze_backbone,
)
|