Skip to content

Commit

Permalink
optimizer code format (#106)
Browse files Browse the repository at this point in the history
* fix metric's dataset metainfo is none bug

* fix image size type is List bug

* fix code format no pass

* optimizer code format
  • Loading branch information
mjq2020 authored Jul 11, 2023
1 parent 77a9355 commit 6974d30
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 231 deletions.
40 changes: 11 additions & 29 deletions edgelab/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
from .cocodataset import CustomCocoDataset
from .data_preprocessors import *
from .fomodataset import FomoDatasets
from .meter import MeterData
from .pipelines import *
from .sensordataset import SensorDataset
from .speechcommand import Speechcommand
from .transforms import *
from .utils.functions import fomo_collate
from .vocdataset import CustomVocdataset
from .yolodataset import CustomYOLOv5CocoDataset

__all__ = [
'Speechcommand',
'MeterData',
'AudioAugs',
'CustomCocoDataset',
'CustomVocdataset',
'FomoDatasets',
'SensorDataset',
'RandomResizedCrop',
'fomo_collate',
'ETADataPreprocessor',
'CustomYOLOv5CocoDataset',
'SensorDataPreprocessor',
'PackSensorInputs',
'LoadSensorFromFile',
'Bbox2FomoMask',
]
from .cocodataset import * # noqa
from .data_preprocessors import * # noqa
from .fomodataset import * # noqa
from .meter import * # noqa
from .pipelines import * # noqa
from .sensordataset import * # noqa
from .speechcommand import * # noqa
from .transforms import * # noqa
from .utils.functions import * # noqa
from .vocdataset import * # noqa
from .yolodataset import * # noqa
137 changes: 0 additions & 137 deletions edgelab/datasets/builder.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import json
from typing import Optional, Union
from typing import Optional

import numpy as np
import torch
from mmcls.models.utils.batch_augments import RandomBatchAugment
from mmcls.structures import (
ClsDataSample,
Expand All @@ -12,10 +9,8 @@
stack_batch_scores,
tensor_split,
)
from mmengine.logging import MessageHub
from mmengine.model.base_model.data_preprocessor import BaseDataPreprocessor

from edgelab.engine.utils.batch_augs import BatchAugs
from edgelab.registry import MODELS


Expand Down
1 change: 0 additions & 1 deletion edgelab/datasets/data_preprocessors/audio_augs.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def __init__(self, fs, p=0.5, fc_lp=None, fc_hp=None):

def __call__(self, sample):
if random.random() < self.p:
a = 0.25
if random.random() < 0.5:
fc = 0.5 + random.random() * 0.25
filt = scipy.signal.firwin(self.num_taps, fc, window='hamming')
Expand Down
41 changes: 2 additions & 39 deletions edgelab/datasets/fomodataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import torch
import torchvision
from mmdet.datasets.coco import CocoDataset
from mmengine.dataset.base_dataset import BaseDataset
from mmengine.registry import DATASETS
from sklearn.metrics import confusion_matrix
from torch.utils.data import Dataset
from torchvision.transforms import ToTensor

from .pipelines.composition import AlbCompose

Expand Down Expand Up @@ -74,42 +71,8 @@ def __len__(self):
return len(self.data)

def __getitem____(self, index):
image, ann = self.data[index]

self.prepare_data(idx=index)
image = np.asarray(image)
return self.pipeline()

bboxes = []
labels = []
min_hw_pixels = 2
for annotation in ann:
# coco annotation specific https://cocodataset.org/#format-data
x, y, width, height = annotation['bbox'][:4]
if width == 0:
width += min_hw_pixels
if height == 0:
height += min_hw_pixels
annotation['bbox'][:4] = [x, y, width, height]
bboxes.append(annotation['bbox'])
labels.append(annotation['category_id'])

bboxes = np.array(bboxes)
labels = np.array(labels)

trans_param = {'image': image, 'bboxes': bboxes, self.bbox_params['label_fields'][0]: labels}

result = self.transform(**trans_param)
image = result['image']
bboxes = result['bboxes']
labels = result[self.bbox_params['label_fields'][0]]

H, W, C = image.shape
bbl = []
for bbox, l in zip(bboxes, labels):
bbl.append([0, l, (bbox[0] + (bbox[2] / 2)) / W, (bbox[1] + (bbox[3] / 2)) / H, bbox[2] / W, bbox[3] / H])

return {'inputs': ToTensor()(image), 'data_samples': torch.from_numpy(np.asarray(bbl))}
result = self.prepare_data(idx=index)
return result

def get_ann_info(self, idx):
ann = self.__getitem__[idx]['target']
Expand Down
2 changes: 1 addition & 1 deletion edgelab/datasets/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def calc_angle(x1, y1, x2, y2):
z = math.sqrt(x * x + y * y)
try:
angle = math.acos((z**2 + 1 - (x - 1) ** 2 - y**2) / (2 * z * 1)) / math.pi * 180
except:
except Exception:
angle = 0

if y < 0:
Expand Down
4 changes: 2 additions & 2 deletions edgelab/datasets/pipelines/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .albu import *
from .albu import * # noqa
from .audio_augs import AudioAugs
from .transforms import Bbox2FomoMask

__all__ = ['AudioAugs', 'RandomResizedCrop', 'Bbox2FomoMask']
__all__ = ['AudioAugs', 'Bbox2FomoMask']
1 change: 0 additions & 1 deletion edgelab/datasets/pipelines/audio_augs.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def __init__(self, fs, p=0.5, fc_lp=None, fc_hp=None):

def __call__(self, sample):
if random.random() < self.p:
a = 0.25
if random.random() < 0.5:
fc = 0.5 + random.random() * 0.25
filt = scipy.signal.firwin(self.num_taps, fc, window='hamming')
Expand Down
4 changes: 1 addition & 3 deletions edgelab/datasets/sensordataset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import glob
import json
import os
from typing import List, Optional, Sequence, Tuple, Union
from typing import Optional, Union

import numpy as np
from mmcls.datasets import CustomDataset

from edgelab.registry import DATASETS
Expand Down
2 changes: 1 addition & 1 deletion edgelab/datasets/speechcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def load_meta_file(self, root, f_meta):
def _get_labels(self, root):
f_names = glob.glob(root + f'{sep}**{sep}*.wav')
self.labels = sorted(list(set([f.split(f'{os.path.sep}')[-2] for f in f_names])))
self.labels = sorted([l for l in self.labels if l in self.words])
self.labels = sorted([label for label in self.labels if label in self.words])

def __getitem__(self, index):
fname = self.audio_files[index]
Expand Down
3 changes: 0 additions & 3 deletions edgelab/datasets/transforms/formatting.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from collections import defaultdict
from collections.abc import Sequence
from functools import partial

import numpy as np
import torch
from mmcls.structures import ClsDataSample
from mmcv.transforms import BaseTransform
from mmengine.utils import is_str
from PIL import Image

from edgelab.registry import TRANSFORMS

Expand Down
1 change: 0 additions & 1 deletion edgelab/datasets/transforms/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import warnings
from typing import Optional

import mmcv
import mmengine.fileio as fileio
import numpy as np
from mmcv.transforms.base import BaseTransform
Expand Down
1 change: 0 additions & 1 deletion edgelab/datasets/utils/audio_augs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def __init__(self, fs, p=0.5, fc_lp=None, fc_hp=None):

def __call__(self, sample):
if random.random() < self.p:
a = 0.25
if random.random() < 0.5:
fc = 0.5 + random.random() * 0.25
filt = scipy.signal.firwin(self.num_taps, fc, window='hamming')
Expand Down
4 changes: 1 addition & 3 deletions edgelab/datasets/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ def defile(files, store_dir):
res.append(cmd)


def download(links: List or AnyStr, store_path: AnyStr or __path__, unzip_dir=None):
def download(links: List or AnyStr, store_path: AnyStr, unzip_dir=None):
if isinstance(links, str):
links = [links]
os.chdir(store_path)
if not os.path.exists('download'):
os.mkdir('download')
os.chdir('download')

print(links)
print(store_path)
for link in links:
file_name = link.split('/')[-1]
unzip = check_compress(file_name)
Expand Down
6 changes: 3 additions & 3 deletions edgelab/datasets/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@FUNCTIONS.register_module()
def fomo_collate(batch):
img, label = [x['inputs'] for x in batch], [y['data_samples'] for y in batch]
for i, l in enumerate(label):
if l.shape[0] > 0:
l[:, 0] = i
for i, label in enumerate(label):
if label.shape[0] > 0:
label[:, 0] = i
return dict(inputs=torch.stack(img), data_samples=[DetDataSample(labels=torch.cat(label, 0))])

0 comments on commit 6974d30

Please sign in to comment.