0% found this document useful (0 votes)
3 views5 pages

Program 9

The document outlines two programs for building convolutional neural networks (CNNs) using TensorFlow and Keras for image classification tasks. The first program focuses on the MNIST dataset, achieving high accuracy, while the second program uses the CIFAR-10 dataset, which is more complex and typically yields lower accuracy. Both programs include data loading, model building, training, and visualization of results such as accuracy and loss plots.

Uploaded by

sanketh0731
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Program 9

The document outlines two programs for building convolutional neural networks (CNNs) using TensorFlow and Keras for image classification tasks. The first program focuses on the MNIST dataset, achieving high accuracy, while the second program uses the CIFAR-10 dataset, which is more complex and typically yields lower accuracy. Both programs include data loading, model building, training, and visualization of results such as accuracy and loss plots.

Uploaded by

sanketh0731
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 9: Build Convolution Neural Network for image classification

import tensorflow as tf

from [Link] import layers, models

import [Link] as plt

import numpy as np

# 1. Load Data

(x_train, y_train), (x_test, y_test) = [Link].load_data()

x_train, x_test = x_train / 255.0, x_test / 255.0

# 2. Build Model

model = [Link]([

layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),

layers.MaxPooling2D((2, 2)),

[Link](),

[Link](64, activation='relu'),

[Link](10, activation='softmax')

])

# 3. Train (Store result in 'history' to plot graphs)

[Link](optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

history = [Link](x_train, y_train, epochs=3, validation_data=(x_test, y_test))

# A. Plot Accuracy & Loss

[Link](figsize=(10, 4))

[Link](1, 2, 1)

[Link]([Link]['accuracy'], label='Train Acc')


[Link]([Link]['val_accuracy'], label='Val Acc')

[Link]('Accuracy'); [Link]()

[Link](1, 2, 2)

[Link]([Link]['loss'], label='Train Loss')

[Link]([Link]['val_loss'], label='Val Loss')

[Link]('Loss'); [Link]()

[Link]()

# B. Show Predictions for first 5 test images

[Link](figsize=(10, 2))

for i in range(5):

img = x_test[i]

# Model predicts

pred = [Link]([Link]([Link](1, 28, 28, 1), verbose=0))

[Link](1, 5, i+1)

[Link](img, cmap='gray')

[Link](f"Pred: {pred}")

[Link]('off')

[Link]()

Program 10: Implement image processing model using Computer Vision


libraries (Tensor Flow, Keras)
import tensorflow as tf

from [Link] import layers, models

import [Link] as plt

import numpy as np

# 1. Load CIFAR-10 Data

(x_train, y_train), (x_test, y_test) = [Link].cifar10.load_data()

# Normalize

x_train, x_test = x_train / 255.0, x_test / 255.0

# Class labels

class_names = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']

# 2. Build Model

model = [Link]([

layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), (3 at the end


represents Red, Green, and Blue. MNIST only had 1)

layers.MaxPooling2D((2, 2)),

layers.Conv2D(64, (3, 3), activation='relu'),

layers.MaxPooling2D((2, 2)),

[Link](),

[Link](64, activation='relu'),

[Link](10, activation='softmax')

])

# 3. Train

[Link](optimizer='adam',

loss='sparse_categorical_crossentropy', metrics=['accuracy'])

history = [Link](x_train, y_train, epochs=3, validation_data=(x_test, y_test))


(Note: more epochs usually mean better accuracy until the model starts
"overfitting". For CIFAR-10, 10-20 epochs is better but it takes a little more time
)

# A. Plot Accuracy & Loss

[Link](figsize=(10, 4))

[Link](1, 2, 1)

[Link]([Link]['accuracy'], label='Train Acc')

[Link]([Link]['val_accuracy'], label='Val Acc')

[Link]('Accuracy'); [Link]()

[Link](1, 2, 2)

[Link]([Link]['loss'], label='Train Loss')

[Link]([Link]['val_loss'], label='Val Loss')

[Link]('Loss'); [Link]()

[Link]()

# B. Show Predictions for first 5 test images

[Link](figsize=(10, 2))

for i in range(5):

img = x_test[i]

pred = [Link]([Link]([Link](1,32,32,3), verbose=0))

[Link](1, 5, i+1)

[Link](img)

[Link](f"T:{class_names[y_test[i][0]]}\nP:{class_names[pred]}")

[Link]('off')

[Link]()

Expected Accuracy:

●​ A simple CNN for MNIST easily gets 99%.

●​ A simple CNN for CIFAR-10 (like the one above) will get around 70% to 75%.
●​ Note: Getting 90%+ on CIFAR-10 requires very large models (like ResNet)
that take hours to train. For a lab session, 70% is considered a very
successful result.

You might also like