{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport shutil\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '/kaggle/input/humpback-whale-identification/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Import Keras libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\ntf.test.gpu_device_name()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.applications import VGG16\nfrom keras.layers import Dense, Flatten, GlobalAveragePooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Variables"},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_SIZE = (224, 224)\nVALIDATION_SPLIT = 0.7\nBATCH_SIZE = 32\nNUM_CLASSES = 5005\nEPOCHS = 10","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load and Prepare the dataset "},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the class label and image name file\nlabel_file = pd.read_csv(path+'/train.csv').rename(columns={'Id': 'label', 'Image': 'filename'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create Image generator for data augmentation\nimage_data_gen = ImageDataGenerator(\n    rescale=1./255.,\n    width_shift_range=[+0.2, 0, -0.2],\n    height_shift_range=[+0.2, 0, -0.2],\n    rotation_range=30,\n    fill_mode=\"nearest\",\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split=VALIDATION_SPLIT\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# read the data directly from the directory\ntrain_gen = image_data_gen.flow_from_dataframe(\n    dataframe=label_file,\n    directory=path+'/train',\n    x_col=\"filename\",\n    y_col=\"label\",\n    target_size=IMAGE_SIZE,\n    color_mode=\"rgb\",\n    class_mode=\"categorical\",\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    interpolation=\"nearest\",\n    validate_filenames=True\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train VGG16 Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# create the model\nvgg16_model = Sequential()\n\n# add the VGG16 layers with weights\nvgg16_model.add(VGG16(\n    include_top=False,\n    weights=\"imagenet\",\n    classes=NUM_CLASSES\n))\n\nvgg16_model.add(GlobalAveragePooling2D())\n\n# add the dense layer\nvgg16_model.add(Dense(units=NUM_CLASSES, activation='softmax'))\n\n\nvgg16_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nvgg16_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = vgg16_model.fit_generator(generator=train_gen, steps_per_epoch=train_gen.n//EPOCHS, epochs=EPOCHS)","execution_count":null,"outputs":[]}],"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}