Experiment No.
5
Name: Zaid shaikh
Roll no : AIML-260
Aim: Design the architecture and implement the autoencoder model for Image Compression.
Introduction
Autoencoders are a class of unsupervised neural networks that learn to compress and reconstruct
data by passing it through a bottleneck (a lower-dimensional latent space). For image compression,
an autoencoder learns an efficient, compact representation of an image and reconstructs the image
from this compressed form.
Autoencoder Architecture
• Autoencoders compress images by learning a lower-dimensional representation.
• Encoder reduces dimensionality; decoder reconstructs image.
• Loss minimization aligns reconstructed images closely with originals.
• Useful for efficient image storage and transmission.
• Tradeoff exists between compression rate and reconstruction quality.
Encoder
• Input: Image tensor (e.g., 28x28x1 or 32x32x3)
• Several convolutional layers with stride > 1 or pooling to downsample spatially
• Activation functions (ReLU)
• Flatten and a dense layer to get the latent vector (compressed code)
Decoder
• Dense layer to reshape latent vector to a small feature map
• Several transposed convolution (ConvTranspose) or upsampling layers
• Activation functions (ReLU)
• Output layer with sigmoid (for normalized pixel values)
Advantages :
• Learns data-driven, adaptive compression schemes.
• Can capture non-linear structures in images.
• End-to-end trainable and flexible.
• Can achieve high compression ratios with reasonable reconstruction quality.
Learns data-driven, adaptive compression schemes.
• Can capture non-linear structures in images.
• End-to-end trainable and flexible.
• Can achieve high compression ratios with reasonable reconstruction quality.
Code:
class AE([Link]):
def _init_(self):
super(AE, self)._init_()
[Link] = [Link](
[Link](28 * 28, 128),
[Link](),
[Link](128, 64),
[Link](),
[Link](64, 36),
[Link](),
[Link](36, 18),
[Link](),
[Link](18, 9)
)
[Link] = [Link](
[Link](9, 18),
[Link](),
[Link](18, 36),
[Link](),
[Link](36, 64),
[Link](),
[Link](64, 128),
[Link](),
[Link](128, 28 * 28),
[Link]()
)
def forward(self, x):
encoded = [Link](x)
decoded = [Link](encoded
return decoded
model = AE()
loss_function = [Link]()
optimizer = [Link]([Link](), lr=1e-3, weight_decay=1e-8)
epochs = 2
outputs = []
losses = []
device = [Link]("cuda" if [Link].is_available() else "cpu")
[Link](device)
for epoch in range(epochs):
for images, _ in loader:
images = [Link](-1, 28 * 28).to(device)
reconstructed = model(images)
loss = loss_function(reconstructed, images)
optimizer.zero_grad()
[Link]()
[Link]()
[Link]([Link]())
[Link]((epoch, images, reconstructed))
print(f"Epoch {epoch+1}/{epochs}, Loss: {[Link]():.6f}")
[Link]('fivethirtyeight')
[Link](figsize=(8, 5))
[Link](losses, label='Loss')
[Link]('Iterations')
[Link]('Loss')
[Link]()
[Link]()
[Link]()
dataiter = iter(loader)
images, _ = next(dataiter)
images = [Link](-1, 28 * 28).to(device)
reconstructed = model(images)
fig, axes = [Link](nrows=2, ncols=10, figsize=(10, 3))
for i in range(10):
axes[0, i].imshow(images[i].cpu().detach().numpy().reshape(28, 28), cmap='gray')
axes[0, i].axis('off')
axes[1, i].imshow(reconstructed[i].cpu().detach().numpy().reshape(28, 28), cmap='gray')
axes[1, i].axis('off')
[Link]()
[Link]()
dataiter = iter(loader)
images, _ = next(dataiter)
images = [Link](-1, 28 * 28).to(device)
reconstructed = model(images)
fig, axes = [Link](nrows=2, ncols=10, figsize=(10, 3))
for i in range(10):
axes[0, i].imshow(images[i].cpu().detach().numpy().reshape(28, 28), cmap='gray')
axes[0, i].axis('off')
axes[1, i].imshow(reconstructed[i].cpu().detach().numpy().reshape(28, 28), cmap='gray')
axes[1, i].axis('off')
[Link]()
Conclusion : Hence we have sucessfully implemented this practical