{"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":"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\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\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\n# import os\n# for 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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndata['id'] = data['id'].apply(lambda x: x + '.jpg')\n\ntrain_df = data.sample(frac=0.8, random_state=42)\nvalid_df = data.drop(train_df.index)\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0.1,\n    zoom_range=0.1,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalid_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df,\n    directory='/kaggle/input/dog-breed-identification/train/',\n    x_col='id',\n    y_col='breed',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='categorical'\n)\n\nvalid_generator = valid_datagen.flow_from_dataframe(\n    valid_df,\n    directory='/kaggle/input/dog-breed-identification/train/',\n    x_col='id',\n    y_col='breed',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='categorical'\n)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tpu_strategy.scope():\n    model = keras.Sequential([\n        layers.Conv2D(32, (3,3), activation='relu', input_shape=(224,224,3)),\n        layers.MaxPooling2D((2,2)),\n        layers.Conv2D(64, (3,3), activation='relu'),\n        layers.MaxPooling2D((2,2)),\n        layers.Conv2D(128, (3,3), activation='relu'),\n        layers.MaxPooling2D((2,2)),\n        layers.Conv2D(256, (3,3), activation='relu'),\n        layers.MaxPooling2D((2,2)),\n        layers.Flatten(),\n        layers.Dense(512, activation='relu'),\n        layers.Dropout(0.5),\n        layers.Dense(120, activation='softmax')\n    ])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tpu_strategy.scope():\n    model.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n    model.fit(train_generator,\n          epochs=10,\n          validation_data=valid_generator)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nAUTOTUNE = tf.data.AUTOTUNE\n\ntest_path = 'test_images/'\ntest_labels = 'test_labels.csv'\n\ntest_data = tf.data.experimental.CsvDataset(\n    test_labels,\n    record_defaults=[tf.string, tf.string],\n    header=True\n)\n\ndef load_and_preprocess_test_image(path, label):\n    image = tf.io.read_file(test_path + path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [224, 224])\n    image = tf.keras.applications.mobilenet_v2.preprocess_input(image)\n    return image, label\n\ntest_dataset = test_data.map(load_and_preprocess_test_image, num_parallel_calls=AUTOTUNE)\n","metadata":{},"execution_count":null,"outputs":[]}]}