This folder contains the training and evaluation scripts for the SpaCy Transformer based NER model. The scripts are based on the SpaCy CLI scripts and have been modified to work with the Label Studio output format. More information can be found here
Table of Contents
A bash script is used to initialize a training job. Model training is fully customizable and users are encouraged to update the parameters in the run_spacy_training.sh and spacy_transfomer_train.cfg files prior to training. The training workflow is as follows:
- Create a new data directory and dump all the JSON files containing annotations from Label Studio and any reviewed parquet files.
- Most parameters can be used with the default value, open the
run_spacy_training.shbash script and update the following fields with absolute paths or relative paths from the root of the repository:DATA_PATH: path to directory with Label Studio labelled dataDATA_OUTPUT_PATH: path to directory to store the split dataset (train/val/test) as well as other data artifacts required for training.MODEL_OUTPUT_PATH: path to store new model artifactsVERSION: Version can be updated to keep track of different training runs.--gpu-id: While executing thespacy traincommand, GPU can be used, if available, by setting this flag to 0.
- Make the training script executable:
chmod +x src/entity_extraction/training/spacy/run_spacy_training.sh- Execute the training script from the :
./src/entity_extraction/training/spacy/run_spacy_training.shTo run full evaluation of the trained model to get detailed metrics and plots, follow these steps:
- Open the
run_evaluation.shbash script and update the following fields:MODEL_NAME- the name to be assigned and put into the results file names etc.MODEL_PATH- the location of the trained model files.OUTPUT_DIR- the location to save the evaluation results.DATA_DIR- the location of the JSON file containing split train/test/val Label Studio output data.GPU- whether to use GPU or not.
- Make the evaluation script executable:
chmod +x src/entity_extraction/training/spacy/run_evaluation.sh- Run the evaluation script results will be generated in the
OUTPUT_DIRfolder. This may take while on CPU and even GPU.
./src/entity_extraction/training/spacy/run_evaluation.sh%%| label: training_pipeline
%%| fig-cap: "This is how the Entity Extraction model training process runs with intermediate files and processes."
%%| fig-height: 6
%%{init: {'theme':'base','themeVariables': {'fontFamily': 'arial','primaryColor': '#BFDFFF','primaryTextColor': '#000','primaryBorderColor': '#4C75A3','lineColor': '#000','secondaryColor': '#006100','tertiaryColor': '#fff'}, 'flowchart' : {'curve':'monotoneY'}}}%%
flowchart TD
F8(Labelled JSON files<br>from LabelStudio) --> C2(spacy_preprocess.py<br>Split into Train/Val/Test<br>Sets by xDD ID)
C2 --> F1(test set)
C2 --> F3(val set)
C2 --> F2(train set)
F3 --> C4
F2 --> C4(run_spacy_training.sh<br>Run Spacy Model Training)
F1 ----> C3(run_evaluation.sh<br>Run Model Evaluation)
F3 --> C3
C4 --> F7(Log Metrics &\nCheckpoints)
C4 --> F4(Log Final\nTrained Model)
F4 --> C3
C3 --> F6(Evaluation<br>Plots)
C3 --> F5(Evaluation results\nJSON)
subgraph Legend
computation[Computation]
fileOutput[File Input/Output]
computation ~~~ fileOutput
style computation fill:#BFDFFF, stroke:#4C75A3
style fileOutput fill:#d3d3d3, stroke:#808080
end
%% create a class for styling the nodes
classDef compute_nodes fill:#BFDFFF, stroke:#4C75A3,stroke-width:2px;
classDef file_nodes fill:#d3d3d3, stroke:#808080,stroke-width:2px;
class F1,F2,F3,F4,F5,F6,F7,F8 file_nodes;
class C2,C3,C4,C5 compute_nodes;
This notebook sets up the NER model training on Google Colab with GPU. Use the following steps to create the setup/folder structure and run the training. The free level of Colab does not allow CLI so a notebook is used to start the training.
- Create a folder in your Google Drive and name it the name of your training run (e.g.
spacy-transformer-v1) - Upload the entire
srcfolder from the repo into the folder you just created - Create a
datafolder inside the folder you just created and upload thetrain.spacyandval.spacyfiles into it - Create a
modelsfolder, this is where checkpoints will be saved during training - Create an
evaluation-resultsfolder, this is where the evaluation results will be saved - Create a copy of the
run_spacy_training.shandrun_evaluation.shfiles fromsrc/entity_extraction/training/spacyand place it in training run folder - Your folder structure should now look like:
spacy-transformer-v1 ├── data │ ├── train.spacy │ └── val.spacy ├── models ├── evaluation-results ├── src ├── colab_start_training.ipynb ├── run_evaluation.sh └── run_spacy_training.sh - Open the
run_spacy_training.shandrun_evaluation.shfiles and change each of the variables/paths to match your current setup. (Note: Google Colab expects absolute paths in the both the files) - Open the
colab_start_training.ipynbfile and run the cells to start training. - Model files and checkpoints will be saved in the
modelsfolder and evaluation results will be saved in theevaluation-resultsfolder.