Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
0ca66e886a | |||
9f8a583857 | |||
6b4e39e5be | |||
145f80849f | |||
45a5f81c8c | |||
50e8912f73 | |||
af2d3fae90 | |||
71a1559dca | |||
3a6e2ceac2 | |||
27f6ba306f | |||
7c7a882f1f | |||
bf43d31225 | |||
04e5f4db8a | |||
a298384f7e | |||
18fac1de15 | |||
fe952b8eb6 | |||
1c7ddef5cb | |||
6d531ae5f3 |
|
@ -2,11 +2,9 @@ name: Modulation Recognition Demo
|
|||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
[main]
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches:
|
||||
[main]
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
ria-demo:
|
||||
|
@ -46,17 +44,19 @@ jobs:
|
|||
fi
|
||||
pip install -r requirements.txt
|
||||
|
||||
|
||||
- name: 1. Generate Recordings
|
||||
run: |
|
||||
mkdir -p data/recordings
|
||||
PYTHONPATH=. python scripts/dataset_manager/data_gen.py --output-dir data/recordings
|
||||
|
||||
- name: 📦 Compress Recordings
|
||||
run: tar -czf recordings.tar.gz -C data/recordings .
|
||||
|
||||
- name: ⬆️ Upload recordings
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: recordings
|
||||
path: data/recordings/**
|
||||
path: recordings.tar.gz
|
||||
|
||||
- name: 2. Build HDF5 Dataset
|
||||
run: |
|
||||
|
@ -113,7 +113,7 @@ jobs:
|
|||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: profile-data
|
||||
path: '**/onnxruntime_profile_*.json'
|
||||
path: "**/onnxruntime_profile_*.json"
|
||||
|
||||
- name: 7. Convert ONNX graph to an ORT file
|
||||
run: |
|
||||
|
|
|
@ -24,7 +24,7 @@ dataset:
|
|||
snr_step: 3
|
||||
|
||||
# Number of iterations (signal recordings) per modulation and SNR combination
|
||||
num_iterations: 3
|
||||
num_iterations: 10
|
||||
|
||||
# Modulation scheme settings; keys must match the `modulation_types` list above
|
||||
# Each entry includes:
|
||||
|
|
0
helpers/__init__.py
Normal file
0
helpers/__init__.py
Normal file
|
@ -41,7 +41,11 @@ class AppConfig:
|
|||
|
||||
|
||||
class AppSettings:
|
||||
"""Application settings, to be initialized from app.yaml configuration file."""
|
||||
"""
|
||||
Application settings,
|
||||
to be initialized from
|
||||
app.yaml configuration file.
|
||||
"""
|
||||
|
||||
def __init__(self, config_file: str):
|
||||
# Load the YAML configuration file
|
||||
|
|
|
@ -2,9 +2,9 @@ import os
|
|||
|
||||
import numpy as np
|
||||
import torch
|
||||
from scripts.model_builder.mobilenetv3 import RFClassifier, mobilenetv3
|
||||
|
||||
from helpers.app_settings import get_app_settings
|
||||
from scripts.model_builder.mobilenetv3 import RFClassifier, mobilenetv3
|
||||
|
||||
|
||||
def convert_to_onnx(ckpt_path: str, fp16: bool = False) -> None:
|
||||
|
@ -21,7 +21,7 @@ def convert_to_onnx(ckpt_path: str, fp16: bool = False) -> None:
|
|||
|
||||
in_channels = 2
|
||||
batch_size = 1
|
||||
slice_length = int(1024 / dataset_cfg.num_slices)
|
||||
slice_length = int(dataset_cfg.recording_length / dataset_cfg.num_slices)
|
||||
num_classes = len(dataset_cfg.modulation_types)
|
||||
|
||||
model = RFClassifier(
|
||||
|
@ -42,7 +42,7 @@ def convert_to_onnx(ckpt_path: str, fp16: bool = False) -> None:
|
|||
model.eval()
|
||||
|
||||
# Generate random sample data
|
||||
base, ext = os.path.splitext(os.path.basename(ckpt_path))
|
||||
base, _ = os.path.splitext(os.path.basename(ckpt_path))
|
||||
if fp16:
|
||||
output_path = os.path.join("onnx_files", f"{base}.onnx")
|
||||
sample_input = torch.from_numpy(
|
||||
|
|
|
@ -29,7 +29,7 @@ def generate_modulated_signals(output_dir: str) -> None:
|
|||
|
||||
for modulation in settings.modulation_types:
|
||||
for snr in np.arange(settings.snr_start, settings.snr_stop, settings.snr_step):
|
||||
for i in range(3):
|
||||
for _ in range(settings.num_iterations):
|
||||
recording_length = settings.recording_length
|
||||
beta = (
|
||||
settings.beta
|
||||
|
|
|
@ -49,8 +49,6 @@ def write_hdf5_file(records: List, output_path: str, dataset_name: str = "data")
|
|||
int(md["sps"]),
|
||||
)
|
||||
|
||||
first_rec, _ = records[0] # records[0] is a tuple of (data, md)
|
||||
|
||||
with h5py.File(output_path, "w") as hf:
|
||||
data_arr = np.stack([rec[0] for rec in records])
|
||||
dset = hf.create_dataset(dataset_name, data=data_arr, compression="gzip")
|
||||
|
|
|
@ -90,7 +90,7 @@ def split_recording(
|
|||
snippet_list = []
|
||||
|
||||
for data, md in recording_list:
|
||||
C, N = data.shape
|
||||
_, N = data.shape
|
||||
L = N // num_snippets
|
||||
for i in range(num_snippets):
|
||||
start = i * L
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import lightning as L
|
||||
import numpy as np
|
||||
import timm
|
||||
import torch
|
||||
from torch import nn
|
||||
|
|
|
@ -2,26 +2,26 @@ import os
|
|||
|
||||
import numpy as np
|
||||
import torch
|
||||
from sklearn.metrics import classification_report
|
||||
|
||||
os.environ["NNPACK"] = "0"
|
||||
from matplotlib import pyplot as plt
|
||||
from mobilenetv3 import RFClassifier, mobilenetv3
|
||||
from modulation_dataset import ModulationH5Dataset
|
||||
from sklearn.metrics import classification_report
|
||||
|
||||
from helpers.app_settings import get_app_settings
|
||||
|
||||
os.environ["NNPACK"] = "0"
|
||||
|
||||
|
||||
def load_validation_data():
|
||||
val_dataset = ModulationH5Dataset(
|
||||
"data/dataset/val.h5", label_name="modulation", data_key="validation_data"
|
||||
)
|
||||
|
||||
X = np.stack([x.numpy() for x, _ in val_dataset]) # shape: (N, C, L)
|
||||
x = np.stack([x.numpy() for x, _ in val_dataset]) # shape: (N, C, L)
|
||||
y = np.array([y.item() for _, y in val_dataset]) # shape: (N,)
|
||||
class_names = list(val_dataset.label_encoder.classes_)
|
||||
|
||||
return X, y, class_names
|
||||
return x, y, class_names
|
||||
|
||||
|
||||
def build_model_from_ckpt(
|
||||
|
@ -75,7 +75,7 @@ def evaluate_checkpoint(ckpt_path: str):
|
|||
classification_report(y_true, y_pred, target_names=class_names, zero_division=0)
|
||||
)
|
||||
|
||||
plot_confusion_matrix_with_counts(
|
||||
print_confusion_matrix(
|
||||
y_true=np.array(y_true),
|
||||
y_pred=np.array(y_pred),
|
||||
classes=class_names,
|
||||
|
@ -84,7 +84,7 @@ def evaluate_checkpoint(ckpt_path: str):
|
|||
)
|
||||
|
||||
|
||||
def plot_confusion_matrix_with_counts(
|
||||
def print_confusion_matrix(
|
||||
y_true: np.ndarray,
|
||||
y_pred: np.ndarray,
|
||||
classes: list[str],
|
||||
|
@ -94,6 +94,7 @@ def plot_confusion_matrix_with_counts(
|
|||
"""
|
||||
Plot a confusion matrix showing both raw counts and (optionally) normalized values.
|
||||
|
||||
|
||||
Args:
|
||||
y_true: true labels (integers 0..C-1)
|
||||
y_pred: predicted labels (same shape as y_true)
|
||||
|
@ -102,8 +103,8 @@ def plot_confusion_matrix_with_counts(
|
|||
title: title for the plot
|
||||
"""
|
||||
# 1) build raw CM
|
||||
C = len(classes)
|
||||
cm = np.zeros((C, C), dtype=int)
|
||||
c = len(classes)
|
||||
cm = np.zeros((c, c), dtype=int)
|
||||
for t, p in zip(y_true, y_pred):
|
||||
cm[t, p] += 1
|
||||
|
||||
|
@ -112,33 +113,48 @@ def plot_confusion_matrix_with_counts(
|
|||
with np.errstate(divide="ignore", invalid="ignore"):
|
||||
cm_norm = cm.astype(float) / cm.sum(axis=1)[:, None]
|
||||
cm_norm = np.nan_to_num(cm_norm)
|
||||
print_confusion_matrix_helper(cm_norm, classes)
|
||||
else:
|
||||
cm_norm = cm
|
||||
print_confusion_matrix_helper(cm, classes)
|
||||
|
||||
# 3) plot
|
||||
fig, ax = plt.subplots(figsize=(8, 8))
|
||||
im = ax.imshow(cm_norm, interpolation="nearest")
|
||||
ax.set_title(title)
|
||||
ax.set_xlabel("Predicted label")
|
||||
ax.set_ylabel("True label")
|
||||
ax.set_xticks(np.arange(C))
|
||||
ax.set_yticks(np.arange(C))
|
||||
ax.set_xticklabels(classes, rotation=45, ha="right")
|
||||
ax.set_yticklabels(classes)
|
||||
|
||||
# 4) annotate
|
||||
for i in range(C):
|
||||
for j in range(C):
|
||||
count = cm[i, j]
|
||||
val = cm_norm[i, j]
|
||||
txt = f"{count}\n{val:.2f}"
|
||||
ax.text(j, i, txt, ha="center", va="center")
|
||||
import numpy as np
|
||||
|
||||
fig.colorbar(im, ax=ax, label="Normalized value" if normalize else "Count")
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
def print_confusion_matrix_helper(matrix, classes=None, normalize=False, digits=2):
|
||||
"""
|
||||
Pretty prints a confusion matrix with x/y labels.
|
||||
|
||||
Parameters:
|
||||
- matrix: square 2D numpy array
|
||||
- labels: list of class labels (default: range(num_classes))
|
||||
- normalize: whether to normalize rows to sum to 1
|
||||
- digits: number of decimal places to show for normalized values
|
||||
"""
|
||||
matrix = np.array(matrix)
|
||||
num_classes = matrix.shape[0]
|
||||
labels = classes or list(range(num_classes))
|
||||
|
||||
# Header
|
||||
print(" " * 9 + "Ground Truth →")
|
||||
header = "Pred ↓ | " + " ".join([f"{str(label):>6}" for label in labels])
|
||||
print(header)
|
||||
print("-" * len(header))
|
||||
|
||||
# Rows
|
||||
for i in range(num_classes):
|
||||
row_vals = matrix[i]
|
||||
if normalize:
|
||||
row_sum = row_vals.sum()
|
||||
row_vals = row_vals / row_sum if row_sum != 0 else row_vals
|
||||
row_str = " ".join([f"{val:>6.{digits}f}" for val in row_vals])
|
||||
else:
|
||||
row_str = " ".join([f"{int(val):>6}" for val in row_vals])
|
||||
print(f"{str(labels[i]):>7} | {row_str}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
settings = get_app_settings()
|
||||
evaluate_checkpoint(os.path.join("checkpoint_files", "inference_recognition_model.ckpt"))
|
||||
evaluate_checkpoint(
|
||||
os.path.join("checkpoint_files", "inference_recognition_model.ckpt")
|
||||
)
|
||||
|
|
|
@ -1,23 +1,22 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
os.environ["NNPACK"] = "0"
|
||||
import lightning as L
|
||||
import mobilenetv3
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import torchmetrics
|
||||
from lightning.pytorch.callbacks import ModelCheckpoint
|
||||
from lightning.pytorch.callbacks import ModelCheckpoint, TQDMProgressBar
|
||||
from modulation_dataset import ModulationH5Dataset
|
||||
|
||||
from helpers.app_settings import get_app_settings
|
||||
|
||||
os.environ["NNPACK"] = "0"
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
data_dir = os.path.abspath(os.path.join(script_dir, ".."))
|
||||
project_root = os.path.abspath(os.path.join(os.getcwd(), ".."))
|
||||
if project_root not in sys.path:
|
||||
sys.path.insert(0, project_root)
|
||||
from lightning.pytorch.callbacks import TQDMProgressBar
|
||||
|
||||
|
||||
class CustomProgressBar(TQDMProgressBar):
|
||||
|
@ -59,8 +58,6 @@ def train_model():
|
|||
print("X shape:", x.shape)
|
||||
print("Y values:", y[:10])
|
||||
break
|
||||
|
||||
unique_labels = list(set([row[label].decode("utf-8") for row in ds_train.metadata]))
|
||||
num_classes = len(ds_train.label_encoder.classes_)
|
||||
|
||||
hparams = {
|
||||
|
|
Loading…
Reference in New Issue
Block a user