{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        break\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-16T00:46:48.047565Z","iopub.execute_input":"2023-05-16T00:46:48.048077Z","iopub.status.idle":"2023-05-16T00:46:59.266244Z","shell.execute_reply.started":"2023-05-16T00:46:48.048046Z","shell.execute_reply":"2023-05-16T00:46:59.264923Z"},"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-05-16T00:46:59.268256Z","iopub.execute_input":"2023-05-16T00:46:59.268595Z","iopub.status.idle":"2023-05-16T00:46:59.849236Z","shell.execute_reply.started":"2023-05-16T00:46:59.268563Z","shell.execute_reply":"2023-05-16T00:46:59.846771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.loadtxt(\"/kaggle/input/dog-breed-identification/labels.csv\", delimiter=\",\", dtype=str, skiprows=1)\nprint(labels[0:5])\ny_train = labels[:,1]\nprint()\nprint(y_train[0:5])","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:46:59.850845Z","iopub.execute_input":"2023-05-16T00:46:59.851975Z","iopub.status.idle":"2023-05-16T00:46:59.899148Z","shell.execute_reply.started":"2023-05-16T00:46:59.851934Z","shell.execute_reply":"2023-05-16T00:46:59.898338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path='/kaggle/input/dog-breed-identification/train/'\ntest_images_path='/kaggle/input/dog-breed-identification/test/'\ntrain_files = [train_images_path + file_name + '.jpg' for file_name in labels[:,0]]\nprint(train_files[:5])","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:46:59.901420Z","iopub.execute_input":"2023-05-16T00:46:59.901940Z","iopub.status.idle":"2023-05-16T00:46:59.920145Z","shell.execute_reply.started":"2023-05-16T00:46:59.901910Z","shell.execute_reply":"2023-05-16T00:46:59.919349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ntam = 100\n\ndef img2arr(image_path):\n    img = tf.keras.utils.load_img(image_path,target_size=(tam,tam))\n    arr = tf.keras.utils.img_to_array(img)/255\n    return arr\n\nprint(img2arr(train_files[0]).shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:46:59.921521Z","iopub.execute_input":"2023-05-16T00:46:59.922028Z","iopub.status.idle":"2023-05-16T00:47:13.801871Z","shell.execute_reply.started":"2023-05-16T00:46:59.921998Z","shell.execute_reply":"2023-05-16T00:47:13.800774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size=-1\nx_train = np.empty((len(train_files[:train_size]),tam,tam,3))\nfor i, file in enumerate(train_files[:train_size]):\n    x_train[i] = img2arr(file)\nprint(x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:47:13.803477Z","iopub.execute_input":"2023-05-16T00:47:13.804343Z","iopub.status.idle":"2023-05-16T00:47:28.131041Z","shell.execute_reply.started":"2023-05-16T00:47:13.804309Z","shell.execute_reply":"2023-05-16T00:47:28.129909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.base import BaseEstimator, ClassifierMixin\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, y):\n    self.labels, ids = np.unique(y, return_inverse=True)\n    yhot = keras.utils.to_categorical(ids)\n    self.model = keras.Sequential([\n        layers.Conv2D(4, 4, activation='relu'),\n        layers.Flatten(),\n        layers.Dense(512, activation=\"relu\"),\n        layers.Dense(yhot.shape[1], activation=\"softmax\")\n    ])\n    self.model.compile(optimizer=\"rmsprop\",\n                       loss=\"categorical_crossentropy\",\n                       metrics=[\"accuracy\"])\n    self.model.fit(X, yhot, epochs=self.epochs, batch_size=self.batch_size)\n    return self\n\n  def predict(self, X, y=None):\n    return self.model.predict(X)\n\nmodelo = RedeNeural()\nmodelo.fit(x_train, y_train[:train_size])\nmodelo.model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:47:28.132873Z","iopub.execute_input":"2023-05-16T00:47:28.133482Z","iopub.status.idle":"2023-05-16T00:47:55.725550Z","shell.execute_reply.started":"2023-05-16T00:47:28.133435Z","shell.execute_reply":"2023-05-16T00:47:55.724764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del x_train","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:47:55.726638Z","iopub.execute_input":"2023-05-16T00:47:55.727028Z","iopub.status.idle":"2023-05-16T00:47:55.736543Z","shell.execute_reply.started":"2023-05-16T00:47:55.726994Z","shell.execute_reply":"2023-05-16T00:47:55.735746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = []\nfor dirname, _, filenames in os.walk('/kaggle/input/dog-breed-identification/test/'):\n    for filename in filenames:\n        test_files.append(os.path.join(dirname, filename))\nprint(len(test_files))","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:47:55.737520Z","iopub.execute_input":"2023-05-16T00:47:55.737950Z","iopub.status.idle":"2023-05-16T00:48:00.165191Z","shell.execute_reply.started":"2023-05-16T00:47:55.737917Z","shell.execute_reply":"2023-05-16T00:48:00.164189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.empty((len(test_files),tam,tam,3))\nfor i, file in enumerate(test_files):\n    if i % 1000:\n        print(i)\n    x_test[i] = img2arr(file)\nprint(x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:48:00.168954Z","iopub.execute_input":"2023-05-16T00:48:00.169526Z","iopub.status.idle":"2023-05-16T00:49:18.383849Z","shell.execute_reply.started":"2023-05-16T00:48:00.169489Z","shell.execute_reply":"2023-05-16T00:49:18.381763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = modelo.predict(x_test)\nnp.savetxt(\"submission.csv\", y_pred, delimiter=\",\")\nprint(y_pred[:10])","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:49:18.385239Z","iopub.execute_input":"2023-05-16T00:49:18.385584Z","iopub.status.idle":"2023-05-16T00:49:24.191884Z","shell.execute_reply.started":"2023-05-16T00:49:18.385558Z","shell.execute_reply":"2023-05-16T00:49:24.190772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del x_test","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:50:18.815141Z","iopub.execute_input":"2023-05-16T00:50:18.815539Z","iopub.status.idle":"2023-05-16T00:50:19.423368Z","shell.execute_reply.started":"2023-05-16T00:50:18.815508Z","shell.execute_reply":"2023-05-16T00:50:19.421396Z"},"trusted":true},"execution_count":null,"outputs":[]}]}