0% found this document useful (0 votes)
10 views

Convolutional Neural Network

convolutional_neural_network in Deep Learning

Uploaded by

mohitdubey42551
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)
10 views

Convolutional Neural Network

convolutional_neural_network in Deep Learning

Uploaded by

mohitdubey42551
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
You are on page 1/ 4

Convolutional Neural Network

Importing the libraries


import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator

tf.__version__

'2.11.0'

Part 1 - Data Preprocessing


Preprocessing the Training set
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set =
train_datagen.flow_from_directory('dataset/training_set',
target_size = (64,
64),
batch_size = 128,
class_mode =
'binary')

Found 8000 images belonging to 2 classes.

Preprocessing the Test set


test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size = (64, 64),
batch_size = 128,
class_mode = 'binary')

Found 2000 images belonging to 2 classes.

Part 2 - Building the CNN


Initialising the CNN
cnn = tf.keras.models.Sequential()

cnn

<keras.engine.sequential.Sequential at 0x22523123700>
Step 1 - Convolution
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3,
activation='relu', input_shape=[64, 64, 3]))

cnn

<keras.engine.sequential.Sequential at 0x22523123700>

Step 2 - Pooling
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

Adding a second convolutional layer


cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3,
activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

cnn

<keras.engine.sequential.Sequential at 0x22523123700>

Step 3 - Flattening
cnn.add(tf.keras.layers.Flatten())

cnn

<keras.engine.sequential.Sequential at 0x22523123700>

Step 4 - Full Connection


cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))

Step 5 - Output Layer


cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

Part 3 - Training the CNN


Compiling the CNN
cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics
= ['accuracy'])

Training the CNN on the Training set and evaluating it on the Test set
cnn.fit(x = training_set, validation_data = test_set, epochs = 3)
Epoch 1/3
63/63 [==============================] - 85s 1s/step - loss: 0.6871 -
accuracy: 0.5540 - val_loss: 0.6532 - val_accuracy: 0.6060
Epoch 2/3
63/63 [==============================] - 69s 1s/step - loss: 0.6314 -
accuracy: 0.6403 - val_loss: 0.5986 - val_accuracy: 0.6775
Epoch 3/3
63/63 [==============================] - 68s 1s/step - loss: 0.5871 -
accuracy: 0.6967 - val_loss: 0.5608 - val_accuracy: 0.7150

<keras.callbacks.History at 0x22523122410>

Part 4 - Making a single prediction


import numpy as np
from keras.preprocessing import image
import keras.utils as image
test_image = image.load_img('dataset/single_prediction/4.jpg',
target_size = (64, 64))

test_image

test_image = image.img_to_array(test_image)

test_image = np.expand_dims(test_image, axis = 0)

result = cnn.predict(test_image)
result

1/1 [==============================] - 0s 49ms/step

array([[1.]], dtype=float32)

training_set.class_indices

{'cats': 0, 'dogs': 1}

if result[0][0] == 1:
prediction = 'dog'
else:
prediction = 'cat'

print(prediction)

dog
import cv2

# Initialize the camera (0 is the default camera)


camera = cv2.VideoCapture(0)

# Check if the camera opened successfully


if not camera.isOpened():
print("Error: Could not open camera.")
else:
# Read a single frame from the camera
ret, frame = camera.read()

# Check if the frame was successfully captured


if ret:
# Display the captured frame
cv2.imshow('Captured Image', frame)

# Save the captured image to a file


cv2.imwrite('captured_image.jpg', frame)

# Wait for any key to close the window


cv2.waitKey(0)
else:
print("Error: Could not capture image.")

# Release the camera and close any open windows


camera.release()
cv2.destroyAllWindows()

You might also like