Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RGPR data augmentation for person reid #504

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions configs/DukeMTMC/sbs_R50_rgpr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
_BASE_: ../Base-SBS.yml

DATASETS:
NAMES: ("DukeMTMC",)
TESTS: ("DukeMTMC",)

INPUT:
AUTOAUG:
ENABLED: False
RGPR:
ENABLED: True
PROB: 0.4

OUTPUT_DIR: logs/dukemtmc/sbs_R50_rgpr
4 changes: 4 additions & 0 deletions fastreid/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@
_C.INPUT.RPT = CN({"ENABLED": False})
_C.INPUT.RPT.PROB = 0.5

# Random Grayscale Patch Replace
_C.INPUT.RGPR = CN({"ENABLED": False})
_C.INPUT.RGPR.PROB = 0.4

# -----------------------------------------------------------------------------
# Dataset
# -----------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions fastreid/data/transforms/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .transforms import *
from .autoaugment import AutoAugment
from .trans_gray import RandomGrayscalePatchReplace


def build_transforms(cfg, is_train=True):
Expand Down Expand Up @@ -59,12 +60,20 @@ def build_transforms(cfg, is_train=True):
do_rpt = cfg.INPUT.RPT.ENABLED
rpt_prob = cfg.INPUT.RPT.PROB

# Random Grayscale Patch Replace
do_rgpr = cfg.INPUT.RGPR.ENABLED
rgpr_prob = cfg.INPUT.RGPR.PROB

if do_autoaug:
res.append(T.RandomApply([AutoAugment()], p=autoaug_prob))

if size_train[0] > 0:
res.append(T.Resize(size_train[0] if len(size_train) == 1 else size_train, interpolation=3))

# after resize and before crop or flip
if do_rgpr:
res.append(RandomGrayscalePatchReplace(rgpr_prob))

if do_crop:
res.append(T.RandomResizedCrop(size=crop_size[0] if len(crop_size) == 1 else crop_size,
interpolation=3,
Expand Down
50 changes: 50 additions & 0 deletions fastreid/data/transforms/trans_gray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# encoding: utf-8

import math
from PIL import Image
import random
import numpy as np
import random
# This is the code of Random Grayscale Patch Replace


class RandomGrayscalePatchReplace(object):

def __init__(self, probability=0.2, sl=0.02, sh=0.4, r1=0.3):
self.probability = probability
self.sl = sl
self.sh = sh
self.r1 = r1

def __call__(self, img):

new = img.convert("L") # Convert from here to the corresponding grayscale image
np_img = np.array(new, dtype=np.uint8)
img_gray = np.dstack([np_img, np_img, np_img])

if random.uniform(0, 1) >= self.probability:
return img

for attempt in range(100):
area = img.size[0] * img.size[1]
target_area = random.uniform(self.sl, self.sh) * area
aspect_ratio = random.uniform(self.r1, 1 / self.r1)

h = int(round(math.sqrt(target_area * aspect_ratio)))
w = int(round(math.sqrt(target_area / aspect_ratio)))

if w < img.size[1] and h < img.size[0]:
x1 = random.randint(0, img.size[0] - h)
y1 = random.randint(0, img.size[1] - w)
img = np.asarray(img).astype('float')

img[y1:y1 + h, x1:x1 + w, 0] = img_gray[y1:y1 + h, x1:x1 + w, 0]
img[y1:y1 + h, x1:x1 + w, 1] = img_gray[y1:y1 + h, x1:x1 + w, 1]
img[y1:y1 + h, x1:x1 + w, 2] = img_gray[y1:y1 + h, x1:x1 + w, 2]

img = Image.fromarray(img.astype('uint8'))

return img

return img