Skip to content

Instantly share code, notes, and snippets.

@Dref360
Last active December 18, 2019 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dref360/d4eafccdd1c9d6e87764c43da50ffb19 to your computer and use it in GitHub Desktop.
Save Dref360/d4eafccdd1c9d6e87764c43da50ffb19 to your computer and use it in GitHub Desktop.
Simple example for a DA pipeline using Sequences
import cv2
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import Sequence
class MySequence(Sequence):
def __init__(self):
self.path = '~/Images/cat.jpg'
self.imgaug = ImageDataGenerator(rotation_range=20,
rescale=1.,
width_shift_range=10)
def __len__(self):
return 10
def __getitem__(self, idx):
X = np.array([cv2.resize(cv2.imread(self.path), (100, 100)) for _ in range(10)]).astype(np.float32) # Fake batch of cats
y = np.copy(X)
for i in range(len(X)):
params = self.imgaug.get_random_transform(X[i].shape)
X[i] = self.imgaug.apply_transform(self.imgaug.standardize(X[i]), params)
y[i] = self.imgaug.apply_transform(self.imgaug.standardize(y[i]), params)
return X, y
if __name__ == '__main__':
sequence = MySequence()
for X, y in sequence:
img = None
for xi, yi in zip(X, y):
im = np.concatenate((xi, yi), 0)
if img is None:
img = im
else:
img = np.concatenate((img, im), 1)
cv2.imshow('wow', img.astype(np.uint8))
cv2.waitKey(1000)
@pkill37
Copy link

pkill37 commented Dec 22, 2018

y[i] = self.imgaug.apply_transform(self.imgaug.standardize(y[i]), params)

Should you ever standardize y? I think it destroys the meaning of the target label.

For example, in my current application, y is a binary mask in the context of background/foreground segmentation. If you standardize y then its pixel values will be arbitrary floats rather than 1 or 0. And then metrics will always report zero accuracy because the model's prediction for each pixel value (1 or 0) always misses the new standardized values (arbitrary floats).

Is there any case where it makes sense to standardize y?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment