1
0
Fork 0

Add random erasing

master
Petr Masopust 6 years ago
parent 25ac925287
commit 069ade5a0a
  1. 1
      README.md
  2. 1
      vectorizer/README.md
  3. 14
      vectorizer/identification/dataloader.py
  4. 13
      vectorizer/identification/losses.py
  5. 13
      vectorizer/identification/train.py
  6. 3
      vectorizer/recognition/train.py

@ -98,6 +98,7 @@ Papers:
* [ArcFace: Additive Angular Margin Loss for Deep Face Recognition](https://arxiv.org/abs/1801.07698) * [ArcFace: Additive Angular Margin Loss for Deep Face Recognition](https://arxiv.org/abs/1801.07698)
* [SphereFace: Deep Hypersphere Embedding for Face Recognition](https://arxiv.org/abs/1704.08063) * [SphereFace: Deep Hypersphere Embedding for Face Recognition](https://arxiv.org/abs/1704.08063)
* [CosFace: Large Margin Cosine Loss for Deep Face Recognition](https://arxiv.org/abs/1801.09414) * [CosFace: Large Margin Cosine Loss for Deep Face Recognition](https://arxiv.org/abs/1801.09414)
* [Random Erasing Data Augmentation](https://arxiv.org/abs/1708.04896)
## Licensing ## Licensing

@ -154,6 +154,7 @@ Papers:
* [ArcFace: Additive Angular Margin Loss for Deep Face Recognition](https://arxiv.org/abs/1801.07698) * [ArcFace: Additive Angular Margin Loss for Deep Face Recognition](https://arxiv.org/abs/1801.07698)
* [SphereFace: Deep Hypersphere Embedding for Face Recognition](https://arxiv.org/abs/1704.08063) * [SphereFace: Deep Hypersphere Embedding for Face Recognition](https://arxiv.org/abs/1704.08063)
* [CosFace: Large Margin Cosine Loss for Deep Face Recognition](https://arxiv.org/abs/1801.09414) * [CosFace: Large Margin Cosine Loss for Deep Face Recognition](https://arxiv.org/abs/1801.09414)
* [Random Erasing Data Augmentation](https://arxiv.org/abs/1708.04896)
## Licensing ## Licensing

@ -27,6 +27,7 @@ from torch.utils.data import Dataset
from torch.utils.data.sampler import Sampler from torch.utils.data.sampler import Sampler
from PIL import Image, ImageEnhance, ImageFilter from PIL import Image, ImageEnhance, ImageFilter
from torchvision import transforms as T
class CSVDataset(Dataset): class CSVDataset(Dataset):
@ -402,6 +403,19 @@ class Resizer(object):
return {'img': new_image, 'annot': annots, 'scale': scale} return {'img': new_image, 'annot': annots, 'scale': scale}
class RandomEraser(object):
def __init__(self):
self.eraser = T.RandomErasing()
def __call__(self, sample):
image, annots, scales = sample['img'], sample['annot'], sample['scale']
image = self.eraser(image)
sample = {'img': image, 'annot': annots, 'scale': scales}
return sample
class Augmenter(object): class Augmenter(object):
"""Convert ndarrays in sample to Tensors.""" """Convert ndarrays in sample to Tensors."""

@ -23,14 +23,11 @@ import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
def memprint(a): def calc_iou(a, b, is_cuda=False):
print(a.shape)
print(a.element_size() * a.nelement())
def calc_iou(a, b):
step = 20 step = 20
IoU = torch.zeros((len(a), len(b))).cuda() IoU = torch.zeros((len(a), len(b)))
if is_cuda:
IoU = IoU.cuda()
step_count = int(len(b) / step) step_count = int(len(b) / step)
if len(b) % step != 0: if len(b) % step != 0:
step_count += 1 step_count += 1
@ -127,7 +124,7 @@ class FocalLoss(nn.Module):
classification = torch.clamp(classification, 1e-4, 1.0 - 1e-4) classification = torch.clamp(classification, 1e-4, 1.0 - 1e-4)
IoU = calc_iou(anchor, bbox_annotation[:, :4]) # num_anchors x num_annotations IoU = calc_iou(anchor, bbox_annotation[:, :4], self.is_cuda) # num_anchors x num_annotations
IoU_max, IoU_argmax = torch.max(IoU, dim=1) # num_anchors x 1 IoU_max, IoU_argmax = torch.max(IoU, dim=1) # num_anchors x 1

@ -32,7 +32,7 @@ from identification.model_level_attention import resnet18, resnet34, resnet50, r
from torch.utils.data import DataLoader from torch.utils.data import DataLoader
from identification.csv_eval import evaluate from identification.csv_eval import evaluate
from identification.dataloader import WIDERDataset, AspectRatioBasedSampler, collater, Resizer, Augmenter, Normalizer, \ from identification.dataloader import WIDERDataset, AspectRatioBasedSampler, collater, Resizer, Augmenter, Normalizer, \
CSVDataset CSVDataset, RandomEraser
is_cuda = torch.cuda.is_available() is_cuda = torch.cuda.is_available()
print('CUDA available: {}'.format(is_cuda)) print('CUDA available: {}'.format(is_cuda))
@ -75,10 +75,10 @@ def main(args=None):
# Create the data loaders # Create the data loaders
if parser.wider_train is None: if parser.wider_train is None:
dataset_train = CSVDataset(train_file=parser.csv_train, class_list=parser.csv_classes, dataset_train = CSVDataset(train_file=parser.csv_train, class_list=parser.csv_classes,
transform=transforms.Compose([Resizer(), Augmenter(), Normalizer()])) transform=transforms.Compose([Resizer(), Augmenter(), Normalizer(), RandomEraser()]))
else: else:
dataset_train = WIDERDataset(train_file=parser.wider_train, img_prefix=parser.wider_train_prefix, dataset_train = WIDERDataset(train_file=parser.wider_train, img_prefix=parser.wider_train_prefix,
transform=transforms.Compose([Resizer(), Augmenter(), Normalizer()])) transform=transforms.Compose([Resizer(), Augmenter(), Normalizer(), RandomEraser()]))
if parser.wider_val is None: if parser.wider_val is None:
if parser.csv_val is None: if parser.csv_val is None:
@ -175,9 +175,10 @@ def main(args=None):
img_data = img_data.cuda() img_data = img_data.cuda()
annot_data = annot_data.cuda() annot_data = annot_data.cuda()
print("GPU memory allocated: %d max memory allocated: %d memory cached: %d max memory cached: %d" % ( print("GPU memory allocated: %d max memory allocated: %d memory cached: %d max memory cached: %d" % (
torch.cuda.memory_allocated() / 1024 ** 2, torch.cuda.max_memory_allocated() / 1024 ** 2, torch.cuda.memory_allocated() / 1024 ** 2, torch.cuda.max_memory_allocated() / 1024 ** 2,
torch.cuda.memory_cached() / 1024 ** 2, torch.cuda.max_memory_cached() / 1024 ** 2)) torch.cuda.memory_cached() / 1024 ** 2, torch.cuda.max_memory_cached() / 1024 ** 2))
classification_loss, regression_loss, mask_loss = retinanet([img_data, annot_data]) classification_loss, regression_loss, mask_loss = retinanet([img_data, annot_data])
del img_data del img_data

@ -50,7 +50,8 @@ class Dataset(torch.utils.data.Dataset):
T.RandomResizedCrop(imagesize), T.RandomResizedCrop(imagesize),
T.RandomHorizontalFlip(), T.RandomHorizontalFlip(),
T.ToTensor(), T.ToTensor(),
normalize normalize,
T.RandomErasing()
]) ])
def __getitem__(self, index): def __getitem__(self, index):

Loading…
Cancel
Save