ResNet Pytorch Model

adam_kossemtini: <p>hi guys,<br>
i would like to add a softmax form my ResNet model i don’t know how?<br>
there is the model :<br>
import torch</p>
<p>import torch.nn as nn</p>
<p>class block(nn.Module):</p>
<pre><code>def __init__(

    self, in_channels, intermediate_channels, identity_downsample=None, stride=1

):

    super(block, self).__init__()

    self.expansion = 4

    self.conv1 = nn.Conv2d(

        in_channels, intermediate_channels, kernel_size=1, stride=1, padding=0

    )

    self.bn1 = nn.BatchNorm2d(intermediate_channels)

    self.conv2 = nn.Conv2d(

        intermediate_channels,

        intermediate_channels,

        kernel_size=3,

        stride=stride,

        padding=1,

    )

    self.bn2 = nn.BatchNorm2d(intermediate_channels )

    self.conv3 = nn.Conv2d(

        intermediate_channels,

        intermediate_channels * self.expansion,

        kernel_size=1,

        stride=1,

        padding=0,

    )

    self.bn3 = nn.BatchNorm2d(intermediate_channels * self.expansion)

    self.relu = nn.ReLU()

    self.identity_downsample = identity_downsample

    self.stride = stride

def forward(self, x):

    identity = x.clone()

    x = self.conv1(x)

    x = self.bn1(x)

    x = self.relu(x)

    x = self.conv2(x)

    x = self.bn2(x)

    x = self.relu(x)

    x = self.conv3(x)

    x = self.bn3(x)

    if self.identity_downsample is not None:

        identity = self.identity_downsample(identity)

    x += identity

    x = self.relu(x)

    return x
</code></pre>
<p>class ResNet(nn.Module):</p>
<pre><code>def __init__(self, block, layers, image_channels, num_classes):

    super(ResNet, self).__init__()

    self.in_channels = 10

    self.conv1 = nn.Conv2d(image_channels, 10, kernel_size=3, stride=2, padding=3)

    self.bn1 = nn.BatchNorm2d(10)

    self.relu = nn.ReLU()

    self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

    self.softmax = nn.Softmax(dim=1)

    # Essentially the entire ResNet architecture are in these 4 lines below

    self.layer1 = self._make_layer(

        block, layers[0], intermediate_channels=32, stride=1

    )

    self.layer2 = self._make_layer(

        block, layers[1], intermediate_channels=64, stride=2

    )

            

    self.layer3 = self._make_layer(

        block, layers[2], intermediate_channels=128, stride=2

    )

    self.avgpool = nn.AdaptiveAvgPool2d((1, 1))

    self.fc = nn.Linear(128 * 4, num_classes)

    

def forward(self, x):

    x = self.conv1(x)

    x = self.bn1(x)

    x = self.relu(x)

    x = self.maxpool(x)

    x = self.layer1(x)

    x = self.layer2(x)

    x = self.layer3(x)

    

    x = self.avgpool(x)

    x = x.reshape(x.shape[0], -1)

   

    x = self.fc(x)

    x = self.softmax(x)

    return x

def _make_layer(self, block, num_residual_blocks, intermediate_channels, stride):

    identity_downsample = None

    layers = []

    if stride != 1 or self.in_channels != intermediate_channels * 4:

        identity_downsample = nn.Sequential(

            nn.Conv2d(

                self.in_channels,

                intermediate_channels * 4,

                kernel_size=1,

                stride=stride,

            ),

            nn.BatchNorm2d(intermediate_channels * 4),

        )

    layers.append(

        block(self.in_channels, intermediate_channels, identity_downsample, stride)

    )

    

    self.in_channels = intermediate_channels * 4

 

    for i in range(num_residual_blocks - 1):

        layers.append(block(self.in_channels, intermediate_channels))

    return nn.Sequential(*layers)
</code></pre>
<p>def ResNet50(img_channel=10, num_classes=2):</p>
<pre><code>return ResNet(block, [4,9,4], img_channel, num_classes)
</code></pre>
<p>loss_fn = nn.CrossEntropyLoss()</p>
<p>import torch.optim as optim</p>
<p>net = ResNet50(img_channel=10, num_classes=2)</p>
<p>optimizer = torch.optim.Adam(net.parameters(), lr = 0.001)</p>

ptrblck: <p>Where and why would you like to add a softmax layer to the model?<br>
Note that multi-class classification use cases either expect logits (for <code>nn.CrossEntropyLoss</code>) or log probabilities (for <code>nn.NLLLoss</code>).</p>