This project explores the translation of Electroencephalography (EEG) signals into natural language text and the classification of sentiment directly from EEG signals. It utilizes various deep learning models, primarily sequence-to-sequence architectures and classifiers, trained and evaluated on the ZuCo dataset.
- EEG-to-Text Decoding: Translates brain activity recorded via EEG into corresponding text using models like BrainTranslator and T5Translator.
- EEG-based Sentiment Analysis: Classifies sentiment (e.g., positive, negative, neutral) directly from EEG signals using baseline models (MLP, LSTM) and fine-tuned transformers.
- Text-based Sentiment Classification: Includes scripts for training standard text classifiers (BERT, BART, RoBERTa) on datasets like the Stanford Sentiment Treebank (SST) for comparison or zero-shot approaches.
- Zero-Shot Sentiment Discovery: Explores predicting sentiment from EEG by first decoding EEG to text and then classifying the generated text using a pre-trained text sentiment classifier.
- Decoding Models:
BrainTranslator(based on BART)BrainTranslatorNaive(simplified BART-based)T5Translator(based on T5)
- Sentiment Models (EEG-based):
BaselineMLPSentenceBaselineLSTMNaiveFineTunePretrainedBert(EEG input adapted for BERT)
- Sentiment Models (Text-based):
- Fine-tuned
BertForSequenceClassification - Fine-tuned
BartForSequenceClassification - Fine-tuned
RobertaForSequenceClassification
- Fine-tuned
- Zero-Shot Pipeline:
ZeroShotSentimentDiscovery(combines a decoder and a text classifier)
- ZuCo Dataset: The primary dataset containing EEG recordings synchronized with reading tasks (Task 1-SR, Task 2-NR, Task 3-TSR, Task 2-NR-2.0).
- Stanford Sentiment Treebank (SST): Used for training text-based sentiment classifiers. A filtered version is generated to avoid overlap with ZuCo sentences.
-
Clone the repository:
git clone https://github.com/AmanSikarwar/EEG-To-Text cd EEG-To-Text -
Install Dependencies: Ensure you have Python 3 and PyTorch installed. Install other required packages (a
requirements.txtfile might be needed):pip install torch transformers numpy tqdm evaluate nltk rouge-score scikit-learn yagmail h5py fuzzy_match
-
Download Datasets: Place the raw ZuCo
.matfiles in the appropriate./dataset/ZuCo/subdirectories (e.g.,./dataset/ZuCo/task1-SR/Matlab_files/). Download the Stanford Sentiment Treebank dataset if needed and place it in./dataset/stanfordsentiment/.
Run the preparation script to convert ZuCo .mat files into .pickle format and generate necessary sentiment label files:
bash scripts/prepare_dataset.shThis script utilizes utilities in the ./util/ directory (construct_dataset_mat_to_pickle_v1.py, construct_dataset_mat_to_pickle_v2.py, get_sentiment_labels.py, get_SST_ternary_dataset.py). Processed data will be stored in ./dataset/ZuCo/.../pickle/ and ./dataset/stanfordsentiment/.
Training is performed using Python scripts. Configuration is often managed via command-line arguments and saved .json files in ./config/. Checkpoints are saved in ./checkpoints/.
- EEG-to-Text Decoding:
- Use
train_decoding2.py(or potentiallytrain_decoding.py). - Example script:
run_and_notify2.shshows how to runtrain_decoding2.pywith specific parameters for a 2-step training process.
- Use
- EEG Sentiment Baseline:
- Use
train_sentiment_baseline.py. - Example script:
scripts/train_eeg_sentiment_baseline.sh
- Use
- Text-based Sentiment Classifier:
- Use
train_sentiment_textbased.py. - Can be trained on ZuCo text or SST.
- Use
Refer to the scripts in the scripts/ directory and run_and_notify2.sh for detailed examples of training commands and parameters.
Evaluation scripts measure the performance using various metrics (BLEU, ROUGE, WER, CER for decoding; Accuracy, F1 for sentiment). Results are typically saved in ./results/ and ./score_results/.
- EEG-to-Text Decoding:
- Use
eval_decoding2.py(or potentiallyeval_decoding.py). - Example usage is shown within
run_and_notify2.sh.
- Use
- Sentiment Analysis:
- Use
eval_sentiment.py. - Example script for zero-shot evaluation:
scripts/eval_sentiment_zeroshot_pipeline.sh
- Use
The run_and_notify2.sh script provides an example of how to run a full training and evaluation pipeline and send email notifications upon completion or failure using yagmail. Configure sender/recipient emails within the script. Logs are stored in ./run_logs/.
.
├── checkpoints/ # Saved model weights (.pt files)
│ ├── decoding/
│ └── eeg_sentiment/
│ └── text_sentiment_classifier/
├── config/ # Configuration files (.json) for experiments
│ ├── decoding/
│ └── eeg_sentiment/
│ └── text_sentiment_classifier/
├── data.py # PyTorch Dataset and DataLoader classes (ZuCo_dataset, SST_tenary_dataset)
├── dataset/ # Raw and processed datasets
│ ├── ZuCo/
│ └── stanfordsentiment/
├── eval_decoding.py # Evaluation script for EEG-to-Text (older version?)
├── eval_decoding2.py # Main evaluation script for EEG-to-Text
├── eval_sentiment.py # Evaluation script for sentiment classification tasks
├── model_decoding.py # Decoding model definitions (BrainTranslator, T5Translator, etc.)
├── model_sentiment.py # Sentiment model definitions (Baselines, ZeroShot, etc.)
├── results/ # Detailed output files from evaluation runs (.txt)
├── run_logs/ # Logs generated by run_and_notify scripts
├── run_and_notify.sh # Example script for running experiments with notifications (older?)
├── run_and_notify2.sh # Example script for running experiments with notifications
├── score_results/ # Summarized score files from evaluation runs (.txt)
├── scripts/ # Helper shell scripts for training/evaluation/preparation
├── train_decoding.py # Training script for EEG-to-Text (older version?)
├── train_decoding2.py # Main training script for EEG-to-Text
├── train_sentiment_baseline.py # Training script for EEG sentiment baselines
├── train_sentiment_textbased.py # Training script for text-based sentiment classifiers
└── util/ # Utility scripts for data processing, etc.