Skip to content

Commit

Permalink
Restructuring of vision modules
Browse files Browse the repository at this point in the history
  • Loading branch information
juliawerner authored and Christoph Gerum committed Nov 21, 2022
1 parent 3f273ab commit 1a2ae37
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 1,132 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ nas_results/

.nv
.tmp/
tmp/

.torch
type_info.json
*.log
.pytest_cache
2 changes: 1 addition & 1 deletion experiments/kvasir/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defaults:
- base_config
- override dataset: kvasir_capsule # Dataset configuration name
- override features: identity # Feature extractor configuration name (use identity for vision datasets)
- override model: timm_resnet152 # Neural network name (for now timm_resnet50 or timm_efficientnet_lite1)
- override model: timm_mobilenetv3_small_075 # Neural network name (for now timm_resnet50 or timm_efficientnet_lite1)
- override scheduler: 1cycle # learning rate scheduler config name
- override optimizer: adamw # Optimizer config name
- override normalizer: null # Feature normalizer (used for quantized neural networks)
Expand Down
2 changes: 1 addition & 1 deletion experiments/kvasir_ae/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defaults:
- base_config
- override dataset: kvasir_unlabeled # Dataset configuration name
- override features: identity # Feature extractor configuration name (use identity for vision datasets)
- override model: timm_resnet152 # Neural network name (for now timm_resnet50 or timm_efficientnet_lite1)
- override model: timm_mobilenetv3_small_075 # Neural network name (for now timm_resnet50 or timm_efficientnet_lite1)
- override scheduler: 1cycle # learning rate scheduler config name
- override optimizer: adamw # Optimizer config name
- override normalizer: null # Feature normalizer (used for quantized neural networks)
Expand Down
2 changes: 1 addition & 1 deletion external/hannah-tvm
1 change: 1 addition & 0 deletions hannah/conf/dataset/fake2d.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
data_folder: ${hydra:runtime.cwd}/datasets/
cls: hannah.datasets.vision.FakeDataset
dataset: fake2d
resolution: 320
num_classes: 10
27 changes: 27 additions & 0 deletions hannah/conf/module/augmentation/kvasir_ae_augment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##
## Copyright (c) 2022 University of Tübingen.
##
## This file is part of hannah.
## See https://atreus.informatik.uni-tuebingen.de/ties/ai/hannah/hannah for further info.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
batch_augment:
transforms:
RandomRotation:
degrees: 180
p: 1.0
RandomVerticalFlip:
p: 0.5
RandomHorizontalFlip:
p: 0.5
33 changes: 33 additions & 0 deletions hannah/conf/module/augmentation/kvasir_ssl_augment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##
## Copyright (c) 2022 University of Tübingen.
##
## This file is part of hannah.
## See https://atreus.informatik.uni-tuebingen.de/ties/ai/hannah/hannah for further info.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
batch_augment:
transforms:
RandomGaussianNoise:
p: 0.5
RandomPosterize:
p: 0.3
RandomSharpness:
p: 0.3
RandomRotation:
degrees: 180
p: 1.0
RandomVerticalFlip:
p: 0.5
RandomHorizontalFlip:
p: 0.5
25 changes: 25 additions & 0 deletions hannah/conf/module/augmentation/mixup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
## Copyright (c) 2022 University of Tübingen.
##
## This file is part of hannah.
## See https://atreus.informatik.uni-tuebingen.de/ties/ai/hannah/hannah for further info.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
mixup:
mixup_alpha: 0.1 # mixup alpha value, mixup is active if > 0.
cutmix_alpha: 0.1 # cutmix alpha value, cutmix is active if > 0.
prob: 1.0 # Probability of performing mixup or cutmix when either/both is enabled
switch_prob: 0.0 # Probability of switching to cutmix when both mixup and cutmix enabled
mode: pair # batch, pair or elem - wise
label_smoothing : 0.0 # Apply label smoothing
36 changes: 36 additions & 0 deletions hannah/conf/module/augmentation/rand_augment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
##
## Copyright (c) 2022 University of Tübingen.
##
## This file is part of hannah.
## See https://atreus.informatik.uni-tuebingen.de/ties/ai/hannah/hannah for further info.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
rand_augment:
N: 1
M: 1
probs:
Identity : 0.5
ShearX: 0.5
ShearY: 0.5
TranslateX: 0.5
TranslateY : 0.5
Rotate : 0.5
Brightness : 0.0
Color : 0.5
Contrast : 0.0
Sharpness : 0.5
Posterize : 0.0
Solarize : 0.0
AutoContrast : 0.0
Equalize : 0.1
16 changes: 0 additions & 16 deletions hannah/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ def mean(self) -> Optional[Tuple[int, ...]]:
"""Returns channel-wise means for dataset if applicable"""
return None

def get_mixup_fn(
self,
) -> Optional[
Callable[[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]
]:
"""Returns an optional dataset specific mixup function"""
return None

def get_batch_augment_fn(
self,
) -> Optional[
Callable[[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]
]:
"""Returns an optional dataset specific batch augment function"""
return None

@property
def weights(self) -> Optional[List[float]]:
"""Class weights for weighted sampling"""
Expand Down
53 changes: 14 additions & 39 deletions hannah/datasets/vision/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from collections import Counter, namedtuple
from typing import Dict, List

import albumentations as A
import cv2
import numpy as np
import pandas as pd
Expand All @@ -32,7 +33,6 @@
from sklearn.model_selection import train_test_split
from timm.data.mixup import Mixup

import albumentations as A
from hannah.modules.augmentation import rand_augment
from hannah.modules.augmentation.batch_augmentation import BatchAugmentationPipeline

Expand All @@ -41,38 +41,18 @@
logger = logging.getLogger(__name__)


class AugmentationMixin:
def get_mixup_fn(self):
mixup = None

augmentation_config = self.config.get("augmentation", None)
if augmentation_config is not None:
mixup_config = augmentation_config.get("mixup", None)
if mixup_config is not None:
mixup_config = Mixup(**mixup_config)

return mixup

def get_batch_augment_fn(self):

batch_augment = None

augmentation_config = self.config.get("augmentation", None)
if augmentation_config is not None:
batch_augment_config = self.config.get("batch_augment", None)
if batch_augment_config is not None:

batch_augment = BatchAugmentationPipeline(
transforms=batch_augment_config.get("batch_transforms", [])
)

return batch_augment


class VisionDatasetBase(AbstractDataset, AugmentationMixin):
class VisionDatasetBase(AbstractDataset):
def __init__(self, config):
self.config = config

@property
def std(self):
pass

@property
def mean(self):
pass


class TorchvisionDatasetBase(VisionDatasetBase):
"""Wrapper around torchvision classification datasets"""
Expand All @@ -88,7 +68,7 @@ def class_counts(self):

def __getitem__(self, index):
data, target = self.dataset[index]
data = np.array(data)
data = np.array(data) / 255
if self.transform:
data = self.transform(image=data)["image"]
return data, target
Expand All @@ -115,19 +95,14 @@ def __init__(self, X, y, classes, transform=None):
self.X = X
self.y = y
self.classes = classes
self.transform = (
transform if transform else A.Compose([A.Normalize(), ToTensorV2()])
)
self.transform = transform if transform else A.Compose([ToTensorV2()])
self.label_to_index = {k: v for v, k in enumerate(classes)}

def __getitem__(self, index):
image = cv2.imread(str(self.X[index]))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32) / 255
label = self.y[index]
if self.transform:
data = self.transform(image=image)["image"]
else:
data = image
data = self.transform(image=image)["image"]
target = self.label_to_index[label]
return {"data": data, "labels": target}

Expand Down
30 changes: 3 additions & 27 deletions hannah/datasets/vision/cifar.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,6 @@ def class_names(self):
def splits(cls, config):
data_folder = config.data_folder
root_folder = os.path.join(data_folder, "CIFAR10")

# print(loaded_transform)
train_transform = A.Compose(
[
A.SmallestMaxSize(max_size=32),
A.ShiftScaleRotate(
shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.5
),
A.RandomCrop(height=32, width=32),
A.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=0.5),
A.RandomBrightnessContrast(p=0.5),
A.Normalize(mean=(0.4914, 0.4822, 0.4465), std=(0.247, 0.243, 0.261)),
ToTensorV2(),
]
)

val_transform = A.Compose(
[
A.SmallestMaxSize(max_size=32),
A.Normalize(mean=(0.4914, 0.4822, 0.4465), std=(0.247, 0.243, 0.261)),
ToTensorV2(),
]
)

test_set = torchvision.datasets.CIFAR10(
root_folder, train=False, download=False
)
Expand All @@ -97,7 +73,7 @@ def splits(cls, config):
train_set, val_set = data.random_split(train_val_set, split_sizes)

return (
cls(config, train_set, train_transform),
cls(config, val_set, val_transform),
cls(config, test_set, val_transform),
cls(config, train_set),
cls(config, val_set),
cls(config, test_set),
)
26 changes: 1 addition & 25 deletions hannah/datasets/vision/kvasir.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
from collections import Counter, namedtuple
from typing import Dict, List

import albumentations as A
import cv2
import pandas as pd
import requests
import torchvision
from albumentations.pytorch import ToTensorV2
from sklearn.model_selection import train_test_split

import albumentations as A
from hannah.modules.augmentation import rand_augment

from .base import ImageDatasetBase
Expand Down Expand Up @@ -104,27 +104,6 @@ def splits(cls, config):
if isinstance(resolution, int):
resolution = (resolution, resolution)

res_x, res_y = resolution

# FIXME(gerum): add back rand augment
train_transform = A.Compose(
[
# A.Resize(int(res_x*1.1), int(res_y*1.1)),
# A.RandomResizedCrop(res_x, res_y),
A.Resize(res_x, res_y),
A.Normalize(mean=config.normalize.mean, std=config.normalize.std),
ToTensorV2(),
]
)

test_transform = A.Compose(
[
A.Resize(res_x, res_y),
A.Normalize(mean=config.normalize.mean, std=config.normalize.std),
ToTensorV2(),
]
)

label_to_folder = {
"Angiectasia": "Angiectasia",
"Pylorus": "Pylorus",
Expand Down Expand Up @@ -232,18 +211,15 @@ def relable_anomaly(X):
train_images,
train_labels,
classes,
transform=train_transform,
),
cls(
val_images,
val_labels,
classes,
transform=test_transform,
),
cls(
test_images,
test_labels,
classes,
transform=test_transform,
),
)
Loading

0 comments on commit 1a2ae37

Please sign in to comment.