Skip to content

Commit 824d02e

Browse files
authored
Merge pull request #7 from qoherent/5-transition-away-from-conda
Transition away from Conda
2 parents 21c5511 + 188255c commit 824d02e

7 files changed

Lines changed: 226 additions & 139 deletions

File tree

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ venv/
2222
# Visual Studio Code specific file
2323
.vscode/
2424

25-
# Project Related Files
26-
/SpectrumSensingDataset/
25+
# Lightning
2726
/lightning_logs/
27+
28+
# Project dataset
29+
/spectrum_sensing_dataset.hdf5

README.md

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Spectrogram Segmentation
22

3-
The successful application of [semantic segmentation](https://www.ibm.com/topics/semantic-segmentation) to radiofrequency (RF) spectrograms holds significant applications
4-
for [spectrum sensing](https://iopscience.iop.org/article/10.1088/1742-6596/2261/1/012016#:~:text=In%20cognitive%20radio%2C%20spectrum%20sensing,user%20can%20use%20the%20spectrum.) and serves as a foundational example showcasing the near-term feasibility of
3+
The successful application of [semantic segmentation](https://www.ibm.com/topics/semantic-segmentation) to radiofrequency (RF) spectrograms has significant
4+
implications for [spectrum sensing](https://iopscience.iop.org/article/10.1088/1742-6596/2261/1/012016#:~:text=In%20cognitive%20radio%2C%20spectrum%20sensing,user%20can%20use%20the%20spectrum.), and serves as a foundational example showcasing the near-term feasibility of
55
[intelligent radio](https://www.qoherent.ai/intelligentradio/) technology.
66

77
In this example, we use [PyTorch](https://pytorch.org/) and [Lightning](https://lightning.ai/docs/pytorch/stable/) to train a segmentation model to identify and
88
differentiate between 5G NR and 4G LTE signals within wideband spectrograms.
99

1010
Qoherent's mission to drive the creation of intelligent radio technology requires a combination of open-source and
1111
proprietary tools. This example, which leverages open-source tools and machine learning frameworks to train on
12-
synthetic radio data generated using MATLAB, showcases our commitment to interoperability and our tool-agnostic
13-
approach to innovation.
12+
synthetic radio data generated using MATLAB's powerful 5G and LTE toolboxes, showcases our commitment to
13+
interoperability and our tool-agnostic approach to innovation.
1414

15-
Classification results are comparable to those reported by MathWorks' AI-based network. For more information,
16-
please refer to the following article by MathWorks:
15+
Classification results are comparable to those achieved by MathWorks' custom network, albeit with more learnables.
16+
For more information, please refer to the following article by MathWorks:
1717
[Spectrum Sensing with Deep Learning to Identify 5G and LTE Signals](https://www.mathworks.com/help/comm/ug/spectrum-sensing-with-deep-learning-to-identify-5g-and-lte-signals.html).
1818

1919
If you found this example interesting or helpful, don't forget to give it a star! ⭐
@@ -24,15 +24,15 @@ If you found this example interesting or helpful, don't forget to give it a star
2424
This example is provided as a Jupyter Notebook. You have the option to either run this example locally or in Google
2525
Colab.
2626

27-
To run this example locally, you'll need to download the project and dataset and set up a Conda
28-
virtual environment. If this seems daunting, we recommend running this example on Google Colab.
27+
To run this example locally, you'll need to download the project and dataset and set up a Python
28+
virtual environment. If this seems daunting, we recommend running this example on Google Colab (Coming soon!).
2929

3030
### Running this example locally
3131

32-
Please note that running this example locally will require approximately 10 GB of free space. Please ensure you
32+
Please note that running this example locally will require approximately 6.1 GB of free space. Please ensure you
3333
have sufficient space available prior to proceeding.
3434

35-
1. Ensure that [Git](https://git-scm.com/downloads) and [Conda](https://conda.io/projects/conda/en/latest/user-guide/install/index.html) are installed on the computer where you plan to run this example.
35+
1. Ensure that [Git](https://git-scm.com/downloads) and [Python](https://www.python.org/downloads/) are installed on the computer where you plan to run this example.
3636
Additionally, if you'd like to accelerate model training with a GPU, you'll require [CUDA](https://docs.nvidia.com/cuda/cuda-quick-start-guide/index.html).
3737

3838

@@ -42,58 +42,94 @@ git clone https://github.com/qoherent/spectrogram-segmentation.git
4242
```
4343

4444

45-
3. Create a Conda environment using the provided `environment.yml` file:
45+
3. Create and activate a Python [virtual environment](https://docs.python.org/3/library/venv.html). This is a best practice for isolating project dependencies.
46+
47+
<details>
48+
<summary><strong>Windows</strong></summary>
49+
50+
Use the following command to create a new directory named `venv` within the project directory:
51+
```commandline
52+
python -m venv venv
53+
```
54+
55+
Then, activate the virtual environment with:
56+
```commandline
57+
venv\Scripts\activate
58+
```
59+
60+
</details>
61+
62+
<details>
63+
<summary><strong>Linux/Mac</strong></summary>
64+
65+
Use the following command to create a new directory named `venv` within the project directory:
4666
```commandline
47-
conda env create -f environment.yml
67+
python3 -m venv venv
4868
```
49-
This will create a new Conda environment named `spectrogram-segmentation` within the Conda installation directory.
5069

70+
Then, activate the virtual environment with:
71+
```commandline
72+
source venv/bin/activate
73+
```
74+
75+
</details>
76+
77+
Activating the virtual environment should modify the command prompt to show `(venv)` at the beginning, indicating
78+
that the virtual environment is active.
5179

52-
4. Active the environment:
80+
81+
4. Install project dependencies from the provided `requirements.txt` file:
5382
```commandline
54-
conda activate spectrogram-segmentation
83+
pip install -r requirements.txt
5584
```
5685

5786

58-
5. Download and unpack the spectrum sensing dataset:
87+
5. Download the spectrum sensing dataset.
88+
89+
<details>
90+
<summary><strong>Windows</strong></summary>
91+
5992
```commandline
6093
python download_dataset.py
6194
```
62-
This command will create a new directory named `SpectrumSensingDataset` at the project's root. The
63-
MathWorks Spectrum Sensing dataset will be downloaded and unpacked into this directory automatically.
95+
96+
</details>
97+
98+
<details>
99+
<summary><strong>Linux/Mac</strong></summary>
100+
101+
```commandline
102+
python3 download_dataset.py
103+
```
104+
105+
</details>
106+
107+
This will download the `spectrum_sensing_dataset.hdf5` source file to the project's root directory.
64108

65109

66110
6. Register the environment kernel with Jupyter:
67111
```commandline
68-
ipython kernel install --user --name=spectrogram-segmentation
112+
ipython kernel install --user --name=venv --display-name "Spectrogram Segmentation (venv)"
69113
```
70114

71115

72-
7. Open the notebook, `spectrogram_segmentation.ipynb`, specifying to use the `spectrogram-segmentation` kernel:
116+
7. Open the notebook, `spectrogram_segmentation.ipynb`, specifying to use the new kernel:
73117
```commandline
74-
jupyter notebook spectrogram_segmentation.ipynb --MultiKernelManager.default_kernel_name=spectrogram-segmentation
118+
jupyter notebook spectrogram_segmentation.ipynb --MultiKernelManager.default_kernel_name=venv
75119
```
76120

77121

78122
8. Give yourself a pat on the back - you're all set up and ready to explore the example! For more information on
79-
navigating the Jupyter Notebook interface and executing code, please check out this tutorial by the Codecademy
123+
navigating the Jupyter Notebook interface and executing code cells, please check out this tutorial by the Codecademy
80124
Team: [How To Use Jupyter Notebooks](https://www.codecademy.com/article/how-to-use-jupyter-notebooks).
81125

82126
Depending on your system specifications and the availability of a CUDA, running this example locally may take
83127
several minutes. If a cell is taking too long to execute, you can interrupt its execution by clicking the "Kernel"
84128
menu and selecting "Interrupt Kernel" or by pressing `Ctrl + C` in the terminal where Jupyter Notebook is running.
85129

86130

87-
9. After you finish exploring, consider removing the dataset from your system and deleting the Conda environment to
88-
free up space. You can delete the Conda environment using the following command:
89-
```commandline
90-
conda env remove --name spectrogram-segmentation
91-
```
92-
93-
### Running this example in Google Colab
94-
95-
**Coming soon:** Don't want the hassle of downloading the project and dataset and setting up a Conda environment?
96-
We've shared the notebook on Google Colab: [Spectrogram Segmentation]().
131+
9. After you finish exploring, consider removing the dataset from your system and deleting the virtual environment to
132+
free up space. Remember to deactivate the virtual environment using the deactivate command before deleting the folder.
97133

98134

99135
## 🤝 Contribution
@@ -119,10 +155,10 @@ This work is a product of the collaborative efforts of the Qoherent team. Of spe
119155

120156
## 🙏 Attribution
121157

122-
The dataset used in this example was prepared by MathWorks and is publicly available [here](https://www.mathworks.com/supportfiles/spc/SpectrumSensing/SpectrumSenseTrainingDataNetwork.tar.gz). For more information
123-
on how this dataset was generated or to generate further spectrum data, please refer to MathWork's article on spectrum
124-
sensing. For more information about Qoherent's use of MATLAB to accelerate intelligent radio research, check out our
125-
[customer story](https://www.mathworks.com/company/user_stories/qoherent-uses-matlab-to-accelerate-research-on-next-generation-ai-for-wireless.html).
158+
The dataset used in this example was prepared by MathWorks using their 5G and LTE toolboxes and is publicly available
159+
[here](https://www.mathworks.com/supportfiles/spc/SpectrumSensing/SpectrumSenseTrainingDataNetwork.tar.gz). For more information on how this dataset was generated or to generate further spectrum data, please refer
160+
to MathWork's article on spectrum sensing. For more information about Qoherent's use of MATLAB to accelerate
161+
intelligent radio research, check out our [customer story](https://www.mathworks.com/company/user_stories/qoherent-uses-matlab-to-accelerate-research-on-next-generation-ai-for-wireless.html).
126162

127163
The DeepLabv3 models used in this example were initially proposed by Chen _et al._ and are further discussed
128164
in their 2017 paper titled '[Rethinking Atrous Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1706.05587)'. The MobileNetV3

download_dataset.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,58 @@
22
Download MathWorks' Spectrum Sensing dataset, if it isn't already downloaded.
33
"""
44

5+
import hashlib
56
import os
6-
import tarfile
7-
import warnings
87

98
import requests
109
from torch.utils.model_zoo import tqdm
1110

12-
mirror = "https://www.mathworks.com/supportfiles/spc/SpectrumSensing/"
13-
resource = "SpectrumSenseTrainingDataNetwork.tar.gz"
11+
12+
def sha256(target: str) -> str:
13+
"""Calculates the SHA256 hash of the target resource.
14+
15+
:param target: The full path, including the filename, of the resource to hash.
16+
:type target: str
17+
18+
:return: The SHA256 hash of the target resource.
19+
"""
20+
sha256_hash = hashlib.sha256()
21+
22+
with open(target, "rb") as file:
23+
for chunk in iter(lambda: file.read(4096), b""):
24+
sha256_hash.update(chunk)
25+
26+
return sha256_hash.hexdigest()
27+
28+
29+
mirror = "https://storage.googleapis.com/qoherent_external_drive/general_dataset_library/"
30+
resource = "spectrum_sensing_dataset_v1.0.hdf5"
1431
file_url = "{}{}".format(mirror, resource)
15-
target_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "SpectrumSensingDataset")
16-
archive_file = os.path.join(target_dir, resource)
32+
sha256_checksum = "8a93aa14145ea1a35cbc191defbbcf90c49ecdb89e6e93f3e55357f182d184c6"
1733

18-
# Ensure the target directory exists.
19-
if not os.path.exists(target_dir):
20-
os.makedirs(target_dir, exist_ok=True)
34+
target = os.path.join(os.path.dirname(os.path.abspath(__file__)), "spectrum_sensing_dataset.hdf5")
2135

22-
# Check if the target directory is empty, and abort the download if it's not.
23-
if len(os.listdir(target_dir)) != 0:
24-
warnings.warn(f"Target directory '{target_dir}' is not empty. Aborting download.")
36+
# Check if the dataset source file already exists.
37+
if os.path.exists(target):
38+
print(f"{target} already exists. Aborting download.")
2539
exit(1)
2640

27-
# Download and extract the dataset into the target directory.
41+
# Download the dataset source file into the target directory.
2842
print(f"Downloading {format(file_url)}")
2943
n_bytes = int(requests.head(file_url).headers.get("Content-Length", 0))
3044

3145
with (
3246
requests.get(file_url, stream=True, timeout=3) as r,
33-
open(archive_file, "wb") as out_file,
34-
tqdm(desc="Downloading", total=n_bytes, unit="B", unit_scale=True) as pbar,
47+
open(target, "wb") as out_file,
48+
tqdm(desc="Downloading MathWorks' Spectrum Sensing Dataset", total=n_bytes, unit="B", unit_scale=True) as pbar,
3549
):
3650
for chunk in r.iter_content(chunk_size=1024):
3751
if chunk:
3852
out_file.write(chunk)
3953
pbar.update(len(chunk))
4054

41-
print(f"Extracting to {target_dir}")
42-
with tarfile.open(archive_file) as tar:
43-
tar.extractall(target_dir)
44-
45-
# Remove the archive file after extraction.
46-
os.remove(archive_file)
47-
print(f"Archive file '{archive_file}' removed.")
55+
if sha256_checksum != sha256(target=target):
56+
raise RuntimeError(
57+
f"Checksum of {target} does not match expected.\n"
58+
f"The download may be corrupted, please remove the corrupted resource and try again."
59+
)

environment.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

requirements.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--extra-index-url https://download.pytorch.org/whl/cu121 # CUDA 12.1 support for torch and torchvision.
2+
# --extra-index-url https://download.pytorch.org/whl/cu118 # CUDA 11.8 support for torch and torchvision.
3+
4+
h5py==3.11.0
5+
jupyter==1.0.0 # Jupyter system, including the notebook and the IPython kernel, all in one go.
6+
lightning==2.3.3
7+
matplotlib==3.9.1
8+
numpy==1.26.4
9+
pandas==2.2.2
10+
pillow==10.4.0
11+
requests==2.32.3
12+
scikit-learn==1.5.1
13+
scipy==1.14.0
14+
tabulate==0.9.0
15+
torch==2.3.1
16+
torchmetrics==1.4.0
17+
torchvision==0.18.1

requirements_dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
black[jupyter]==24.4.2
2+
flake8==7.1.0
3+
flake8-nb==0.5.3
4+
isort==5.13.2

0 commit comments

Comments
 (0)