{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Making the necessary imports"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Please ensure that tensorflow version 1.13 is installed. Otherwise the code won't work\n# !pip install tensorflow==1.13.0rc1\n# import tensorflow as tf\n# print(tf.__version__)\nimport numpy as np \nimport pandas as pd \nfrom tensorflow.python.keras.applications import ResNet50\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Flatten, Dropout, BatchNormalization\n\nimport h5py","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Reading the data\nThe data consists of images (divided into the test and the train folders) each with there unique IDs. In the \"labels.csv\" corresponding to each ID is the name of the breed of the dog present in the image"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpg\"\n\ntrain_df=pd.read_csv('../input/dog-breed-identification/labels.csv')\ntest_df=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\n\n\ntrain_df[\"id\"]=train_df[\"id\"].apply(append_ext)\ntest_df[\"id\"]=test_df[\"id\"].apply(append_ext)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Distribution of Different breeds across the dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(13, 6))\ntrain_df['breed'].value_counts().plot(kind='bar')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model\nThe model is based upon the concepts of tranfer learning. A Deep convolutional neural network RESNET50 is used without its top layer. The model has been trained on a very generalised dataset and its learning can be used for our purposes. To retain the learning of this model its first layer is freezed and other layers atop it are trained. Additional layers added are Dense Layer (512) Activation Layer Dense Layer (120)"},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes = 120\nresnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nTL_model = Sequential()\nTL_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))\nTL_model.add(Dense(512))\nTL_model.add(Activation('relu'))\nTL_model.add(Dropout(0.5))\nTL_model.add(Dense(num_classes, activation='softmax'))\n\nTL_model.layers[0].trainable = False\n\nTL_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])\nTL_model.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Preperation"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.python.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nimage_size = 224\ndatagen = ImageDataGenerator(preprocessing_function=preprocess_input, \n                             rescale=1./255.,\n                             horizontal_flip=True,\n                             width_shift_range = 0.2,\n                             height_shift_range = 0.2,\n                             validation_split=0.2)\ntrain_batch_size = 10\ntrain_generator=datagen.flow_from_dataframe(\n                        dataframe=train_df,\n                        directory=\"../input/dog-breed-identification/train/\",\n                        x_col=\"id\",\n                        y_col=\"breed\",\n                        has_ext=False,\n                        subset=\"training\",\n                        batch_size=train_batch_size,\n                        seed=50,\n                        shuffle=True,\n                        class_mode=\"categorical\",\n                        target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\n                        dataframe=train_df,\n                        directory=\"../input/dog-breed-identification/train/\",\n                        x_col=\"id\",\n                        y_col=\"breed\",\n                        has_ext=False,\n                        subset=\"validation\",\n                        batch_size=1,\n                        seed=42,\n                        shuffle=True,\n                        class_mode=\"categorical\",\n                        target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)\n\ntest_generator=test_datagen.flow_from_dataframe(\n                            dataframe=test_df,\n                            directory=\"../input/dog-breed-identification/test/\",\n                            x_col=\"id\",\n                            y_col=None,\n                            has_ext=False,\n                            batch_size=1,\n                            seed=42,\n                            shuffle=False,\n                            class_mode=None,\n                            target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Training the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_step_size = train_generator.n\nvalidation_step_size = valid_generator.n\n\nTL_model.fit_generator(generator=train_generator,\n                    steps_per_epoch=train_step_size,\n                    validation_data=valid_generator,\n                    validation_steps=validation_step_size,\n                    epochs=25\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Evaluation"},{"metadata":{"trusted":true},"cell_type":"code","source":"TL_model.evaluate_generator(generator=valid_generator)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator.reset()\npred = TL_model.predict_generator(test_generator,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Conversion to CSV"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels = list(labels.keys())\ndf = pd.DataFrame(data=pred,\n                 columns=labels)\n\ncolumns = list(df)\ncolumns.sort()\ndf = df.reindex(columns=columns)\n\ntest_df=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nfilenames = test_df[\"id\"]\ndf[\"id\"]  = filenames\n\ncols = df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ndf = df[cols]\n# df.head(5)\ndf.to_csv(\"submission2.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}