A comprehensive Named Entity Recognition project that implements multiple BERT-based models and baseline approaches for biomedical entity extraction using BIO tagging scheme.
This project trains and evaluates multiple NER models:
- Baseline Model: RoBERTa model
- Main Models: BioBERT, and BioMedBERT models with CRF layers
- Task: Extract medical entities from biomedical text using BIO (Begin-Inside-Outside) tagging
NLP/
├── src/
│ ├── custom_model.py # Custom BERT+CRF model implementation
│ ├── utils.py # Utility functions for data processing and evaluation
│ └── __init__.py
├── data/
│ ├── mts_data.xml # Original MTS biomedical data in BioC XML format
│ ├── train.csv # Training data
│ ├── validation.csv # Validation data
│ └── test.csv # Test data
├── data_iob2/
│ ├── train.iob2 # Training data in IOB2 format
│ ├── validation.iob2 # Validation data in IOB2 format
│ ├── test.iob2 # Test data in IOB2 format
│ └── mts_data.iob2 # MTS data in IOB2 format
├── baseline/
│ ├── BiLSTM_baseline.ipynb # Baseline model used during project proposal
│ ├── span_f1.py # Span-level F1 score calculation
│ └── [test files] # Baseline test data in IOB2 format
├── results/
│ ├── MTS_results_*.csv # Model results on MTS data
│ ├── MM_test_results_*.csv # Model results on MM test data
│ └── [various result files]
├── main_train.py # Main training pipeline for BERT-based models
├── baseline_train.py # Training pipeline for baseline RoBERTa model
├── bio_tagger.py # BIO tagging implementation
├── encode_tags.py # Tag encoding utilities
├── convert_xml_to_iob2.ipynb # Notebook to convert XML to IOB2 format
├── analysis.ipynb # Analysis and evaluation notebook
├── requirements.txt # Project dependencies
└── README.md # This file
Install required packages:
pip install -r requirements.txt- torch: Deep learning framework
- transformers: Hugging Face transformers for BERT models
- pytorch_crf: CRF layer implementation
- seqeval: Evaluation metrics for NER tasks
- datasets: HuggingFace datasets library
- bioc: BioC XML format parsing
- pandas/numpy: Data manipulation
- matplotlib/seaborn: Visualization
pip install -r requirements.txtThe data should be in IOB2 format (provided in data_iob2/ folder). If you have raw XML data:
- Use the
convert_xml_to_iob2.ipynbnotebook to convert BioC XML format to IOB2 format - The conversion preserves entity annotations with BIO tagging scheme
Ensure all required files exist:
# Check data files
ls data_iob2/train.iob2
ls data_iob2/validation.iob2
ls data_iob2/test.iob2Run the baseline training:
python baseline_train.pyWhat it does:
- Loads training/validation/test data from
data_iob2/folder - Trains a RoBERTa model for token classification
- Performs early stopping based on validation F1 score
- Saves results to
results/folder
Expected output:
MTS_results_baseline.csv- Results on MTS test setMM_test_results_baseline.csv- Results on MM test set- Model checkpoints in model cache
Run the main training pipeline:
python main_train.pyWhat it does:
- Through a single main() function, the program takes the following chosen models as input to train it:
- BioBERT (dmis-lab/biobert-base-cased-v1.1)
- BioMedBERT (d4data/biomedical-bert-base-cased)
- Uses weighted CRF loss for better entity recognition
- Implements early stopping and model checkpointing
- Saves results to
results/folder
Expected output:
MTS_results_biobert.csv- BioBERT results on MTS dataMTS_results_biomed.csv- BioMedBERT results on MTS data- Model checkpoints and evaluation metrics
Training notebooks are available on Google Colab for GPU acceleration:
- Baseline Training: Colab Notebook
- Main Models Training: Colab Notebook
The IOB2 format is a standard for NER tasks:
word tag
Paris B-LOCATION
is O
nice O
. O
Each sentence is separated by an empty line. Tags follow the BIO scheme:
- B-ENTITY: Beginning of an entity
- I-ENTITY: Inside an entity (continuation)
- O: Outside any entity
- PERSON: Person names
- LOCATION: Geographic locations
- ORG: Organizations
- MEDICAL: Symptoms, History, Action
- O: No entity
BertCRFForNER: Custom model combining BERT with CRF layer
- BERT encoder for contextual representations
- Linear layer for emission scores
- CRF layer for sequence-level constraints
- Handles subword tokenization and label alignment
WeightedTrainer: Custom trainer class extending HuggingFace Trainer
- Implements weighted loss computation
- Proper state dict handling for CRF modules
Key functions:
read_file(): Parse IOB2 format filestokenize_and_align_labels(): Align subword tokens with original labelscompute_metrics_model(): Calculate F1, precision, recall at token and entity levelget_predictions(): Generate predictions on test datavalidate_bio()/fix_bio(): Validate and correct BIO tag sequenceslabel_config(): Configure entity labels and their mappings
BIOTagger: Converts character-level entity annotations to BIO-tagged sequences
- Handles entity boundary detection
- Converts between different entity type mappings
- Produces valid BIO sequences
train_data = read_file('data_iob2/train.iob2') # Returns list of (words, tags) tuples- Converts words to subword tokens using model tokenizer
- Aligns entity labels to subword tokens (first token gets label, rest ignored)
- Prepares features for model input
- Uses
TrainerAPI from HuggingFace transformers - Early stopping when validation F1 plateaus
- Saves best model checkpoint
- Computes metrics: F1 score, precision, recall (both token and entity-level)
- Generates predictions on test set
- Creates classification reports per entity type
- Saves results to CSV files
Results files contain metrics for each entity type:
- Precision: Percentage of predicted entities that are correct
- Recall: Percentage of gold entities that were found
- F1 Score: Harmonic mean of precision and recall
- Support: Number of entities in test set
Common settings in training scripts:
- Batch size: 8-16 (adjust based on GPU memory)
- Learning rate: 2e-5 (standard for BERT fine-tuning)
- Epochs: 20 (with early stopping)
- Random seed: 52 (for reproducibility)
- Max sequence length: 512 (BERT max)
- Recommended: NVIDIA GPU with 8GB+ VRAM
- CPU: Significantly slower
- Colab: Use GPU runtime for faster training
This is an academic project for Natural Language Processing course.