0% found this document useful (0 votes)
7 views10 pages

Deep Learning and Machine Learning Models

The document outlines the implementation of deep learning and machine learning models for image classification using TensorFlow and scikit-learn. It includes code for a Convolutional Neural Network (CNN) and a Deep Convolutional Neural Network (DCNN) for image data, as well as Random Forest and Support Vector Machine (SVM) models for classification tasks. The models are trained on images stored in Google Drive, with data augmentation techniques applied, and performance metrics such as accuracy and classification reports are provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Deep Learning and Machine Learning Models

The document outlines the implementation of deep learning and machine learning models for image classification using TensorFlow and scikit-learn. It includes code for a Convolutional Neural Network (CNN) and a Deep Convolutional Neural Network (DCNN) for image data, as well as Random Forest and Support Vector Machine (SVM) models for classification tasks. The models are trained on images stored in Google Drive, with data augmentation techniques applied, and performance metrics such as accuracy and classification reports are provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT-4

IMAGE DATA TYPE:


[Link] LEARNING MODEL:
CNN:
PROGRAM:
import tensorflow as tf

from [Link] import ImageDataGenerator

from [Link] import Sequential

from [Link] import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

from [Link] import drive

# Mount Google Drive

[Link]('/content/drive')

# Directories

train_dir = '/content/drive/MyDrive/DATASET/TRAIN'

val_dir = '/content/drive/MyDrive/DATASET/TEST'

# Data augmentation and preparation

train_datagen = ImageDataGenerator(rescale=1./255, rotation_range=20, zoom_range=0.15,


horizontal_flip=True)

val_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150, 150),


batch_size=32, class_mode='categorical')

val_generator = val_datagen.flow_from_directory(val_dir, target_size=(150, 150), batch_size=32,


class_mode='categorical')

# CNN Model

model = Sequential([

Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),

MaxPooling2D(2, 2),

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

MaxPooling2D(2, 2),

Conv2D(128, (3, 3), activation='relu'),

MaxPooling2D(2, 2),
Flatten(),

Dense(512, activation='relu'),

Dropout(0.5),

Dense(len(train_generator.class_indices), activation='softmax')

])

# Compile the model

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

# Train the model

history = [Link](train_generator, epochs=10, validation_data=val_generator)

OUTPUT:

DCNN:
PROGRAM:
import tensorflow as tf

from [Link] import ImageDataGenerator

from [Link] import Sequential

from [Link] import Conv2D, MaxPooling2D, Flatten, Dense, Dropout,


BatchNormalization

from [Link] import drive

# Mount Google Drive

[Link]('/content/drive')

# Directories
train_dir = '/content/drive/MyDrive/New folder (2)/train'

val_dir = '/content/drive/MyDrive/New folder (2)/test'

# Data augmentation and preparation

train_datagen = ImageDataGenerator(rescale=1./255, rotation_range=20, zoom_range=0.15,


horizontal_flip=True)

val_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150, 150),


batch_size=32, class_mode='categorical')

val_generator = val_datagen.flow_from_directory(val_dir, target_size=(150, 150), batch_size=32,


class_mode='categorical')

# Deep Convolutional Neural Network (DCNN) Model

model = Sequential([

Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),

BatchNormalization(),

MaxPooling2D(2, 2),

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

BatchNormalization(),

MaxPooling2D(2, 2),

Conv2D(128, (3, 3), activation='relu'),

BatchNormalization(),

MaxPooling2D(2, 2),

Conv2D(256, (3, 3), activation='relu'),

BatchNormalization(),

MaxPooling2D(2, 2),

Conv2D(512, (3, 3), activation='relu'),

BatchNormalization(),

MaxPooling2D(2, 2),

Flatten(),

Dense(512, activation='relu'),

Dropout(0.5),

Dense(len(train_generator.class_indices), activation='softmax')

])
# Compile the model

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

# Model summary (optional, to see the structure)

[Link]()

# Train the model

history = [Link](train_generator, epochs=10, validation_data=val_generator)

[Link]('Learning Curve for Random Forest Model')

[Link]('Training Set Size')

[Link]('Accuracy')

[Link](loc='best')

[Link](True)

[Link]()

OUTPUT:
[Link] LEARNING MODEL:
RANDOM FOREST:
PROGRAM:
import numpy as np

import os

from sklearn.model_selection import train_test_split

from [Link] import RandomForestClassifier

from [Link] import classification_report, accuracy_score

from PIL import Image

from [Link] import drive

# Mount Google Drive

[Link]('/content/drive')

# Function to load images from a directory and preprocess them

def load_images_from_folder(folder_path, image_size=(64, 64)):

images = []

labels = []

for label in [Link](folder_path):

label_folder = [Link](folder_path, label)


if [Link](label_folder):

for filename in [Link](label_folder):

img_path = [Link](label_folder, filename)

img = [Link](img_path).convert('L') # Convert to grayscale

img = [Link](image_size) # Resize image

img_array = [Link](img).flatten() # Flatten the image

[Link](img_array)

[Link](label)

return [Link](images), [Link](labels)

# Load image dataset

folder_path = '/content/drive/MyDrive/DATASET' # Change to your dataset path

X, y = load_images_from_folder(folder_path)

# Split the dataset into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the Random Forest model

rf_model = RandomForestClassifier(n_estimators=100, random_state=42)

rf_model.fit(X_train, y_train)

# Make predictions on the test set

y_pred = rf_model.predict(X_test)

# Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")

print("Classification Report:")

print(classification_report(y_test, y_pred))

[Link]('Learning Curve for Random Forest Model')

[Link]('Training Set Size')

[Link]('Accuracy')

[Link](loc='best')

[Link](True)

[Link]()
OUTPUT:

SVM:
PROGRAM:
import numpy as np

import os

from sklearn.model_selection import train_test_split, learning_curve

from [Link] import SVC

from [Link] import classification_report, accuracy_score

from PIL import Image

from [Link] import drive


import [Link] as plt

# Mount Google Drive

[Link]('/content/drive')

# Function to load images from a directory and preprocess them

def load_images_from_folder(folder_path, image_size=(64, 64)):

images = []

labels = []

for label in [Link](folder_path):

label_folder = [Link](folder_path, label)

if [Link](label_folder):

for filename in [Link](label_folder):

img_path = [Link](label_folder, filename)

img = [Link](img_path).convert('L') # Convert to grayscale

img = [Link](image_size) # Resize image

img_array = [Link](img).flatten() # Flatten the image

[Link](img_array)

[Link](label)

return [Link](images), [Link](labels)

# Load image dataset

folder_path = '/content/drive/MyDrive/DATASET' # Change to your dataset path

X, y = load_images_from_folder(folder_path)

# Split the dataset into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the Random Forest model

rf_model = RandomForestClassifier(n_estimators=100, random_state=42)

rf_model.fit(X_train, y_train)

# Make predictions on the test set

y_pred = rf_model.predict(X_test)

# Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")

print(classification_report(y_test, y_pred))

# Plotting the Learning Curve

train_sizes, train_scores, val_scores = learning_curve(rf_model, X_train, y_train, cv=5,

train_sizes=[Link](0.1, 1.0, 10),

scoring='accuracy', n_jobs=-1)

# Compute mean and standard deviation for training and validation scores

train_scores_mean = [Link](train_scores, axis=1)

train_scores_std = [Link](train_scores, axis=1)

val_scores_mean = [Link](val_scores, axis=1)

val_scores_std = [Link](val_scores, axis=1)

# Plotting the learning curve

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

[Link](train_sizes, train_scores_mean, 'o-', label='Training Accuracy')

[Link](train_sizes, val_scores_mean, 's-', label='Validation Accuracy')

# Fill between the curves for standard deviation

plt.fill_between(train_sizes, train_scores_mean - train_scores_std,

train_scores_mean + train_scores_std, alpha=0.1, color="r")

plt.fill_between(train_sizes, val_scores_mean - val_scores_std,

val_scores_mean + val_scores_std, alpha=0.1, color="g")

[Link]('Learning SVM')

[Link]('Training Set Size')

[Link]('Accuracy')

[Link](loc='best')

[Link](True)

[Link]()
OUTPUT:

You might also like