-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
74 lines (61 loc) · 2.63 KB
/
Copy pathmodels.py
File metadata and controls
74 lines (61 loc) · 2.63 KB
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
import torch
import torch.nn as nn
from torchvision import models
class SimpleEncoder(nn.Module):
def __init__(self, num_classes=10, embedding_dim=128):
super(SimpleEncoder, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) # 32x16x16
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) # 64x8x8
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.relu3 = nn.ReLU()
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2) # 128x4x4
self.flatten = nn.Flatten()
self.fc_embedding = nn.Linear(128 * 4 * 4, embedding_dim)
self.relu_embedding = nn.ReLU()
self.fc_classifier = nn.Linear(embedding_dim, num_classes)
def get_embeddings(self, x):
x = self.pool1(self.relu1(self.conv1(x)))
x = self.pool2(self.relu2(self.conv2(x)))
x = self.pool3(self.relu3(self.conv3(x)))
x = self.flatten(x)
x = self.relu_embedding(self.fc_embedding(x))
return x
def forward(self, x):
embeddings = self.get_embeddings(x)
output = self.fc_classifier(embeddings)
return output
class BackdoorModel(nn.Module):
"""
Model for backdoor attacks and detection
Has separate paths for feature extraction and classification
"""
def __init__(self, num_classes=10, feature_dim=128):
super(BackdoorModel, self).__init__()
# Base backbone (ResNet18)
self.backbone = models.resnet18(weights=None)
num_ftrs = self.backbone.fc.in_features
self.backbone.fc = nn.Identity() # Remove classifier
# Feature embedding layer
self.feature_layer = nn.Sequential(
nn.Linear(num_ftrs, feature_dim),
nn.ReLU()
)
# Classification head
self.classifier = nn.Linear(feature_dim, num_classes)
def get_embeddings(self, x):
"""Extract features without classification"""
features = self.backbone(x)
embeddings = self.feature_layer(features)
return embeddings
def forward(self, x, return_features=False):
"""Full forward pass with optional feature return"""
features = self.backbone(x)
embeddings = self.feature_layer(features)
if return_features:
return embeddings
logits = self.classifier(embeddings)
return logits