Skillversitas

Image Classification with CNNs

The CNN is the model the image classifies: the Conv2d the filter (the spatial the feature, the weight the share), the MaxPool2d the down (the spatial the half, the scale the invariant), and the fc the 10 class (the flatten the classify). The 7x7 the stride 2 the ImageNet's pattern (the VGG, the ResNet), the batch_norm the stability (the internal covariate shift), and the 99% the MNIST's accuracy (the Linear's 85% the CNN's gain). This chapter covers the CNN (the Conv2d, the Pool, the fc), the shape (the (N, C, H, W) the track), the batch_norm (the stability), and the MNIST the 99% the first run.

The CNN: The Conv2d and the Pool

# the CNN (the):
import torch.nn as nn

class CNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 32, 3, padding=1),  # the (N,32,28,28)
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(2),                 # the (N,32,14,14)
            nn.Conv2d(32, 64, 3, padding=1), # the (N,64,14,14)
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(2),                 # the (N,64,7,7)
        )
        self.classifier = nn.Sequential(
            nn.Flatten(),                    # the (N, 3136)
            nn.Linear(64 * 7 * 7, 128),      # the
            nn.ReLU(),
            nn.Dropout(0.25),                # the
            nn.Linear(128, 10),              # the
        )
    def forward(self, x):
        return self.classifier(self.features(x))

The CNN is the two: the features (the Conv2d the filter the 32, the BatchNorm2d the stability, the ReLU the non-linear, the MaxPool2d the down) and the classifier (the Flatten the (N, 3136), the Linear the 128, the Dropout the overfit's guard, the Linear the 10). The shape is the track: the (N, 1, 28, 28) the input, the Conv2d(1, 32, 3, padding=1) the (N, 32, 28, 28) (the padding the size the keep), the MaxPool2d(2) the (N, 32, 14, 14) (the half), the Conv2d(32, 64, 3, padding=1) the (N, 64, 14, 14), the MaxPool2d(2) the (N, 64, 7, 7), and the Flatten the (N, 3136) - the Conv2d the spatial the feature (the edge, the corner, the texture), the Pool the scale the invariant, and the fc the classify - the next section's batch_norm is the stability, and the MNIST the run.

The MNIST: The 99%

# the MNIST (the):
#   the
model = CNN().to(device)
opt = optim.Adam(model.parameters(), lr=1e-3)
lossf = nn.CrossEntropyLoss()

for epoch in range(5):
    model.train()
    for x, y in train_loader:
        x, y = x.to(device), y.to(device)
        opt.zero_grad()
        loss = lossf(model(x), y)
        loss.backward()
        opt.step()
    print(epoch, f"{loss.item():.3f}", f"{evaluate():.3f}")
#   the
#   the
#   the
#   the

The run is the CNN the Adam (the lr the 1e-3, the SGD's 0.01 the Adam's 10x the small): the epoch the loss the 0.05, the test the 99.2% (the Linear's 85% the CNN's 14% the gain, the spatial the feature the digit the learn). The Adam is the SGD's upgrade (the momentum plus the adaptive the lr the per-param, the optimizer chapter the detail), and the batch_norm is the stability (the internal covariate shift the reduce, the lr the high the allow) - the CNN the MNIST's pattern, the CIFAR10 the 32x32 the 3 channel the same, the ImageNet the 224x224 the 1000 class the ResNet the scale. The rule is the one: the Conv2d the feature, the Pool the down, the fc the classify, the batch_norm the stability, and the cnn-detail chapter the Conv2d the kernel the stride the padding the detail.

The Working Rules

  • The Conv2d is the filter, the shape the track. The Conv2d(1, 32, 3, padding=1) the (N, 32, 28, 28) (the padding the size keep), the spatial the feature (the edge, the texture), and the weight the share (the translation the invariant, the parameter the small).
  • The MaxPool2d is the down, the scale the invariant. The 2x2 the half (the (28,28) to the (14,14)), the stride 2 the ImageNet's pattern, and the flatten the (N, 3136) the fc the input.
  • The batch_norm is the stability, the Dropout the overfit. The BatchNorm2d the internal covariate shift (the lr the high the allow), and the Dropout(0.25) the regularize (the train the mask, the eval the no).
  • The Adam is the SGD's upgrade, the 99% the CNN's. The lr the 1e-3 (the SGD's 0.01 the 10x), the MNIST the 99.2% (the Linear's 85% the gain), and the CIFAR10 the 3 channel the same, the ImageNet the ResNet the scale.

Summary

The CNN is the image's model: the Conv2d the filter (the spatial the feature, the edge the texture, the weight the share), the MaxPool2d the down (the half, the scale the invariant), and the fc the classify (the Flatten the (N, 3136), the Linear the 10). The shape is the track: the (N, 1, 28, 28) to the (N, 32, 28, 28) (the padding=1 the keep), to the (N, 32, 14, 14) (the Pool), to the (N, 64, 7, 7), to the (N, 3136) - the batch_norm the stability (the lr the high), the Dropout the overfit the guard. The MNIST run is the Adam (the lr the 1e-3) the 99.2% (the Linear's 85% the 14% the gain), and the CIFAR10 the 3 channel the same, the ImageNet the ResNet the scale. The next chapter's cnn-detail is the Conv2d the kernel the stride the padding the detail, and the transfer-learning the pretrain the fine-tune the real's path.