{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# KerasのImageDataGeneratorを利用して「Petals to the Metal - Flower Classification on TPU」にデータ拡張を実施\n\n* ポイント\n    * データの読み込みのみ。学習と予測の部分はなし。\n    * Keras の `ImageDataGenerator` を利用してデータ拡張を実施\n\n次を参考に実施  \nhttps://keras.io/ja/preprocessing/image/","metadata":{}},{"cell_type":"markdown","source":"# 設定値の決定\n\n* target_size: 読み込んだ画像をこのサイズに変換して利用する\n* batch_size: 学習時のデータに対するbatch_size\n","metadata":{}},{"cell_type":"code","source":"target_size = [192, 192]\nbatch_size = 50","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:28.209923Z","iopub.execute_input":"2023-01-27T09:40:28.210417Z","iopub.status.idle":"2023-01-27T09:40:28.221410Z","shell.execute_reply.started":"2023-01-27T09:40:28.210377Z","shell.execute_reply":"2023-01-27T09:40:28.214896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# データの読み込み準備\n\n「104-flowers-garden-of-eden」という外部データを利用する。  \nこちらのデータは「Petals to the Metal - Flower Classification on TPU」の画像をJPEG形式に変換して、クラス毎のディレクトリに保存してくれている。\n`flow_from_directory`で画像を読み込む際の形式と一致している。\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_dir = '/kaggle/input/104-flowers-garden-of-eden/jpeg-192x192/train'\n\nclasses = ['pink primrose',    'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea',     'wild geranium',     'tiger lily',           'moon orchid',              'bird of paradise', 'monkshood',        'globe thistle',         # 00 - 09\n           'snapdragon',       \"colt's foot\",               'king protea',      'spear thistle', 'yellow iris',       'globe-flower',         'purple coneflower',        'peruvian lily',    'balloon flower',   'giant white arum lily', # 10 - 19\n           'fire lily',        'pincushion flower',         'fritillary',       'red ginger',    'grape hyacinth',    'corn poppy',           'prince of wales feathers', 'stemless gentian', 'artichoke',        'sweet william',         # 20 - 29\n           'carnation',        'garden phlox',              'love in the mist', 'cosmos',        'alpine sea holly',  'ruby-lipped cattleya', 'cape flower',              'great masterwort', 'siam tulip',       'lenten rose',           # 30 - 39\n           'barberton daisy',  'daffodil',                  'sword lily',       'poinsettia',    'bolero deep blue',  'wallflower',           'marigold',                 'buttercup',        'daisy',            'common dandelion',      # 40 - 49\n           'petunia',          'wild pansy',                'primula',          'sunflower',     'lilac hibiscus',    'bishop of llandaff',   'gaura',                    'geranium',         'orange dahlia',    'pink-yellow dahlia',    # 50 - 59\n           'cautleya spicata', 'japanese anemone',          'black-eyed susan', 'silverbush',    'californian poppy', 'osteospermum',         'spring crocus',            'iris',             'windflower',       'tree poppy',            # 60 - 69\n           'gazania',          'azalea',                    'water lily',       'rose',          'thorn apple',       'morning glory',        'passion flower',           'lotus',            'toad lily',        'anthurium',             # 70 - 79\n           'frangipani',       'clematis',                  'hibiscus',         'columbine',     'desert-rose',       'tree mallow',          'magnolia',                 'cyclamen ',        'watercress',       'canna lily',            # 80 - 89\n           'hippeastrum ',     'bee balm',                  'pink quill',       'foxglove',      'bougainvillea',     'camellia',             'mallow',                   'mexican petunia',  'bromelia',         'blanket flower',        # 90 - 99\n           'trumpet creeper',  'blackberry lily',           'common tulip',     'wild rose']                                                                                                                                               # 100 - 102\nclass_num = len(classes)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-27T09:40:28.227728Z","iopub.execute_input":"2023-01-27T09:40:28.228239Z","iopub.status.idle":"2023-01-27T09:40:28.249558Z","shell.execute_reply.started":"2023-01-27T09:40:28.228197Z","shell.execute_reply":"2023-01-27T09:40:28.247537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"読み込まれた画像を確認するための関数を準備","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\ndef show_images(generator, image_num=38, seed=1):\n    train_data = generator.flow_from_directory(\n        train_dir, color_mode='rgb', classes=classes, batch_size=batch_size,\n        target_size=target_size,\n        shuffle=False, seed=seed)\n\n    fig, ax=plt.subplots(3, 3, figsize=(12,12))    \n    for i in range(3):\n        for j in range(3):\n            ax[i,j].imshow(train_data[0][0][image_num])\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:28.254060Z","iopub.execute_input":"2023-01-27T09:40:28.254472Z","iopub.status.idle":"2023-01-27T09:40:28.275150Z","shell.execute_reply.started":"2023-01-27T09:40:28.254440Z","shell.execute_reply":"2023-01-27T09:40:28.274165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# rotation_range\n\n画像をランダムに回転する回転範囲．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=90)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:28.278626Z","iopub.execute_input":"2023-01-27T09:40:28.279492Z","iopub.status.idle":"2023-01-27T09:40:28.289405Z","shell.execute_reply.started":"2023-01-27T09:40:28.279451Z","shell.execute_reply":"2023-01-27T09:40:28.288368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:28.291721Z","iopub.execute_input":"2023-01-27T09:40:28.296618Z","iopub.status.idle":"2023-01-27T09:40:33.996862Z","shell.execute_reply.started":"2023-01-27T09:40:28.296583Z","shell.execute_reply":"2023-01-27T09:40:33.995836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# width_shift_range\n\nランダムに水平シフトする範囲．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    width_shift_range=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:33.998309Z","iopub.execute_input":"2023-01-27T09:40:33.998739Z","iopub.status.idle":"2023-01-27T09:40:34.003948Z","shell.execute_reply.started":"2023-01-27T09:40:33.998704Z","shell.execute_reply":"2023-01-27T09:40:34.002707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:34.005376Z","iopub.execute_input":"2023-01-27T09:40:34.005841Z","iopub.status.idle":"2023-01-27T09:40:38.970183Z","shell.execute_reply.started":"2023-01-27T09:40:34.005808Z","shell.execute_reply":"2023-01-27T09:40:38.969161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# height_shift_range\n\nランダムに垂直シフトする範囲．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    height_shift_range=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:38.973464Z","iopub.execute_input":"2023-01-27T09:40:38.974015Z","iopub.status.idle":"2023-01-27T09:40:38.979319Z","shell.execute_reply.started":"2023-01-27T09:40:38.973977Z","shell.execute_reply":"2023-01-27T09:40:38.978319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:38.982458Z","iopub.execute_input":"2023-01-27T09:40:38.982871Z","iopub.status.idle":"2023-01-27T09:40:44.771072Z","shell.execute_reply.started":"2023-01-27T09:40:38.982823Z","shell.execute_reply":"2023-01-27T09:40:44.770243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# shear_range\nシアー強度（反時計回りのシアー角度）．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    shear_range=90.0)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:44.772466Z","iopub.execute_input":"2023-01-27T09:40:44.773088Z","iopub.status.idle":"2023-01-27T09:40:44.778491Z","shell.execute_reply.started":"2023-01-27T09:40:44.773047Z","shell.execute_reply":"2023-01-27T09:40:44.777267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:44.779892Z","iopub.execute_input":"2023-01-27T09:40:44.780305Z","iopub.status.idle":"2023-01-27T09:40:49.868759Z","shell.execute_reply.started":"2023-01-27T09:40:44.780272Z","shell.execute_reply":"2023-01-27T09:40:49.867943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# zoom_range\n\nランダムにズームする範囲．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    zoom_range=[0.5, 1.5])","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:49.869943Z","iopub.execute_input":"2023-01-27T09:40:49.870659Z","iopub.status.idle":"2023-01-27T09:40:49.876239Z","shell.execute_reply.started":"2023-01-27T09:40:49.870620Z","shell.execute_reply":"2023-01-27T09:40:49.875037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:49.877706Z","iopub.execute_input":"2023-01-27T09:40:49.878653Z","iopub.status.idle":"2023-01-27T09:40:55.069303Z","shell.execute_reply.started":"2023-01-27T09:40:49.878619Z","shell.execute_reply":"2023-01-27T09:40:55.064798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# channel_shift_range\n\nランダムにチャンネルをシフトする範囲．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    channel_shift_range=128.0)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:55.070999Z","iopub.execute_input":"2023-01-27T09:40:55.072003Z","iopub.status.idle":"2023-01-27T09:40:55.077549Z","shell.execute_reply.started":"2023-01-27T09:40:55.071964Z","shell.execute_reply":"2023-01-27T09:40:55.076047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:55.079250Z","iopub.execute_input":"2023-01-27T09:40:55.080302Z","iopub.status.idle":"2023-01-27T09:40:57.636065Z","shell.execute_reply.started":"2023-01-27T09:40:55.080269Z","shell.execute_reply":"2023-01-27T09:40:57.635252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# horizontal_flip\n\n水平方向に入力をランダムに反転します．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:57.637511Z","iopub.execute_input":"2023-01-27T09:40:57.638070Z","iopub.status.idle":"2023-01-27T09:40:57.642635Z","shell.execute_reply.started":"2023-01-27T09:40:57.638034Z","shell.execute_reply":"2023-01-27T09:40:57.641817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:57.644083Z","iopub.execute_input":"2023-01-27T09:40:57.644632Z","iopub.status.idle":"2023-01-27T09:40:59.753184Z","shell.execute_reply.started":"2023-01-27T09:40:57.644599Z","shell.execute_reply":"2023-01-27T09:40:59.752166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# vertical_flip\n\n垂直方向に入力をランダムに反転します．","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(\n    rescale=1./255,\n    vertical_flip=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:59.756376Z","iopub.execute_input":"2023-01-27T09:40:59.757074Z","iopub.status.idle":"2023-01-27T09:40:59.762454Z","shell.execute_reply.started":"2023-01-27T09:40:59.757018Z","shell.execute_reply":"2023-01-27T09:40:59.761427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T09:40:59.763983Z","iopub.execute_input":"2023-01-27T09:40:59.764530Z","iopub.status.idle":"2023-01-27T09:41:01.959244Z","shell.execute_reply.started":"2023-01-27T09:40:59.764456Z","shell.execute_reply":"2023-01-27T09:41:01.958418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}