{"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\n#for 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":"2023-05-16T19:16:33.812722Z","iopub.execute_input":"2023-05-16T19:16:33.813211Z","iopub.status.idle":"2023-05-16T19:16:33.819623Z","shell.execute_reply.started":"2023-05-16T19:16:33.813147Z","shell.execute_reply":"2023-05-16T19:16:33.818726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras.applications.resnet_v2 import ResNet50V2\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:16:29.39491Z","iopub.execute_input":"2023-05-16T19:16:29.395378Z","iopub.status.idle":"2023-05-16T19:16:29.402354Z","shell.execute_reply.started":"2023-05-16T19:16:29.395341Z","shell.execute_reply":"2023-05-16T19:16:29.400879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/dog-breed-identification/train'\ntest_path = '/kaggle/input/dog-breed-identification/test'\n\n#reading dataset labels\ntrain_labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ntest_labels = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:16:36.628356Z","iopub.execute_input":"2023-05-16T19:16:36.62883Z","iopub.status.idle":"2023-05-16T19:16:37.006117Z","shell.execute_reply.started":"2023-05-16T19:16:36.628793Z","shell.execute_reply":"2023-05-16T19:16:37.005063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head(20)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:16:43.510783Z","iopub.execute_input":"2023-05-16T19:16:43.511186Z","iopub.status.idle":"2023-05-16T19:16:43.52716Z","shell.execute_reply.started":"2023-05-16T19:16:43.511137Z","shell.execute_reply":"2023-05-16T19:16:43.525796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:16:49.656319Z","iopub.execute_input":"2023-05-16T19:16:49.65676Z","iopub.status.idle":"2023-05-16T19:16:49.687Z","shell.execute_reply.started":"2023-05-16T19:16:49.656728Z","shell.execute_reply":"2023-05-16T19:16:49.685871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels['id'] = train_labels['id'].apply(lambda x: x + \".jpg\")\ntest_labels['id'] = test_labels['id'].apply(lambda x: x + \".jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:16:53.811539Z","iopub.execute_input":"2023-05-16T19:16:53.812013Z","iopub.status.idle":"2023-05-16T19:16:53.829083Z","shell.execute_reply.started":"2023-05-16T19:16:53.811979Z","shell.execute_reply":"2023-05-16T19:16:53.827793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train_labels.breed.unique()\ninclude = ['beagle', 'chihuahua', 'doberman','french_bulldog', 'golden_retriever', 'malamute', 'pug', 'saint_bernard', 'scottish_deerhound','tibetan_mastiff']\nfor i in x :\n    if i not in include:\n        train_labels = train_labels.drop(train_labels[ train_labels['breed'] == i ].index)\nprint(train_labels.shape)\ntrain_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:16:57.705981Z","iopub.execute_input":"2023-05-16T19:16:57.707156Z","iopub.status.idle":"2023-05-16T19:16:57.932948Z","shell.execute_reply.started":"2023-05-16T19:16:57.707078Z","shell.execute_reply":"2023-05-16T19:16:57.93186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n                                   rescale = 1./255, \n                                   validation_split = 0.2,\n                                   shear_range = 0.2, \n                                   zoom_range = 0.2, horizontal_flip = True, \n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   rotation_range = 20,\n                                   brightness_range=[0.2,1.0])\n\ntrain_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n                                                 directory = train_path,\n                                                 x_col = \"id\",\n                                                 y_col = \"breed\",\n                                                 batch_size = 16,\n                                                 subset=\"training\",\n                                                 class_mode = \"categorical\",\n                                                 target_size = (224,224),\n                                                 seed = 42,\n                                                 shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:17:08.617735Z","iopub.execute_input":"2023-05-16T19:17:08.618928Z","iopub.status.idle":"2023-05-16T19:17:09.304819Z","shell.execute_reply.started":"2023-05-16T19:17:08.618877Z","shell.execute_reply":"2023-05-16T19:17:09.303577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n                                                 directory = train_path,\n                                                 x_col = \"id\",\n                                                 y_col = \"breed\",\n                                                 batch_size = 16,\n                                                 subset=\"validation\",\n                                                 class_mode = \"categorical\",\n                                                 target_size = (224,224),\n                                                 seed = 42,\n                                                 shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:17:20.479218Z","iopub.execute_input":"2023-05-16T19:17:20.479663Z","iopub.status.idle":"2023-05-16T19:17:21.290648Z","shell.execute_reply.started":"2023-05-16T19:17:20.479633Z","shell.execute_reply":"2023-05-16T19:17:21.289366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\n#defining test set\ntest_set = test_datagen.flow_from_dataframe(dataframe = test_labels,\n                                                 directory = test_path,\n                                                 x_col = \"id\",\n                                                 y_col = None,\n                                                 batch_size = 16,\n                                                 class_mode = None,\n                                                 seed = 42,\n                                                 shuffle=False,\n                                                 target_size = (224,224))","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:17:24.891721Z","iopub.execute_input":"2023-05-16T19:17:24.892156Z","iopub.status.idle":"2023-05-16T19:17:30.998664Z","shell.execute_reply.started":"2023-05-16T19:17:24.892128Z","shell.execute_reply":"2023-05-16T19:17:30.997389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Construir o modelo de rede neural\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(224, 224, 3)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(120, activation='softmax')  # 120 raças de cães no total\n])","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:19:59.045802Z","iopub.execute_input":"2023-05-16T19:19:59.047078Z","iopub.status.idle":"2023-05-16T19:19:59.223717Z","shell.execute_reply.started":"2023-05-16T19:19:59.047039Z","shell.execute_reply":"2023-05-16T19:19:59.222552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compilar e treinar o modelo\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(train_datagen , epochs=10, validation_data=validate_set)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:24:20.613935Z","iopub.execute_input":"2023-05-16T19:24:20.615041Z","iopub.status.idle":"2023-05-16T19:24:20.6812Z","shell.execute_reply.started":"2023-05-16T19:24:20.614997Z","shell.execute_reply":"2023-05-16T19:24:20.679548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Avaliar o modelo no conjunto de teste\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_directory(\n    test_dir,\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode='categorical'\n)\n\ntest_loss, test_acc = model.evaluate(test_generator)\nprint('Acurácia no conjunto de teste:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:23:01.729723Z","iopub.execute_input":"2023-05-16T19:23:01.730253Z","iopub.status.idle":"2023-05-16T19:23:01.765315Z","shell.execute_reply.started":"2023-05-16T19:23:01.730213Z","shell.execute_reply":"2023-05-16T19:23:01.763712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.batch_size","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:17:36.812769Z","iopub.execute_input":"2023-05-16T19:17:36.813231Z","iopub.status.idle":"2023-05-16T19:17:36.820076Z","shell.execute_reply.started":"2023-05-16T19:17:36.813184Z","shell.execute_reply":"2023-05-16T19:17:36.818968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = ResNet50V2(input_shape = [224,224,3], weights = 'imagenet', include_top = False)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:17:41.943604Z","iopub.execute_input":"2023-05-16T19:17:41.944083Z","iopub.status.idle":"2023-05-16T19:18:03.940721Z","shell.execute_reply.started":"2023-05-16T19:17:41.944043Z","shell.execute_reply":"2023-05-16T19:18:03.938236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in resnet.layers:\n    layer.trainable = False\n\nx = keras.layers.Flatten()(resnet.output)\n\nx = keras.layers.Dropout(0.4)(x)\n\npred = keras.layers.Dense(10, activation='softmax')(x)\n\nmodel = tf.keras.models.Model(inputs=resnet.input, outputs=pred)\n\nopt = tf.keras.optimizers.Adam(learning_rate = 1e-5)\nmodel.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])\n\ntrain_step = train_set.n//train_set.batch_size\nvalidate_step = validate_set.n//validate_set.batch_size\n\nresnet50 = model.fit(train_set,validation_data = validate_set,epochs = 30,steps_per_epoch = train_step, validation_steps = validate_step)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:18:19.158882Z","iopub.execute_input":"2023-05-16T19:18:19.159333Z","iopub.status.idle":"2023-05-16T19:18:19.199738Z","shell.execute_reply.started":"2023-05-16T19:18:19.159297Z","shell.execute_reply":"2023-05-16T19:18:19.198225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TEST=test_set.n//test_set.batch_size\n\ntest_set.reset()\n\npred=model.predict(test_set,steps=STEP_SIZE_TEST,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:15:25.567977Z","iopub.execute_input":"2023-05-16T19:15:25.568991Z","iopub.status.idle":"2023-05-16T19:15:25.608262Z","shell.execute_reply.started":"2023-05-16T19:15:25.568932Z","shell.execute_reply":"2023-05-16T19:15:25.606635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet50.history['accuracy'])\nplt.plot(resnet50.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\nplt.plot(resnet50.history['loss'])\nplt.plot(resnet50.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:15:28.485381Z","iopub.execute_input":"2023-05-16T19:15:28.485759Z","iopub.status.idle":"2023-05-16T19:15:28.519787Z","shell.execute_reply.started":"2023-05-16T19:15:28.48573Z","shell.execute_reply":"2023-05-16T19:15:28.518151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\npredicted_class_indices=np.argmax(pred,axis=1)\n\nlabels = (train_set.class_indices)\n\nlabels = dict((v,k) for k,v in labels.items())\n\npredictions = [labels[k] for k in predicted_class_indices]","metadata":{"execution":{"iopub.status.busy":"2023-05-16T19:15:31.81375Z","iopub.execute_input":"2023-05-16T19:15:31.81414Z","iopub.status.idle":"2023-05-16T19:15:31.846015Z","shell.execute_reply.started":"2023-05-16T19:15:31.814111Z","shell.execute_reply":"2023-05-16T19:15:31.844327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}