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

EXP 2

Cnn
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)
4 views

EXP 2

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

EXP 2:

AIM :Design Artificial Neural Networks for Identifying and Classifying an


actor using Kaggle
Dataset.

import tensorflow as tf

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Flatten

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import os

from google.colab import drive

drive.mount('/content/drive')

# Define the path to your dataset

train_dir = '/content/drive/MyDrive/actor - 12/Train'

validation_dir = '/content/drive/MyDrive/actor - 12/Test'

# Preprocess the images

train_datagen = ImageDataGenerator(rescale=1./255)

validation_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(

train_dir,

target_size=(150, 150),

batch_size=20,

class_mode='binary'

)
validation_generator = validation_datagen.flow_from_directory(

validation_dir,

target_size=(150, 150),

batch_size=20,

class_mode='binary'

# Build a simple ANN model

model = Sequential([

Flatten(input_shape=(150, 150, 3)),

Dense(128, activation='relu'),

Dense(64, activation='relu'),

Dense(1, activation='sigmoid')

])

# Compile the model

model.compile(optimizer='adam',

loss='binary_crossentropy',

metrics=['accuracy'])

# Train the model

history = model.fit(

train_generator,

steps_per_epoch=100, # Number of batches per epoch

epochs=10,

validation_data=validation_generator,

validation_steps=50
)

# Print final accuracy for training and validation

train_accuracy = history.history['accuracy'][-1]

val_accuracy = history.history['val_accuracy'][-1]

print(f"Final Training Accuracy: {train_accuracy * 100:.2f}%")

print(f"Final Validation Accuracy: {val_accuracy * 100:.2f}%")

import numpy as np

from tensorflow.keras.preprocessing import image

from tensorflow.keras.models import load_model

# Load the trained model

# Load and preprocess the image

img_path = '/content/ff.jpg' # Replace with the path to your image

img = image.load_img(img_path, target_size=(150, 150)) # Ensure the


image size matches the input size of the model

img_array = image.img_to_array(img)

img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to


match the model's input shape

img_array /= 255.0 # Rescale the image (same as during training)

# Predict the class of the image

prediction = model.predict(img_array)

predicted_class = 1 if prediction[0][0] > 0.5 else 0


confidence_score = prediction[0][0]

# Define the actual class (you need to know this)

actual_class = 1 # Replace with the actual class of the image

# Display the actual and predicted class

print(f"Actual Class: {actual_class}")

print(f"Predicted Class: {predicted_class}")

print(f"Confidence Score: {confidence_score:.4f}")

# Optional: Map the class indices to human-readable class names if


available

class_names = {0: 'Class 0', 1: 'Class 1'}

print(f"Actual Class Name: {class_names[actual_class]}")

print(f"Predicted Class Name: {class_names[predicted_class]}")

# Display the image

plt.imshow(img)

plt.title(f"Actual: {class_names[actual_class]}, Predicted:


{class_names[predicted_class]}")

plt.axis('off') # Hide the axis

plt.show()

You might also like