{"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)\nimport os\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\ndog_breed_filepath = '/kaggle/input/dog-breed-identification'\ntraining_images_filepath = os.path.join(dog_breed_filepath, 'train')\n\ntest_images_filepath = os.path.join(dog_breed_filepath, 'test')\nlabels_filepath = os.path.join(dog_breed_filepath, 'labels.csv')\n\n\ntrain_labels = pd.read_csv(labels_filepath)\n\n\ntest_labels = pd.read_csv(os.path.join(dog_breed_filepath, 'sample_submission.csv'))\n\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        break\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-11T16:42:41.738460Z","iopub.execute_input":"2023-06-11T16:42:41.738831Z","iopub.status.idle":"2023-06-11T16:42:45.353098Z","shell.execute_reply.started":"2023-06-11T16:42:41.738801Z","shell.execute_reply":"2023-06-11T16:42:45.351915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.354931Z","iopub.execute_input":"2023-06-11T16:42:45.355745Z","iopub.status.idle":"2023-06-11T16:42:45.385062Z","shell.execute_reply.started":"2023-06-11T16:42:45.355707Z","shell.execute_reply":"2023-06-11T16:42:45.384098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image\nimg = np.asarray(Image.open('/kaggle/input/dog-breed-identification/train/7d9c53fa846ea7e34bc3efa6830ab668.jpg'))\nimgplot = plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.388134Z","iopub.execute_input":"2023-06-11T16:42:45.389042Z","iopub.status.idle":"2023-06-11T16:42:45.776964Z","shell.execute_reply.started":"2023-06-11T16:42:45.389004Z","shell.execute_reply":"2023-06-11T16:42:45.776106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inserindo o final do nome do arquivo","metadata":{}},{"cell_type":"code","source":"train_labels['img_file'] = train_labels['id'].apply(lambda x: x + \".jpg\")\ntest_labels['img_file'] = test_labels['id'].apply(lambda x: x + \".jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.779757Z","iopub.execute_input":"2023-06-11T16:42:45.780360Z","iopub.status.idle":"2023-06-11T16:42:45.796177Z","shell.execute_reply.started":"2023-06-11T16:42:45.780325Z","shell.execute_reply":"2023-06-11T16:42:45.795164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.799346Z","iopub.execute_input":"2023-06-11T16:42:45.799946Z","iopub.status.idle":"2023-06-11T16:42:45.813513Z","shell.execute_reply.started":"2023-06-11T16:42:45.799909Z","shell.execute_reply":"2023-06-11T16:42:45.812395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.815352Z","iopub.execute_input":"2023-06-11T16:42:45.815708Z","iopub.status.idle":"2023-06-11T16:42:45.841148Z","shell.execute_reply.started":"2023-06-11T16:42:45.815666Z","shell.execute_reply":"2023-06-11T16:42:45.840217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nclasses = sorted(list(set(train_labels['breed'])))\nnum_classes = len(classes)\n\nim_size = 128\nbatch_size = 16\nencoder = LabelEncoder()\n\nclass_to_num = dict(zip(classes, range(num_classes)))\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.842728Z","iopub.execute_input":"2023-06-11T16:42:45.843129Z","iopub.status.idle":"2023-06-11T16:42:45.859140Z","shell.execute_reply.started":"2023-06-11T16:42:45.843069Z","shell.execute_reply":"2023-06-11T16:42:45.858156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Carregamento das imagens","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.utils import load_img\nfrom tensorflow.keras.utils import img_to_array\nfrom tensorflow.keras.utils import to_categorical\nfrom tqdm import tqdm\nfrom keras.utils import Sequence\nfrom typing import Tuple, Iterable\nimport math\n\nclass CustomDataGenerator(Sequence):\n    def __init__(\n        self, images_path: Iterable, labels: Iterable,\n        target_size: Tuple[int, int], batch_size: int\n    ):\n\n        self.batch_size = batch_size\n        self.labels = labels\n        self.images_path = images_path\n        self.target_size = target_size[:2]\n\n    def __getitem__(self, index: int):\n\n        low = index * self.batch_size\n        high = min(low + self.batch_size, len(self.images_path))\n        batch_input = self.images_path[low:high]\n        batch_labels = self.labels[low:high]\n        batch_x = np.array([\n            img_to_array(load_img(image_path, target_size=self.target_size))\n            for image_path in batch_input\n        ])\n        batch_y = np.array([\n            class_to_num[breed] for breed in batch_labels\n        ])\n        return batch_x, to_categorical(batch_y, num_classes=120)\n    \n    def __len__(self):\n        return math.ceil(len(self.images_path) / self.batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.860827Z","iopub.execute_input":"2023-06-11T16:42:45.861261Z","iopub.status.idle":"2023-06-11T16:42:45.872382Z","shell.execute_reply.started":"2023-06-11T16:42:45.861228Z","shell.execute_reply":"2023-06-11T16:42:45.871426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import path\n\nall_image_paths = [\n    path.join(training_images_filepath, img_name)\n    for img_name in train_labels['img_file'].values\n]\nall_labels = train_labels['breed'].values\n\ndataset_length = len(all_labels)\n\nvalidation_split = 0.3\n\ntrain_end_index = math.ceil(dataset_length * (1 - validation_split))\n\ntrain_images_path = all_image_paths[:train_end_index]\ntrain_labels = all_labels[:train_end_index]\n\nval_images_path = all_image_paths[train_end_index:]\nval_labels = all_labels[train_end_index:]","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.874043Z","iopub.execute_input":"2023-06-11T16:42:45.874468Z","iopub.status.idle":"2023-06-11T16:42:45.904238Z","shell.execute_reply.started":"2023-06-11T16:42:45.874437Z","shell.execute_reply":"2023-06-11T16:42:45.903257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = CustomDataGenerator(train_images_path, train_labels, target_size=(im_size, im_size, 3), batch_size=batch_size)\nval_generator = CustomDataGenerator(val_images_path, val_labels, target_size=(im_size, im_size, 3), batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.908198Z","iopub.execute_input":"2023-06-11T16:42:45.908690Z","iopub.status.idle":"2023-06-11T16:42:45.915526Z","shell.execute_reply.started":"2023-06-11T16:42:45.908664Z","shell.execute_reply":"2023-06-11T16:42:45.914545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_pair = train_generator[0]\nfirst_batch_x, first_batch_y = first_pair\nfirst_batch_x.shape, first_batch_y.shape,","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.916954Z","iopub.execute_input":"2023-06-11T16:42:45.917356Z","iopub.status.idle":"2023-06-11T16:42:45.956965Z","shell.execute_reply.started":"2023-06-11T16:42:45.917324Z","shell.execute_reply":"2023-06-11T16:42:45.956011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel train_labels\ndel test_labels\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:45.958493Z","iopub.execute_input":"2023-06-11T16:42:45.958817Z","iopub.status.idle":"2023-06-11T16:42:46.215757Z","shell.execute_reply.started":"2023-06-11T16:42:45.958786Z","shell.execute_reply":"2023-06-11T16:42:46.214441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin\nfrom sklearn.pipeline import Pipeline\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.metrics import accuracy_score\nimport numpy as np\n\nclass RedeNeural(BaseEstimator, ClassifierMixin):\n    def __init__(self, epochs=5, batch_size=128):\n        self.epochs = epochs\n        self.batch_size = batch_size\n\n    def fit(self, X=None, y=None):\n        self.labels, ids = np.unique(y, return_inverse=True)\n        self.model = keras.Sequential([\n            layers.Conv2D(4, 4, activation='relu'),\n            layers.Flatten(),\n            layers.Dense(512, activation=\"relu\"),\n            layers.Dense(120, activation=\"softmax\")\n        ])\n        self.model.compile(optimizer=\"rmsprop\",\n                           loss=\"categorical_crossentropy\",\n                           metrics=[\"accuracy\"])\n        return self.model.fit(train_generator, epochs=self.epochs, batch_size=self.batch_size, validation_data=val_generator)\n\n    def predict(self, X, y=None):\n        probabilities = self.model.predict(X)\n        ypred = self.labels[np.argmax(probabilities, axis=1)]\n        return ypred\n\nclass DividePor255(BaseEstimator, TransformerMixin):\n    def fit(self, X=None, y=None):\n        return self\n    def transform(self, X, y=None):\n        return np.array(X, dtype=\"float32\") / 255\n\nmodelo = RedeNeural(epochs=6)\n\nhistory = modelo.fit(None, None)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:42:46.217519Z","iopub.execute_input":"2023-06-11T16:42:46.218001Z","iopub.status.idle":"2023-06-11T16:45:05.912723Z","shell.execute_reply.started":"2023-06-11T16:42:46.217966Z","shell.execute_reply":"2023-06-11T16:45:05.911686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del modelo\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:45:05.914491Z","iopub.execute_input":"2023-06-11T16:45:05.914953Z","iopub.status.idle":"2023-06-11T16:45:06.179849Z","shell.execute_reply.started":"2023-06-11T16:45:05.914916Z","shell.execute_reply":"2023-06-11T16:45:06.178611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot result\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\n\n# Accuracy\nplt.plot(epochs, acc, label='Train accuracy')\nplt.plot(epochs, val_acc, label='Val accuracy')\nplt.title('Training & validation accuracy')\nplt.legend()\n\n# Loss\nplt.figure()\nplt.plot(epochs, loss, label='Train loss')\nplt.plot(epochs, val_loss, label='Val loss')\nplt.title('Training & validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T16:45:06.181723Z","iopub.execute_input":"2023-06-11T16:45:06.182155Z","iopub.status.idle":"2023-06-11T16:45:06.704505Z","shell.execute_reply.started":"2023-06-11T16:45:06.182110Z","shell.execute_reply":"2023-06-11T16:45:06.703518Z"},"trusted":true},"execution_count":null,"outputs":[]}]}