{"cells":[{"metadata":{"trusted":true,"_uuid":"239e8464a47560d6add88ef1ae081375eaa64f1d"},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8e69cbcadb38cad515d6ca8649882adb0313669"},"cell_type":"code","source":"from 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":{"trusted":true,"_uuid":"627548ee53dc80d52d65ce49b395a51fd726193c"},"cell_type":"code","source":"traindf=pd.read_csv('../input/dog-breed-identification/labels.csv')\ntestdf=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cb4eecf8d5291ca9e07e4c106204d47d86ec8b41"},"cell_type":"markdown","source":"## Is there any missing class:"},{"metadata":{"trusted":true,"_uuid":"c01eecc0843c398d99bf91603a550dbc0eaa1e6a"},"cell_type":"code","source":"\n\ntraindf.isnull().values.any()\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d5929088b9bbb54c0fac9fbc473ea2268284e76"},"cell_type":"markdown","source":"## Is there a problem of Class Imbalance ?"},{"metadata":{"trusted":true,"_uuid":"f4754abfc8d2b13baa28b8503b3caa75ff571ffe"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(13, 6))\ntraindf['breed'].value_counts().plot(kind='bar')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0a6808aed7b21ee93ec7a6844388c7766589e22"},"cell_type":"code","source":"def class_percentages(labels):\n    class_map={}\n    for i in labels:\n        if str(i) not in class_map:\n            class_map[str(i)]=1\n        else:\n            class_map[str(i)]+=1\n    #     print(class_map)\n    return class_map\n\np=class_percentages(traindf.breed.values)\n# print(p)\n# for i in p.items():\n#     print(i)\n\nprint(\"Class with maximum images-\"+str(max(p, key=p.get))+\"  \"+str(p[max(p, key=p.get)]))\nprint(\"Class with maximum images-\"+str(min(p,key=p.get)) +\"  \"+str(p[min(p, key=p.get)]))\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bbefde29368d88a4658ce2ab4dd7d3609ab2cfc5"},"cell_type":"markdown","source":"## Clearly , there is some amount of class imbalance ,\n## as biggest class contains almost double of smallest class"},{"metadata":{"trusted":true,"_uuid":"d2f67d0e02a6dcfa1a7ed83bd86527e3a29793cb"},"cell_type":"code","source":"traindf.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"33595d442a0d3e3db494f9c443201463b7e7cc1c"},"cell_type":"markdown","source":"\n## My Model using extracted features from ResNet Pretrained\n\n"},{"metadata":{"trusted":true,"_uuid":"8c920a81a19a01543ccd5573f15fdb6685014f89"},"cell_type":"code","source":"num_classes = 120\nresnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nmy_new_model = Sequential()\nmy_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))\nmy_new_model.add(Dense(512))\nmy_new_model.add(Activation('relu'))\nmy_new_model.add(Dropout(0.5))\nmy_new_model.add(Dense(num_classes, activation='softmax'))\n\n# Say not to train first layer (ResNet) model. It is already trained\nmy_new_model.layers[0].trainable = False\n\nmy_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84174d590f0901b75f928c85989af5aa0b1fee94"},"cell_type":"markdown","source":"## Data Preperation"},{"metadata":{"trusted":true,"_uuid":"dc795fe1b79beb4f758fe84c0c642d99e1de1f43"},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"161cafb4910859b81ef6c1fc6aa06c82b474b6f6"},"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\n                        dataframe=traindf,\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=32,\n                        seed=50,\n                        shuffle=True,\n                        class_mode=\"categorical\",\n                        target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9edb633fa03b9f0fd08a35d380ff43f231fc433"},"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\n                        dataframe=traindf,\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":{"_uuid":"ee3b220ad7e083a0fc652d34f33b6d68da14d23f"},"cell_type":"markdown","source":"## Test Data Preperation"},{"metadata":{"trusted":true,"_uuid":"14039dee79d99512a28b64b358c7fbe024b75a78"},"cell_type":"code","source":"test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)\n\ntest_generator=test_datagen.flow_from_dataframe(\n                            dataframe=testdf,\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))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7fb66ccadec607d1e74eaefe05e7b0fd298e5a5"},"cell_type":"code","source":"STEP_SIZE_TRAIN=train_generator.n\nSTEP_SIZE_VALID=valid_generator.n\n\nprint(STEP_SIZE_TRAIN)\nprint(STEP_SIZE_VALID)\n\nmy_new_model.fit_generator(generator=train_generator,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=valid_generator,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=3\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"819a6c3ed049e73593d0cff3460e7c68af73239f"},"cell_type":"code","source":"\nmy_new_model.evaluate_generator(generator=valid_generator)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7798c269f7868595e94f4a7c6cb6cea28ebbcc19"},"cell_type":"code","source":"test_generator.reset()\npred=my_new_model.predict_generator(test_generator,verbose=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8098a59ec33c58abb266dde5b56190a43c108c4"},"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\nfilenames = testdf[\"id\"]\ndf[\"id\"]  = filenames\n\ncols = df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ndf = df[cols]\ndf.head(5)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"98567fb9c01117ea56a729dd6e657b99fb717282"},"cell_type":"markdown","source":"\n## Saving Test Data Output to 'submission.csv'"},{"metadata":{"trusted":true,"_uuid":"fcf9137e8e8b3e43cf4f2196312aba46caa312bc"},"cell_type":"code","source":"df.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e3b6110f6e68eaa7c49c284157910b78dbd486b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}