回答編集履歴

7

ジェネレータを使う場合に必要なsteps_per_epochの追記

2022/11/07 06:54

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -63,12 +63,15 @@
63
63
  )
64
64
  yield x, y
65
65
 
66
+ batch_size = 8
66
- train = x_datagen_base.flow(x_train, y_train, batch_size = 8)
67
+ train = x_datagen_base.flow(x_train, y_train, batch_size = batch_size)
67
68
 
68
- history_model = model.fit(
69
+ model.fit(
69
70
  myflow(train),
71
+ batch_size = 8,
70
- epochs = 10,
72
+ epochs = 50,
71
- shuffle = True,
73
+ validation_data = (x_valid, y_valid),
74
+ steps_per_epoch = int(x_train.shape[0] / batch_size)
72
75
  )
73
76
  ```
74
77
  `brightness_range = [0.7, 1.117],`が入っていると変換後全部`0`になるのでコメントアウトしておきました.適宜変更してください.
@@ -115,11 +118,14 @@
115
118
  )
116
119
  yield x, y
117
120
 
121
+ batch_size = 8
118
- train = x_datagen_base.flow(x_train, y_train, batch_size = 8)
122
+ train = x_datagen_base.flow(x_train, y_train, batch_size = batch_size)
119
123
 
120
- history_model = model.fit(
124
+ model.fit(
121
125
  myflow(train),
126
+ batch_size = 8,
122
- epochs = 10,
127
+ epochs = 50,
123
- shuffle = True,
128
+ validation_data = (x_valid, y_valid),
129
+ steps_per_epoch = int(x_train.shape[0] / batch_size)
124
130
  )
125
131
  ```

6

append answer

2022/11/06 11:38

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -76,3 +76,50 @@
76
76
 
77
77
  詳しい扱い方は次の公式サイトを確認してください.
78
78
  [TensorFlow v2.10.0 - tf.keras.preprocessing.image.ImageDataGenerator](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator)
79
+
80
+ ### 追記
81
+ 予測値`y[i]`をOne-Hot Encodingしている場合,`y[i].argmax()`でラベル値を取得する必要がある.
82
+ ```Python
83
+ import tensorflow as tf
84
+ from tensorflow.python import keras
85
+ from keras.preprocessing.image import ImageDataGenerator
86
+ from keras.utils import to_categorical
87
+ import numpy as np
88
+
89
+ N, W, H, C = 64, 32, 32, 3
90
+ x_train = np.random.randint(0, 255, N * W * H * C).reshape(N, W, H, C)
91
+ y_train = np.random.randint(0, 3, N).reshape(N, -1)
92
+ y_train = to_categorical(y_train)
93
+
94
+ x_datagen_base = ImageDataGenerator(
95
+ rescale = 1 / 255,
96
+ dtype = np.float64
97
+ )
98
+
99
+ x_datagen_partial = ImageDataGenerator(
100
+ rotation_range = 180,
101
+ fill_mode = "constant",
102
+ vertical_flip = True,
103
+ #brightness_range = [0.7, 1.117],
104
+ horizontal_flip = True,
105
+ dtype = np.float64
106
+ )
107
+
108
+ def myflow(gen: ImageDataGenerator):
109
+ for x, y in gen:
110
+ for i, _x in enumerate(x):
111
+ if y[i].argmax() != 0: # labelが0以外であれば変換処理
112
+ x[i] = x_datagen_base.apply_transform(
113
+ _x,
114
+ x_datagen_partial.get_random_transform(_x.shape)
115
+ )
116
+ yield x, y
117
+
118
+ train = x_datagen_base.flow(x_train, y_train, batch_size = 8)
119
+
120
+ history_model = model.fit(
121
+ myflow(train),
122
+ epochs = 10,
123
+ shuffle = True,
124
+ )
125
+ ```

5

fix answer

2022/11/03 11:50

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -1,6 +1,6 @@
1
- ランダムにx_train_generatorとx_train_generator2のいずれかを適用したものを返す,というように作ることは可能ですが,ジェネレータが交互に同じデータを返すこともあるので,これを避けるため交互に返さないように書く必要があり非常に大変です.
1
+ `model.fit()`に2つ渡すことはできないので,代わりにランダムに`x_train_generator``x_train_generator2`のいずれかを適用したものを返す,というように作ることは可能ですが,ジェネレータが交互に同じデータを返すこともあるので,これを避けるため交互に返さないように書く必要があり非常に大変です.項目別に選びたいという後述の要件から,ジェネレータに渡すデータを切り分けようとしていることが伺えますが,`model.fit()`に渡すからには項目をシャッフルしたいですし切り分けて混ぜる,というのは非常にめんどくさいです.
2
2
 
3
- 項目の内つだけ正規化し他の項目はいろいろな変換処理をできるようにすることを推奨します.そのために`yield`を使って次のようにジェネレータを定義します.
3
+ なので,3項目の内1つだけ正規化し他の2項目はいろいろな変換処理をできるようにすることを推奨します.そのために`yield`を使って次のようにジェネレータを定義します.
4
4
 
5
5
  ここで大事なのは,「`yield`で1バッチ分のデータを返す」ということです.これさえ叶えばジェネレータの中身はなんだって良いです.したがって
6
6
 

4

update answer

2022/11/03 10:53

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -27,7 +27,7 @@
27
27
  `x_datagen_base`は正規化のみ,`x_datagen_partial`は色々な変換処理を組み込みました.
28
28
  必ずしも`x_datagen_partial`である必要はなく,1枚の画像変換ができる処理であればなんでも良いです.
29
29
 
30
- その際は独自の変換関数`image_transform()`などを作成して`x[i] = data_transform(x[i])`と書くことができます.
30
+ その際は独自の変換関数`image_transform()`などを作成して`x[i] = data_transform(x[i])`と書くことができます.`ImageDataGenerator`に無いような,Random ErasingやCutout,Grid MaskなどさまざまなData Augmentation手法の適用が可能です.
31
31
 
32
32
  ```Python
33
33
  import tensorflow as tf

3

fix answer

2022/11/03 10:48

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  三項目の内一つだけ正規化し他の二項目はいろいろな変換処理をできるようにすることを推奨します.そのために`yield`を使って次のようにジェネレータを定義します.
4
4
 
5
- ここで大事なのは,「`yield`で1バッチ分のデータを返す」ということです.これさえ叶えば`myflow()`の中身はなんだって良いです.したがって
5
+ ここで大事なのは,「`yield`で1バッチ分のデータを返す」ということです.これさえ叶えばジェネレータの中身はなんだって良いです.したがって
6
6
 
7
7
  ```Python
8
8
  def myflow(gen: ImageDataGenerator):
@@ -71,8 +71,8 @@
71
71
  shuffle = True,
72
72
  )
73
73
  ```
74
-
74
+ `brightness_range = [0.7, 1.117],`が入っていると変換後全部`0`になるのでコメントアウトしておきました.適宜変更してください.
75
- 最終的に`model.fit()`には`myflow(train)`を渡してください.同様に,`validation`に関して作成るこも可能
75
+ 最終的に`model.fit()`にはジェネレータ`myflow(train)`を渡してください.同様に,`validation`も自作ジェネレータ`myflow()`を通して`model.fit()`に渡すと良いしょう
76
76
 
77
77
  詳しい扱い方は次の公式サイトを確認してください.
78
78
  [TensorFlow v2.10.0 - tf.keras.preprocessing.image.ImageDataGenerator](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator)

2

fix answer

2022/11/03 10:42

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -66,10 +66,10 @@
66
66
  train = x_datagen_base.flow(x_train, y_train, batch_size = 8)
67
67
 
68
68
  history_model = model.fit(
69
- myflow(train)
69
+ myflow(train),
70
70
  epochs = 10,
71
71
  shuffle = True,
72
- )
72
+ )
73
73
  ```
74
74
 
75
75
  最終的に`model.fit()`には`myflow(train)`を渡してください.同様に,`validation`に関して作成することも可能です.

1

fix answer

2022/11/03 10:41

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -20,7 +20,7 @@
20
20
  if y[i] != 0:
21
21
  x[i] = x_datagen_base.apply_transform(
22
22
  _x,
23
- x_datagen_partial.get_random_transform((W, H, C))
23
+ x_datagen_partial.get_random_transform(_x.shape)
24
24
  )
25
25
  yield x, y
26
26
  ```
@@ -59,7 +59,7 @@
59
59
  if y[i] != 0:
60
60
  x[i] = x_datagen_base.apply_transform(
61
61
  _x,
62
- x_datagen_partial.get_random_transform((W, H, C))
62
+ x_datagen_partial.get_random_transform(_x.shape)
63
63
  )
64
64
  yield x, y
65
65
 
@@ -73,3 +73,6 @@
73
73
  ```
74
74
 
75
75
  最終的に`model.fit()`には`myflow(train)`を渡してください.同様に,`validation`に関して作成することも可能です.
76
+
77
+ 詳しい扱い方は次の公式サイトを確認してください.
78
+ [TensorFlow v2.10.0 - tf.keras.preprocessing.image.ImageDataGenerator](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator)