This project demonstrates how to predict Quality of Service (QoS) metrics (e.g., download/upload speeds, latency) from the initial packets captured during a speed test. Instead of running prolonged tests that consume time and resources, we leverage early packet features to make accurate predictions. By integrating data collection, feature extraction, model training, and explainability, we create a reproducible pipeline to showcase how to optimize speed testing.
What this pipeline does:
- Capture Network Traffic: Use
tsharkandpysharkto capture and parse packet-level data. - Extract Packet-Level Features: Focus on the earliest packets in a speed test and extract features like packet size, inter-arrival times, TCP headers, etc.
- Collect QoS Metrics: Run a speed test command (
speedtest-cliorspeedtest) to measure final QoS metrics. - Merge and Preprocess Data: Combine packet features with QoS metrics into a single dataset, handling missing values and encoding categorical variables.
- Train a Neural Network Model (PyTorch): Train a feed-forward neural network to predict QoS metrics from initial packet features.
- Explain the Model (Trustee): Use Trustee to approximate the neural network’s behavior with a decision tree, providing a global interpretation of which features matter most.
All code for data collection, preprocessing, training, and explanation is contained in a single Jupyter notebook: data_collection.ipynb. This notebook:
- Runs the pipeline multiple times (optional), if desired, to collect data.
- Preprocesses the merged dataset (features and QoS metrics).
- Trains the neural network model.
- Uses Trustee to explain the trained model.
- Python 3.8+
- Jupyter Notebook: To run
data_collection.ipynb. - Network Tools:
tshark,curl - Python Packages:
pysharkfor packet parsingspeedtest-clior officialspeedtestbinary for QoS metricspandas,numpy,scikit-learnfor data handling and preprocessingtorchfor the neural networktrusteefor model explanationmatplotliborplotlyif you want additional visualizations
Install packages via:
pip install pyshark speedtest-cli scikit-learn torch torchvision trustee pandas numpy matplotlib
Make sure tshark and curl are installed:
sudo apt-get update && sudo apt-get install -y tshark curl
If using Ookla’s official speedtest:
curl -s https://install.speedtest.net/app/cli/install.deb.sh | sudo bash
sudo apt-get install speedtest
-
Open the Notebook: Start Jupyter:
jupyter notebookThen open
data_collection.ipynbin your browser. -
Configure Environment Variables (If Using NetUnicorn): If you have NetUnicorn environment variables like
NETUNICORN_ENDPOINT,NETUNICORN_LOGIN, andNETUNICORN_PASSWORD, set them before running cells in the notebook. The code will use these for remote execution if needed. -
Data Collection (Optional): If the code in
data_collection.ipynbincludes sections to run the pipeline multiple times, execute those cells to:- Install dependencies
- Start packet capture
- Run speed tests
- Stop capture and extract features
- Collect QoS metrics
- Save
features_<unique_id>.jsonandqos_metrics_<unique_id>.json
Adjust parameters like
num_runsand node selection within the notebook cells. -
Preprocessing: The notebook contains cells to merge the collected JSON files into a single dataset. It will:
- Load
features.jsonandqos_metrics.jsonfrom previous runs - Encode categorical features (e.g.,
protocol) - Scale numerical features
- Split into training and testing sets
Simply run these cells in order.
- Load
-
Model Training: Next, run the training cells:
- Define the PyTorch model (e.g.,
QoSPredictor) - Train using the provided dataset, printing training and test losses
- Adjust hyperparameters (hidden units, learning rate, epochs) within the notebook if desired.
- Define the PyTorch model (e.g.,
-
Model Explanation (Trustee): Finally, run the cells that use Trustee:
- Wrap the trained model with a
predictmethod. - Use
RegressionTrusteeto approximate the neural network’s predictions with a decision tree. - Display fidelity scores and optionally visualize the tree.
This step shows you which features are most influential in predicting QoS.
- Wrap the trained model with a
To reproduce the results:
- Collect Sufficient Data: Run the data collection cells multiple times to gather enough samples. Adjust conditions (like waiting between runs) as indicated in the report.
- Preprocess and Scale Data: Execute preprocessing cells exactly as described in the notebook.
- Train the Model: Run the training cell multiple epochs (e.g., 1000 epochs) or tweak hyperparameters as the report states.
- Check Loss and Metrics: Ensure training/test losses match the approximate numbers in the report.
- Run Trustee Explanation: Compare the produced decision tree and fidelity scores to the ones mentioned in the report. If they differ, ensure you used the same dataset size, model parameters, and conditions.
- If
speedtest-clifails, consider retry logic or use the officialspeedtest. - If you get NaNs in training, check preprocessing steps and ensure no columns are all NaN or zero variance.
- If
tsharkrequires root access, run Jupyter with appropriate permissions or run capture steps in a separate environment. - Refer to code comments in
data_collection.ipynbfor detailed explanations of each step.
For questions or issues, please raise a GitHub Issue in this repository or contact the project authors.