{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom sklearn.model_selection import train_test_split\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nimport tensorflow_hub as hub\nimport random\n\n\nimport os\n\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Reading Labels","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Visualizing unique breeds count","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"labels[\"breed\"].value_counts().plot.bar(figsize=(20, 10));","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Importing images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames = os.listdir('../input/dog-breed-identification/train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames.sort()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames[:20]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Adding images and labels into dataframe\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame({'filenames': filenames,\n                   'breeds': labels['breed']})\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample=random.choice(filenames)\nimage = load_img(\"../input/dog-breed-identification/train/\"+sample)\nplt.imshow(image)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Splitting the data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df,val_df = train_test_split(df,test_size = 0.2,random_state = 42)\ntrain_df.shape,val_df.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Preprocessing the train ,val & test data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_data = train_datagen.flow_from_dataframe( \n    train_df,\n    \"../input/dog-breed-identification/train/\", \n    x_col='filenames',\n    y_col='breeds',\n    target_size=(224,224),\n    class_mode='categorical',\n    batch_size=32\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_datagen = ImageDataGenerator(rescale = 1./255)\nval_data = valid_datagen.flow_from_dataframe(val_df,\n                                             \"../input/dog-breed-identification/train/\",\n                                             x_col = 'filenames',\n                                             y_col = 'breeds',\n                                             target_size = (224,224),\n                                             class_mode = 'categorical',\n                                             batch_size = 32)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_files = os.listdir(\"../input/dog-breed-identification/test/\")\ntest_files.sort()\ntest_df = pd.DataFrame({\n    'test_files': test_files\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_data = test_gen.flow_from_dataframe(\n    test_df, \n    \"../input/dog-breed-identification/test/\", \n    x_col='test_files',\n    y_col=None,\n    class_mode=None,\n    target_size=(224,224),\n    batch_size=32,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Creating a function which build a model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"INPUT_SHAPE = [None, 224, 224, 3] \n\nOUTPUT_SHAPE = 120 \n\nMODEL_URL = \"https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model(input_shape=INPUT_SHAPE, output_shape=OUTPUT_SHAPE, model_url=MODEL_URL):\n    print(\"Building model with:\", MODEL_URL)\n\n  \n    model = tf.keras.Sequential([\n       hub.KerasLayer(MODEL_URL), \n       tf.keras.layers.Dense(units=OUTPUT_SHAPE, \n                          activation=\"softmax\")])\n\n  \n    model.compile(\n       loss=tf.keras.losses.CategoricalCrossentropy(), \n       optimizer=tf.keras.optimizers.Adam(), \n       metrics=[\"accuracy\"])\n\n \n    model.build(INPUT_SHAPE) \n  \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"early_stopping = EarlyStopping(monitor=\"val_accuracy\",\n                                                  patience=3) \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train_model():\n    model = create_model()\n\n    model.fit(x=train_data,\n            epochs=50,\n            validation_data=val_data,\n            validation_freq=1, \n            callbacks=early_stopping)\n    return model\n  \n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Training the model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = train_model()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Predicting on test data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.predict(test_data,verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds[:10]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* preparing a prediction data set for submission","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = (train_data.class_indices)\nlabels = list(labels.keys())\ndf = pd.DataFrame(data=preds,\n                 columns=labels)\n\ncolumns = list(df)\ncolumns.sort()\ndf = df.reindex(columns=columns)\n\nfilenames = sub[\"id\"]\ndf[\"id\"]  = filenames\n\ncols = df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ndf = df[cols]\ndf.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('submission_df.csv',index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}