You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DroneDetector/train_scripts/ImageDatasetCreate_spec_ima...

292 lines
8.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "4fdb98fc-65bb-467e-be0c-168fee9b0fca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cuda:0\n"
]
},
{
"data": {
"text/plain": [
"<contextlib.ExitStack at 0x76adb5fa4260>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import time\n",
"import io\n",
"import cv2\n",
"import copy\n",
"import os\n",
"from tqdm import tqdm\n",
"import torch.nn as nn\n",
"import torch\n",
"import torchvision\n",
"from torch.utils.data import Dataset\n",
"from torch import default_generator, randperm\n",
"from PIL import Image\n",
"#from torch._utils import _accumulate\n",
"import csv\n",
"from torch.utils.data.dataset import Subset\n",
"from scipy import ndimage\n",
"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
"print(device)\n",
"batch_size = 16\n",
"momentum=0.9\n",
"lr = 1e-3\n",
"import random\n",
"sub_sample = 0.5\n",
"import matplotlib\n",
"import gc\n",
"import torchsig.utils as u\n",
"import torchsig.transforms.transforms as T\n",
"from torchsig.transforms import functional as F\n",
"matplotlib.use('Agg')\n",
"import matplotlib as mpl\n",
"mpl.rcParams['agg.path.chunksize'] = 256*256\n",
"plt.ioff()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4848b066-2e09-4c1c-b8fa-8e3fa84d907a",
"metadata": {},
"outputs": [],
"source": [
"s = T.Spectrogram(nperseg=1024)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9267fbe1",
"metadata": {},
"outputs": [],
"source": [
"def sig2pic_with_spec(path_to_data, filename, specT=None,figsize=(16,16), dpi=16, resize = None):\n",
" try:\n",
" if specT is None:\n",
" specT = T.Spectrogram(nperseg=1024)\n",
" with open(path_to_data + filename, 'rb') as file:\n",
" tmp = np.frombuffer(file.read(), dtype=np.complex64)\n",
" signal = tmp\n",
"\n",
" #rint(\"vSE ok\")\n",
"\n",
" spectr = np.array(F.spectrogram(signal, fft_size=specT.fft_size, fft_stride=specT.fft_stride)[:, :figsize[0] * dpi])\n",
" #print(\"VSE OK\")\n",
" mag = np.abs(signal)\n",
" real = signal.real\n",
"\n",
" fig2 = plt.figure(figsize = figsize)\n",
" plt.axes(ylim=(-1, 1))\n",
"\n",
" plt.plot(real, color='black')\n",
" plt.gca().set_axis_off()\n",
" plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)\n",
" plt.margins(0,0)\n",
" buf2 = io.BytesIO()\n",
" fig2.savefig(buf2, format=\"png\", dpi=dpi)\n",
" buf2.seek(0)\n",
" img_arr2 = np.frombuffer(buf2.getvalue(), dtype=np.uint8)\n",
" buf2.close()\n",
" img2 = cv2.imdecode(img_arr2, 1)\n",
" img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)\n",
" plt.clf()\n",
" plt.cla()\n",
" plt.close()\n",
" plt.close(fig2)\n",
"\n",
" fig3 = plt.figure(figsize = figsize)\n",
" plt.axes(ylim=(-1, 1))\n",
"\n",
" plt.plot(mag, color='black')\n",
" plt.gca().set_axis_off()\n",
" plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)\n",
" plt.margins(0,0)\n",
" buf3 = io.BytesIO()\n",
" fig3.savefig(buf3, format=\"png\", dpi=dpi)\n",
" buf3.seek(0)\n",
" img_arr3 = np.frombuffer(buf3.getvalue(), dtype=np.uint8)\n",
" buf3.close()\n",
" img3 = cv2.imdecode(img_arr3, 1)\n",
" img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)\n",
" plt.clf()\n",
" plt.cla()\n",
" plt.close()\n",
" plt.close(fig3)\n",
"\n",
" if resize != None:\n",
" resized_real = cv2.resize(img2, resize)\n",
" resized_mag = cv2.resize(img3, resize)\n",
" resized_spectr = cv2.resize(spectr, resize)\n",
" img = np.asarray([resized_real, resized_mag, resized_spectr], dtype=np.float32)\n",
" return img\n",
" img = np.asarray([img2, img3, spectr], dtype=np.float32)\n",
" return img\n",
" except Exception as e:\n",
" print(str(e))\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "448da74a-e0ae-44d8-9877-8dd1f257a24f",
"metadata": {},
"outputs": [],
"source": [
"selected_freq=915\n",
"\n",
"path_to_binaries = f'/home/sibsci/dataset/drone/{selected_freq}'\n",
"path_to_pictures = f'/home/sibsci/dataset_img/drone/{selected_freq}_jpg'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ac4945a8-29c4-4da4-945f-08658953e3e5",
"metadata": {},
"outputs": [],
"source": [
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6f226f86-5d72-4573-8af6-750128b70263",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2026-04-06_11-37-43: 100%|██████████| 42/42 [00:11<00:00, 3.55it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dir: 2026-04-06_11-37-43 finished!\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"size = (256,256)\n",
"\n",
"if not os.path.exists(path_to_pictures):\n",
" os.makedirs(path_to_pictures,exist_ok=True)\n",
"\n",
"for subdir in os.listdir(path_to_binaries):\n",
" filepath = path_to_binaries + '/' + subdir + '/'\n",
"\n",
" if not os.path.isdir(filepath):\n",
" continue\n",
"\n",
" files = os.listdir(filepath)\n",
" k = max(1, int(len(files) * 0.04))\n",
" files = random.sample(files, k)\n",
" for file in tqdm(files, desc=subdir):\n",
" full_input_path = filepath + file\n",
"\n",
" if not os.path.isfile(full_input_path):\n",
" continue\n",
"\n",
" if file in ('run.log', 'reading_in_progress'):\n",
" continue\n",
"\n",
" save_base = subdir + '__' + file\n",
"\n",
" savepath = path_to_pictures + '/' + save_base + '.npy'\n",
" savepath_real_png = path_to_pictures + '/' + save_base + '_real' + '.png'\n",
" savepath_imag_png = path_to_pictures + '/' + save_base + '_imag' + '.png'\n",
" savepath_spec_png = path_to_pictures + '/' + save_base + '_spec' + '.png'\n",
"\n",
" if not os.path.exists(savepath):\n",
" img = sig2pic_with_spec(path_to_data=filepath, filename=file, specT=s, resize=size)\n",
" gc.collect()\n",
"\n",
" try:\n",
" plt.imshow(img[0])\n",
" plt.savefig(savepath_real_png)\n",
" plt.clf()\n",
" plt.cla()\n",
" plt.close()\n",
"\n",
" plt.imshow(img[1])\n",
" plt.savefig(savepath_imag_png)\n",
" plt.clf()\n",
" plt.cla()\n",
" plt.close()\n",
"\n",
" plt.imshow(img[2])\n",
" plt.savefig(savepath_spec_png)\n",
" plt.clf()\n",
" plt.cla()\n",
" plt.close()\n",
"\n",
" np.save(savepath, img)\n",
"\n",
" except Exception:\n",
" continue\n",
"\n",
" print('Dir: ', subdir , ' finished!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58ff5fbd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv-train",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}