### Install Dependencies Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/README.md Install the required dependencies for running EmotiEffLib examples by executing this command. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install EmotiEffLib with All Dependencies (from source) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from the source code with all possible dependencies. ```sh pip install .[all] ``` -------------------------------- ### Install EmotiEffLib with ONNX Support (from source) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from the source code with basic ONNX support. ```sh pip install . ``` -------------------------------- ### Install EmotiEffLib with All Dependencies (from PyPI) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from PyPI with all possible dependencies. ```sh pip install emotiefflib[all] ``` -------------------------------- ### Install EmotiEffLib with ONNX and Torch Support (from source) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from the source code with ONNX and Torch support. ```sh pip install .[torch] ``` -------------------------------- ### Install EmotiEffLib with ONNX Support (from PyPI) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from PyPI with basic ONNX support. ```sh pip install emotiefflib ``` -------------------------------- ### Install EmotiEffLib with ONNX and Torch Support (from PyPI) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from PyPI with ONNX and Torch support. ```sh pip install emotiefflib[torch] ``` -------------------------------- ### Set Up Output Directory Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW8/bah.ipynb Defines the output directory path and creates it if it does not exist. This is a standard setup for saving results. ```python OUTDIR=os.path.join(TEST_DATA_DIR,'outputs') print(OUTDIR) if not os.path.exists(OUTDIR): os.makedirs(OUTDIR) ``` -------------------------------- ### Load Test Set Examples Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW6/cexpr.ipynb Reads a list of test set examples from a specified file. Assumes the file path is correctly defined in `DATA_DIR` and the file is comma-separated. ```python with open(DATA_DIR+'../test_set_examples/CVPR_6th_ABAW_CE_test_set_example.txt', 'r') as f: test_set_sample=f.read().splitlines() print(len(test_set_sample),test_set_sample[:5]) ``` -------------------------------- ### Install EmotiEffLib with ONNX and Engagement Support (from source) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from the source code with ONNX and TensorFlow support for engagement prediction. ```sh pip install .[engagement] ``` -------------------------------- ### Set Up Device and Model Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/Predict engagement and emotions on video.ipynb Configures the processing device (GPU or CPU) and selects the appropriate EmotiEffLib model. This setup is crucial for performance and compatibility. ```python test_dir = "data" device = "cuda" if torch.cuda.is_available() else "cpu" model_name = get_model_list()[0] ``` -------------------------------- ### Install EmotiEffLib with Torch Support Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/Predict engagement and emotions on video.ipynb Installs the EmotiEffLib library with all optional dependencies, including Torch support. This is a prerequisite for using advanced features. ```bash !pip install emotiefflib[all] ``` -------------------------------- ### Install EmotiEffLib with ONNX and Engagement Support (from PyPI) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from PyPI with ONNX and TensorFlow support for engagement prediction. ```sh pip install emotiefflib[engagement] ``` -------------------------------- ### Setup DataLoader for Training and Testing Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/abaw4_lsd_train.ipynb Initializes ImageFolder datasets and DataLoaders for training and testing, configuring batch size, shuffling, and multi-processing workers. ```python kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {} print(train_dir,test_dir) train_dataset = datasets.ImageFolder(root=train_dir, transform=train_transforms) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, **kwargs) test_dataset = datasets.ImageFolder(root=test_dir, transform=test_transforms) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, **kwargs) print(len(train_dataset), len(test_dataset)) ``` -------------------------------- ### Install EmotiEffLib with Torch and Engagement Support (from PyPI) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from PyPI with Torch and TensorFlow support for engagement prediction. ```sh pip install emotiefflib[torch,engagement] ``` -------------------------------- ### Training Epoch Output Example Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/abaw4_lsd_train.ipynb Example output from a single training epoch, showing loss, accuracy, and validation metrics. ```text Output: (4670,) (4670,) Epoch : 1 - loss : 0.9188 - acc: 0.9201 - val_acc: 0.5901 - val_f1: 0.5624 ``` -------------------------------- ### Install EmotiEffLib with Torch and Engagement Support (from source) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the library from the source code with Torch and TensorFlow support for engagement prediction. ```sh pip install .[torch,engagement] ``` -------------------------------- ### Calibration Output Example Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW10/abaw10_expr_va_au.ipynb This is an example output from the calibration process, showing frame index, a score, and a list of predicted values for action units. ```text 0.5 0 0.5270383115248468 [0.5532955361645552, 0.46052072742248934, 0.5676132939288541, 0.6081603369668356, 0.7208157113816389, 0.7371809402910641, 0.7228156221616712, 0.35819799018707527, 0.19182435360392894, 0.2456166868198307, 0.7970498745901836, 0.36136866478003604] 3 0.538943238075162 [0.5690584951148332, 0.4699850459303568, 0.5841102492657579, 0.6207622439725776, 0.7289476466235185, 0.7455725571969808, 0.735656019746199, 0.38063335679099225, 0.2031634446397188, 0.2568733370958639, 0.8056904583103055, 0.36686600221483945] 5 0.5403137831120037 [0.5714308635240509, 0.47138584786933163, 0.5877006690872554, 0.6246094303110398, 0.7305194423201364, 0.7455661909601603, 0.7392031462491271, 0.3872960877724845, 0.19776048586069464, 0.2580460005739822, 0.8066067275823552, 0.3636405052334256] 7 0.5401521255813982 [0.5720722545928993, 0.4706574858660388, 0.5904985715602249, 0.6279213927099452, 0.7309950810798089, 0.7440205225404813, 0.741381689713229, 0.3921002210759027, 0.18746238716148445, 0.25812627460773296, 0.8065187404653372, 0.3600708856036935] 10 0.5392338695761005 [0.5714379605819867, 0.47288563424038166, 0.5928172039675216, 0.6307517830112129, 0.7295052535538746, 0.7402813106907217, 0.7434034205509352, 0.3982717989002357, 0.17161893503841813, 0.25804261098378745, 0.805332137721375, 0.356458385672756] 15 0.5355940790976784 [0.5684582649197467, 0.47110357496114175, 0.5926554604678942, 0.63513764393943, 0.726919522231314, 0.7339525729175862, 0.744027964804191, 0.39885861214762786, 0.14284049866014215, 0.2620973029680465, 0.8032846828196317, 0.3477928483353884] 20 0.5305522720828864 [0.5625802065185059, 0.46693905737469066, 0.5901151929024, 0.6355678733787142, 0.7230328771803738, 0.7302339567385947, 0.7428415975802064, 0.39961190168175936, 0.11417298563590407, 0.26163128491620113, 0.8015808295359401, 0.33831950155134577] 25 0.5262195674360082 [0.5550957775134054, 0.46192277913312785, 0.5904256172492901, 0.6364768708749123, 0.7197607436981599, 0.7275777713822245, 0.7407000521600251, 0.39768238082696866, 0.09233166677488475, 0.2617590647547721, 0.8001625358494947, 0.330739549014832] 50 0.5105572321867163 [0.5250627698199093, 0.43788278489995525, 0.5776929933440517, 0.6393326301658836, 0.7069389974672171, 0.7143039905525759, 0.7317167816176913, 0.3890498057223596, 0.052972422324092265, 0.2499877600979192, 0.7966396719594053, 0.30510617826953473] ``` -------------------------------- ### TensorFlow/Keras Setup and Version Check Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW6/eri.ipynb Imports necessary TensorFlow and Keras modules, checks the TensorFlow version, and configures GPU memory growth. This is a standard setup for deep learning tasks. ```python import tensorflow as tf from tensorflow.keras.models import Model,Sequential, load_model,model_from_json from tensorflow.keras.applications import mobilenet,mobilenet_v2,densenet,inception_resnet_v2,inception_v3,vgg16,resnet_v2,resnet import efficientnet.tfkeras as enet from tensorflow.keras.layers import Flatten, Dense, Dropout,GlobalAveragePooling2D,Activation, Conv2D, Reshape,DepthwiseConv2D,Input from tensorflow.keras.optimizers import SGD, Adam from tensorflow.keras.utils import to_categorical from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard, Callback, CSVLogger, EarlyStopping from tensorflow.keras.metrics import top_k_categorical_accuracy import numpy as np from sklearn.metrics import confusion_matrix from tensorflow.keras.preprocessing.image import ImageDataGenerator import tensorflow.keras.backend as K print(tf.__version__) from tensorflow.compat.v1.keras.backend import set_session config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True sess=tf.compat.v1.Session(config=config) set_session(sess) ``` -------------------------------- ### Dependency and Option Setup Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotieffcpplib/CMakeLists.txt Finds required packages like OpenCV and sets up build options for tests, Torch, ONNX Runtime, and shared libraries. Includes a fatal error if no inference engine is specified. ```cmake find_package(OpenCV REQUIRED) option(BUILD_TESTS "Build EmotiEffCppLib with tests" OFF) option(WITH_TORCH "Path to the direcotry with Libtorch" OFF) option(WITH_ONNX "Path to the direcotry with ONNXRuntime" OFF) option(BUILD_SHARED_LIBS "Build shared library instead of static" OFF) if(NOT WITH_TORCH AND NOT WITH_ONNX) message(FATAL_ERROR "At least one inference engine should be specified: -DWITH_TORCH or -DWITH_ONNX") endif() ``` -------------------------------- ### Get Completion from Messages Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/emotions_llm/emotions_experiments.ipynb Helper function to get a completion from a list of messages using the Aura provider. Ensure g4f is installed and updated. ```python def get_completion_from_messages(messages): response = g4f.ChatCompletion.create( model=model_name, messages=messages, temperature=0, provider=g4f.Provider.Aura ) return response ``` -------------------------------- ### Get Completion from Content Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/emotions_llm/emotions_experiments.ipynb Helper function to get a completion from a given content string using the Aura provider. Ensure g4f is installed and updated. ```python def get_completion(content): response = g4f.ChatCompletion.create( model=model_name, messages=[{"role": "user", "content": content}], temperature=0, provider=g4f.Provider.Aura ) return response ``` -------------------------------- ### Clone EmotiEffCppLib Repository Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotieffcpplib/README.md Clone the EmotiEffCppLib repository and its submodules to get started. ```sh git clone https://github.com/sb-ai-lab/EmotiEffLib.git cd EmotiEffLib git submodule update --init --recursive ``` -------------------------------- ### Print PyTorch Version Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/abaw4_lsd_train.ipynb Displays the installed PyTorch version. Useful for verifying the environment setup. ```python print(f"Torch: {torch.__version__}") ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/emotiefflib/README.md Install the necessary dependencies for running tests using pip. ```sh pip install -r tests/requirements.txt ``` -------------------------------- ### Set up test directory, device, and model (ONNX) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/One image emotion recognition.ipynb Defines the test directory, selects the CPU device, and retrieves the first available model name for ONNX inference. ```python test_dir = os.path.join("..", "..", "..", "tests") device = "cpu" model_name = get_model_list()[0] ``` -------------------------------- ### Initialize Video Path and Recognizer Variables Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/cpp/Predict emotions on video.ipynb Sets the path to the input video file and declares variables for the recognition backend, model name, recognizer instance, and file extension. ```cpp auto videoPath = emotiEffLibRootDir + "/tests/data/video_samples/emotions/Angry/Angry.mp4"; std::string backend; std::string modelName; std::unique_ptr fer; std::string ext; ``` -------------------------------- ### Model Instantiation and Optimizer/Scheduler Setup Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW10/abaw10_expr_va_au.ipynb Instantiates a SimpleMLP3 model and configures an Adam optimizer with a ReduceLROnPlateau learning rate scheduler. Weight decay is also applied. ```python # instantiate with dropout=0.3 or tune #model = SimpleMLP(input_dim=input_shape[0], hidden_dim=128, out_dim=num_labels).to(device) #model = SimpleMLP2(input_dim=input_shape[0], hidden_dim=256, out_dim=num_labels, dropout=0.3).to(device) model = SimpleMLP3(input_dim=input_shape[0], out_dim=num_labels).to(device) print(model) # ---------- 4) Optimizer + scheduler + weight_decay ---------- optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=2) ``` -------------------------------- ### Initialize Dataset and Retrieve Labels Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/personalized_models/02 - Engagement user-independent model training.ipynb Configures the dataset for binary classification and retrieves video-to-label mappings. This step is crucial for preparing the data for model training. ```python ENGAGE_WILD_DATASET, BIN_CLASSIFICATION, N_CLASSES, DATASET_NAME, BASE_DATASET_DIR, ext = define_dataset(False, True) video2label, labels_list = get_video2label() print(labels_list) print(video2label) ``` -------------------------------- ### Install EmotiEffLib with Engagement Support Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/Predict engagement and emotions on video.ipynb Installs the EmotiEffLib library with support for engagement prediction. This command should be run in a terminal or notebook cell. ```bash !pip install emotiefflib[engagement] ``` -------------------------------- ### Initialize Video Path and Recognizer Variables Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/cpp/Predict engagement and emotions on video.ipynb Sets the path to the video file and declares variables for the backend, model name, EmotiEffLib recognizer, and file extension. ```cpp auto videoPath = emotiEffLibRootDir + "/tests/data/video_samples/engagement/engaged/1_video1.mp4"; std::string backend; std::string modelName; std::unique_ptr fer; std::string ext; ``` -------------------------------- ### Initialize PyTorch Environment and Load Model Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/abaw3_train.ipynb Sets up the PyTorch environment, including device selection (GPU if available) and imports necessary libraries. Loads a pre-trained feature extractor model. ```python import glob from itertools import chain import os import random import zipfile import matplotlib.pyplot as plt import numpy as np import pandas as pd import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from PIL import Image from sklearn.model_selection import train_test_split from torch.optim.lr_scheduler import StepLR from torch.utils.data import DataLoader, Dataset from torchvision import datasets, transforms from tqdm.notebook import tqdm print(f"Torch: {torch.__version__}") device = 'cuda' use_cuda = torch.cuda.is_available() print(use_cuda) ``` ```python if False: PATH='enet_b2_8.pt' #PATH='enet_b2_7.pt' IMG_SIZE=260 else: #PATH='enet_b0_7.pt' #PATH='enet_b0_8_best_afew.pt' PATH='enet_b0_8_best_vgaf.pt' IMG_SIZE=224 test_transforms = transforms.Compose( [ transforms.Resize((IMG_SIZE,IMG_SIZE)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ] ) print(PATH) feature_extractor_model = torch.load('../../models/affectnet_emotions/'+PATH) ``` -------------------------------- ### Build Doxygen Documentation Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/README.md Run these commands in the project's root directory to generate Doxygen documentation. The output will be in the 'html' folder. ```bash doxygen doxygen Doxyfile-cpp doxygen Doxyfile-python ``` -------------------------------- ### Load EngageWild Dataset and Mappings Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/personalized_models/02 - Engagement user-independent model training.ipynb Initializes the EngageWild dataset configuration, including binary classification status, number of classes, dataset name, base directory, and file extension. It also retrieves video-to-label mappings and prints the available labels and the mapping dictionary. ```python ENGAGE_WILD_DATASET, BIN_CLASSIFICATION, N_CLASSES, DATASET_NAME, BASE_DATASET_DIR, ext = define_dataset(True, False) video2label, labels_list = get_video2label() print(labels_list) print(video2label) ``` -------------------------------- ### Set up test directory, device, and model (PyTorch) Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/One image emotion recognition.ipynb Defines the test directory, selects the CUDA device if available otherwise CPU, and retrieves the first available model name for PyTorch inference. ```python test_dir = os.path.join("..", "..", "..", "tests") device = "cuda" if torch.cuda.is_available() else "cpu" model_name = get_model_list()[0] ``` -------------------------------- ### Install Specific timm Library Version Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/README.md Install timm version 0.4.5, which is required for EmotiEffLib v0.1 to ensure compatibility with EfficientNet models for PyTorch. ```bash pip install timm==0.4.5 ``` -------------------------------- ### Install EmotiEffLib with ONNX Support Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/One image emotion recognition.ipynb Installs the EmotiEffLib library using pip. This command ensures that the library is available for use in the current Python environment. ```bash !pip install emotiefflib ``` -------------------------------- ### Load EngageWild Dataset and Mappings Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/personalized_models/02 - Engagement user-independent model training.ipynb Initializes the EngageWild dataset for binary classification, retrieves the list of labels, and prints the video-to-label mapping. This is essential for setting up the data pipeline. ```python ENGAGE_WILD_DATASET, BIN_CLASSIFICATION, N_CLASSES, DATASET_NAME, BASE_DATASET_DIR, ext = define_dataset(True, True) video2label, labels_list = get_video2label() print(labels_list) print(video2label) ``` -------------------------------- ### Training Configuration Settings Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/train_faces_torch.ipynb Sets up hyperparameters for the training process, including batch size, number of epochs, learning rate, and device selection (CPU/GPU). ```python # Training settings batch_size = 48#64 #48# 32 #8 #16 # epochs = 40 lr = 3e-5 gamma = 0.7 seed = 42 device = 'cuda' use_cuda = torch.cuda.is_available() print(use_cuda) ``` -------------------------------- ### Install Dependencies in Colab Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/Predict engagement and emotions on video.ipynb Checks if the code is running in Google Colab and installs necessary dependencies from a requirements file. This ensures the environment is set up correctly for the tutorial. ```python try: import google.colab import urllib.request IN_COLAB = True urllib.request.urlretrieve("https://github.com/sb-ai-lab/EmotiEffLib/blob/main/docs/tutorials/python/requirements.txt?raw=true", "requirements.txt") !pip install -r requirements.txt except: IN_COLAB = False ``` -------------------------------- ### DDAMNet Model Setup and Loading Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW8/emi.ipynb Initializes and loads a DDAMNet model, specifying the number of classes and heads. It also loads pre-trained weights from a checkpoint file. ```python import sys DDAMFN_PATH='/home/avsavchenko/src/distr/DDAMFN' sys.path.append(DDAMFN_PATH) from networks.DDAM import DDAMNet if True: feature_extractor_model = DDAMNet(num_class=8, num_head=2) model_name='affectnet8_epoch4_acc0.6462' else: feature_extractor_model = DDAMNet(num_class=10, num_head=2) model_name='affectnet8_epoch9_acc0.642_mtl_2' feature_extractor_model.load_state_dict(torch.load(DDAMNFN_PATH+'/checkpoints/'+model_name+'.pth')['model_state_dict']) feature_extractor_model.bn=torch.nn.Identity() print(model_name) ``` -------------------------------- ### Deprecation Warning Example Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/personalized_models/02 - Engagement user-independent model training.ipynb This is an example of a DeprecationWarning related to converting a NumPy array with ndim > 0 to a scalar. It indicates a potential issue in future NumPy versions. ```python /media/deelvin_disk/echuraev/tmp_dir/ipykernel_4051215/1902217451.py:28: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.) y_pred.append(float(pred[0])) ``` -------------------------------- ### Load and Inspect Test Set Sample Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW7/cexpr.ipynb Reads a test set file, splits it into lines, and prints the count and first five entries. Assumes 'DATA_DIR' is defined. ```python with open(DATA_DIR+'../test_set_examples/CER.txt', 'r') as f: test_set_sample=f.read().splitlines() print(len(test_set_sample),test_set_sample[:5]) ``` -------------------------------- ### Install Required Libraries Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/personalized_models/03 - Custom dataset features extractor.ipynb Installs necessary Python packages for deep learning tasks. This includes libraries like mtcnn, timm, tensorflow, numba, and specific versions of PyTorch. ```python if False: !pip install mtcnn !pip install timm==0.4.5 !pip install tensorflow !pip install numba #!pip install keras==2.4.3 #!pip install -q torch==1.7.1 torchvision !pip install torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html ``` -------------------------------- ### Install EmotiEffLib with Torch support Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/docs/tutorials/python/One image emotion recognition.ipynb Installs the EmotiEffLib library with PyTorch support using pip. Note: The output indicates a potential issue with the package name or shell interpretation. ```bash !pip install emotiefflib[torch] ``` -------------------------------- ### Define Data and Model Parameters Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/personalized_models/04 - Engagement user-adaptation.ipynb Sets up various configuration parameters including data directories, dataset paths, model parameters like batch size, number of classes, image set size, epochs, learning rate, and concatenation strategy. ```python DATA_DIR = '/media/deelvin_disk/echuraev/Workspace/HSE/engagement/' USERS_DATASET = "/media/deelvin_disk/echuraev/Workspace/HSE/datasets/Engagement_dataset/" USERS_FEATURES = USERS_DATASET + "features_with_balanced/" USERS_WEIGHTS = USERS_DATASET + 'weights_with_balanced/' TABLE_NAME = "04_User-adaptation.xlsx" BATCH_SIZE = 128 N_CLASSES = 2 IMAGE_SET_SIZE=70 EPOCH_NUM = 12 LR = 1e-4 TABLE_NAME = None CONCATENATE_STAT = True THRESHOLD = 0.4 REPEAT = 10 ``` -------------------------------- ### Emotion Classification Setup Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/video_summarizer.ipynb Configures the mapping between numerical indices and emotion labels, and defines a list of interesting emotional states for analysis. This setup is conditional based on the number of classifier biases. ```python if len(classifier_bias)==7: idx_to_class={0: 'Anger', 1: 'Disgust', 2: 'Fear', 3: 'Happiness', 4: 'Neutral', 5: 'Sadness', 6: 'Surprise'} INTERESTING_STATES=[0,1,2,3,6] else: idx_to_class={0: 'Anger', 1: 'Contempt', 2: 'Disgust', 3: 'Fear', 4: 'Happiness', 5: 'Neutral', 6: 'Sadness', 7: 'Surprise'} INTERESTING_STATES=[0,2,3,4,7] print(idx_to_class) ``` -------------------------------- ### Initialize PyTorch and Device Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/video_summarizer.ipynb This snippet initializes PyTorch, imports necessary libraries like PIL and torchvision, and sets the computation device to CUDA if available. ```python import torch from PIL import Image from torchvision import datasets, transforms print(f"Torch: {torch.__version__}") device = 'cuda' ``` -------------------------------- ### Data Processing and Output Example Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/abaw5_expr_va_au_train.ipynb This snippet shows an example of data processing and the resulting output format, likely representing predictions or intermediate values during training. It includes numerical values and arrays. ```python 0.1 0.5091455374196453 (0.5279939392250242, 0.6166666666666666, array([0.55987886, 0.43465787, 0.56224317, 0.62654832, 0.74539631, 0.75222757, 0.7285629 , 0.33105139, 0.18511054, 0.20258621, 0.8408712 , 0.36679294]), array([0.8, 0.7, 0.7, 0.6, 0.4, 0.5, 0.6, 0.8, 0.7, 0.8, 0.3, 0.5]), array([0.89047318, 0.89392512, 0.86332642, 0.81484223, 0.77535237, 0.81338429, 0.86023336, 0.9498089 , 0.94286913, 0.92862846, 0.77820992, 0.8147884 ])) 0.2 0.5127973921074275 (0.533017397148989, 0.6249999999999999, array([0.56484658, 0.44359876, 0.56877713, 0.62935288, 0.74865063, 0.754916 , 0.73146233, 0.34267134, 0.19023254, 0.20882875, 0.84174832, 0.37112349]), array([0.7, 0.7, 0.7, 0.6, 0.5, 0.5, 0.6, 0.8, 0.7, 0.8, 0.3, 0.6]), array([0.87937717, 0.89669968, 0.86721575, 0.81786801, 0.79496272, 0.8154097 , 0.86255933, 0.95284365, 0.94462313, 0.93507702, 0.77923721, 0.84530859])) 0.30000000000000004 0.5161879402744332 (0.5375026584034432, 0.6166666666666666, array([0.56976915, 0.45084511, 0.57360552, 0.63160508, 0.75165132, 0.75679927, 0.73335565, 0.35297151, 0.19374649, 0.21774768, 0.84249049, 0.37544463]), array([0.7, 0.7, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.7, 0.8, 0.3, 0.6]), array([0.88203286, 0.89877668, 0.87046134, 0.80362286, 0.79684234, 0.81687885, 0.8642281 , 0.95496999, 0.94522874, 0.94091998, 0.78018599, 0.84601288])) 0.4 0.5187270490065781 (0.541014384144264, 0.6166666666666666, array([0.57316591, 0.45698558, 0.57729665, 0.63373932, 0.7542145 , 0.75784588, 0.73414719, 0.35982333, 0.19863126, 0.2245393 , 0.84299787, 0.37878582]), array([0.7, 0.7, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.7, 0.8, 0.3, 0.6]), array([0.88380481, 0.89999013, 0.87300487, 0.80574471, 0.79829354, 0.81769081, 0.8652352 , 0.95611166, 0.94510762, 0.94478463, 0.78093739, 0.84583345])) 0.5 0.5199393283667891 (0.5430868951332039, 0.6166666666666666, array([0.57564271, 0.46087072, 0.57899923, 0.6347521 , 0.75550435, 0.75756078, 0.73303063, 0.36535557, 0.20331393, 0.22754806, 0.84300553, 0.38145913]), array([0.7, 0.7, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.7, 0.8, 0.3, 0.6]), array([0.88474461, 0.90025705, 0.87444935, 0.80709947, 0.79867261, 0.81742614, 0.86495483, 0.95664325, 0.94413641, 0.94737078, 0.78120654, 0.84459757])) 0.6000000000000001 0.5198255232950532 (0.543975201831992, 0.6249999999999999, array([0.57638351, 0.46688322, 0.57987711, 0.6332615 , 0.7558231 , 0.755736 , 0.73109244, 0.36942135, 0.20410174, 0.23078234, 0.84263162, 0.38170849]), array([0.7, 0.8, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.7, 0.8, 0.3, 0.6]), array([0.88472443, 0.91312501, 0.87501458, 0.80686172, 0.7981141 , 0.81579774, 0.86405764, 0.95661409, 0.94202801, 0.94938722, 0.78110337, 0.84139011])) 0.7000000000000001 0.5178935122387572 (0.5436623812129876, 0.6333333333333332, array([0.57502695, 0.47127263, 0.58108291, 0.62987163, 0.75483367, 0.75371778, 0.72896558, 0.36984061, 0.20351354, 0.23411769, 0.84176021, 0.37994538]), array([0.7, 0.8, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.8, 0.8, 0.3, 0.6]), array([0.88329341, 0.91254407, 0.8750886 , 0.80534546, 0.79638926, 0.81397195, 0.8629496 , 0.95592774, 0.95789932, 0.95086758, 0.78052692, 0.83702976])) 0.8 0.5149169725953825 (0.5426215845931188, 0.6416666666666666, array([0.57209059, 0.47380567, 0.57948074, 0.6253632 , 0.75328333, 0.75140504, 0.72562433, 0.36879743, 0.20502473, 0.23820447, 0.84024417, 0.37813532]), array([0.7, 0.8, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.8, 0.8, 0.3, 0.7]), array([0.88070277, 0.91101212, 0.87379216, 0.80276604, 0.79408347, 0.8119914 , 0.86091298, 0.9546627 , 0.95493186, 0.95158085, 0.77910936, 0.86103186])) 0.9 0.511355118935657 (0.5398550602179175, 0.65, array([0.56802046, 0.47297454, 0.57562929, 0.62092852, 0.7507178 , 0.74875759, 0.72278971, 0.36296607, 0.20020751, 0.24060946, 0.83867702, 0.37598276]), array([0.8, 0.8, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.8, 0.8, 0.3, 0.7]), array([0.89164177, 0.90838559, 0.87161871, 0.8001754 , 0.79085134, 0.80972375, 0.85908496, 0.95232776, 0.94985824, 0.95170646, 0.77771647, 0.85579899])) 1.0 0.5075692080786435 (0.5367145284583944, 0.65, array([0.56438654, 0.47163507, 0.57207889, 0.61677155, 0.74818918, 0.74534595, 0.71942028, 0.35743029, 0.19360184, 0.24140214, 0.83680711, 0.37350551]), array([0.8, 0.8, 0.7, 0.5, 0.5, 0.5, 0.6, 0.8, 0.8, 0.8, 0.3, 0.7]), array([0.8884859 , 0.90530374, 0.8692748 , 0.79757579, 0.78782557, 0.80682583, 0.8569631 , 0.94949937, 0.94323473, 0.95116814, 0.77594452, 0.85026333])) ``` -------------------------------- ### Load and Parse Test Set Examples Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/ABAW10/abaw10_expr_va_au.ipynb Loads sample data from a text file, parses it to extract image locations and action unit labels, and organizes it by video. ```python with open(os.path.join(DATA_DIR,'test_set_examples/CVPR_6th_ABAW_AU_test_set_example.txt'),'r') as f: test_set_sample=f.read().splitlines() print(len(test_set_sample),test_set_sample[:5]) test_set_videos={} for s in test_set_sample[1:]: videoname,img_name=s.split(',')[0].split('/') if videoname not in test_set_videos: test_set_videos[videoname]=[] test_set_videos[videoname].append(img_name) print(len(test_set_videos)) ``` -------------------------------- ### Print TensorFlow Version Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/ABAW/abaw3_train.ipynb Prints the installed TensorFlow version. Useful for debugging and ensuring compatibility. ```python print(tf.__version__) ``` -------------------------------- ### Model Configuration and Loading Source: https://github.com/sb-ai-lab/emotiefflib/blob/main/training_and_examples/train_ramas.ipynb This snippet shows how to conditionally select and configure different base models like EfficientNet, MobileNet, or InceptionV3 based on a flag. It also demonstrates loading a pre-trained model from a file. ```python USE_EFFICIENT_NET=False if USE_EFFICIENT_NET: #import efficientnet.keras as enet base_model = enet.EfficientNetB3(weights=None) #base_model.load_weights('/home/avsavchenko/distr/efficientnet/efficientnet-b3-weights.h5') INPUT_SIZE = base_model.input_shape[1] INPUT_SIZE=(INPUT_SIZE,INPUT_SIZE) net_description='affectnet_enet3' net_model=enet BATCH_SIZE=16 print(INPUT_SIZE) elif False: net_model=mobilenet net_description='mobilenet_v1' INPUT_SIZE = (224, 224) BATCH_SIZE=80 base_model = mobilenet.MobileNet(input_shape=(INPUT_SIZE[0],INPUT_SIZE[1],3), include_top=True, weights='imagenet') elif False: net_model=inception_v3 net_description='inception_v3' INPUT_SIZE = (299, 299) BATCH_SIZE=32 base_model = inception_v3.InceptionV3(input_shape=(INPUT_SIZE[0],INPUT_SIZE[1],3), include_top=True, weights='imagenet') else: base_model=load_model('age_gender_tf2_224_deep-03-0.13-0.97.h5') base_model.summary() net_description='my_mobilenet_8' if False: net_model=vgg16 else: def mobilenet_preprocess_input(x,**kwargs): x[..., 0] -= 103.939 x[..., 1] -= 116.779 x[..., 2] -= 123.68 return x preprocessing_function=mobilenet_preprocess_input BATCH_SIZE=80 INPUT_SIZE = (224, 224) ```