Multilayer network: linear map → nonlinear activation (ReLU) → linear map … Without the nonlinearity, the whole stack collapses to one linear model.
Backpropagation = the chain rule, applied layer by layer from the loss back to every weight θ.
Training: stochastic gradient descent, momentum, Adam; fight overfitting with early stopping, dropout and weight decay (= ridge, lecture 2).
When a network wins: with many data. With few data, a forest on good descriptors is better (lecture 9: crossing near 500 compounds).
Every input was a vector of features with no order. But a micrograph is a grid of pixels and an XRD pattern is a line of intensities: neighbours belong together. A dense layer ignores that, and needs millions of weights.
Replace the dense layer by a small filter shared across the image: the convolution. Then pooling, deep architectures, segmentation with U-Net, transfer learning, augmentation, and 1-D networks for spectra.
A dense layer on a 256 × 256 micrograph needs about 6.5 million weights. Why does a convolutional layer that works better need only a few hundred?
Why may you rotate a micrograph to create "new" training images, but must be careful when you shift an XRD pattern along 2θ?
Why does a network pretrained on everyday photos (cats, cars) help to classify steel microstructures from only a hundred images?
A grain-boundary segmentation is "90 % accurate", yet the grain size computed from it is badly wrong. How?
A grey SEM micrograph of 256 × 256 pixels is 65 536 numbers (C = 1 channel; a colour photo has C = 3). What matters is local: a lamella, a boundary, a pore, a dislocation.
An XRD pattern from 10° to 80° in 0.1° steps is 701 intensities. Each peak is local; its position encodes a lattice spacing (Bragg).
Pixels → 100 hidden units: \((65\,536 + 1)\times 100 = 6\,553\,700\) weights. And a lamella learned in one corner must be learned again in every other corner.
Today's tasks: grain and phase segmentation in SEM micrographs · defect detection in TEM · crystal system from an XRD pattern.
With w = [−1, 0, 1] the output is "right neighbour minus left neighbour": +1 where the signal steps up, −1 where it steps down, 0 on flat parts. A kernel is a pattern detector; this one detects edges.
• K = 1: each pixel is only rescaled; no neighbours are used.
• K = image size: one output, which is exactly one dense neuron. A dense layer is the extreme case of a convolution.
• Units: z has the units of x times w; ReLU keeps only positive matches.
Each kernel is a learned local template: edge, line, spot, texture. It assumes a pattern means the same thing anywhere (fine for micrographs), so it cannot know absolute position, and it is not built to handle a change of scale (magnification) or of contrast it has never seen.
The transposed kernel looks for horizontal edges; it only sees the small bump of the boundary.
The output is a map of where vertical edges are, and how strong. A patch inside one uniform grain would give 0 (e.g. 24 − 24); this small image is too narrow to have one, so every output here sees part of the boundary. A CNN learns kernels like this one by itself.
| N | K | P | S | output O | name |
|---|---|---|---|---|---|
| 5 | 3 | 0 | 1 | 3 | "valid" |
| 5 | 3 | 1 | 1 | 5 | "same" size |
| 5 | 3 | 1 | 2 | 3 | downsampling |
| 256 | 3 | 1 | 2 | 128 | halves the image |
For S = 1 the size is kept when 2P = K − 1, i.e. P = 1 for a 3 × 3 kernel and P = 2 for 5 × 5. That is why kernels have odd sizes. If (N + 2P − K)/S is not a whole number, the last pixels are simply skipped.
Max pooling: keep the largest value in each 2 × 2 block (stride 2). No weights. Average pooling would give 3.5, 1, 1, 6.
Pooling makes the receptive field grow geometrically, so deep units see a whole grain or colony, and small shifts do not change the output. The price: the exact position is lost. Segmentation needs it back (U-Net).
320 weights can be pinned down by one image's 65 536 pixels; 6.5 million cannot be pinned down by a few hundred images: that is overfitting (lecture 9). Locality + sharing is a strong, correct assumption for micrographs, so we lose almost nothing by making it.
Input: 64 × 64 grey patch. Task: 4 microstructure classes. All convolutions 3 × 3 with padding 1, stride 1; pooling 2 × 2.
| layer | output shape | parameters |
|---|---|---|
| input | 64 × 64 × 1 | 0 |
| conv 3 × 3, 16 maps + ReLU | 64 × 64 × 16 | (9·1 + 1)·16 = 160 |
| max pool 2 × 2 | 32 × 32 × 16 | 0 |
| conv 3 × 3, 32 maps + ReLU | 32 × 32 × 32 | (9·16 + 1)·32 = 4 640 |
| max pool 2 × 2 | 16 × 16 × 32 | 0 |
| conv 3 × 3, 64 maps + ReLU | 16 × 16 × 64 | (9·32 + 1)·64 = 18 496 |
| global average pool | 64 | 0 |
| dense → 4 classes (softmax) | 4 | (64 + 1)·4 = 260 |
| total | 23 556 |
Softmax: \(p_c = e^{z_c} / \sum_k e^{z_k}\), the multiclass sigmoid (lecture 5); loss \(-\log p_{\text{true}}\).
The last conv units see 18 × 18 pixels: choose the patch scale so that one lamella spacing or grain fits in it, or add another block.
| network | year | idea | parameters |
|---|---|---|---|
| LeNet-5 | 1998 | conv + pool for handwritten digits | ~6 × 10⁴ |
| AlexNet | 2012 | GPUs, ReLU, dropout; won ImageNet by a wide margin | ~6 × 10⁷ |
| VGG-16 | 2014 | only stacked 3 × 3 kernels | ~1.4 × 10⁸ |
| ResNet-50 | 2015 | skip (residual) connections, very deep | ~2.5 × 10⁷ |
Early layers learn edges and blobs; deeper layers combine them into textures and objects. For materials images, small networks are often enough; the big ones matter for transfer learning.
Simulated grains (Voronoi cells) with random brightness, dark boundaries about 1.5 px wide, uneven illumination, blur and noise. Boundaries are 19 % of the pixels. True boundary masks are known.
\(\sqrt{z_x^2 + z_y^2}\) from a vertical and a horizontal 3 × 3 kernel, like worked problem 1 but with weights 1, 2, 1 (Sobel). It lights up on both sides of a dark boundary, and at every step between two grains of different brightness.
A hand-designed kernel finds "change", not "boundary". We need a kernel tuned to what a boundary looks like in these images, so we learn it from labelled pixels.
| model (test image) | pixel accuracy | boundary IoU |
|---|---|---|
| "everything is grain" | 0 | |
| brightness threshold (tuned on training image) | ||
| one 5 × 5 conv + sigmoid (26 weights) |
Training: logistic regression on every 5 × 5 patch of a second simulated image (cross-entropy, Adam). This is a one-layer CNN: one kernel, one bias, a sigmoid.
Blue = negative weight (wants dark), orange = positive (wants bright).
A dark line with brighter surroundings: a centre–surround detector. It compares a pixel with its neighbours, so uneven illumination and dark grains fool it much less. It would fail on bright boundaries or ones wider than its 5-pixel view.
Intergranular crack in a hardened steel (SEM): grains, boundaries and the crack are typical segmentation targets.
KassJuanebe, CC BY-SA 4.0
Conv blocks + pooling, as before: the map shrinks, the channels grow. It learns what is in the image, but loses the exact position.
Upsampling (e.g. transposed convolution) doubles the size at each level, back to the input size: one class probability per pixel.
Copy the fine encoder maps to the decoder at the same level: sharp boundaries come back. Loss: per-pixel cross-entropy, often plus a Dice term for rare classes.
When one class is rare, accuracy is dominated by the easy class (the accuracy trap of lecture 5). Report IoU or Dice for the rare class, and check the quantity you really need: grain count and size.
Edges, blobs and textures are the same in photos and micrographs, so early layers transfer. Objects (cats) do not. Defect detection in TEM, with a few hundred labelled images, typically works this way. It fails when the images are physically different: diffraction patterns, or contrast that flips with imaging conditions.
Rotations by 90°, flips, random crops, small brightness and contrast changes, noise. A grain does not care which way the microscope's stage was turned.
Zoom changes the apparent grain size: forbidden if the label is a size. Rotation is wrong if direction matters (rolling direction, elongated grains, growth direction).
Noise, background, overall intensity scale, peak broadening (crystallite size), changed relative intensities (texture), tiny shifts within the instrument's zero error.
A 0.2° shift is not "noise": it is a different lattice parameter, i.e. a different composition or strain. Shifting is allowed only if the label (e.g. crystal system) does not depend on it. A composition or strain change shifts peaks by Δ2θ = −2 tan θ · Δd/d, more at high angle; a uniform shift mimics a zero-offset error.
One weight per 2θ value per class: it learns "intensity at 38.4° means cubic". When the lattice parameter changes, every peak moves to a new 2θ, and that weight is useless.
Local peak motifs, independent of where they sit. It cannot use exact peak positions as a ruler (a ratio rule like the cubic sequence √1 : √2 : √3 is not local); a deeper network or physics-based features are needed for that.
Simulated primitive cubic, tetragonal, hexagonal cells; random a = 3–6 Å, c/a, intensities.
| model (test set) | accuracy |
|---|---|
| chance (3 balanced classes) | 0.33 |
| nearest neighbour, raw pattern | |
| dense softmax, raw pattern | |
| 1-D CNN |
import torch, torch.nn as nn C = lambda i, o: [nn.Conv2d(i, o, 3, padding=1), nn.ReLU()] net = nn.Sequential(*C(1, 16), nn.MaxPool2d(2), *C(16, 32), nn.MaxPool2d(2), *C(32, 64), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(64, 4)) print(sum(p.numel() for p in net.parameters())) # 23556 opt = torch.optim.Adam(net.parameters(), lr=1e-3) for xb, yb in train_loader: k = int(torch.randint(4, (1,))) # random 90° turn xb = torch.rot90(xb, k, dims=(2, 3)) loss = nn.functional.cross_entropy(net(xb), yb) opt.zero_grad(); loss.backward(); opt.step() xrd = nn.Sequential(nn.Conv1d(1, 16, 9), nn.ReLU(), nn.MaxPool1d(4), nn.Conv1d(16, 32, 9), nn.ReLU(), nn.AdaptiveMaxPool1d(1), nn.Flatten(), nn.Linear(32, 3)) # lecture used max + mean pooling (64 numbers); max only here
nn.functional.conv2d and check the 18s.Data and notebook are posted with this lecture. Team projects: the one-page proposal is due this week.
Locality and weight sharing: (9 + 1) × 32 = 320 weights, used at every position, versus one weight per pixel per unit (6.5 million). Fewer weights overfit less, and a pattern learned once is found everywhere.
A rotated micrograph shows the same microstructure (unless direction matters). A peak position is physics: 0.2° at 2θ = 40° changes d by 0.48 %, like 11 % Ge in Si.
Early layers of a pretrained CNN detect edges, blobs and textures, which also make up micrographs. Only a small head must be learned from the hundred images.
Boundaries are a small fraction of pixels: "all grain" is already 90 % accurate (IoU 0). A few missing boundary pixels merge grains. Use IoU or Dice, and check the grain count.
Lab 10: small CNNs, augmentation, transfer learning, 1-D CNN for XRD. Team projects: one-page proposal due. Problem set 3 (weeks 9–10) is due in week 11.
A crystal is neither a grid nor a line: atoms are nodes, neighbours within a cutoff are edges. Message passing generalizes today's shared kernel to irregular neighbourhoods (CGCNN, MEGNet, ALIGNN), with formation energies across about 10⁵ structures.
All schematics and plots were drawn for this course. The micrographs of example 1 and the XRD patterns of example 2 are simulated (numpy, work/lecture10); model sizes of LeNet, AlexNet, VGG and ResNet are rounded orders of magnitude.