{"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 # 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 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\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 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","execution":{"iopub.status.busy":"2021-05-25T17:47:13.126853Z","iopub.execute_input":"2021-05-25T17:47:13.127264Z","iopub.status.idle":"2021-05-25T17:47:27.039695Z","shell.execute_reply.started":"2021-05-25T17:47:13.127154Z","shell.execute_reply":"2021-05-25T17:47:27.029026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:27.050178Z","iopub.execute_input":"2021-05-25T17:47:27.050428Z","iopub.status.idle":"2021-05-25T17:47:27.817042Z","shell.execute_reply.started":"2021-05-25T17:47:27.050402Z","shell.execute_reply":"2021-05-25T17:47:27.816159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/dog-breed-identification/train'\ntest_dir = '../input/dog-breed-identification/test'\ntrain_size = len(os.listdir('../input/dog-breed-identification/train'))\ntest_size = len(os.listdir('../input/dog-breed-identification/test'))\n\ntrain_size,test_size","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:27.821866Z","iopub.execute_input":"2021-05-25T17:47:27.823962Z","iopub.status.idle":"2021-05-25T17:47:27.849173Z","shell.execute_reply.started":"2021-05-25T17:47:27.823918Z","shell.execute_reply":"2021-05-25T17:47:27.848432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/dog-breed-identification/labels.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:27.853021Z","iopub.execute_input":"2021-05-25T17:47:27.85493Z","iopub.status.idle":"2021-05-25T17:47:27.903958Z","shell.execute_reply.started":"2021-05-25T17:47:27.85489Z","shell.execute_reply":"2021-05-25T17:47:27.903157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extracting different classes\ndog_breeds = sorted(df['breed'].unique())\nn_classes = len(dog_breeds)\nprint(n_classes)\ndog_breeds","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:27.90746Z","iopub.execute_input":"2021-05-25T17:47:27.909524Z","iopub.status.idle":"2021-05-25T17:47:27.92815Z","shell.execute_reply.started":"2021-05-25T17:47:27.909484Z","shell.execute_reply":"2021-05-25T17:47:27.927441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting classes to numbers\nclass_to_num = dict(zip(dog_breeds,range(n_classes)))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:27.931522Z","iopub.execute_input":"2021-05-25T17:47:27.933447Z","iopub.status.idle":"2021-05-25T17:47:27.938412Z","shell.execute_reply.started":"2021-05-25T17:47:27.933407Z","shell.execute_reply":"2021-05-25T17:47:27.937543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function to load and convert images to array\nfrom keras.preprocessing.image import load_img\nfrom keras.utils import to_categorical\n\ndef images_to_array(data_dir,df,image_size):\n    image_names = df['id']\n    image_labels = df['breed']\n    data_size = len(image_names)\n    \n    X = np.zeros([data_size,image_size[0],image_size[1],image_size[2]],dtype = np.uint8)\n    y = np.zeros([data_size,1],dtype = np.uint8)\n    for i in range(data_size):\n        img_name = image_names[i]\n        img_dir = os.path.join(data_dir,img_name+'.jpg')\n        img_pixels = load_img(img_dir,target_size=image_size)\n        X[i] = img_pixels\n        y[i] = class_to_num[image_labels[i]]\n        \n    y = to_categorical(y)\n    \n    ind = np.random.permutation(data_size)\n    X = X[ind]\n    y = y[ind]\n    print('Ouptut Data Size: ', X.shape)\n    print('Ouptut Label Size: ', y.shape)\n    return X, y     ","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:27.939374Z","iopub.execute_input":"2021-05-25T17:47:27.939773Z","iopub.status.idle":"2021-05-25T17:47:32.799891Z","shell.execute_reply.started":"2021-05-25T17:47:27.939743Z","shell.execute_reply":"2021-05-25T17:47:32.798967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Selecting image size according to pretrained models\nimg_size = (299,299,3)\nX, y = images_to_array(train_dir,df,img_size)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:47:32.801252Z","iopub.execute_input":"2021-05-25T17:47:32.801636Z","iopub.status.idle":"2021-05-25T17:48:37.491586Z","shell.execute_reply.started":"2021-05-25T17:47:32.801597Z","shell.execute_reply":"2021-05-25T17:48:37.490538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import BatchNormalization, Dense, GlobalAveragePooling2D,Lambda, Dropout, InputLayer, Input\n\ndef get_features(model_name, data_preprocessor, input_size, data):\n    #Prepare pipeline.\n    input_layer = Input(input_size)\n    preprocessor = Lambda(data_preprocessor)(input_layer)\n    base_model = model_name(weights='imagenet', include_top=False,\n                            input_shape=input_size)(preprocessor)\n    avg = GlobalAveragePooling2D()(base_model)\n    feature_extractor = Model(inputs = input_layer, outputs = avg)\n    #Extract feature.\n    feature_maps = feature_extractor.predict(data, batch_size=32, verbose=1)\n    print('Feature maps shape: ', feature_maps.shape)\n    return feature_maps","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:48:37.494515Z","iopub.execute_input":"2021-05-25T17:48:37.494838Z","iopub.status.idle":"2021-05-25T17:48:37.501037Z","shell.execute_reply.started":"2021-05-25T17:48:37.49481Z","shell.execute_reply":"2021-05-25T17:48:37.500114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extracting features using InceptionV3\nfrom keras.applications.inception_v3 import InceptionV3, preprocess_input\ninception_preprocessor = preprocess_input\ninception_features = get_features(InceptionV3,\n                                  inception_preprocessor,\n                                  img_size, X)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:48:37.502878Z","iopub.execute_input":"2021-05-25T17:48:37.503226Z","iopub.status.idle":"2021-05-25T17:49:11.208578Z","shell.execute_reply.started":"2021-05-25T17:48:37.503191Z","shell.execute_reply":"2021-05-25T17:49:11.207565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extracting features using Xception\nfrom keras.applications.xception import Xception, preprocess_input\nxception_preprocessor = preprocess_input\nxception_features = get_features(Xception,\n                                 xception_preprocessor,\n                                 img_size, X)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:49:11.21031Z","iopub.execute_input":"2021-05-25T17:49:11.210679Z","iopub.status.idle":"2021-05-25T17:49:57.824336Z","shell.execute_reply.started":"2021-05-25T17:49:11.210622Z","shell.execute_reply":"2021-05-25T17:49:57.823501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extracting features using InceptionResnetV2\nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\ninc_resnet_preprocessor = preprocess_input\ninc_resnet_features = get_features(InceptionResNetV2,\n                                   inc_resnet_preprocessor,\n                                   img_size, X)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:49:57.828101Z","iopub.execute_input":"2021-05-25T17:49:57.829995Z","iopub.status.idle":"2021-05-25T17:51:07.659522Z","shell.execute_reply.started":"2021-05-25T17:49:57.829955Z","shell.execute_reply":"2021-05-25T17:51:07.658693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#concatinating features\nfinal_features = np.concatenate([inception_features,\n                                 xception_features,\n                                 inc_resnet_features,], axis=-1)\nprint('Final feature maps shape', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:51:07.663391Z","iopub.execute_input":"2021-05-25T17:51:07.664513Z","iopub.status.idle":"2021-05-25T17:51:07.769214Z","shell.execute_reply.started":"2021-05-25T17:51:07.664441Z","shell.execute_reply":"2021-05-25T17:51:07.768292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Callbacks\nfrom keras.callbacks import EarlyStopping\nEarlyStop_callback = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\nmy_callback=[EarlyStop_callback]\n#Building Model\nfrom keras.models import Sequential\nmodel = Sequential()\nmodel.add(InputLayer(final_features.shape[1:]))\nmodel.add(Dropout(0.7))\nmodel.add(Dense(120,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:51:07.77323Z","iopub.execute_input":"2021-05-25T17:51:07.775302Z","iopub.status.idle":"2021-05-25T17:51:07.805571Z","shell.execute_reply.started":"2021-05-25T17:51:07.775238Z","shell.execute_reply":"2021-05-25T17:51:07.804704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compiling Model\nmodel.compile(optimizer='adam',\n            loss='categorical_crossentropy',\n            metrics=['accuracy'])\n#Training Model\nhistory = model.fit(final_features,\n                  y,\n                  batch_size=32,\n                  epochs=50,\n                  validation_split=0.1,\n                  callbacks=my_callback)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:51:07.809317Z","iopub.execute_input":"2021-05-25T17:51:07.811497Z","iopub.status.idle":"2021-05-25T17:51:17.471401Z","shell.execute_reply.started":"2021-05-25T17:51:07.811459Z","shell.execute_reply":"2021-05-25T17:51:17.470609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting test images to array\ndef images_to_array2(data_dir,df, img_size):\n    images_names = df['id']\n    data_size = len(images_names)\n    X = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)\n    \n    for i in range(data_size):\n        image_name = images_names[i]\n        img_dir = os.path.join(data_dir, image_name+'.jpg')\n        img_pixels = load_img(img_dir, target_size=img_size)\n        X[i] = img_pixels\n        \n    print('Ouptut Data Size: ', X.shape)\n    return X","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:51:17.47278Z","iopub.execute_input":"2021-05-25T17:51:17.473131Z","iopub.status.idle":"2021-05-25T17:51:17.478895Z","shell.execute_reply.started":"2021-05-25T17:51:17.473083Z","shell.execute_reply":"2021-05-25T17:51:17.478107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\ntest_data = images_to_array2(test_dir, sample_df, img_size)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:51:17.480234Z","iopub.execute_input":"2021-05-25T17:51:17.480835Z","iopub.status.idle":"2021-05-25T17:52:39.463977Z","shell.execute_reply.started":"2021-05-25T17:51:17.48078Z","shell.execute_reply":"2021-05-25T17:52:39.462458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extract test data features.\ninception_features = get_features(InceptionV3, inception_preprocessor, img_size, test_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:52:39.465321Z","iopub.execute_input":"2021-05-25T17:52:39.465688Z","iopub.status.idle":"2021-05-25T17:53:08.427337Z","shell.execute_reply.started":"2021-05-25T17:52:39.465631Z","shell.execute_reply":"2021-05-25T17:53:08.425312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xception_features = get_features(Xception, xception_preprocessor, img_size, test_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:53:08.431447Z","iopub.execute_input":"2021-05-25T17:53:08.431717Z","iopub.status.idle":"2021-05-25T17:53:53.949063Z","shell.execute_reply.started":"2021-05-25T17:53:08.431688Z","shell.execute_reply":"2021-05-25T17:53:53.947629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inc_resnet_features = get_features(InceptionResNetV2, inc_resnet_preprocessor, img_size, test_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:53:53.952507Z","iopub.execute_input":"2021-05-25T17:53:53.953024Z","iopub.status.idle":"2021-05-25T17:55:04.384386Z","shell.execute_reply.started":"2021-05-25T17:53:53.952981Z","shell.execute_reply":"2021-05-25T17:55:04.383423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features = np.concatenate([inception_features,\n                                 xception_features,\n                                 inc_resnet_features],axis=-1)\nprint('Final feature maps shape', test_features.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:55:04.387341Z","iopub.execute_input":"2021-05-25T17:55:04.387721Z","iopub.status.idle":"2021-05-25T17:55:04.475698Z","shell.execute_reply.started":"2021-05-25T17:55:04.387683Z","shell.execute_reply":"2021-05-25T17:55:04.474625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_features, batch_size=32)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:55:04.47709Z","iopub.execute_input":"2021-05-25T17:55:04.477439Z","iopub.status.idle":"2021-05-25T17:55:05.059894Z","shell.execute_reply.started":"2021-05-25T17:55:04.477401Z","shell.execute_reply":"2021-05-25T17:55:05.058991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for breed in dog_breeds:\n    sample_df[breed] = y_pred[:,class_to_num[breed]]\nsample_df.to_csv('pred.csv', index=None)\nsample_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:55:05.061259Z","iopub.execute_input":"2021-05-25T17:55:05.06166Z","iopub.status.idle":"2021-05-25T17:55:08.02262Z","shell.execute_reply.started":"2021-05-25T17:55:05.061619Z","shell.execute_reply":"2021-05-25T17:55:08.021645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}