質問編集履歴

4

ソースコードの修正

2023/01/09 10:11

投稿

semisemi
semisemi

スコア18

test CHANGED
File without changes
test CHANGED
@@ -11,178 +11,52 @@
11
11
 
12
12
  ### 発生している問題
13
13
 
14
- 今の変換処理が、ImageDataGeneratorの変換→Mixup→Random Erasingの順処理され、Mixupは毎回発生するよになっています。
14
+ apply_transformmixupまくできていない
15
15
 
16
16
  ### 該当のソースコード
17
17
 
18
18
  ```python
19
+ from random_eraser import get_random_eraser
20
+
19
- class MyImagedatagenerator(image.ImageDataGenerator):
21
+ class MyImageDataGenerator(ImageDataGenerator):
22
+ def __init__(self,mix_up_alpha = 0.0, *args, **kwargs):
23
+ self.random_eraser = get_random_eraser()
24
+ super().__init__(*args, **kwargs)
25
+ assert mix_up_alpha >= 0.0
26
+ self.mix_up_alpha = mix_up_alpha
27
+
28
+ ##mixup用
29
+ def img(gen: ImageDataGenerator):
30
+ for x, y in gen:
31
+ yield x, y
20
32
 
21
- def __init__(self,
22
- random_erasing_probability = None,
23
- random_erasing_area_ratio = [0.02, 0.4],
33
+ def mix_up(self, X1, y1, X2, y2):
34
+ assert X1.shape[0] == y1.shape[0] == X2.shape[0] == y2.shape[0]
35
+ batch_size = X1.shape[0]
24
- random_erasing_aspect_ratio = [0.3, 1/0.3],
36
+ l = np.random.beta(self.mix_up_alpha, self.mix_up_alpha, batch_size)
25
- random_erasing_mask_value = [0, 1],
37
+ X_l = l.reshape(batch_size, 1, 1, 1)
26
- mixup_alpha=None,
27
- *args, **kwargs
28
- ):
29
- super().__init__(*args, **kwargs)
38
+ y_l = l.reshape(batch_size, 1)
30
- #random erasingを発生させる確率
39
+ X = X1 * X_l + X2 * (1-X_l)
31
- self.random_erasing_probability = random_erasing_probability
32
- #マスクする面積の範囲(相対値で表現)
40
+ y = y1 * y_l + y2 * (1-y_l)
41
+ return X, y
42
+
33
- self.random_erasing_area_ratio = random_erasing_area_ratio
43
+ def apply_transform(self, x, transform_parameters):
34
- #アスペクト比の範囲
44
+
35
- self.random_erasing_aspect_ratio = random_erasing_aspect_ratio
36
- #マスクする値の取りうる範囲
37
- self.random_erasing_mask_value = random_erasing_mask_value
38
-
39
- self.mixup_alpha = mixup_alpha
45
+ x = self.mix_up_alpha(x) # 先に処理する
40
-
41
-
42
- def random_erasing(self, X, erasing_probability, erasing_area_ratio_range,
46
+ x = self.random_eraser(x)
43
- erasing_aspect_ratio_range, random_erasing_mask_value
47
+ return super().apply_transform(x, transform_parameters)
44
- ):
45
- X_copy = X.copy()
46
- batch_size, H, W, C = X_copy.shape
47
- original_area = H * W
48
48
 
49
- for batch_index in range(batch_size):
50
-
51
- if erasing_probability < np.random.rand():
52
- continue
53
-
54
- # はみ出さないようリトライするループ
55
- while True:
56
-
57
- # マスクする面積をサンプリング
58
- erasing_area = np.random.uniform(
59
- erasing_area_ratio_range[0], erasing_area_ratio_range[1]
60
- ) * original_area
61
-
62
- # マスクするアスペクト比をサンプリング
63
- erasing_aspect_ratio = np.random.uniform(
64
- erasing_aspect_ratio_range[0], erasing_aspect_ratio_range[1]
65
- )
66
-
67
- # 面積とアスペクト比から高さと幅を計算
68
- erasing_height = int(np.sqrt(erasing_area * erasing_aspect_ratio))
69
- erasing_width = int(np.sqrt(erasing_area / erasing_aspect_ratio))
70
-
71
- # マスクを配置する端点をサンプリング
72
- erasing_left_top_x = np.random.randint(0, W)
73
- erasing_left_top_y = np.random.randint(0, H)
74
-
75
- # マスクが元画像をはみ出すかどうかを計算
76
- if erasing_left_top_x + erasing_width <= W \
77
- and erasing_left_top_y + erasing_height <= H:
78
- break
79
-
80
- # マスクする値の生成
81
- erasing_values = np.random.uniform(
82
- random_erasing_mask_value[0], random_erasing_mask_value[1],
83
- (erasing_height, erasing_width, C)
84
- )
85
-
86
- X_copy[batch_index,
87
- erasing_left_top_y:erasing_left_top_y + erasing_height,
88
- erasing_left_top_x:erasing_left_top_x + erasing_width, :] = erasing_values
89
-
90
- # X_copy = X_copy.astype("float64")
91
-
92
- return X_copy
93
-
94
-
95
- def mixup(self, X1, y1, X2, y2):
96
- assert X1.shape[0] == y1.shape[0] == X2.shape[0] == y2.shape[0]
97
- batch_size = X1.shape[0]
98
- l = np.random.beta(self.mixup_alpha, self.mixup_alpha, batch_size)
99
- X_l = l.reshape(batch_size, 1, 1, 1)
100
- y_l = l.reshape(batch_size, 1)
101
- X = X1 * X_l + X2 * (1-X_l)
102
- y = y1 * y_l + y2 * (1-y_l)
103
- return X, y
104
-
105
- def apply_transform(self, seed=None,*args, **kwargs):
106
- # batch_gen = super().apply_transform(*args, **kwargs)
107
-
108
- # if seed is None:
109
- # batch_gen2 = super().apply_transform(seed=seed,*args, **kwargs)
110
- # else:
111
- # batch_gen2 = super().apply_transform(seed=seed+777, *args, **kwargs) # seed+777はseedと同じでなければ何でも良い
112
-
113
- while True:
114
- batch_x, batch_y = next()
115
- batch_x_2, batch_y_2 = next()
116
-
117
- if self.mixup_alpha is not None:
118
- batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
119
-
120
- if self.random_erasing_probability is not None:
121
- batch_x = self.random_erasing(
122
- batch_x,
123
- self.random_erasing_probability,
124
- self.random_erasing_area_ratio,
125
- self.random_erasing_aspect_ratio,
126
- self.random_erasing_mask_value)
127
-
128
- batch_gen = super().apply_transform(*args, **kwargs)
129
-
130
- if seed is None:
131
- batch_gen2 = super().apply_transform(seed=seed,*args, **kwargs)
132
- else:
133
- batch_gen2 = super().apply_transform(seed=seed+777, *args, **kwargs) # seed+777はseedと同じでなければ何でも良い
134
-
135
- batch_x = batch_x.astype("float64") / 255.0
136
-
137
- yield (batch_x, batch_y)
138
-
139
- # def flow(self, seed=None, *args, **kwargs):
140
-
141
- # batch_gen = super().flow(seed=seed, *args, **kwargs)
142
-
143
- # if seed is None:
144
- # batch_gen2 = super().flow(seed=seed, *args, **kwargs)
145
- # else:
146
- # batch_gen2 = super().flow(seed=seed+777, *args, **kwargs)
147
-      
148
-
149
- #while True:
150
-
151
- # batch_x, batch_y = next(batch_gen)
152
- # batch_x_2, batch_y_2 = next(batch_gen2)
153
-
154
-
155
- # if self.random_erasing_probability is not None:
156
- # batch_x = self.random_erasing(
157
- # batch_x,
158
- # self.random_erasing_probability,
159
- # self.random_erasing_area_ratio,
160
- # self.random_erasing_aspect_ratio,
161
- # self.random_erasing_mask_value)
162
-
163
-
164
-
165
- # if self.mixup_alpha is not None:
166
- # batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
167
-
168
- #batch_x = batch_x.astype("float64") / 255.0
169
-
170
- #yield (batch_x, batch_y)
171
49
 
172
50
  ```
173
51
 
174
52
  ### 試したこと
175
- Mixupの発生させる確立を指定しようと思い、random_erasingのerasing_probabilityのように指定したかったのですができませんでした。
176
53
 
54
+ 回答を参考にmixupを実装したかったのですが、どのようにdef imgの画像ジェネレーターとmixupするのか詰まっている状況です。
177
55
 
178
56
  ### 補足情報(FW/ツールのバージョンなど)
179
57
  ・https://dev.classmethod.jp/articles/tensorflow-image-generator-custom/
180
58
  ・https://dev.classmethod.jp/articles/tensorflow-image-generator-custom-mixup/
181
59
  上記二つのサイトを参考に実装しました。
182
-
183
- 追記
184
- apply_transformメソッドの追加を行いたかったのですが、変換前のバッチをどのように取得してよいかわからず詰まっております。
185
-
186
60
 
187
61
 
188
62
  使用ツール

3

ソースコードの修正, 追記

2022/12/16 14:43

投稿

semisemi
semisemi

スコア18

test CHANGED
File without changes
test CHANGED
@@ -102,39 +102,72 @@
102
102
  y = y1 * y_l + y2 * (1-y_l)
103
103
  return X, y
104
104
 
105
+ def apply_transform(self, seed=None,*args, **kwargs):
106
+ # batch_gen = super().apply_transform(*args, **kwargs)
107
+
108
+ # if seed is None:
109
+ # batch_gen2 = super().apply_transform(seed=seed,*args, **kwargs)
110
+ # else:
111
+ # batch_gen2 = super().apply_transform(seed=seed+777, *args, **kwargs) # seed+777はseedと同じでなければ何でも良い
112
+
113
+ while True:
114
+ batch_x, batch_y = next()
115
+ batch_x_2, batch_y_2 = next()
116
+
117
+ if self.mixup_alpha is not None:
118
+ batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
119
+
120
+ if self.random_erasing_probability is not None:
121
+ batch_x = self.random_erasing(
122
+ batch_x,
123
+ self.random_erasing_probability,
124
+ self.random_erasing_area_ratio,
125
+ self.random_erasing_aspect_ratio,
126
+ self.random_erasing_mask_value)
127
+
128
+ batch_gen = super().apply_transform(*args, **kwargs)
129
+
130
+ if seed is None:
131
+ batch_gen2 = super().apply_transform(seed=seed,*args, **kwargs)
132
+ else:
133
+ batch_gen2 = super().apply_transform(seed=seed+777, *args, **kwargs) # seed+777はseedと同じでなければ何でも良い
134
+
135
+ batch_x = batch_x.astype("float64") / 255.0
136
+
137
+ yield (batch_x, batch_y)
105
138
 
106
- def flow(self, seed=None, *args, **kwargs):
139
+ # def flow(self, seed=None, *args, **kwargs):
107
140
 
108
- batch_gen = super().flow(seed=seed, *args, **kwargs)
141
+ # batch_gen = super().flow(seed=seed, *args, **kwargs)
109
142
 
110
- if seed is None:
143
+ # if seed is None:
111
- batch_gen2 = super().flow(seed=seed, *args, **kwargs)
144
+ # batch_gen2 = super().flow(seed=seed, *args, **kwargs)
112
- else:
145
+ # else:
113
- batch_gen2 = super().flow(seed=seed+777, *args, **kwargs)
146
+ # batch_gen2 = super().flow(seed=seed+777, *args, **kwargs)
114
-      #seed+777はseedと同じでなければ何でも良い
147
+      
115
148
 
116
- while True:
149
+ #while True:
117
150
 
118
- batch_x, batch_y = next(batch_gen)
151
+ # batch_x, batch_y = next(batch_gen)
119
- batch_x_2, batch_y_2 = next(batch_gen2)
152
+ # batch_x_2, batch_y_2 = next(batch_gen2)
120
153
 
121
154
 
122
- if self.random_erasing_probability is not None:
155
+ # if self.random_erasing_probability is not None:
123
- batch_x = self.random_erasing(
156
+ # batch_x = self.random_erasing(
124
- batch_x,
157
+ # batch_x,
125
- self.random_erasing_probability,
158
+ # self.random_erasing_probability,
126
- self.random_erasing_area_ratio,
159
+ # self.random_erasing_area_ratio,
127
- self.random_erasing_aspect_ratio,
160
+ # self.random_erasing_aspect_ratio,
128
- self.random_erasing_mask_value)
161
+ # self.random_erasing_mask_value)
129
162
 
130
163
 
131
164
 
132
- if self.mixup_alpha is not None:
165
+ # if self.mixup_alpha is not None:
133
- batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
166
+ # batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
134
167
 
135
- batch_x = batch_x.astype("float64") / 255.0
168
+ #batch_x = batch_x.astype("float64") / 255.0
136
169
 
137
- yield (batch_x, batch_y)
170
+ #yield (batch_x, batch_y)
138
171
 
139
172
  ```
140
173
 
@@ -147,6 +180,11 @@
147
180
  ・https://dev.classmethod.jp/articles/tensorflow-image-generator-custom-mixup/
148
181
  上記二つのサイトを参考に実装しました。
149
182
 
183
+ 追記
184
+ apply_transformメソッドの追加を行いたかったのですが、変換前のバッチをどのように取得してよいかわからず詰まっております。
185
+
186
+
187
+
150
188
  使用ツール
151
189
  anacondaを使用
152
190
  python 3.8.13

2

ソースコードの修正

2022/12/15 07:40

投稿

semisemi
semisemi

スコア18

test CHANGED
File without changes
test CHANGED
@@ -127,12 +127,12 @@
127
127
  self.random_erasing_aspect_ratio,
128
128
  self.random_erasing_mask_value)
129
129
 
130
- batch_x = batch_x.astype("float64") / 255.0
130
+
131
131
 
132
132
  if self.mixup_alpha is not None:
133
133
  batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
134
134
 
135
- batch_x = batch_x.astype("float64") / 255.0
135
+ batch_x = batch_x.astype("float64") / 255.0
136
136
 
137
137
  yield (batch_x, batch_y)
138
138
 

1

ソースコードの修正

2022/12/15 07:36

投稿

semisemi
semisemi

スコア18

test CHANGED
File without changes
test CHANGED
@@ -126,7 +126,9 @@
126
126
  self.random_erasing_area_ratio,
127
127
  self.random_erasing_aspect_ratio,
128
128
  self.random_erasing_mask_value)
129
+
129
-
130
+ batch_x = batch_x.astype("float64") / 255.0
131
+
130
132
  if self.mixup_alpha is not None:
131
133
  batch_x, batch_y = self.mixup(batch_x, batch_y, batch_x_2, batch_y_2)
132
134