{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Welcome \n\n![](https://miro.medium.com/max/1210/0*Utma-dS47hSoQ6Zt)\n\n** This kernel is based on Image Augmentation and the following is applied :**\n* Horizontal Flip\n* Width Shift\n* Random 45 degree rotation\n* Filling\n* Random Zoom (10%)","attachments":{},"execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nImage_path='/kaggle/input/siim-isic-melanoma-classification/jpeg/'\n# dtype string because its reads in string format\ntrain_csv=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv',dtype=str)\ntest_csv=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv',dtype=str)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# It's Augment time.\n\n**We can use various augmentation and even add preprocessing filters**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_augmenter=ImageDataGenerator(\n    rescale=1./255, \n    #rotation range and fill mode only\n    #samplewise_center=True, \n    #samplewise_std_normalization=True, \n    horizontal_flip = True, \n    #vertical_flip = True, \n    #height_shift_range= 0.05, \n    width_shift_range=0.1, \n    rotation_range=45, \n    #shear_range = 0.1,\n    fill_mode = 'nearest',\n    zoom_range=0.10,\n    #preprocessing_function=function_name,\n    )\n\ntest_augmenter=ImageDataGenerator(\n    rescale=1./255\n    )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We need to add the format of the images in the end or Use glob for flexibility","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def jpg_tag(image_name):\n    return image_name+'.jpg'\n\ntrain_csv['image_name']=train_csv['image_name'].apply(jpg_tag)\ntest_csv['image_name']=test_csv['image_name'].apply(jpg_tag)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#displaying of new dataframe\ntest_csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size=16\nIMG_size=224\ntrain_generator=train_augmenter.flow_from_dataframe(\ndataframe=train_csv,\ndirectory=Image_path+'train',\n#save_to_dir='augmented',\n#save_prefix='_aug'\n#save_format='jpg'\nx_col='image_name',\ny_col='target',\nbatch_size=batch_size,\nseed=42,\nshuffle=True,\nclass_mode='binary',\ntarget_size=(IMG_size,IMG_size)\n)\n\ntest_generator=test_augmenter.flow_from_dataframe(\ndataframe=test_csv,\ndirectory=Image_path+'test',\nx_col='image_name',\nbatch_size=batch_size, #preffered 1\nshuffle=False,\nclass_mode=None,\ntarget_size=(IMG_size,IMG_size)\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Success reading all the Images **","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# OUTPUT OF AUGMENTATED IMAGES","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef plotImages(images_arr):\n    fig, axes = plt.subplots(1, 5, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip( images_arr, axes):\n        ax.imshow(img)\n    plt.tight_layout()\n    plt.show()\n    \n    \naugmented_images = [train_generator[0][0][0] for i in range(5)]\nplotImages(augmented_images)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# VOILA DONE , use this in fit function now","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Image source: Medium","execution_count":null}],"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":4,"nbformat_minor":4}