0% found this document useful (0 votes)
9 views16 pages

NN - DL Project

neural network and deep learning project

Uploaded by

vskaviya623
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)
9 views16 pages

NN - DL Project

neural network and deep learning project

Uploaded by

vskaviya623
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/ 16

# STEP 1: Mount Google Drive

from google.colab import drive


drive.mount('/content/drive')

---------------------------------------------------------------------------------------------------------------------------

# STEP 2: Unzip the dataset


import zipfile
import os

# ✅ Double-check the exact path in your Google Drive


zip_path = '/content/drive/MyDrive/Kaggle/FEB2014.zip'
extract_path = '/content/mood_dataset'

# Check if the zip file exists


if os.path.exists(zip_path):
print("✅ Zip file found. Extracting now...")
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_path)
print("✅ Extraction complete!")
else:
print("❌ Zip file not found at the path:", zip_path)

---------------------------------------------------------------------------------------------------------------------------

import shutil

# Check and rename if necessary


try:
shutil.move('/content/mood_dataset/Train Data', '/content/mood_dataset/train')
shutil.move('/content/mood_dataset/Test Data', '/content/mood_dataset/test')
print("✅ Folder names updated!")
except:
print("⚠️Folders might already be renamed or not found.")

---------------------------------------------------------------------------------------------------------------------------

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
import matplotlib.pyplot as plt
---------------------------------------------------------------------------------------------------------------------------

img_width, img_height = 48, 48


batch_size = 32

train_dir = '/content/mood_dataset/train'
test_dir = '/content/mood_dataset/test'

train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
zoom_range=0.2,
horizontal_flip=True
)

test_datagen = ImageDataGenerator(rescale=1./255)

train_data = train_datagen.flow_from_directory(
train_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical'
)

test_data = test_datagen.flow_from_directory(
test_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical'
)
---------------------------------------------------------------------------------------------------------------------------

model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(img_width, img_height, 3)),
MaxPooling2D(2, 2),

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


MaxPooling2D(2, 2),

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


MaxPooling2D(2, 2),

Flatten(),
Dense(128, activation='relu'),
Dropout(0.3),
Dense(train_data.num_classes, activation='softmax')
])

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


model.summary()

---------------------------------------------------------------------------------------------------------------------------
history = model.fit(
train_data,
epochs=10,
validation_data=test_data
)

---------------------------------------------------------------------------------------------------------------------------

model.save('/content/mood_model.h5')

---------------------------------------------------------------------------------------------------------------------------

plt.plot(history.history['accuracy'], label='Training Accuracy')


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

mood_to_music = {
"happy": ["https://youtu.be/ZbZSe6N_BXs", "https://youtu.be/60ItHLz5WEA"],
"sad": ["https://youtu.be/4N3N1MlvVc4"],
"angry": ["https://youtu.be/kXYiU_JCYtU"],
"neutral": ["https://youtu.be/fLexgOxsZu0"],
"surprise": ["https://youtu.be/34Na4j8AVgA"]
}

# Later use:
# predicted_mood = "happy"
# for link in mood_to_music[predicted_mood]:
# print(link)
---------------------------------------------------------------------------------------------------------------------------

pip install tensorflow opencv-python

---------------------------------------------------------------------------------------------------------------------------

from google.colab import files


files.download('/content/mood_model.h5')

---------------------------------------------------------------------------------------------------------------------------

import cv2
import numpy as np
from tensorflow.keras.models import load_model
from IPython.display import display, Javascript, Image
from google.colab.output import eval_js
from base64 import b64decode

# Load the model and compile it


model = load_model("mood_model.h5")
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # Re-
compile the model

emotion_labels = ['angry', 'happy', 'sad', 'surprise', 'neutral']

# Function to take a photo and return the base64 encoded image


def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);

const video = document.createElement('video');


video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});

document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();

// Resize the output to fit the video element.


google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

// Wait for Capture to be clicked.


await new Promise((resolve) => capture.onclick = resolve);

const canvas = document.createElement('canvas');


canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
# Capture the image and handle potential errors
filename = None # Initialize filename
try:
filename = take_photo()
print('Saved to {}'.format(filename))

# Show the image which was just taken.


display(Image(filename))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))

# Proceed only if filename is defined


if filename:
# Process the captured image
img = cv2.imread(filename)
face = cv2.resize(img, (48, 48))
face = face / 255.0
face = np.expand_dims(face, axis=0)

prediction = model.predict(face)
mood_index = np.argmax(prediction)
mood = emotion_labels[mood_index]

# Display the result with the image


print(f"Predicted Mood: {mood}")
else:
print("Error: Could not capture image. Please check webcam permissions.")

---------------------------------------------------------------------------------------------------------------------------
!pip install face_recognition
!apt-get install cmake # Install or upgrade CMake
!pip install dlib --upgrade --force-reinstall # Reinstall dlib (Optional)

import cv2
import numpy as np
from tensorflow.keras.models import load_model
import face_recognition

# Load the model and compile it


model = load_model("mood_model.h5")
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Get the number of classes the model predicts


# Access the output shape using the 'output' property
num_classes = model.layers[-1].output.shape[1]
print(f"Number of classes predicted by the model: {num_classes}")

# Update emotion_labels to match the model's output shape dynamically


emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise'][:num_classes]

# Load a sample image for testing


# Replace 'path/to/your/image.jpg' with the actual path
image_path = 'path/to/your/image.jpg'
frame = cv2.imread(image_path)

if frame is None:
print("Error: Could not load sample image.")
else:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(frame)
# Loop through each face found in the frame
for (top, right, bottom, left) in face_locations:
# Extract the face region
face_image = frame[top:bottom, left:right]

# Preprocess the face for the model


face_image = cv2.resize(face_image, (48, 48))
face_image = face_image / 255.0
face_image = np.expand_dims(face_image, axis=0)

# Predict the mood


prediction = model.predict(face_image)
mood_index = np.argmax(prediction)
mood = emotion_labels[mood_index]

# Draw a box around the face and display the predicted mood
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(frame, mood, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255,
255, 255), 1)

# Display the resulting image


cv2.imshow('Sample Image', frame)
cv2.waitKey(0) # Wait for a key press
cv2.destroyAllWindows()
# 🎮 Flappy Bird DQN Agent on Google Colab (Step-by-Step)

# ✅ Step 1: Install Dependencies


!pip install pygame
!pip install git+https://github.com/ntasfi/PyGame-Learning-Environment.git
!pip install gym
!pip install numpy matplotlib

# ✅ Step 2: Import Libraries


import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from collections import deque
from ple.games.flappybird import FlappyBird
from ple import PLE
import matplotlib.pyplot as plt

# ✅ Step 3: Create Flappy Bird Environment


game = FlappyBird()
env = PLE(game, fps=30, display_screen=False)
env.init()
actions = env.getActionSet()

# ✅ Step 4: Define DQN Network


class DQN(nn.Module):
def __init__(self, input_dim, output_dim):
super(DQN, self).__init__()
self.fc1 = nn.Linear(input_dim, 128)
self.fc2 = nn.Linear(128, 64)
self.out = nn.Linear(64, output_dim)

def forward(self, x):


x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return self.out(x)

# ✅ Step 5: Define Replay Buffer


class ReplayBuffer:
def __init__(self, capacity):
self.buffer = deque(maxlen=capacity)

def push(self, state, action, reward, next_state, done):


self.buffer.append((state, action, reward, next_state, done))

def sample(self, batch_size):


batch = random.sample(self.buffer, batch_size)
state, action, reward, next_state, done = map(np.array, zip(*batch))
return state, action, reward, next_state, done

def __len__(self):
return len(self.buffer)

# ✅ Step 6: Utilities for State Conversion


def get_state():
state_dict = env.getGameState()
return np.array(list(state_dict.values()), dtype=np.float32)

# ✅ Step 7: Train the Agent

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


input_dim = len(env.getGameState())
output_dim = len(actions)

policy_net = DQN(input_dim, output_dim).to(device)


target_net = DQN(input_dim, output_dim).to(device)
target_net.load_state_dict(policy_net.state_dict())
target_net.eval()

optimizer = optim.Adam(policy_net.parameters(), lr=1e-4)


replay_buffer = ReplayBuffer(10000)

BATCH_SIZE = 64
GAMMA = 0.99
EPSILON_START = 1.0
EPSILON_END = 0.01
EPSILON_DECAY = 5000
TARGET_UPDATE = 1000

epsilon = EPSILON_START
step_count = 0

num_episodes = 300
episode_rewards = []

for episode in range(num_episodes):


env.reset_game()
state = get_state()
total_reward = 0

while not env.game_over():


step_count += 1
epsilon = EPSILON_END + (EPSILON_START - EPSILON_END) * \
np.exp(-1. * step_count / EPSILON_DECAY)

if random.random() < epsilon:


action = random.randrange(output_dim)
else:
with torch.no_grad():
state_tensor = torch.FloatTensor(state).unsqueeze(0).to(device)
q_values = policy_net(state_tensor)
action = q_values.max(1)[1].item()

reward = env.act(actions[action])
next_state = get_state()
done = env.game_over()

replay_buffer.push(state, action, reward, next_state, done)


state = next_state
total_reward += reward

if len(replay_buffer) >= BATCH_SIZE:


states, actions_b, rewards, next_states, dones = replay_buffer.sample(BATCH_SIZE)

states = torch.FloatTensor(states).to(device)
actions_b = torch.LongTensor(actions_b).unsqueeze(1).to(device)
rewards = torch.FloatTensor(rewards).unsqueeze(1).to(device)
next_states = torch.FloatTensor(next_states).to(device)
dones = torch.FloatTensor(dones).unsqueeze(1).to(device)

q_values = policy_net(states).gather(1, actions_b)


next_q_values = target_net(next_states).max(1)[0].unsqueeze(1)
expected_q = rewards + (GAMMA * next_q_values * (1 - dones))

loss = F.mse_loss(q_values, expected_q)


optimizer.zero_grad()
loss.backward()
optimizer.step()

if step_count % TARGET_UPDATE == 0:
target_net.load_state_dict(policy_net.state_dict())

episode_rewards.append(total_reward)
print(f"Episode {episode+1}, Reward: {total_reward:.2f}, Epsilon: {epsilon:.4f}")

# ✅ Step 8: Plot Training Progress


plt.plot(episode_rewards)
plt.xlabel("Episode")
plt.ylabel("Reward")
plt.title("Training Progress")
plt.show()

# ✅ Step 9: Save Trained Model


torch.save(policy_net.state_dict(), "flappy_dqn.pth")

Great question! ✅ After you've trained and saved the model (flappy_dqn.pth), you can load and test it
by making the agent play the game using the trained weights.
Since Google Colab can't show real-time PyGame screens, we'll make the agent play silently and
print results. You can also log scores or use GIF recording if needed.

✅ Step-by-Step: Test the Saved DQN Model


Add this at the bottom of your notebook or in a new cell:
# ✅ Step 10: Load and Evaluate Saved Model
# Load trained weights
policy_net.load_state_dict(torch.load("flappy_dqn.pth", map_location=device))
policy_net.eval()

# Let agent play a few episodes


test_episodes = 5
test_rewards = []

for ep in range(test_episodes):
env.reset_game()
state = get_state()
total_reward = 0

while not env.game_over():


state_tensor = torch.FloatTensor(state).unsqueeze(0).to(device)
with torch.no_grad():
q_values = policy_net(state_tensor)
action = q_values.max(1)[1].item()

reward = env.act(actions[action])
state = get_state()
total_reward += reward

test_rewards.append(total_reward)
print(f"Test Episode {ep+1} Reward: {total_reward:.2f}")

🧪 Output Example
You’ll see something like:
Test Episode 1 Reward: 12.0
Test Episode 2 Reward: 15.0
...
This confirms the agent is playing with its learned policy!

🔄 Optional: Show it Visually (Run Locally)


If you're curious to see the bird flap:
 Set display_screen=True inside PLE() (only works locally, not in Colab):
env = PLE(game, fps=30, display_screen=True) # 👈 use this locally
Then run the test loop again — you’ll see the game on your screen.

Want me to help you:


 🔁 Record & download gameplay as a GIF in Colab?
 🧠 Add epsilon control during test?
 🔄 Run continuous evaluation?
Let me know — ready to tweak it with you! 😄

You might also like