1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kerasでフォルダ内の画像を訓練とテストデータに別ける方法

1
Posted at

はじめに

kerasの本とかを参考にしていると、物理的にフォルダを訓練とテストデータに別けて使う例が多い気がする。
自分で簡単にやる分には、フォルダ一つでライブラリでいい感じにやってほしいと思い公式doc見てオプションがあったのでメモ。

条件

kaggleのデートセットで画像が種類のフォルダごとに別れているような場合に使う。

ソース

データの水増しとか、他のオプションはなし。

画像データ前処理
# 規格化と訓練データと検証データの比率
train_datagen = ImageDataGenerator(
    rescale=1./255,
    validation_split=0.2
)
# 訓練データ
path = 'フォルダのパス'
size = '画像のサイズ'
train_generator = train_datagen.flow_from_directory(
    path,
    target_size=(size, size),
    batch_size=20,
    class_mode='categorical',
    subset='training',
)
# 検証データ
validation_generator  = train_datagen.flow_from_directory(
    path,
    target_size=(size, size),
    batch_size=20,
    class_mode='categorical',
    subset='validation',
)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?