Course materials · AI Applications

Week 9 · CNN layer operations and Tensor shapes

English translation

Before practice

01 Image features

Example scenario To find edges or patterns in a photo, what should you examine alongside individual pixel values?

Description
  • Edges and patterns appear as relationships between neighboring pixels
  • Convolution: apply a Filter to small regions to compute features at each position

02 How to find the same features

Example scenario The same pattern appears at multiple positions in a photo. Can features detected at one position also be detected elsewhere?

Description
  • Detect features at multiple positions using the same Parameters
  • Reduces the burden of separate Parameters at each position. Does not guarantee identical outputs for all position changes

Practice

1. Inspect layer shapes with a simple CNN

The input shape is (N,1,28,28). Implement a CNN in the following order.

  1. Conv2d(1,32,3,padding=1) → ReLU() → MaxPool2d(2)
  2. Conv2d(32,64,3,padding=1) → ReLU() → MaxPool2d(2)
  3. Conv2d(64,128,3,padding=1) → ReLU() → MaxPool2d(2,padding=1)
  4. Flatten(start_dim=1) → Linear(2048,625) → ReLU()
  5. Dropout() → Linear(625,10)

In one table, record each Layer's name, predicted output shape, and actual output shape. Fill in predictions before running the code. If any row differs, explain which operation changed the dimensions.

2. Batch size and parameters

  1. Feed inputs with Batch Sizes N∈{1,8,32}N\in\{1,8,32\} to the same CNN built in the previous activity. Keep the other input axes at (1,28,28) and run in model.eval() mode.
  2. Record NN, final output shape, and Parameter count in three columns. Compute the Parameter count with sum(p.numel() for p in model.parameters()); model is the CNN you built.
  3. Check whether each row has output shape (N,10) and whether the Parameter count is the same across all three rows. Also distinguish which Layers have trainable Parameters.

Submission and saving

Include code, actual outputs and plots, and your own interpretation. Submit through KLAS; check the semester schedule and latest announcements for the deadline.

Filename: Name_StudentID_Week9.ipynb.

Restart the runtime, run all code from the top, then save.

Optional practice

When input image size does not match Linear input size

  1. After running the small model in the first example, add a Code Cell at the bottom of the notebook and run the following code.
    for side in [32, 28]:
        debug_image = torch.randn(1, 1, side, side)
        with torch.no_grad():
            debug_features = small[:-1](debug_image)
            print("Input:", debug_image.shape,
                  "Flatten:", debug_features.shape,
                  "Linear in_features:", small[-1].in_features)
            try:
                print("Output:", small(debug_image).shape)
            except (RuntimeError, ValueError) as error:
                print(type(error).__name__, str(error))
  2. In a Text Cell, tabulate the Flatten shape and execution success for each image size. Find which of the two cases matches the input count required by Linear, and record how many inputs were supplied in the failing case.