{"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":"import os\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom skimage import io\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import Input, Dense, Conv2D, MaxPool2D, Dropout, Flatten, AveragePooling2D\nfrom tensorflow.keras.optimizers import RMSprop, SGD\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.xception import Xception\n\nSEED = 42\nSIZE = (224, 224)\nBATCH_SIZE = 32\n\npd.set_option('display.max_rows', None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-16T07:20:32.828577Z","iopub.execute_input":"2022-05-16T07:20:32.828942Z","iopub.status.idle":"2022-05-16T07:20:39.398691Z","shell.execute_reply.started":"2022-05-16T07:20:32.828857Z","shell.execute_reply":"2022-05-16T07:20:39.39796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:20:39.400568Z","iopub.execute_input":"2022-05-16T07:20:39.401086Z","iopub.status.idle":"2022-05-16T07:20:39.449686Z","shell.execute_reply.started":"2022-05-16T07:20:39.401049Z","shell.execute_reply":"2022-05-16T07:20:39.449068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['id'] = labels['id'].apply(lambda x: x + '.jpg')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:20:39.450977Z","iopub.execute_input":"2022-05-16T07:20:39.451487Z","iopub.status.idle":"2022-05-16T07:20:39.476145Z","shell.execute_reply.started":"2022-05-16T07:20:39.451449Z","shell.execute_reply":"2022-05-16T07:20:39.471326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['breed'].value_counts().plot.bar(figsize=(16, 8))","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:20:39.480268Z","iopub.execute_input":"2022-05-16T07:20:39.480487Z","iopub.status.idle":"2022-05-16T07:20:41.095304Z","shell.execute_reply.started":"2022-05-16T07:20:39.480461Z","shell.execute_reply":"2022-05-16T07:20:41.094605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator = ImageDataGenerator(rescale= 1./255, validation_split=0.2, rotation_range=20,\n                                    zoom_range=0.1, width_shift_range=0.2, height_shift_range=0.2,\n                                    shear_range=0.1, horizontal_flip=True, fill_mode=\"nearest\")","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:20:41.096231Z","iopub.execute_input":"2022-05-16T07:20:41.096472Z","iopub.status.idle":"2022-05-16T07:20:41.103752Z","shell.execute_reply.started":"2022-05-16T07:20:41.096438Z","shell.execute_reply":"2022-05-16T07:20:41.102996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = data_generator.flow_from_dataframe(labels, directory='../input/dog-breed-identification/train/', x_col='id', y_col='breed', target_size=SIZE, class_mode='categorical', batch_size=BATCH_SIZE, shuffle=True, seed=SEED, subset='training')\nval_generator = data_generator.flow_from_dataframe(labels, directory='../input/dog-breed-identification/train/', x_col='id', y_col='breed', target_size=SIZE, class_mode='categorical', batch_size=BATCH_SIZE, shuffle=True, seed=SEED, subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:20:41.104941Z","iopub.execute_input":"2022-05-16T07:20:41.105764Z","iopub.status.idle":"2022-05-16T07:21:05.55124Z","shell.execute_reply.started":"2022-05-16T07:20:41.105735Z","shell.execute_reply":"2022-05-16T07:21:05.550405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = next(train_generator)\n\nfig = plt.figure(figsize=(15, 10))\n\nfor i in range(12):\n    fig.add_subplot(3, 4, i+1)\n    plt.imshow(img[i])\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:05.552528Z","iopub.execute_input":"2022-05-16T07:21:05.55291Z","iopub.status.idle":"2022-05-16T07:21:06.817539Z","shell.execute_reply.started":"2022-05-16T07:21:05.552876Z","shell.execute_reply":"2022-05-16T07:21:06.816852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss', mode = 'min', patience=10)\ncheckpoint = ModelCheckpoint(filepath = './weights.hdf5', verbose=1, save_best_only=True)\\","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:06.81862Z","iopub.execute_input":"2022-05-16T07:21:06.818995Z","iopub.status.idle":"2022-05-16T07:21:06.827109Z","shell.execute_reply.started":"2022-05-16T07:21:06.81896Z","shell.execute_reply":"2022-05-16T07:21:06.826084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = Xception(weights=\"imagenet\", include_top=False, input_tensor=Input(shape=(224, 224, 3)))\n\nbase_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:06.829009Z","iopub.execute_input":"2022-05-16T07:21:06.82963Z","iopub.status.idle":"2022-05-16T07:21:11.543527Z","shell.execute_reply.started":"2022-05-16T07:21:06.829591Z","shell.execute_reply":"2022-05-16T07:21:11.542798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:11.54665Z","iopub.execute_input":"2022-05-16T07:21:11.547022Z","iopub.status.idle":"2022-05-16T07:21:11.556993Z","shell.execute_reply.started":"2022-05-16T07:21:11.54699Z","shell.execute_reply":"2022-05-16T07:21:11.556335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"head_model = AveragePooling2D(pool_size=(4, 4))(base_model.output)\nhead_model = Flatten(name='flatten')(head_model)\nhead_model = Dense(1024, activation='relu')(head_model)\nhead_model = Dropout(0.3)(head_model)\nhead_model = Dense(512, activation='relu')(head_model)\nhead_model = Dropout(0.3)(head_model)\nhead_model = Dense(120, activation='softmax')(head_model)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:11.558571Z","iopub.execute_input":"2022-05-16T07:21:11.558861Z","iopub.status.idle":"2022-05-16T07:21:11.596958Z","shell.execute_reply.started":"2022-05-16T07:21:11.558788Z","shell.execute_reply":"2022-05-16T07:21:11.596327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(inputs=base_model.input, outputs=head_model)\noptimizer = SGD(learning_rate=0.1, momentum=0.9, decay=0.01)\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=optimizer, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:11.598242Z","iopub.execute_input":"2022-05-16T07:21:11.598463Z","iopub.status.idle":"2022-05-16T07:21:11.621656Z","shell.execute_reply.started":"2022-05-16T07:21:11.598431Z","shell.execute_reply":"2022-05-16T07:21:11.621038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1 = model.fit(train_generator, epochs=5, validation_data=val_generator, callbacks=[checkpoint])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:21:11.622909Z","iopub.execute_input":"2022-05-16T07:21:11.623166Z","iopub.status.idle":"2022-05-16T07:34:20.139682Z","shell.execute_reply.started":"2022-05-16T07:21:11.623134Z","shell.execute_reply":"2022-05-16T07:34:20.138928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history1.history['accuracy'], label='training accuracy')\nplt.plot(history1.history['val_accuracy'], label='validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:34:20.141383Z","iopub.execute_input":"2022-05-16T07:34:20.141628Z","iopub.status.idle":"2022-05-16T07:34:20.336354Z","shell.execute_reply.started":"2022-05-16T07:34:20.141596Z","shell.execute_reply":"2022-05-16T07:34:20.335706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history1.history['loss'], label='training loss')\nplt.plot(history1.history['val_loss'], label='validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:34:20.337387Z","iopub.execute_input":"2022-05-16T07:34:20.338142Z","iopub.status.idle":"2022-05-16T07:34:20.539933Z","shell.execute_reply.started":"2022-05-16T07:34:20.338106Z","shell.execute_reply":"2022-05-16T07:34:20.539331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers[len(base_model.layers)//2:]:\n    layer.trainable = True\n\noptimizer = SGD(learning_rate=0.01, momentum=0.9, decay=0.001)\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=optimizer, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:34:20.541Z","iopub.execute_input":"2022-05-16T07:34:20.541246Z","iopub.status.idle":"2022-05-16T07:34:20.557347Z","shell.execute_reply.started":"2022-05-16T07:34:20.541214Z","shell.execute_reply":"2022-05-16T07:34:20.556547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model.fit(train_generator, epochs=10, validation_data=val_generator, callbacks=[checkpoint])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T07:34:20.558382Z","iopub.execute_input":"2022-05-16T07:34:20.559239Z","iopub.status.idle":"2022-05-16T08:00:36.541537Z","shell.execute_reply.started":"2022-05-16T07:34:20.559204Z","shell.execute_reply":"2022-05-16T08:00:36.54033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history2.history['accuracy'], label='training accuracy')\nplt.plot(history2.history['val_accuracy'], label='validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T08:00:36.543212Z","iopub.execute_input":"2022-05-16T08:00:36.543482Z","iopub.status.idle":"2022-05-16T08:00:36.726597Z","shell.execute_reply.started":"2022-05-16T08:00:36.543447Z","shell.execute_reply":"2022-05-16T08:00:36.725871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history2.history['loss'], label='training loss')\nplt.plot(history2.history['val_loss'], label='validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T08:00:36.727749Z","iopub.execute_input":"2022-05-16T08:00:36.728075Z","iopub.status.idle":"2022-05-16T08:00:36.909201Z","shell.execute_reply.started":"2022-05-16T08:00:36.728039Z","shell.execute_reply":"2022-05-16T08:00:36.908553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('./weights.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T08:00:36.910224Z","iopub.execute_input":"2022-05-16T08:00:36.910601Z","iopub.status.idle":"2022-05-16T08:00:37.243475Z","shell.execute_reply.started":"2022-05-16T08:00:36.910565Z","shell.execute_reply":"2022-05-16T08:00:37.242484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = True\n\noptimizer = SGD(learning_rate=0.01, momentum=0.9, decay=0.001)\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=optimizer, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T08:00:37.244818Z","iopub.execute_input":"2022-05-16T08:00:37.245064Z","iopub.status.idle":"2022-05-16T08:00:37.27432Z","shell.execute_reply.started":"2022-05-16T08:00:37.245016Z","shell.execute_reply":"2022-05-16T08:00:37.273571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history3 = model.fit(train_generator, epochs=100, validation_data=val_generator, callbacks=[checkpoint, early_stopping])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T08:00:37.275588Z","iopub.execute_input":"2022-05-16T08:00:37.275807Z","iopub.status.idle":"2022-05-16T08:59:44.315459Z","shell.execute_reply.started":"2022-05-16T08:00:37.275777Z","shell.execute_reply":"2022-05-16T08:59:44.314709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history3.history['accuracy'], label='training accuracy')\nplt.plot(history3.history['val_accuracy'], label='validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T08:59:44.316923Z","iopub.execute_input":"2022-05-16T08:59:44.317134Z","iopub.status.idle":"2022-05-16T09:00:10.118529Z","shell.execute_reply.started":"2022-05-16T08:59:44.317109Z","shell.execute_reply":"2022-05-16T09:00:10.117889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history3.history['loss'], label='training loss')\nplt.plot(history3.history['val_loss'], label='validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:00:10.119667Z","iopub.execute_input":"2022-05-16T09:00:10.119919Z","iopub.status.idle":"2022-05-16T09:00:10.317621Z","shell.execute_reply.started":"2022-05-16T09:00:10.119886Z","shell.execute_reply":"2022-05-16T09:00:10.316876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_files_names = os.listdir('../input/dog-breed-identification/test/')\ntest_set = pd.DataFrame(test_images_files_names, columns=['id'])\ntest_set.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:00:10.318839Z","iopub.execute_input":"2022-05-16T09:00:10.319091Z","iopub.status.idle":"2022-05-16T09:00:10.667945Z","shell.execute_reply.started":"2022-05-16T09:00:10.319057Z","shell.execute_reply":"2022-05-16T09:00:10.667243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_generator = ImageDataGenerator(rescale= 1./255)\ntest_generator = test_data_generator.flow_from_dataframe(test_set, directory='../input/dog-breed-identification/test/', x_col='id',target_size=SIZE, class_mode=None, batch_size=BATCH_SIZE, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:00:10.669153Z","iopub.execute_input":"2022-05-16T09:00:10.6697Z","iopub.status.idle":"2022-05-16T09:00:32.319884Z","shell.execute_reply.started":"2022-05-16T09:00:10.669657Z","shell.execute_reply":"2022-05-16T09:00:32.319095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('./weights.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:00:32.321453Z","iopub.execute_input":"2022-05-16T09:00:32.321977Z","iopub.status.idle":"2022-05-16T09:00:32.618732Z","shell.execute_reply.started":"2022-05-16T09:00:32.321937Z","shell.execute_reply":"2022-05-16T09:00:32.617953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prop = model.predict(test_generator)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-16T16:47:51.516945Z","iopub.execute_input":"2022-05-16T16:47:51.517812Z","iopub.status.idle":"2022-05-16T16:47:51.533306Z","shell.execute_reply.started":"2022-05-16T16:47:51.517765Z","shell.execute_reply":"2022-05-16T16:47:51.532477Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame(columns=[\"id\"] + [*train_generator.class_indices.keys()])\nresults","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:02:14.505726Z","iopub.execute_input":"2022-05-16T09:02:14.505985Z","iopub.status.idle":"2022-05-16T09:02:14.526738Z","shell.execute_reply.started":"2022-05-16T09:02:14.505952Z","shell.execute_reply":"2022-05-16T09:02:14.52607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results[\"id\"] = [os.path.splitext(file)[0] for file in os.listdir('../input/dog-breed-identification/test/')]\nresults.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:02:14.528131Z","iopub.execute_input":"2022-05-16T09:02:14.528396Z","iopub.status.idle":"2022-05-16T09:02:14.580293Z","shell.execute_reply.started":"2022-05-16T09:02:14.528361Z","shell.execute_reply":"2022-05-16T09:02:14.57953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results[[*train_generator.class_indices.keys()]] = y_prop\nresults.head()\n\nresults.to_csv(\"results.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T09:02:14.581434Z","iopub.execute_input":"2022-05-16T09:02:14.584214Z","iopub.status.idle":"2022-05-16T09:02:16.716072Z","shell.execute_reply.started":"2022-05-16T09:02:14.58418Z","shell.execute_reply":"2022-05-16T09:02:16.715227Z"},"trusted":true},"execution_count":null,"outputs":[]}]}