{"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 pandas as pd\nimport numpy as np\nimport os\nfrom os.path import join\n\nimport PIL.Image\nfrom PIL import ImageOps\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Sequential, load_model, Model\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\n\nimport itertools\nfrom sklearn.metrics import classification_report, confusion_matrix\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:16.838477Z","iopub.execute_input":"2021-09-26T00:19:16.838862Z","iopub.status.idle":"2021-09-26T00:19:16.854770Z","shell.execute_reply.started":"2021-09-26T00:19:16.838825Z","shell.execute_reply":"2021-09-26T00:19:16.853249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1 - Dataset","metadata":{}},{"cell_type":"code","source":"# Local dos dados de treino e teste\ntrain_dir = '../input/dog-breed-identification/train'\ntest_dir = '../input/dog-breed-identification/test'","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:16.856555Z","iopub.execute_input":"2021-09-26T00:19:16.856811Z","iopub.status.idle":"2021-09-26T00:19:16.867086Z","shell.execute_reply.started":"2021-09-26T00:19:16.856778Z","shell.execute_reply":"2021-09-26T00:19:16.866335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tamanho dos dados de treino e teste\ntrain_img_fpaths = [join(train_dir, f) for f in os.listdir(train_dir)]\ntest_img_fpaths = [join(test_dir, f) for f in os.listdir(test_dir)]\n\ntrain_size = len(train_img_fpaths)\ntest_size = len(test_img_fpaths)\n\ntrain_size,test_size","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:16.868187Z","iopub.execute_input":"2021-09-26T00:19:16.868793Z","iopub.status.idle":"2021-09-26T00:19:18.127950Z","shell.execute_reply.started":"2021-09-26T00:19:16.868757Z","shell.execute_reply":"2021-09-26T00:19:18.127241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.1 Treino","metadata":{}},{"cell_type":"code","source":"# Dataframe de treino\ndf_train = pd.read_csv('../input/dog-breed-identification/labels.csv')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:18.129396Z","iopub.execute_input":"2021-09-26T00:19:18.129653Z","iopub.status.idle":"2021-09-26T00:19:18.183358Z","shell.execute_reply.started":"2021-09-26T00:19:18.129619Z","shell.execute_reply":"2021-09-26T00:19:18.182660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Quantidade de raças\nlen(df_train.breed.unique())","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:18.186890Z","iopub.execute_input":"2021-09-26T00:19:18.187189Z","iopub.status.idle":"2021-09-26T00:19:18.199158Z","shell.execute_reply.started":"2021-09-26T00:19:18.187159Z","shell.execute_reply":"2021-09-26T00:19:18.198444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reorganizando dataframe\ndf_train.rename(columns = {\"breed\": \"label\"}, inplace = True)\ndf_train[\"id\"] = df_train[\"id\"].apply(lambda x: x+\".\"+\"jpg\")\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:18.200980Z","iopub.execute_input":"2021-09-26T00:19:18.201226Z","iopub.status.idle":"2021-09-26T00:19:18.219200Z","shell.execute_reply.started":"2021-09-26T00:19:18.201196Z","shell.execute_reply":"2021-09-26T00:19:18.218447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.2 Teste","metadata":{}},{"cell_type":"code","source":"test_files = os.listdir(test_dir)\ndf_test = pd.DataFrame({\"id\": test_files, \"label\": \"boston_bull\"})\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:18.220336Z","iopub.execute_input":"2021-09-26T00:19:18.220627Z","iopub.status.idle":"2021-09-26T00:19:18.240641Z","shell.execute_reply.started":"2021-09-26T00:19:18.220593Z","shell.execute_reply":"2021-09-26T00:19:18.239960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tamanho dos dataframes de treino e teste\n\ntrain_size = len(df_train)\ntest_size = len(df_test)\n\ntrain_size,test_size","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:18.242032Z","iopub.execute_input":"2021-09-26T00:19:18.242273Z","iopub.status.idle":"2021-09-26T00:19:18.249977Z","shell.execute_reply.started":"2021-09-26T00:19:18.242242Z","shell.execute_reply":"2021-09-26T00:19:18.249020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2 - Image Generator Treino e Validação","metadata":{}},{"cell_type":"code","source":"# Dados de Treino\ntrain_data_gen = ImageDataGenerator(rescale = 1./255, validation_split = 0.2)\ntrain_generator = train_data_gen.flow_from_dataframe(df_train, train_dir, x_col = 'id', y_col = 'label', subset = 'training',\n                                                     color_mode = 'rgb', class_mode = 'categorical', target_size = (299, 299),\n                                                     batch_size = 32, shuffle = True, seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:18.251648Z","iopub.execute_input":"2021-09-26T00:19:18.251904Z","iopub.status.idle":"2021-09-26T00:19:35.038024Z","shell.execute_reply.started":"2021-09-26T00:19:18.251873Z","shell.execute_reply":"2021-09-26T00:19:35.037285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dados de Validação\nval_data_generator = ImageDataGenerator(rescale = 1./255, validation_split = 0.2)\nvalidation_generator = val_data_generator.flow_from_dataframe(df_train, train_dir, x_col = 'id', y_col = 'label',\n                                                              subset = 'validation', color_mode = 'rgb',\n                                                              class_mode = 'categorical', target_size = (299, 299),\n                                                              batch_size = 32, shuffle = True, seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:35.039309Z","iopub.execute_input":"2021-09-26T00:19:35.039715Z","iopub.status.idle":"2021-09-26T00:19:38.284677Z","shell.execute_reply.started":"2021-09-26T00:19:35.039678Z","shell.execute_reply":"2021-09-26T00:19:38.283967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3 - Processando imagens","metadata":{}},{"cell_type":"code","source":"# Função para transformar imagens em array\ndef image_preocessing(path):\n    image = PIL.Image.open(path)\n    size = (299, 299)\n    image = ImageOps.fit(image, size, PIL.Image.ANTIALIAS) #Retorna uma versão redimensionada e recortada da imagem, recortada no proporção e tamanho solicitados.\n    image_array = np.asarray(image)\n    \n    return image_array","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:38.285901Z","iopub.execute_input":"2021-09-26T00:19:38.286526Z","iopub.status.idle":"2021-09-26T00:19:38.291818Z","shell.execute_reply.started":"2021-09-26T00:19:38.286487Z","shell.execute_reply":"2021-09-26T00:19:38.290938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_aux = df_train.copy()\ndf_train_aux['img_path'] = train_dir + '/' + df_train_aux['id']\ndf_train_aux","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:38.292989Z","iopub.execute_input":"2021-09-26T00:19:38.293415Z","iopub.status.idle":"2021-09-26T00:19:38.317303Z","shell.execute_reply.started":"2021-09-26T00:19:38.293351Z","shell.execute_reply":"2021-09-26T00:19:38.316591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transformando imagens em arrays\ndogs = []\nlabel_names=[]\n\nfor i in range(0, len(df_train_aux)):\n    label = df_train_aux.iloc[i,1]\n    filename = df_train_aux.iloc[i,2]\n    image = image_preocessing(filename)\n    dogs.append(image)\n    label_names.append(label)\n    \nprint(np.shape(label_names))\nprint(np.shape(dogs))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:19:38.318559Z","iopub.execute_input":"2021-09-26T00:19:38.318842Z","iopub.status.idle":"2021-09-26T00:21:33.077646Z","shell.execute_reply.started":"2021-09-26T00:19:38.318809Z","shell.execute_reply":"2021-09-26T00:21:33.076755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizando algumas imagens\nplt.figure(figsize = (15,15))\nfor i in range(9):\n    plt.subplot(330 + 1 + i)\n    image = dogs\n    plt.imshow(image[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:21:33.081406Z","iopub.execute_input":"2021-09-26T00:21:33.081643Z","iopub.status.idle":"2021-09-26T00:21:34.484371Z","shell.execute_reply.started":"2021-09-26T00:21:33.081618Z","shell.execute_reply":"2021-09-26T00:21:34.483588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4 - CNN","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, kernel_size = (3, 3), activation = 'selu', input_shape=(299, 299, 3)))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Conv2D(64, kernel_size = (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Conv2D(128, kernel_size = (3,3), activation = 'selu'))\nmodel.add(Flatten())\nmodel.add(Dense(40, activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(20, activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(len(list(train_generator.class_indices.keys())), activation = 'softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:21:34.485433Z","iopub.execute_input":"2021-09-26T00:21:34.485901Z","iopub.status.idle":"2021-09-26T00:21:36.456968Z","shell.execute_reply.started":"2021-09-26T00:21:34.485855Z","shell.execute_reply":"2021-09-26T00:21:36.456282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compila o modelo\nmodel.compile(loss = 'categorical_crossentropy', optimizer = Adam(), metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:21:36.459873Z","iopub.execute_input":"2021-09-26T00:21:36.460083Z","iopub.status.idle":"2021-09-26T00:21:36.475585Z","shell.execute_reply.started":"2021-09-26T00:21:36.460058Z","shell.execute_reply":"2021-09-26T00:21:36.474844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Salvando melhor modelo\ncallbacks_list = [\n    keras.callbacks.ModelCheckpoint(filepath = 'my_model.h5',monitor = 'val_loss', save_best_only = True, verbose = 1),\n    keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 10,verbose = 1)\n]","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:21:36.478431Z","iopub.execute_input":"2021-09-26T00:21:36.478685Z","iopub.status.idle":"2021-09-26T00:21:36.485901Z","shell.execute_reply.started":"2021-09-26T00:21:36.478659Z","shell.execute_reply":"2021-09-26T00:21:36.485224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    steps_per_epoch = train_generator.samples // 32,\n                    epochs = 20,\n                    callbacks = callbacks_list,\n                    validation_data = validation_generator,\n                    verbose = 1,\n                    validation_steps = validation_generator.samples // 32)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:21:36.488934Z","iopub.execute_input":"2021-09-26T00:21:36.489631Z","iopub.status.idle":"2021-09-26T00:37:32.403465Z","shell.execute_reply.started":"2021-09-26T00:21:36.489601Z","shell.execute_reply":"2021-09-26T00:37:32.402730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4.1 Avaliando o modelo","metadata":{}},{"cell_type":"code","source":"history_dict = history.history\nloss_values = history_dict['loss']\nval_loss_values = history_dict['val_loss']\n\nepochs_x = range(1, len(loss_values) + 1)\nplt.figure(figsize=(10,10))\nplt.subplot(2,1,1)\nplt.plot(epochs_x, loss_values, 'bo', label='Training loss')\nplt.plot(epochs_x, val_loss_values, 'b', label='Validation loss')\nplt.title('Training and validation Loss and Acc')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.subplot(2,1,2)\nacc_values = history_dict['accuracy']\nval_acc_values = history_dict['val_accuracy']\nplt.plot(epochs_x, acc_values, 'bo', label='Training acc')\nplt.plot(epochs_x, val_acc_values, 'b', label='Validation acc')\n#plt.title('Training and validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Acc')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:37:32.405015Z","iopub.execute_input":"2021-09-26T00:37:32.405807Z","iopub.status.idle":"2021-09-26T00:37:32.761376Z","shell.execute_reply.started":"2021-09-26T00:37:32.405764Z","shell.execute_reply":"2021-09-26T00:37:32.760711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the best saved model\nmodel = load_model('my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:37:32.762705Z","iopub.execute_input":"2021-09-26T00:37:32.763059Z","iopub.status.idle":"2021-09-26T00:37:33.525435Z","shell.execute_reply.started":"2021-09-26T00:37:32.763023Z","shell.execute_reply":"2021-09-26T00:37:33.524607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using the validation dataset\nscore = model.evaluate_generator(validation_generator)\nprint('Val loss:', score[0])\nprint('Val accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:37:33.526571Z","iopub.execute_input":"2021-09-26T00:37:33.526812Z","iopub.status.idle":"2021-09-26T00:37:45.588705Z","shell.execute_reply.started":"2021-09-26T00:37:33.526782Z","shell.execute_reply":"2021-09-26T00:37:45.587980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4.2 Predição","metadata":{}},{"cell_type":"code","source":"test_data_gen = ImageDataGenerator(rescale = 1./255)\ntest_generator = test_data_gen.flow_from_dataframe(df_test, test_dir, x_col = 'id', y_col = 'label', color_mode = 'rgb',\n                                                   class_mode = 'categorical', target_size = (299, 299), batch_size = 32, shuffle = False,\n                                                   seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:37:45.590950Z","iopub.execute_input":"2021-09-26T00:37:45.592124Z","iopub.status.idle":"2021-09-26T00:38:04.641181Z","shell.execute_reply.started":"2021-09-26T00:37:45.592086Z","shell.execute_reply":"2021-09-26T00:38:04.640414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using the test dataset\nscore = model.evaluate_generator(test_generator)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:38:04.642464Z","iopub.execute_input":"2021-09-26T00:38:04.642726Z","iopub.status.idle":"2021-09-26T00:39:46.117595Z","shell.execute_reply.started":"2021-09-26T00:38:04.642690Z","shell.execute_reply":"2021-09-26T00:39:46.116772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = (train_generator.class_indices)\nlabel_map_inv = {v: k for k, v in label_map.items()}","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:39:46.118791Z","iopub.execute_input":"2021-09-26T00:39:46.119080Z","iopub.status.idle":"2021-09-26T00:39:46.123888Z","shell.execute_reply.started":"2021-09-26T00:39:46.119045Z","shell.execute_reply":"2021-09-26T00:39:46.122783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypreds = model.predict_generator(generator = test_generator, steps = len(test_generator), verbose = 1)\nypreds","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:39:46.124994Z","iopub.execute_input":"2021-09-26T00:39:46.127810Z","iopub.status.idle":"2021-09-26T00:40:46.216702Z","shell.execute_reply.started":"2021-09-26T00:39:46.127774Z","shell.execute_reply":"2021-09-26T00:40:46.215815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = ypreds.argmax(axis = -1)\nypred","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.218227Z","iopub.execute_input":"2021-09-26T00:40:46.218856Z","iopub.status.idle":"2021-09-26T00:40:46.227071Z","shell.execute_reply.started":"2021-09-26T00:40:46.218818Z","shell.execute_reply":"2021-09-26T00:40:46.226297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.228498Z","iopub.execute_input":"2021-09-26T00:40:46.229125Z","iopub.status.idle":"2021-09-26T00:40:46.843482Z","shell.execute_reply.started":"2021-09-26T00:40:46.229087Z","shell.execute_reply":"2021-09-26T00:40:46.842822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir_files = os.listdir(test_dir)\ntest_gen_files = test_generator.filenames\nsubmission_files = submission[\"id\"]\nlen(submission_files)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.844775Z","iopub.execute_input":"2021-09-26T00:40:46.845079Z","iopub.status.idle":"2021-09-26T00:40:46.856773Z","shell.execute_reply.started":"2021-09-26T00:40:46.845045Z","shell.execute_reply":"2021-09-26T00:40:46.855894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_files","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.858184Z","iopub.execute_input":"2021-09-26T00:40:46.858438Z","iopub.status.idle":"2021-09-26T00:40:46.866056Z","shell.execute_reply.started":"2021-09-26T00:40:46.858403Z","shell.execute_reply":"2021-09-26T00:40:46.865127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = {}\nl = len(test_gen_files)\nfor i in range(l):\n    m[test_gen_files[i]] = ypreds[i] ","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.867820Z","iopub.execute_input":"2021-09-26T00:40:46.868103Z","iopub.status.idle":"2021-09-26T00:40:46.882750Z","shell.execute_reply.started":"2021-09-26T00:40:46.868070Z","shell.execute_reply":"2021-09-26T00:40:46.881984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels = list(labels.keys())","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.884149Z","iopub.execute_input":"2021-09-26T00:40:46.884434Z","iopub.status.idle":"2021-09-26T00:40:46.892176Z","shell.execute_reply.started":"2021-09-26T00:40:46.884400Z","shell.execute_reply":"2021-09-26T00:40:46.891418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypreds_sync = []\nfor f in submission_files:\n    ypreds_sync.append(m[f+\".jpg\"])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.893349Z","iopub.execute_input":"2021-09-26T00:40:46.893819Z","iopub.status.idle":"2021-09-26T00:40:46.906343Z","shell.execute_reply.started":"2021-09-26T00:40:46.893783Z","shell.execute_reply":"2021-09-26T00:40:46.905606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(data = ypreds_sync, columns = labels)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:46.907390Z","iopub.execute_input":"2021-09-26T00:40:46.908118Z","iopub.status.idle":"2021-09-26T00:40:47.702719Z","shell.execute_reply.started":"2021-09-26T00:40:46.908085Z","shell.execute_reply":"2021-09-26T00:40:47.701944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"id\"] = submission_files\n\ncols = test_df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ntest_df = test_df[cols]\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T00:40:47.704031Z","iopub.execute_input":"2021-09-26T00:40:47.704269Z","iopub.status.idle":"2021-09-26T00:40:47.734310Z","shell.execute_reply.started":"2021-09-26T00:40:47.704237Z","shell.execute_reply":"2021-09-26T00:40:47.733524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5 - Transfer Learning from a Deep Model","metadata":{}},{"cell_type":"markdown","source":"## 5.1 - Image Generator Treino e Validação","metadata":{}},{"cell_type":"code","source":"# Dados de Treino\ntrain_data_gen = ImageDataGenerator(preprocessing_function = preprocess_input, validation_split = 0.2)\ntrain_generator = train_data_gen.flow_from_dataframe(df_train, train_dir, x_col = 'id', y_col = 'label', subset = 'training',\n                                                     color_mode = 'rgb', class_mode = 'categorical', target_size = (299, 299),\n                                                     batch_size = 32, shuffle = True, seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:11:50.606362Z","iopub.execute_input":"2021-09-26T01:11:50.606636Z","iopub.status.idle":"2021-09-26T01:12:01.906064Z","shell.execute_reply.started":"2021-09-26T01:11:50.606606Z","shell.execute_reply":"2021-09-26T01:12:01.905250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dados de Validação\nval_data_generator = ImageDataGenerator(preprocessing_function = preprocess_input, validation_split = 0.2)\nvalidation_generator = val_data_generator.flow_from_dataframe(df_train, train_dir, x_col = 'id', y_col = 'label',\n                                                              subset = 'validation', color_mode = 'rgb',\n                                                              class_mode = 'categorical', target_size = (299, 299),\n                                                              batch_size = 32, shuffle = True, seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:12:01.907543Z","iopub.execute_input":"2021-09-26T01:12:01.908287Z","iopub.status.idle":"2021-09-26T01:12:02.009747Z","shell.execute_reply.started":"2021-09-26T01:12:01.908247Z","shell.execute_reply":"2021-09-26T01:12:02.009036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = InceptionResNetV2(weights = 'imagenet', include_top = False, input_shape = ((299,299, 3)))\n\nx = base_model.output\nx = Flatten()(x)\nx = Dense(100, activation = 'relu')(x)\npredictions = Dense(len(list(train_generator.class_indices.keys())), activation = 'softmax',\n                    kernel_initializer = 'random_uniform')(x)\n\nmodel = Model(inputs = base_model.input, outputs = predictions)\n\n# Freezing pretrained layers\nfor layer in base_model.layers:\n    layer.trainable = False\n    \noptimizer = Adam()\nmodel.compile(optimizer=  optimizer,loss = 'categorical_crossentropy',metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:12:04.689890Z","iopub.execute_input":"2021-09-26T01:12:04.690639Z","iopub.status.idle":"2021-09-26T01:12:09.782272Z","shell.execute_reply.started":"2021-09-26T01:12:04.690601Z","shell.execute_reply":"2021-09-26T01:12:09.781524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Salvando melhor modelo\ncallbacks_list = [\n    keras.callbacks.ModelCheckpoint(filepath = 'my_model.h5',monitor = 'val_loss', save_best_only = True, verbose = 1),\n    keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 10,verbose = 1)\n]","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:12:09.783745Z","iopub.execute_input":"2021-09-26T01:12:09.783977Z","iopub.status.idle":"2021-09-26T01:12:09.789631Z","shell.execute_reply.started":"2021-09-26T01:12:09.783944Z","shell.execute_reply":"2021-09-26T01:12:09.788781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    steps_per_epoch = train_generator.samples // 32,\n                    epochs = 50,\n                    callbacks = callbacks_list,\n                    validation_data = validation_generator,\n                    verbose = 1,\n                    validation_steps = validation_generator.samples // 32)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:12:09.791052Z","iopub.execute_input":"2021-09-26T01:12:09.791700Z","iopub.status.idle":"2021-09-26T01:26:17.917643Z","shell.execute_reply.started":"2021-09-26T01:12:09.791666Z","shell.execute_reply":"2021-09-26T01:26:17.916973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5.1 Avaliando o modelo","metadata":{}},{"cell_type":"code","source":"history_dict = history.history\nloss_values = history_dict['loss']\nval_loss_values = history_dict['val_loss']\n\nepochs_x = range(1, len(loss_values) + 1)\nplt.figure(figsize = (10,10))\nplt.subplot(2,1,1)\nplt.plot(epochs_x, loss_values, 'bo', label = 'Training loss')\nplt.plot(epochs_x, val_loss_values, 'b', label = 'Validation loss')\nplt.title('Training and validation Loss and Acc')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.subplot(2,1,2)\nacc_values = history_dict['accuracy']\nval_acc_values = history_dict['val_accuracy']\nplt.plot(epochs_x, acc_values, 'bo', label = 'Training acc')\nplt.plot(epochs_x, val_acc_values, 'b', label = 'Validation acc')\n#plt.title('Training and validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Acc')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:26:17.919664Z","iopub.execute_input":"2021-09-26T01:26:17.920015Z","iopub.status.idle":"2021-09-26T01:26:18.249426Z","shell.execute_reply.started":"2021-09-26T01:26:17.919962Z","shell.execute_reply":"2021-09-26T01:26:18.248731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the best saved model\nmodel = load_model('my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:26:18.250751Z","iopub.execute_input":"2021-09-26T01:26:18.251176Z","iopub.status.idle":"2021-09-26T01:26:24.304654Z","shell.execute_reply.started":"2021-09-26T01:26:18.251137Z","shell.execute_reply":"2021-09-26T01:26:24.303915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using the validation dataset\nscore = model.evaluate_generator(validation_generator)\nprint('Val loss:', score[0])\nprint('Val accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:26:24.307523Z","iopub.execute_input":"2021-09-26T01:26:24.307723Z","iopub.status.idle":"2021-09-26T01:26:42.517399Z","shell.execute_reply.started":"2021-09-26T01:26:24.307699Z","shell.execute_reply":"2021-09-26T01:26:42.516614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5.2 Predição","metadata":{}},{"cell_type":"code","source":"test_data_gen = ImageDataGenerator(preprocessing_function = preprocess_input)\ntest_generator = test_data_gen.flow_from_dataframe(df_test, test_dir, x_col = 'id', y_col = 'label', color_mode = 'rgb',\n                                                   class_mode = 'categorical', target_size = (299, 299), batch_size = 32, shuffle = False,\n                                                   seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:26:42.518712Z","iopub.execute_input":"2021-09-26T01:26:42.519185Z","iopub.status.idle":"2021-09-26T01:26:45.820990Z","shell.execute_reply.started":"2021-09-26T01:26:42.519147Z","shell.execute_reply":"2021-09-26T01:26:45.819607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using the test dataset\nscore = model.evaluate_generator(test_generator)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:26:45.822215Z","iopub.execute_input":"2021-09-26T01:26:45.822488Z","iopub.status.idle":"2021-09-26T01:28:00.506801Z","shell.execute_reply.started":"2021-09-26T01:26:45.822451Z","shell.execute_reply":"2021-09-26T01:28:00.506033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = (train_generator.class_indices)\nlabel_map_inv = {v: k for k, v in label_map.items()}","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:28:00.508230Z","iopub.execute_input":"2021-09-26T01:28:00.508664Z","iopub.status.idle":"2021-09-26T01:28:00.513470Z","shell.execute_reply.started":"2021-09-26T01:28:00.508626Z","shell.execute_reply":"2021-09-26T01:28:00.512717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypreds = model.predict_generator(generator = test_generator, steps = len(test_generator), verbose = 1)\nypreds","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:28:00.516515Z","iopub.execute_input":"2021-09-26T01:28:00.516776Z","iopub.status.idle":"2021-09-26T01:29:19.958937Z","shell.execute_reply.started":"2021-09-26T01:28:00.516740Z","shell.execute_reply":"2021-09-26T01:29:19.958178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = ypreds.argmax(axis = -1)\nypred","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:19.960363Z","iopub.execute_input":"2021-09-26T01:29:19.960617Z","iopub.status.idle":"2021-09-26T01:29:19.968650Z","shell.execute_reply.started":"2021-09-26T01:29:19.960583Z","shell.execute_reply":"2021-09-26T01:29:19.967812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:19.970092Z","iopub.execute_input":"2021-09-26T01:29:19.970456Z","iopub.status.idle":"2021-09-26T01:29:20.263110Z","shell.execute_reply.started":"2021-09-26T01:29:19.970424Z","shell.execute_reply":"2021-09-26T01:29:20.262295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir_files = os.listdir(test_dir)\ntest_gen_files = test_generator.filenames\nsubmission_files = submission[\"id\"]\nlen(submission_files)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.264519Z","iopub.execute_input":"2021-09-26T01:29:20.264778Z","iopub.status.idle":"2021-09-26T01:29:20.276292Z","shell.execute_reply.started":"2021-09-26T01:29:20.264744Z","shell.execute_reply":"2021-09-26T01:29:20.275508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_files","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.277574Z","iopub.execute_input":"2021-09-26T01:29:20.278020Z","iopub.status.idle":"2021-09-26T01:29:20.284638Z","shell.execute_reply.started":"2021-09-26T01:29:20.277970Z","shell.execute_reply":"2021-09-26T01:29:20.283850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = {}\nl = len(test_gen_files)\nfor i in range(l):\n    m[test_gen_files[i]] = ypreds[i] ","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.286135Z","iopub.execute_input":"2021-09-26T01:29:20.286547Z","iopub.status.idle":"2021-09-26T01:29:20.298836Z","shell.execute_reply.started":"2021-09-26T01:29:20.286512Z","shell.execute_reply":"2021-09-26T01:29:20.298191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels = list(labels.keys())","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.299977Z","iopub.execute_input":"2021-09-26T01:29:20.300465Z","iopub.status.idle":"2021-09-26T01:29:20.307767Z","shell.execute_reply.started":"2021-09-26T01:29:20.300431Z","shell.execute_reply":"2021-09-26T01:29:20.307074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypreds_sync = []\nfor f in submission_files:\n    ypreds_sync.append(m[f+\".jpg\"])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.308969Z","iopub.execute_input":"2021-09-26T01:29:20.309448Z","iopub.status.idle":"2021-09-26T01:29:20.325259Z","shell.execute_reply.started":"2021-09-26T01:29:20.309412Z","shell.execute_reply":"2021-09-26T01:29:20.324419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(data = ypreds_sync, columns = labels)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.326269Z","iopub.execute_input":"2021-09-26T01:29:20.326991Z","iopub.status.idle":"2021-09-26T01:29:20.939966Z","shell.execute_reply.started":"2021-09-26T01:29:20.326955Z","shell.execute_reply":"2021-09-26T01:29:20.939149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"id\"] = submission_files\n\ncols = test_df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ntest_df = test_df[cols]\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T01:29:20.941133Z","iopub.execute_input":"2021-09-26T01:29:20.941452Z","iopub.status.idle":"2021-09-26T01:29:20.981632Z","shell.execute_reply.started":"2021-09-26T01:29:20.941416Z","shell.execute_reply":"2021-09-26T01:29:20.980971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resultado","metadata":{}},{"cell_type":"markdown","source":"### *O modelo desenvolvido manualmente apresentou um comportamento estranho durante a predição dos dados de teste. Além disso, apresentou acurácia baixa para dados de validação e acurácia 0 para o dados de teste. Já o modelo baseado em arquiteturas clássicas e transfer learning apresentou um resultado mais razoável com acurácia de 84 %. No entanto, ambos os modelos apresentaram overfitting.*","metadata":{}}]}