{"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\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:04:37.171571Z","iopub.execute_input":"2023-05-16T00:04:37.172077Z","iopub.status.idle":"2023-05-16T00:04:37.181107Z","shell.execute_reply.started":"2023-05-16T00:04:37.172033Z","shell.execute_reply":"2023-05-16T00:04:37.179201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\n# This is a sample Notebook to demonstrate how to read \"MNIST Dataset\"\n#\nimport numpy as np # linear algebra\nimport struct\nfrom array import array\nfrom os.path  import join\n\n#\n# MNIST Data Loader Class\n#\nclass MnistDataloader(object):\n    def __init__(self, training_images_filepath,training_labels_filepath,\n                 test_images_filepath, test_labels_filepath):\n        self.training_images_filepath = training_images_filepath\n        self.training_labels_filepath = training_labels_filepath\n        self.test_images_filepath = test_images_filepath\n        self.test_labels_filepath = test_labels_filepath\n    \n    def read_images_labels(self, images_filepath, labels_filepath):        \n        labels = []\n        with open(labels_filepath, 'rb') as file:\n            magic, size = struct.unpack(\">II\", file.read(8))\n            if magic != 2049:\n                raise ValueError('Magic number mismatch, expected 2049, got {}'.format(magic))\n            labels = array(\"B\", file.read())        \n        \n        with open(images_filepath, 'rb') as file:\n            magic, size, rows, cols = struct.unpack(\">IIII\", file.read(16))\n            if magic != 2051:\n                raise ValueError('Magic number mismatch, expected 2051, got {}'.format(magic))\n            image_data = array(\"B\", file.read())        \n        images = []\n        for i in range(size):\n            images.append([0] * rows * cols)\n        for i in range(size):\n            img = np.array(image_data[i * rows * cols:(i + 1) * rows * cols])\n            img = img.reshape(28, 28)\n            images[i][:] = img            \n        \n        return images, labels\n            \n    def load_data(self):\n        x_train, y_train = self.read_images_labels(self.training_images_filepath, self.training_labels_filepath)\n        x_test, y_test = self.read_images_labels(self.test_images_filepath, self.test_labels_filepath)\n        return (x_train, y_train),(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:04:37.703154Z","iopub.execute_input":"2023-05-16T00:04:37.703597Z","iopub.status.idle":"2023-05-16T00:04:37.72019Z","shell.execute_reply.started":"2023-05-16T00:04:37.70356Z","shell.execute_reply":"2023-05-16T00:04:37.71892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !wget https://github.com/fboldt/aulasann/raw/main/mnist.zip\n# !unzip mnist.zip","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:04:38.334125Z","iopub.execute_input":"2023-05-16T00:04:38.334616Z","iopub.status.idle":"2023-05-16T00:04:38.339832Z","shell.execute_reply.started":"2023-05-16T00:04:38.334574Z","shell.execute_reply":"2023-05-16T00:04:38.338637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\n# Verify Reading Dataset via MnistDataloader class\n#\n%matplotlib inline\nimport random\nimport matplotlib.pyplot as plt\n\ntrain_path = '../input/dog-breed-identification/train'\ntest_path = '../input/dog-breed-identification/test'\n\n#reading dataset labels\ntrain_labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\ntest_labels = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\n\ntrain_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-16T00:04:38.859718Z","iopub.execute_input":"2023-05-16T00:04:38.860719Z","iopub.status.idle":"2023-05-16T00:04:39.809623Z","shell.execute_reply.started":"2023-05-16T00:04:38.860659Z","shell.execute_reply":"2023-05-16T00:04:39.808397Z"},"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-16T00:04:39.812234Z","iopub.execute_input":"2023-05-16T00:04:39.813115Z","iopub.status.idle":"2023-05-16T00:04:40.089132Z","shell.execute_reply.started":"2023-05-16T00:04:39.81306Z","shell.execute_reply":"2023-05-16T00:04:40.087758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1)\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-16T00:06:15.586139Z","iopub.execute_input":"2023-05-16T00:06:15.586597Z","iopub.status.idle":"2023-05-16T00:06:16.045408Z","shell.execute_reply.started":"2023-05-16T00:06:15.586563Z","shell.execute_reply":"2023-05-16T00:06:16.044296Z"},"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)\n\ntest_datagen = ImageDataGenerator(rescale=1)\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-16T00:06:21.432012Z","iopub.execute_input":"2023-05-16T00:06:21.43249Z","iopub.status.idle":"2023-05-16T00:06:34.854214Z","shell.execute_reply.started":"2023-05-16T00:06:21.432453Z","shell.execute_reply":"2023-05-16T00:06:34.85311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_set.batch_size","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:19:12.671196Z","iopub.execute_input":"2023-05-16T00:19:12.671704Z","iopub.status.idle":"2023-05-16T00:19:12.680723Z","shell.execute_reply.started":"2023-05-16T00:19:12.671664Z","shell.execute_reply":"2023-05-16T00:19:12.679335Z"},"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, 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(96,(4, 4),strides = 4, activation='relu'),\n            layers.MaxPool2D((3, 3), strides=2),\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        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, y):\n        return self\n    def transform(self, X, y=None):\n        return np.array(X, dtype=\"float32\") / 255\n\nclass MudaShape(BaseEstimator, TransformerMixin):\n    def fit(self, X, y):\n        return self\n    def transform(self, X, y=None):\n        return X.reshape((-1,28,28,1))\n\nmodelo = Pipeline([\n    (\"scaler\", DividePor255()), \n    (\"reshape\", MudaShape()), \n    (\"ann\", RedeNeural())\n])\nmodelo.fit(train_set,test_labels)\nypred = modelo.predict(test_set)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-16T00:22:32.526842Z","iopub.execute_input":"2023-05-16T00:22:32.527371Z"},"trusted":true},"execution_count":null,"outputs":[]}]}