Batch - 7 FINAL Review (DEEP LEARNING)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

AUTOMATIC VACANT

PARKING PLACES
DETECTION USING DEEP
LEARNING

Project Members:

T.KUMARA SWAMY, SK.HUSSAIN,

S.JOHN JOSHUA

ID NO:

2100069003,2100069008,2100069030

Under the Guidance : Dr.P.Vidyullatha

Koneru Lakshmaiah Education Foundation


(Deemed to be University)

1
2
Dataset Details
Taken 12,416 image samples along
with corresponding captions

Train images = 8692


Test images = 1243
Validation images = 2484

Dataset reference link:


https://www.kaggle.com/datasets/a
mmarnassanalhajali/pklot-dataset
Deep Learning Models Used for Project

6. Convolutional
1. Data Loading And Neural Network
7. VGG Model
Preprocessing (CNN)
Implementation

2. Sequential Model
5. Random Mini-
For Binary 8. RNN Model
Batch Evaluation
Classification

3. Sequential Model
By Adding Various 4. Sequential Model
9. LSTM
Optimization For Multi-Class
Techniques

4
DEVELOPE PROJECT TITLE USING WEB
APPLICATION

5
6
Data Loading And Preprocessing
 For image captioning, data loading involves fetching image-caption pairs from a dataset
like MSCOCO. Preprocessing includes resizing images to a fixed size, normalizing pixel
values, and tokenizing captions into words for input into the neural network model.
These steps ensure compatibility between the visual and textual data for training the deep
learning model.

• 1. Resizing
• 2. Grayscale Conversion
• 3. Cropping
• 4. Rotation
• 5. Flipping (Horizontal/Vertical)
• 6. Brightness Adjustment
• 7. Contrast Adjustment
• 8. Gaussian Blurring
• 9. Histogram Equalization
• 10. Edge Detection
• 11. Padding
7
Data Loading And Preprocessing
Data Loading And Preprocessing
Data Loading And Preprocessing
Binary Classification

 The project employs a binary classification model to predict the presence or absence of a
target variable based on input features. Through preprocessing steps like scaling,
encoding, and handling missing values, the data is prepared for training. The model
undergoes training on the training dataset and is evaluated on a separate testing set to
assess its performance and generalization ability.

• 1. Binary Classification Model


• 2. Preprocessing
• 3. Feature Engineeringv
• 4. Model Training
• 5. Evaluation
Binary Classification
Binary Classification

from tensorflow.keras.models import Sequential


from tensorflow.keras.layers import Dense, Flatten

model = Sequential([
Flatten(input_shape=(img_height, img_width, 3)),
Dense(128, activation='relu'),
Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])


history = model.fit(train_generator, epochs=10, validation_data=val_generator)
Binary Classification
Optimization Techniques
Optimization techniques are critical for enhancing the efficiency and convergence of machine learning
models. Here are descriptions of three common techniques:

Adam : Adaptive learning rates and momentum for faster convergence.


RMSprop: Adaptive adjustment of learning rates using root mean square gradients.
SGD: Updates model parameters using gradients for iterative optimization.

1. Data Pre-processing
2. Model Selection
3. Adam Optimization
4. RMSprop Optimization
5. Stochastic Gradient Descent (SGD) Optimization
6. Model Training and Evaluation
7. Comparison and Analysis
Sequential Model for Different Optimization Techniques
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.optimizers import SGD, RMSprop, Adam
import matplotlib.pyplot as plt

# Define paths to your dataset


train_dir = 'C:\\Users\\tadik\\PycharmProjects\\DEEP LEARNING\\parking_slot_images_dataset'
test_dir = 'C:\\Users\\tadik\\PycharmProjects\\DEEP LEARNING\\parking_slot_images_dataset'

# Data preprocessing and augmentation


train_datagen = ImageDataGenerator(
rescale=1.0/255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
Optimization techniques
# Build a convolutional neural network model
def build_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)),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(512, activation='relu'),
Dense(1, activation='sigmoid')
])
return model

17
# Function to train the model with a given optimizer
def train_model(optimizer, epochs=20):
model = build_model()
model.compile(optimizer=optimizer,
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(train_generator, epochs=epochs, validation_data=test_generator)
return history

# Train models with different optimizers


sgd_history = train_model(SGD(lr=0.01, momentum=0.9))
rmsprop_history = train_model(RMSprop(lr=0.001))
adam_history = train_model(Adam(lr=0.001))

# Plotting the training history


plt.plot(sgd_history.history['val_accuracy'], label='SGD')
plt.plot(rmsprop_history.history['val_accuracy'], label='RMSprop')
plt.plot(adam_history.history['val_accuracy'], label='Adam')
plt.xlabel('Epochs')
plt.ylabel('Validation Accuracy')
plt.title('Optimizer Comparison')
plt.legend()
plt.show()
18
Sequential Model for Different Optimization Techniques
Sequential Model For Multi-Class
 The Sequential Model for multi-class classification is a neural network architecture in Keras
that arranges layers sequentially, facilitating the creation of classifiers for scenarios with
multiple classes. It enables streamlined construction of deep learning models, where input
samples are categorized into various predefined classes based on learned features. This
model is widely utilized for tasks like image recognition, natural language processing, and
sentiment analysis.

1. Importing Libraries
2. Randomly Selecting Classes
3. Setting Parameters
4. Data Preprocessing and Augmentation
5. Loading Training Data
6. Defining the Model
7. Compiling the Model
8. Training the Model
9. Plotting Training Accuracy
Sequential Model For Multi-Class
from tensorflow.keras.layers import Dropout

model = Sequential([
Flatten(input_shape=(img_height, img_width, 3)),
Dense(256, activation='relu'),
Dropout(0.5),
Sequential Model Dense(128, activation='relu'),
Dropout(0.3),
For Multi-Class Dense(num_classes, activation='softmax')
])

model.compile(optimizer='adam',
loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(train_generator, epochs=20,
validation_data=val_generator)
Sequential Model For Multi-Class
Random Mini-Batch

 Random Mini-Batch training, a pivotal strategy in deep learning, involves dividing data into
small, random subsets called mini-batches. These subsets are utilized iteratively to update
model parameters, offering efficiency and enhanced convergence. This approach optimizes
memory usage, accelerates training through parallel processing, improves generalization, and
introduces stochasticity for regularization.

• Importing Libraries
• Defining the Neural Network Class
• Data Transformation and Loading
• Setting Parameters
• Model Initialization
• Defining Loss Function and Optimizer
• Training Loop
• Monitoring Performance
• Plotting Training Accuracy
Random Mini-Batch
from tensorflow.keras.preprocessing.image import img_to_array,
load_img
import numpy as np

# Load and preprocess a random sample of images


random_images = [img_to_array(load_img(os.path.join(dataset_path,
'val', file), target_size=(img_height, img_width))) / 255.0
Random for file in
np.random.choice(os.listdir(os.path.join(dataset_path, 'val')), size=32)]

Mini-Batch random_images = np.array(random_images)


random_labels = np.random.randint(num_classes, size=32)

# Evaluate the model on the random mini-batch


loss, acc = model.evaluate(random_images, random_labels, verbose=0)
print(f'Random mini-batch evaluation: Loss = {loss:.4f}, Accuracy =
{acc:.4f}')
Random Mini-Batch
Convolutional Neural Network Model(CNN)
 The CNN model in the image captioning project extracts visual features from input images,
enabling subsequent layers to generate descriptive captions. By analyzing spatial hierarchies and
semantic information, the CNN effectively captures image content for accurate captioning. This
hierarchical feature extraction enhances the model's ability to produce contextually relevant
descriptions.

• Importing Libraries
• Data Augmentation
• Loading Training Data
• Defining the Model
• Compiling the Model
• Training the Model
• Displaying Training Accuracy
• Plotting Training Accuracy
Convolutional Neural Network Model(CNN)
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import LearningRateScheduler, EarlyStopping
import matplotlib.pyplot as plt

# Define paths to your dataset


train_dir = 'C:\\Users\\tadik\\PycharmProjects\\DEEP LEARNING\\parking_slot_images_dataset'
test_dir = 'C:\\Users\\tadik\\PycharmProjects\\DEEP LEARNING\\parking_slot_images_dataset'

# Data preprocessing and augmentation


train_datagen = ImageDataGenerator(
rescale=1.0/255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)

test_datagen = ImageDataGenerator(rescale=1.0/255)
Convolutional Neural Network Model(CNN)

# Build a convolutional neural network model


def build_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)),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(512, activation='relu'),
Dropout(0.5),
Dense(1, activation='sigmoid')
])
return model

30
Convolutional Neural Network Model(CNN)
# Function to train the model with a given optimizer
def train_model(optimizer, epochs=20):
model = build_model()
model.compile(optimizer=optimizer,
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(train_generator, epochs=epochs, validation_data=test_generator, callbacks=[lr_scheduler, early_stopping])
return history

# Train model with Adam optimizer


adam_history = train_model(Adam())

# Plotting the training history


plt.plot(adam_history.history['accuracy'], label='Training Accuracy')
plt.plot(adam_history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Model Accuracy')
plt.legend()
plt.show()

31
Convolutional Neural Network Model(CNN)
Visual Geometry Group(VGG)

 The VGG model, originally designed for image classification, has been adapted for image captioning tasks in
deep learning projects. Leveraging its deep convolutional architecture with multiple layers, VGG extracts
intricate features from images. These features are then fed into recurrent neural networks to generate
descriptive captions, enhancing the model's ability to understand and articulate visual content.

1. Custom Image Data Generator


2. VGG Model Architecture
3. Model Compilation
4. Data Augmentation
5. Model Training
6. Model Evaluation
7. Visualization

33
Visual Geometry Group
import tensorflow as tf
from tensorflow.keras.preprocessing.image import
ImageDataGenerator
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout
import matplotlib.pyplot as plt

# Set the path to your dataset


train_data_dir = 'path_to_your_training_dataset'
validation_data_dir = 'path_to_your_validation_dataset'

# Parameters
img_width, img_height = 224, 224
batch_size = 32
epochs = 10

34
Visual Geometry Group
# Data Preprocessing
train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2,
zoom_range=0.2, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size, class_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size, class_mode='binary')

# Load VGG model


vgg_model = VGG16(weights='imagenet', include_top=False,
input_shape=(img_width, img_height, 3))

# Freeze VGG layers


for layer in vgg_model.layers:
layer.trainable = False 35
Visual Geometry Group
# Customizing the VGG model
model = Sequential()
model.add(vgg_model)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

# Compile the model


model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Training
history = model.fit(train_generator, steps_per_epoch=train_generator.samples // batch_size,
validation_data=validation_generator,
validation_steps=validation_generator.samples // batch_size,
epochs=epochs)

36
LSTM

 In image captioning using deep learning, LSTM (Long Short-Term Memory) models are utilized for their ability
to capture sequential information and long-range dependencies. These models excel in understanding
context and generating coherent captions by learning from both image features and preceding words.
Through recurrent connections and gating mechanisms, LSTM networks effectively encode and decode
image-context relationships, providing meaningful descriptions for images.

1. Importing Libraries
2. Loading Images and Captions
3. Preprocessing Images and Captions
4. Tokenizing and Padding Captions
5. Loading Labels (Assuming binary classification)
6. Building the LSTM Model
7. Compiling the Model
8. Training the Model
9. Evaluating the Model
10. Extracting Patterns and Insights

37
LSTM
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt

# Parameters
img_width, img_height = 224, 224
batch_size = 32
epochs = 50
n_lstm_units = 128
38
LSTM
# Load pre-trained VGG model
vgg_model = VGG16(weights='imagenet', include_top=False,
input_shape=(img_width, img_height, 3))

# Freeze VGG layers


for layer in vgg_model.layers:
layer.trainable = False

# Build LSTM model


model = Sequential()
model.add(vgg_model)
model.add(tf.keras.layers.TimeDistributed(tf.keras.layers.Flatten
()))
model.add(LSTM(n_lstm_units))
model.add(Dense(1, activation='sigmoid'))

# Compile the model


model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
39
MODEL Epochs Optimizer Activation function Acuuracy

Sequential Model For 10 Adam ReLU(Hidden Layer) 66


Binary Classification Sigmoid(OutPut layer)

Sequential Model By 20 SGD ReLU(Hidden Layer) 69


Adding Various RMS Sigmoid(OutPut layer)
Optimization ADAM
Techniques

Sequential Model For 20 Adam ReLU(Hidden Layer) 68


Multi-Class Softmax(OutPut layer)

Convolutional Neural 10 Adam ReLU(Hidden Layer) 70


Network (CNN) Sigmoid(OutPut layer)
Implementation

VGG Model 20 Adam ReLU(Hidden Layer) 69


Sigmoid(OutPut layer)

LSTM Model 10 Adam tanh 69


40
Conclusion
In this project, we explored various deep learning
models for classifying parking slot images. We started
with simple sequential models for binary and multi-
class classification, and then incorporated
optimization techniques like early stopping and
learning rate reduction. We also implemented
convolutional neural networks (CNNs), transfer
learning with pre-trained models like VGG, and
recurrent neural networks (RNNs) like SimpleRNN
and LSTM. Throughout the process, we emphasized
data preprocessing, model evaluation using
techniques like random mini-batch evaluation, and
visualizing model performance through accuracy and
loss plots. By comparing the results of these diverse
models, we gained insights into their strengths and
weaknesses for this specific computer vision task.
41
42

You might also like