import torch import torch.nn as nn import torch.nn.functional as F class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_planes, planes, stride=1, downsample=None, norm_layer=None): super().__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False) self.bn2 = norm_layer(planes) self.downsample = downsample def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class ResNet(nn.Module): # ResNet-18 adapted for CIFAR (32x32): 3x3 stem, no maxpool. def __init__(self, block, layers, num_classes=100, width=64, norm_layer=None): super().__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d self.inplanes = width self.conv1 = nn.Conv2d(3, width, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = norm_layer(width) self.relu = nn.ReLU(inplace=True) self.layer1 = self._make_layer(block, width, layers[0], stride=1, norm_layer=norm_layer) self.layer2 = self._make_layer(block, width*2, layers[1], stride=2, norm_layer=norm_layer) self.layer3 = self._make_layer(block, width*4, layers[2], stride=2, norm_layer=norm_layer) self.layer4 = self._make_layer(block, width*8, layers[3], stride=2, norm_layer=norm_layer) self.avgpool = nn.AdaptiveAvgPool2d(1) self.fc = nn.Linear(width*8*block.expansion, num_classes) self._initialize_weights() def _make_layer(self, block, planes, blocks, stride, norm_layer): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), norm_layer(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride=stride, downsample=downsample, norm_layer=norm_layer)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, stride=1, norm_layer=norm_layer)) return nn.Sequential(*layers) def _initialize_weights(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1.0) nn.init.constant_(m.bias, 0.0) elif isinstance(m, nn.Linear): nn.init.kaiming_uniform_(m.weight, a=5**0.5) if m.bias is not None: nn.init.constant_(m.bias, 0.0) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = torch.flatten(x, 1) x = self.fc(x) return x def resnet18_cifar(num_classes=100, width=64): # Standard ResNet-18 for CIFAR-100 with adjustable width (default 64). return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=num_classes, width=width)