### Installation Guide Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/README.md Provides step-by-step instructions for setting up the ResEmoteNet environment, including Conda environment creation, Python installation, repository cloning, and dependency installation. ```bash conda create --n "fer" conda activate fer conda install python=3.8 git clone https://github.com/ArnabKumarRoy02/ResEmoteNet.git pip install -r requirement.txt ``` -------------------------------- ### Device Setup and Model Initialization Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/plot_gcam.ipynb Sets up the computation device (MPS or CPU) and initializes the ResEmoteNet model. It also loads the pre-trained weights from 'best_model.pth'. ```Python import cv2 import torch import numpy as np import torch.nn.functional as F from torchvision import transforms import matplotlib.pyplot as plt from PIL import Image device = torch.device("mps" if torch.backends.mps.is_available() else "cpu") print(f"Using device: {device}") from approach.ResEmoteNet import ResEmoteNet model = ResEmoteNet() model.load_state_dict(torch.load("best_model.pth", map_location=device)) model.eval() ``` -------------------------------- ### Execute Data Preprocessing Scripts Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/data_preprocessing/README.md Command-line instructions to run the Python scripts for data preprocessing. This includes renaming files, moving files, and generating CSV labels. ```bash # Run the rename script python rename.py # Run the move script python move.py # Run the CSV generation script python data_csv.py ``` -------------------------------- ### Training Script Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/README.md Demonstrates how to run the main training script for ResEmoteNet. This involves navigating to the training directory and executing the Python script. ```bash cd train_files python ResEmoteNet_train.py ``` -------------------------------- ### Move Files to Common Partitioned Directory Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/data_preprocessing/README.md This script consolidates all images from subdirectories within each partition (e.g., 'angry', 'disgust') into a single directory for that partition. This simplifies data loading. ```python import os import shutil def move_files(dataset_path): for partition in ['train', 'test', 'val']: partition_path = os.path.join(dataset_path, partition) for class_name in os.listdir(partition_path): class_path = os.path.join(partition_path, class_name) for filename in os.listdir(class_path): src_filepath = os.path.join(class_path, filename) dest_filepath = os.path.join(partition_path, filename) shutil.move(src_filepath, dest_filepath) # Remove the now empty class directory os.rmdir(class_path) # Example usage: # move_files('rafdb') ``` -------------------------------- ### Generate CSV Labels for Dataset Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/data_preprocessing/README.md This script generates CSV files (e.g., `train_labels.csv`) for each partition. Each CSV contains two columns: `image_name` and `class`, where the class is represented by an integer. ```python import os import csv def generate_csv(dataset_path): class_mapping = { 'angry': 3, 'disgust': 7, 'fear': 1, 'happy': 0, 'neutral': 6, 'sad': 4, 'surprise': 2 } for partition in ['train', 'test', 'val']: partition_path = os.path.join(dataset_path, partition) csv_filename = f"{partition}_labels.csv" csv_filepath = os.path.join(dataset_path, csv_filename) with open(csv_filepath, 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(['image_name', 'class']) for filename in os.listdir(partition_path): if filename.endswith('.jpg'): parts = filename.split('_') if len(parts) >= 3: class_name = parts[-1].split('.')[0] if class_name in class_mapping: writer.writerow([filename, class_mapping[class_name]]) # Example usage: # generate_csv('rafdb') ``` -------------------------------- ### Project Citation Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/README.md Provides the BibTeX entry for citing the ResEmoteNet project in academic publications. This includes author information, title, journal, and publication details. ```text @ARTICLE{10812829, author={Roy, Arnab Kumar and Kathania, Hemant Kumar and Sharma, Adhitiya and Dey, Abhishek and Ansari, Md. Sarfaraj Alam}, journal={IEEE Signal Processing Letters}, title={ResEmoteNet: Bridging Accuracy and Loss Reduction in Facial Emotion Recognition}, year={2024}, pages={1-5}, keywords={Emotion recognition;Feature extraction;Convolutional neural networks;Accuracy;Training;Computer architecture;Residual neural networks;Facial features;Face recognition;Facial Emotion Recognition;Convolutional Neural Network;Squeeze and Excitation Network;Residual Network}, doi={10.1109/LSP.2024.3521321} } ``` -------------------------------- ### Grad-CAM Calculation Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/plot_gcam.ipynb This snippet implements the Grad-CAM algorithm. It registers a hook to capture forward and backward passes of the final convolutional layer, performs a forward and backward pass with the model, and calculates the class activation map (CAM). ```Python from hook import Hook final_layer = model.conv3 hook = Hook() hook.register_hook(final_layer) img_tensor = process_image(img_path) logits = model(img_tensor) probabilities = F.softmax(logits, dim=1) predicted_class = torch.argmax(probabilities, dim=1) predicted_class_idx = predicted_class.item() print(f'Predicted class: {predicted_class_idx}') one_hot_output = torch.FloatTensor(1, probabilities.shape[1]).zero_() one_hot_output[0][predicted_class_idx] = 1 logits.backward(one_hot_output, retain_graph=True) gradients = hook.backward_out feature_maps = hook.forward_out hook.unregister_hook() weights = torch.mean(gradients, dim=[2, 3], keepdim=True) cam = torch.sum(weights * feature_maps, dim=1, keepdim=True) cam = cam.clamp(min=0).squeeze() cam -= cam.min() cam /= cam.max() cam = cam.cpu().detach().numpy() cam = cv2.resize(cam, (64, 64)) ``` -------------------------------- ### Rename Files in Dataset Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/data_preprocessing/README.md This script renames files within the dataset to a consistent format: `partition_index_class.jpg`. It processes files in the 'train', 'test', and 'val' partitions. ```python import os def rename_files(dataset_path): for partition in ['train', 'test', 'val']: partition_path = os.path.join(dataset_path, partition) for class_name in os.listdir(partition_path): class_path = os.path.join(partition_path, class_name) for i, filename in enumerate(os.listdir(class_path)): old_filepath = os.path.join(class_path, filename) new_filename = f"{partition}_{i+1}_{class_name}.jpg" new_filepath = os.path.join(class_path, new_filename) os.rename(old_filepath, new_filepath) # Example usage: # rename_files('rafdb') ``` -------------------------------- ### Displaying Grad-CAM Heatmap Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/plot_gcam.ipynb Visualizes the calculated Grad-CAM heatmap using matplotlib. It displays the raw heatmap and then applies the heatmap to the original image for a superimposed view. ```Python plt.matshow(cam, cmap='jet') plt.axis('off') plt.show() heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET) heatmap = np.float32(heatmap) / 255 superimposed_img = heatmap*.5 + np.float32(img) / 255 # heatmap * 0.9 superimposed_img = cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB) plt.figure(figsize=(10, 30)) plt.subplot(1, 3, 1) plt.imshow(img) plt.axis('off') plt.legend() plt.subplot(1, 3, 2) cax = plt.matshow(cam, cmap='jet', fignum=0) plt.axis('off') plt.colorbar(cax, ax=plt.gca(), fraction=0.045, pad=0.05) plt.legend() plt.subplot(1, 3, 3) plt.imshow(superimposed_img) plt.axis('off') plt.legend() plt.savefig('gcam_anger.png', dpi=400, bbox_inches='tight', pad_inches=0.1) plt.show() ``` -------------------------------- ### Image Preprocessing Function Source: https://github.com/arnabkumarroy02/resemotenet/blob/main/plot_gcam.ipynb Defines a function to preprocess input images for the ResEmoteNet model. This includes resizing, converting to a tensor, and normalizing the image according to the model's requirements. ```Python img_path = 'data/valid/test_0017_aligned_anger.jpg' img = Image.open(img_path) def process_image(image_path): preprocess = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((64, 64)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) img = cv2.imread(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = preprocess(img).unsqueeze(0) return img ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.