{"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 of all the libraries we'll use\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport os\nfrom IPython.display import display, Image\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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# Libraries useful for our neural network model\n        \nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nimport keras\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import BatchNormalization, Dense, GlobalAveragePooling2D, Lambda, Dropout, InputLayer, Input\nfrom keras.callbacks import EarlyStopping\nfrom tqdm import tqdm\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:46.743112Z","iopub.execute_input":"2021-09-24T10:15:46.743418Z","iopub.status.idle":"2021-09-24T10:15:48.542922Z","shell.execute_reply.started":"2021-09-24T10:15:46.743389Z","shell.execute_reply":"2021-09-24T10:15:48.542083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_num_files(path):\n    '''\n    Function which counts the number of files in a folder.\n    '''\n    if not os.path.exists(path):\n        return 0\n    return sum([len(files) for root, directory, files in os.walk(path)])","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.544308Z","iopub.execute_input":"2021-09-24T10:15:48.544621Z","iopub.status.idle":"2021-09-24T10:15:48.550814Z","shell.execute_reply.started":"2021-09-24T10:15:48.544587Z","shell.execute_reply":"2021-09-24T10:15:48.548812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Paths\ntrain_dir = '/kaggle/input/dog-breed-identification/train'\ntest_dir = '/kaggle/input/dog-breed-identification/test'\n# Count/Print train and test samples.\ndata_size = get_num_files(train_dir)\ntest_size = get_num_files(test_dir)\nprint('Data samples size: ', data_size)\nprint('Test samples size: ', test_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.552904Z","iopub.execute_input":"2021-09-24T10:15:48.553247Z","iopub.status.idle":"2021-09-24T10:15:48.593524Z","shell.execute_reply.started":"2021-09-24T10:15:48.553215Z","shell.execute_reply":"2021-09-24T10:15:48.592802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read CSV of our data\n\nsample_df = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nlabels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nlabels.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.594983Z","iopub.execute_input":"2021-09-24T10:15:48.595309Z","iopub.status.idle":"2021-09-24T10:15:48.893139Z","shell.execute_reply.started":"2021-09-24T10:15:48.595277Z","shell.execute_reply":"2021-09-24T10:15:48.892231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# Append the extension of the picture\n\ndef append_ext(fn):\n    \"\"\" Function which appends the extension of a picture\"\"\"\n    return fn+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.894573Z","iopub.execute_input":"2021-09-24T10:15:48.89495Z","iopub.status.idle":"2021-09-24T10:15:48.899536Z","shell.execute_reply.started":"2021-09-24T10:15:48.894913Z","shell.execute_reply":"2021-09-24T10:15:48.898569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Applying the extension of a picture\n\nlabels_ext_train = labels\nlabels_ext_train[\"id\"] = labels_ext_train[\"id\"].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.901012Z","iopub.execute_input":"2021-09-24T10:15:48.901387Z","iopub.status.idle":"2021-09-24T10:15:48.91503Z","shell.execute_reply.started":"2021-09-24T10:15:48.901352Z","shell.execute_reply":"2021-09-24T10:15:48.914189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Applying the extension of a picture\n\nsample_df[\"id\"] = sample_df[\"id\"].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.917135Z","iopub.execute_input":"2021-09-24T10:15:48.917382Z","iopub.status.idle":"2021-09-24T10:15:48.92721Z","shell.execute_reply.started":"2021-09-24T10:15:48.91736Z","shell.execute_reply":"2021-09-24T10:15:48.92646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creation of the Series for testing images\n\nlabels_ext_test = pd.DataFrame(sample_df[\"id\"], columns = ['id'])","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.928568Z","iopub.execute_input":"2021-09-24T10:15:48.928979Z","iopub.status.idle":"2021-09-24T10:15:48.93691Z","shell.execute_reply.started":"2021-09-24T10:15:48.928944Z","shell.execute_reply":"2021-09-24T10:15:48.936182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_ext_test","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.940616Z","iopub.execute_input":"2021-09-24T10:15:48.940989Z","iopub.status.idle":"2021-09-24T10:15:48.95238Z","shell.execute_reply.started":"2021-09-24T10:15:48.940927Z","shell.execute_reply":"2021-09-24T10:15:48.95143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create list of alphabetically sorted labels.\ndog_breeds = sorted(list(set(labels['breed'])))\nn_classes = len(dog_breeds)\nprint(n_classes)\ndog_breeds[:10]","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.953948Z","iopub.execute_input":"2021-09-24T10:15:48.954286Z","iopub.status.idle":"2021-09-24T10:15:48.962142Z","shell.execute_reply.started":"2021-09-24T10:15:48.954253Z","shell.execute_reply":"2021-09-24T10:15:48.9611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Map each label string to an integer label.\n\nclass_to_num = dict(zip(dog_breeds, range(n_classes)))\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.963675Z","iopub.execute_input":"2021-09-24T10:15:48.964088Z","iopub.status.idle":"2021-09-24T10:15:48.975886Z","shell.execute_reply.started":"2021-09-24T10:15:48.964051Z","shell.execute_reply":"2021-09-24T10:15:48.974919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generation of transformed images for train and validation data\n\ndatagen=ImageDataGenerator(rescale=1./255.,validation_split=0.25)\n\ntrain_generator=datagen.flow_from_dataframe(\ndataframe=labels_ext_train,\ndirectory=\"../input/dog-breed-identification/train\",\nx_col=\"id\",\ny_col=\"breed\",\nsubset=\"training\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(299,299))\n\nvalid_generator=datagen.flow_from_dataframe(\ndataframe=labels_ext_train,\ndirectory=\"../input/dog-breed-identification/train\",\nx_col=\"id\",\ny_col=\"breed\",\nsubset=\"validation\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(299,299))","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:15:48.976999Z","iopub.execute_input":"2021-09-24T10:15:48.977562Z","iopub.status.idle":"2021-09-24T10:16:00.411484Z","shell.execute_reply.started":"2021-09-24T10:15:48.977527Z","shell.execute_reply":"2021-09-24T10:16:00.410601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Same thing for testing images\n\ntest_datagen=ImageDataGenerator(rescale=1./255.)\n\ntest_generator=test_datagen.flow_from_dataframe(\ndataframe=labels_ext_test,\ndirectory=\"../input/dog-breed-identification/test\",\nx_col=\"id\",\ny_col=None,\nbatch_size=32,\nseed=42,\nshuffle=False,\nclass_mode=None,\ntarget_size=(32,32))","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:16:00.412766Z","iopub.execute_input":"2021-09-24T10:16:00.413257Z","iopub.status.idle":"2021-09-24T10:16:04.215876Z","shell.execute_reply.started":"2021-09-24T10:16:00.413221Z","shell.execute_reply":"2021-09-24T10:16:04.215042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Selection of the pre-trained model\n\nimg_size = (299,299,3)\nmodel = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=img_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:16:04.217221Z","iopub.execute_input":"2021-09-24T10:16:04.217557Z","iopub.status.idle":"2021-09-24T10:16:10.182069Z","shell.execute_reply.started":"2021-09-24T10:16:04.217523Z","shell.execute_reply":"2021-09-24T10:16:10.181159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define an early stopping to stop after a number of epochs which the validation loss doesn't decrease anymore\n\nEarlyStop_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)\nmy_callback=[EarlyStop_callback]","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:16:10.184085Z","iopub.execute_input":"2021-09-24T10:16:10.184424Z","iopub.status.idle":"2021-09-24T10:16:10.189418Z","shell.execute_reply.started":"2021-09-24T10:16:10.18439Z","shell.execute_reply":"2021-09-24T10:16:10.188052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare DNN model\n\nx = model.output\nx = GlobalAveragePooling2D()(x)\n# let's add a fully-connected layer\nx = Dense(1024, activation='relu')(x)\n# and a logistic layer -- let's say we have 120 classes\npredictions = Dense(n_classes, activation='softmax')(x)\n\nmodel = Model(inputs=model.input, outputs=predictions)\n\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:16:10.190707Z","iopub.execute_input":"2021-09-24T10:16:10.191213Z","iopub.status.idle":"2021-09-24T10:16:10.281349Z","shell.execute_reply.started":"2021-09-24T10:16:10.191176Z","shell.execute_reply":"2021-09-24T10:16:10.280608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training of the model\n\nSTEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size\nSTEP_SIZE_TEST=test_generator.n//test_generator.batch_size\n\nmodel.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=5\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:16:10.282466Z","iopub.execute_input":"2021-09-24T10:16:10.282807Z","iopub.status.idle":"2021-09-24T10:17:00.037379Z","shell.execute_reply.started":"2021-09-24T10:16:10.282774Z","shell.execute_reply":"2021-09-24T10:17:00.035023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation of the model\n\nmodel.evaluate_generator(generator=valid_generator, steps=STEP_SIZE_TEST)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:17:00.03831Z","iopub.status.idle":"2021-09-24T10:17:00.038691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prediction of labels for testing images\n\ntest_generator.reset()\npred = model.predict_generator(test_generator, steps = STEP_SIZE_TEST,verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:17:00.039705Z","iopub.status.idle":"2021-09-24T10:17:00.04014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices = np.argmax(pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:17:00.041085Z","iopub.status.idle":"2021-09-24T10:17:00.041656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions = [labels[k] for k in predicted_class_indices]","metadata":{"execution":{"iopub.status.busy":"2021-09-24T10:17:00.042759Z","iopub.status.idle":"2021-09-24T10:17:00.043309Z"},"trusted":true},"execution_count":null,"outputs":[]}]}