Vidit2003 commited on
Commit
f0584a5
·
verified ·
1 Parent(s): 04feddf

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +133 -1
README.md CHANGED
@@ -43,7 +43,98 @@ It serves as a benchmark for performance for self-supervised models.
43
 
44
  Use the code below to get started with the model.
45
 
46
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  ## Training Details
49
 
@@ -62,6 +153,47 @@ We have utilized the self-supervised learning framework called DINO. We pre-trai
62
 
63
  We used three transforms mainly for preprocessing: SaturationNoiseInjector(), SelfImageNormalize(), Resize(224,224)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  ## Evaluation
66
 
67
  <!-- This section describes the evaluation protocols and provides the results. -->
 
43
 
44
  Use the code below to get started with the model.
45
 
46
+ ```
47
+ from transformers import AutoModel
48
+ import torch
49
+ import torch.nn as nn
50
+ import torchvision
51
+ from torchvision import transforms as v2
52
+ import numpy as np
53
+
54
+ # Noise Injector transformation
55
+ class SaturationNoiseInjector(nn.Module):
56
+ def __init__(self, low=200, high=255):
57
+ super().__init__()
58
+ self.low = low
59
+ self.high = high
60
+
61
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
62
+ channel = x[0].clone()
63
+ noise = torch.empty_like(channel).uniform_(self.low, self.high)
64
+ mask = (channel == 255).float()
65
+ noise_masked = noise * mask
66
+ channel[channel == 255] = 0
67
+ channel = channel + noise_masked
68
+ x[0] = channel
69
+ return x
70
+
71
+
72
+ # Self Normalize transformation
73
+ class PerImageNormalize(nn.Module):
74
+ def __init__(self, eps=1e-7):
75
+ super().__init__()
76
+ self.eps = eps
77
+ self.instance_norm = nn.InstanceNorm2d(
78
+ num_features=1,
79
+ affine=False,
80
+ track_running_stats=False,
81
+ eps=self.eps,
82
+ )
83
+
84
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
85
+ if x.dim() == 3:
86
+ x = x.unsqueeze(0)
87
+ x = self.instance_norm(x)
88
+ if x.shape[0] == 1:
89
+ x = x.squeeze(0)
90
+ return x
91
+
92
+
93
+ # Load model
94
+ device = "cuda"
95
+ model = AutoModel.from_pretrained("CaicedoLab/CHAMMI-75", trust_remote_code=True)
96
+ model.to(device).eval()
97
+
98
+ # Define transforms
99
+ transform = v2.Compose([
100
+ SaturationNoiseInjector(),
101
+ PerImageNormalize(),
102
+ v2.Resize(size=(224, 224), antialias=True),
103
+ ])
104
+
105
+ # Generate random batch (N, C, H, W)
106
+ batch_size = 2
107
+ num_channels = 3
108
+ images = torch.randint(0, 256, (batch_size, num_channels, 512, 512), dtype=torch.float32)
109
+
110
+ print(f"Input shape: {images.shape} (N={batch_size}, C={num_channels}, H=512, W=512)")
111
+ print()
112
+
113
+ # Bag of Channels (BoC) - process each channel independently
114
+ with torch.no_grad():
115
+ batch_feat = []
116
+ images = images.to(device)
117
+
118
+ for c in range(images.shape[1]):
119
+ # Extract single channel: (N, C, H, W) -> (N, 1, H, W)
120
+ single_channel = images[:, c, :, :].unsqueeze(1)
121
+
122
+ # Apply transforms
123
+ single_channel = transform(single_channel.squeeze(1)).unsqueeze(1)
124
+
125
+ # Extract features
126
+ output = model.forward_features(single_channel)
127
+ feat_temp = output["x_norm_clstoken"].cpu().detach().numpy()
128
+ batch_feat.append(feat_temp)
129
+
130
+ # Concatenate features from all channels
131
+ features = np.concatenate(batch_feat, axis=1)
132
+
133
+ print(f"Output shape: {features.shape}")
134
+ print(f" - Batch size (N): {features.shape[0]}")
135
+ print(f" - Feature dimension (C * feature_dim): {features.shape[1]}")
136
+ ```
137
+
138
 
139
  ## Training Details
140
 
 
153
 
154
  We used three transforms mainly for preprocessing: SaturationNoiseInjector(), SelfImageNormalize(), Resize(224,224)
155
 
156
+ ```
157
+ # Noise Injector transformation
158
+ class SaturationNoiseInjector(nn.Module):
159
+ def __init__(self, low=200, high=255):
160
+ super().__init__()
161
+ self.low = low
162
+ self.high = high
163
+
164
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
165
+ channel = x[0].clone()
166
+ noise = torch.empty_like(channel).uniform_(self.low, self.high)
167
+ mask = (channel == 255).float()
168
+ noise_masked = noise * mask
169
+ channel[channel == 255] = 0
170
+ channel = channel + noise_masked
171
+ x[0] = channel
172
+ return x
173
+
174
+
175
+ # Self Normalize transformation
176
+ class PerImageNormalize(nn.Module):
177
+ def __init__(self, eps=1e-7):
178
+ super().__init__()
179
+ self.eps = eps
180
+ self.instance_norm = nn.InstanceNorm2d(
181
+ num_features=1,
182
+ affine=False,
183
+ track_running_stats=False,
184
+ eps=self.eps,
185
+ )
186
+
187
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
188
+ if x.dim() == 3:
189
+ x = x.unsqueeze(0)
190
+ x = self.instance_norm(x)
191
+ if x.shape[0] == 1:
192
+ x = x.squeeze(0)
193
+ return x
194
+ ```
195
+
196
+
197
  ## Evaluation
198
 
199
  <!-- This section describes the evaluation protocols and provides the results. -->