-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
23 lines (18 loc) · 819 Bytes
/
model.py
File metadata and controls
23 lines (18 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import torch
import torch.nn as nn
from torchvision import models
class StainLevelModel(nn.Module):
def __init__(self, num_classes=6):
super().__init__()
# Load weights
self.net = models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1)
# Modify first layer: 3 channels (RGB) -> 4 channels (RGB + Mask)
old_weights = self.net.conv1.weight.data
self.net.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
with torch.no_grad():
self.net.conv1.weight[:, :3, :, :] = old_weights
self.net.conv1.weight[:, 3, :, :] = old_weights.mean(dim=1)
# Output layer for 6 classes
self.net.fc = nn.Linear(self.net.fc.in_features, num_classes)
def forward(self, x):
return self.net(x)