質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

1295閲覧

ImageDataGeneratorを継承したクラスを作成したのですが、エラーが出てしましました。

Tera0724

総合スコア18

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/02/03 02:40

編集2022/01/12 10:55

現在、Deep Learningを使用した学習を行っているのですが、データの水増しについてクラス継承を用いた自作のジェネレータを作成しようと考えました。ジェネレータの作成について参考にした資料は以下に示します。
参考資料
作成したプログラムについては以下に示す通りです。

python

1# agumantation用クラス 2class MyImageDataGenerator(ImageDataGenerator): 3 def __init__(self, featurewise_center = False, samplewise_center = False, 4 featurewise_std_normalization = False, samplewise_std_normalization = False, 5 zca_whitening = False, zca_epsilon = 1e-06, rotation_range = 0.0, width_shift_range = 0.0, 6 height_shift_range = 0.0, brightness_range = None, shear_range = 0.0, zoom_range = 0.0, 7 channel_shift_range = 0.0, fill_mode = 'nearest', cval = 0.0, horizontal_flip = False, 8 vertical_flip = False, rescale = None, preprocessing_function = None, data_format = None, validation_split = 0.0, 9 random_crop = None, mix_up_alpha = 0.0, cutout_mask_size = 0): 10 super().__init__(featurewise_center, samplewise_center, featurewise_std_normalization, samplewise_std_normalization, 11 zca_whitening, zca_epsilon, rotation_range, width_shift_range, height_shift_range, brightness_range, 12 shear_range, zoom_range, channel_shift_range, fill_mode, cval, horizontal_flip, vertical_flip, 13 rescale, preprocessing_function, data_format, validation_split) 14 15 assert random_crop == None or len(random_crop) == 2 16 self.random_crop_size = random_crop 17 18 19 assert cutout_mask_size >= 0 20 self.cutout_mask_size = cutout_mask_size 21 22 23 24 def random_crop(self, original_img): 25 # Note: image_data_format is 'channel_last' 26 assert original_img.shape[2] == 3 27 if original_img.shape[0] < self.random_crop_size[0] or original_img.shape[1] < self.random_crop_size[1]: 28 raise ValueError(f"Invalid random_crop_size : original = {original_img.shape}, crop_size = {self.random_crop_size}") 29 30 height, width = original_img.shape[0], original_img.shape[1] 31 dy, dx = self.random_crop_size 32 x = np.random.randint(0, width - dx + 1) 33 y = np.random.randint(0, height - dy + 1) 34 return original_img[y:(y+dy), x:(x+dx), :] 35 36 def cutout(self, x, y): 37 return np.array(list(map(self._cutout, x))), y 38 39 def _cutout(self, image_origin): 40 # 最後に使うfill()は元の画像を書き換えるので、コピーしておく 41 image = np.copy(image_origin) 42 mask_value = image.mean() 43 44 h, w, _ = image.shape 45 # マスクをかける場所のtop, leftをランダムに決める 46 # はみ出すことを許すので、0以上ではなく負の値もとる(最大mask_size // 2はみ出す) 47 top = np.random.randint(0 - self.cutout_mask_size // 2, h - self.cutout_mask_size) 48 left = np.random.randint(0 - self.cutout_mask_size // 2, w - self.cutout_mask_size) 49 bottom = top + self.cutout_mask_size 50 right = left + self.cutout_mask_size 51 52 # はみ出した場合の処理 53 if top < 0: 54 top = 0 55 if left < 0: 56 left = 0 57 58 # マスク部分の画素値を平均値で埋める 59 image[top:bottom, left:right, :].fill(mask_value) 60 return image 61 62 def flow_from_dataframe(self, dataframe, directory=None, x_col='filename', y_col='class', 63 target_size=(256, 256), color_mode='rgb', classes=None, class_mode='categorical', 64 batch_size=32, shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png', 65 subset=None, interpolation='nearest', drop_duplicates=True): 66 67 # 親クラスのflow_from_dataframe 68 batches = super().flow_from_dataframe(dataframe, directory, x_col, y_col, 69 target_size, color_mode, classes, class_mode, 70 batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, 71 subset, interpolation, drop_duplicates) 72 # 拡張処理 73 while True: 74 # Random crop 75 if self.random_crop_size != None: 76 x = np.zeros((batch_x.shape[0], self.random_crop_size[0], self.random_crop_size[1], 3)) 77 for i in range(batch_x.shape[0]): 78 x[i] = self.random_crop(batch_x[i]) 79 batch_x = x 80 81 if self.cutout_mask_size > 0: 82 batch_x, batch_y = next(batches) 83 result = self.cutout(batch_x, batch_y) 84 batch_x, batch_y = result 85 86 yield (batch_x, batch_y) 87 88 89 # 返り値 90 yield (batch_x, batch_y)

この中の

batches = super().flow_from_dataframe(dataframe, directory, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, subset, interpolation, drop_duplicates)

の部分において、以下のようなエラーが出てしまいます。
ValueError: ('Invalid color mode:', None, '; expected "rgb", "rgba", or "grayscale".')
color_modeについてデフォルト引数でrgbに設定しているのですがエラーが消えません。また、調べてみたのですが、
flow_from_dataframeを使用したものが無く原因がわからない状態です。
上記エラーの原因について教えていただけると助かります。よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

親クラスの関数定義を再確認してみてください。
実はcolor_modeを渡しているところが親の関数定義的にcolor_modeではないのではないでしょうか?
どこかずれているのではないかと推測します。

見直しても分からないようであれば、
親クラスの定義を質問に追記ください。

投稿2021/02/03 03:32

WhiteTempest

総合スコア404

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問