-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from yu4u/feature-datagen
Feature datagen
- Loading branch information
Showing
5 changed files
with
170 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import numpy as np | ||
|
||
|
||
class MixupGenerator(): | ||
def __init__(self, X_train, y_train, batch_size=32, alpha=0.2, shuffle=True, datagen=None): | ||
self.X_train = X_train | ||
self.y_train = y_train | ||
self.batch_size = batch_size | ||
self.alpha = alpha | ||
self.shuffle = shuffle | ||
self.sample_num = len(X_train) | ||
self.datagen = datagen | ||
|
||
def __call__(self): | ||
while True: | ||
indexes = self.__get_exploration_order() | ||
itr_num = int(len(indexes) // (self.batch_size * 2)) | ||
|
||
for i in range(itr_num): | ||
batch_ids = indexes[i * self.batch_size * 2:(i + 1) * self.batch_size * 2] | ||
X, y = self.__data_generation(batch_ids) | ||
|
||
yield X, y | ||
|
||
def __get_exploration_order(self): | ||
indexes = np.arange(self.sample_num) | ||
|
||
if self.shuffle: | ||
np.random.shuffle(indexes) | ||
|
||
return indexes | ||
|
||
def __data_generation(self, batch_ids): | ||
_, h, w, c = self.X_train.shape | ||
l = np.random.beta(self.alpha, self.alpha, self.batch_size) | ||
X_l = l.reshape(self.batch_size, 1, 1, 1) | ||
y_l = l.reshape(self.batch_size, 1) | ||
|
||
X1 = self.X_train[batch_ids[:self.batch_size]] | ||
X2 = self.X_train[batch_ids[self.batch_size:]] | ||
X = X1 * X_l + X2 * (1 - X_l) | ||
|
||
if self.datagen: | ||
for i in range(self.batch_size): | ||
X[i] = self.datagen.random_transform(X[i]) | ||
X[i] = self.datagen.standardize(X[i]) | ||
|
||
if isinstance(self.y_train, list): | ||
y = [] | ||
|
||
for y_train_ in self.y_train: | ||
y1 = y_train_[batch_ids[:self.batch_size]] | ||
y2 = y_train_[batch_ids[self.batch_size:]] | ||
y.append(y1 * y_l + y2 * (1 - y_l)) | ||
else: | ||
y1 = self.y_train[batch_ids[:self.batch_size]] | ||
y2 = self.y_train[batch_ids[self.batch_size:]] | ||
y = y1 * y_l + y2 * (1 - y_l) | ||
|
||
return X, y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import numpy as np | ||
|
||
|
||
def get_random_eraser(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255): | ||
def eraser(input_img): | ||
img_h, img_w, _ = input_img.shape | ||
p_1 = np.random.rand() | ||
|
||
if p_1 > p: | ||
return input_img | ||
|
||
while True: | ||
s = np.random.uniform(s_l, s_h) * img_h * img_w | ||
r = np.random.uniform(r_1, r_2) | ||
w = int(np.sqrt(s / r)) | ||
h = int(np.sqrt(s * r)) | ||
left = np.random.randint(0, img_w) | ||
top = np.random.randint(0, img_h) | ||
|
||
if left + w <= img_w and top + h <= img_h: | ||
break | ||
|
||
c = np.random.uniform(v_l, v_h) | ||
input_img[top:top + h, left:left + w, :] = c | ||
|
||
return input_img | ||
|
||
return eraser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters