Last active
December 16, 2025 15:49
-
-
Save keivalya/9afcf227aae3161f3f54a35850e22fc7 to your computer and use it in GitHub Desktop.
Image Encoder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Encoding image using Convolutional Neural Net | |
| class ImageEncoderTinyCNN(nn.Module): | |
| def __init__(self, d_model=128): | |
| super().__init__() | |
| self.conv1 = nn.Conv2d(3, 32, kernel_size=5, stride=2, padding=2) | |
| self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1) | |
| self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1) | |
| self.proj = nn.Linear(128, d_model) | |
| self.ln = nn.LayerNorm(d_model) | |
| def forward(self, x): | |
| x = F.relu(self.conv1(x)) | |
| x = F.relu(self.conv2(x)) | |
| x = F.relu(self.conv3(x)) | |
| x = x.mean(dim=[2, 3]) # GAP | |
| x = self.proj(x) | |
| x = self.ln(x) | |
| return x # (B, d_model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment