Ravindra BagaleCourses & study guides मराठी Track your progress

48. Small CNN Image Project in PyTorch

48.2 Synthesize Tiny Images

Why. Build a toy dataset in memory so students without downloads can practise.

What. Class 0 images: brighter left half. Class 1: brighter right half. Crude but separable for a demo CNN.

How.

Synthesize tensors

  1. Create zeros tensors.
  2. Fill left or right half.
  3. Add a little noise.
  4. Stack labels.
import torch

def make_batch(n=64):
 x = torch.rand(n, 1, 28, 28) * 0.2
 y = torch.randint(0, 2, (n,))
 for i in range(n):
 if y[i] == 0:
 x[i, 0, :, :14] += 0.8
 else:
 x[i, 0, :, 14:] += 0.8
 return x, y

X, y = make_batch(8)
print(X.shape, y)