|
| 1 | +import torch.nn as nn |
| 2 | +import torch.nn.functional as F |
| 3 | + |
| 4 | + |
| 5 | +class RandomClass(object): |
| 6 | + def __init__(self, a): |
| 7 | + self.a = a |
| 8 | + def forward(self, x): |
| 9 | + return self.a + x |
| 10 | + |
| 11 | +class WeirdModelWithoutForward(nn.Module): |
| 12 | + def __init__(self, a): |
| 13 | + self.a = a |
| 14 | + |
| 15 | +class WeirdModelWithoutInit(nn.Module): |
| 16 | + |
| 17 | + def forward(x): |
| 18 | + return x |
| 19 | + |
| 20 | +class NetWithConvBiasSetToTrueWithARandomChange(nn.Module): |
| 21 | + def __init__(self): |
| 22 | + super(NetWithConvBiasSetToTrueWithARandomChange, self).__init__() |
| 23 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 24 | + kernel_size=5, |
| 25 | + stride=1) |
| 26 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=True) |
| 27 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 28 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 29 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 30 | + self.dense2 = nn.Linear(50, 10) |
| 31 | + |
| 32 | + def forward(self, x): |
| 33 | + x = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 34 | + x = self.conv2(x) |
| 35 | + x = x / 2 |
| 36 | + x = self.conv2_bn(x) |
| 37 | + x = F.relu(F.max_pool2d(x, 2)) |
| 38 | + x = x.view(-1, 320) |
| 39 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 40 | + return F.relu(self.dense2(x)) |
| 41 | + |
| 42 | + |
| 43 | +class NetWithConvBiasSetToTrueWithARandomAddedLineBetweenConvAndBN(nn.Module): |
| 44 | + def __init__(self): |
| 45 | + super(NetWithConvBiasSetToTrueWithARandomAddedLineBetweenConvAndBN, self).__init__() |
| 46 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 47 | + kernel_size=5, |
| 48 | + stride=1, bias=False) |
| 49 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=True) # Noncompliant {{Remove bias for convolutions before batch norm layers to save time and memory.}} |
| 50 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 51 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 52 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 53 | + self.dense2 = nn.Linear(50, 10) |
| 54 | + self.idx = 0 |
| 55 | + def forward(self, x): |
| 56 | + x = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 57 | + x = self.conv2(x) |
| 58 | + self.idx += 1 |
| 59 | + x = self.conv2_bn(x) |
| 60 | + x = F.relu(F.max_pool2d(x, 2)) |
| 61 | + x = x.view(-1, 320) |
| 62 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 63 | + return F.relu(self.dense2(x)) |
| 64 | + |
| 65 | +class NetWithConvBiasSetToTrueWithDiffVariableName(nn.Module): |
| 66 | + def __init__(self): |
| 67 | + super(NetWithConvBiasSetToTrueWithDiffVariableName, self).__init__() |
| 68 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 69 | + kernel_size=5, |
| 70 | + stride=1, bias=False) |
| 71 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=True) # Noncompliant {{Remove bias for convolutions before batch norm layers to save time and memory.}} |
| 72 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 73 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 74 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 75 | + self.dense2 = nn.Linear(50, 10) |
| 76 | + self.idx = 0 |
| 77 | + def forward(self, x): |
| 78 | + x1 = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 79 | + x2 = self.conv2(x1) |
| 80 | + self.idx += 1 |
| 81 | + x3 = self.conv2_bn(x2) |
| 82 | + x4 = F.relu(F.max_pool2d(x3, 2)) |
| 83 | + x5 = x4.view(-1, 320) |
| 84 | + x6 = F.relu(self.dense1_bn(self.dense1(x5))) |
| 85 | + return F.relu(self.dense2(x6)) |
| 86 | + |
| 87 | +class CompNetWithConvBiasSetToTrueWithDiffVariableName(nn.Module): |
| 88 | + def __init__(self): |
| 89 | + super(CompNetWithConvBiasSetToTrueWithDiffVariableName, self).__init__() |
| 90 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 91 | + kernel_size=5, |
| 92 | + stride=1, bias=False) |
| 93 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=False) |
| 94 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 95 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 96 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 97 | + self.dense2 = nn.Linear(50, 10) |
| 98 | + self.idx = 0 |
| 99 | + def forward(self, x): |
| 100 | + x1 = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 101 | + x2 = self.conv2(x1) |
| 102 | + self.idx += 1 |
| 103 | + x3 = self.conv2_bn(x2) |
| 104 | + x4 = F.relu(F.max_pool2d(x3, 2)) |
| 105 | + x5 = x4.view(-1, 320) |
| 106 | + x6 = F.relu(self.dense1_bn(self.dense1(x5))) |
| 107 | + return F.relu(self.dense2(x6)) |
| 108 | + |
| 109 | +class NetWithConvBiasSetToTrue(nn.Module): |
| 110 | + def __init__(self): |
| 111 | + super(NetWithConvBiasSetToTrue, self).__init__() |
| 112 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 113 | + kernel_size=5, |
| 114 | + stride=1) |
| 115 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=True) # Noncompliant {{Remove bias for convolutions before batch norm layers to save time and memory.}} |
| 116 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 117 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 118 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 119 | + self.dense2 = nn.Linear(50, 10) |
| 120 | + |
| 121 | + def forward(self, x): |
| 122 | + x = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 123 | + x = self.conv2(x) |
| 124 | + x = self.conv2_bn(x) |
| 125 | + x = F.relu(F.max_pool2d(x, 2)) |
| 126 | + x = x.view(-1, 320) |
| 127 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 128 | + return F.relu(self.dense2(x)) |
| 129 | + |
| 130 | +class NetWithDefaultConvBias(nn.Module): |
| 131 | + def __init__(self): |
| 132 | + super(NetWithDefaultConvBias, self).__init__() |
| 133 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 134 | + kernel_size=5, |
| 135 | + stride=1) |
| 136 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=True) # Noncompliant {{Remove bias for convolutions before batch norm layers to save time and memory.}} |
| 137 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 138 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 139 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 140 | + self.dense2 = nn.Linear(50, 10) |
| 141 | + |
| 142 | + def forward(self, x): |
| 143 | + x = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 144 | + x = F.relu(F.max_pool2d(self.conv2_bn(self.conv2(x)), 2)) |
| 145 | + x = x.view(-1, 320) |
| 146 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 147 | + return F.relu(self.dense2(x)) |
| 148 | + |
| 149 | +class NonCompliantNetWithSequentialKeywordParam(nn.Module): |
| 150 | + def __init__(self): |
| 151 | + super(NonCompliantNetWithSequentialKeywordParam, self).__init__() |
| 152 | + self.encoder = nn.Sequential( |
| 153 | + nn.Conv2d(in_channels=1, out_channels=10, kernel_size=5, stride=1), |
| 154 | + nn.MaxPool2d(2), |
| 155 | + nn.ReLU(), |
| 156 | + nn.Conv2d(10, 20, kernel_size=5, bias=True), # Noncompliant {{Remove bias for convolutions before batch norm layers to save time and memory.}} |
| 157 | + nn.BatchNorm2d(20), |
| 158 | + nn.MaxPool2d(2), |
| 159 | + nn.ReLU() |
| 160 | + ) |
| 161 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 162 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 163 | + self.dense2 = nn.Linear(50, 10) |
| 164 | + def forward(self, x): |
| 165 | + x = self.encoder(x) |
| 166 | + x = x.view(-1, 320) |
| 167 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 168 | + return F.relu(self.dense2(x)) |
| 169 | + |
| 170 | + |
| 171 | +class NonCompliantNetWithSequentialPosParam(nn.Module): |
| 172 | + def __init__(self): |
| 173 | + super(NonCompliantNetWithSequentialPosParam, self).__init__() |
| 174 | + self.encoder = nn.Sequential( |
| 175 | + nn.Conv2d(in_channels=1, out_channels=10, kernel_size=5, stride=1, bias=False), |
| 176 | + nn.MaxPool2d(2), |
| 177 | + nn.ReLU(), |
| 178 | + nn.Conv2d(10, 20, kernel_size=5), # Noncompliant {{Remove bias for convolutions before batch norm layers to save time and memory.}} |
| 179 | + nn.BatchNorm2d(20), |
| 180 | + nn.MaxPool2d(2), |
| 181 | + nn.ReLU() |
| 182 | + ) |
| 183 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 184 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 185 | + self.dense2 = nn.Linear(50, 10) |
| 186 | + def forward(self, x): |
| 187 | + x = self.encoder(x) |
| 188 | + x = x.view(-1, 320) |
| 189 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 190 | + return F.relu(self.dense2(x)) |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +class CompliantNet(nn.Module): |
| 195 | + def __init__(self): |
| 196 | + super(CompliantNet, self).__init__() |
| 197 | + self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, |
| 198 | + kernel_size=5, |
| 199 | + stride=1) |
| 200 | + self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias=False) |
| 201 | + self.conv2_bn = nn.BatchNorm2d(20) |
| 202 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 203 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 204 | + self.dense2 = nn.Linear(50, 10) |
| 205 | + |
| 206 | + def forward(self, x): |
| 207 | + x = F.relu(F.max_pool2d(self.conv1(x), 2)) |
| 208 | + x = F.relu(F.max_pool2d(self.conv2_bn(self.conv2(x)), 2)) |
| 209 | + x = x.view(-1, 320) |
| 210 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 211 | + return F.relu(self.dense2(x)) |
| 212 | + |
| 213 | + |
| 214 | +class CompliantNetWithSequential(nn.Module): |
| 215 | + def __init__(self): |
| 216 | + super(CompliantNetWithSequential, self).__init__() |
| 217 | + self.encoder = nn.Sequential( |
| 218 | + nn.Conv2d(in_channels=1, out_channels=10, kernel_size=5, stride=1), |
| 219 | + nn.MaxPool2d(2), |
| 220 | + nn.ReLU(), |
| 221 | + nn.Conv2d(10, 20, kernel_size=5, bias=False), |
| 222 | + nn.BatchNorm2d(20), |
| 223 | + nn.MaxPool2d(2), |
| 224 | + nn.ReLU() |
| 225 | + ) |
| 226 | + self.dense1 = nn.Linear(in_features=320, out_features=50) |
| 227 | + self.dense1_bn = nn.BatchNorm1d(50) |
| 228 | + self.dense2 = nn.Linear(50, 10) |
| 229 | + def forward(self, x): |
| 230 | + x = self.encoder(x) |
| 231 | + x = x.view(-1, 320) |
| 232 | + x = F.relu(self.dense1_bn(self.dense1(x))) |
| 233 | + return F.relu(self.dense2(x)) |
| 234 | + |
| 235 | + |
| 236 | + |
0 commit comments