{"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\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":"2021-07-14T22:39:57.225566Z","iopub.execute_input":"2021-07-14T22:39:57.225934Z","iopub.status.idle":"2021-07-14T22:40:31.904706Z","shell.execute_reply.started":"2021-07-14T22:39:57.225884Z","shell.execute_reply":"2021-07-14T22:40:31.900828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_hub as hub\nfrom sklearn.model_selection import train_test_split\nimport pathlib\nimport os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n##Permite usar los datasets de Tensorflow\nimport tensorflow_datasets as tfds\nimport matplotlib.pyplot as plt\nfrom keras_preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-07-14T22:40:31.90685Z","iopub.execute_input":"2021-07-14T22:40:31.907202Z","iopub.status.idle":"2021-07-14T22:40:37.298415Z","shell.execute_reply.started":"2021-07-14T22:40:31.907165Z","shell.execute_reply":"2021-07-14T22:40:37.297573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\ndf['id'] = df['id']+\".jpg\"\ndf.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T22:40:37.300318Z","iopub.execute_input":"2021-07-14T22:40:37.300649Z","iopub.status.idle":"2021-07-14T22:40:37.363621Z","shell.execute_reply.started":"2021-07-14T22:40:37.300614Z","shell.execute_reply":"2021-07-14T22:40:37.36291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = df['breed'].unique()\nclasses","metadata":{"execution":{"iopub.status.busy":"2021-07-14T22:40:37.366454Z","iopub.execute_input":"2021-07-14T22:40:37.366703Z","iopub.status.idle":"2021-07-14T22:40:37.377369Z","shell.execute_reply.started":"2021-07-14T22:40:37.366678Z","shell.execute_reply":"2021-07-14T22:40:37.376452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = (32, 32)\nbatch_size = 32\nimage_res = 224\ndatagen=ImageDataGenerator(rescale=1./255,validation_split=0.25)\ntrain_generator=datagen.flow_from_dataframe(\n    dataframe=df, \n    directory=\"../input/dog-breed-identification/train\", \n    x_col=\"id\", \n    y_col=\"breed\", \n    class_mode=\"categorical\", \n    subset=\"training\",\n    seed=1337,\n    shuffle=True,\n    target_size=image_size, \n    batch_size=batch_size,\n)\nvalid_generator=datagen.flow_from_dataframe(\n    dataframe=df, \n    directory=\"../input/dog-breed-identification/train\", \n    x_col=\"id\", \n    y_col=\"breed\", \n    class_mode=\"categorical\", \n    subset=\"validation\",\n    seed=1337,\n    shuffle=True,\n    target_size=image_size, \n    batch_size=batch_size,\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T22:40:37.378722Z","iopub.execute_input":"2021-07-14T22:40:37.37916Z","iopub.status.idle":"2021-07-14T22:40:41.451893Z","shell.execute_reply.started":"2021-07-14T22:40:37.379123Z","shell.execute_reply":"2021-07-14T22:40:41.451019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes=len(classes)\ninput_shape=image_size + (3,)\n\n#model = make_model_1(input_shape, num_classes)\n#model = make_model_fv(num_classes)\nmodel = keras.Sequential(\n    [\n        keras.Input(shape=input_shape),\n        layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n        layers.experimental.preprocessing.RandomRotation(0.5),\n        #layers.experimental.preprocessing.Rescaling(1./255),\n        layers.Activation(\"relu\"),\n        layers.Conv2D(512,(3,3), activation=\"relu\",padding=\"same\"),\n        layers.Activation(\"relu\"),\n        layers.MaxPooling2D(),\n        layers.Dropout(0.25),\n        \n        \n        layers.Conv2D(512,(3,3), activation=\"relu\",padding=\"same\"),\n        layers.Activation(\"relu\"),\n        layers.Conv2D(512,(3,3), activation=\"relu\",padding=\"same\"),\n        layers.MaxPooling2D(),\n        layers.Dropout(0.25),\n        \n        layers.Flatten(),\n        layers.Dense(512, activation='relu'),\n        layers.Dense(num_classes, activation=\"softmax\"),\n    ]\n)\n# Construir el modelo y ver la arquitectura\n\nmodel.build(input_shape)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T22:49:29.750214Z","iopub.execute_input":"2021-07-14T22:49:29.750561Z","iopub.status.idle":"2021-07-14T22:49:29.889356Z","shell.execute_reply.started":"2021-07-14T22:49:29.750527Z","shell.execute_reply":"2021-07-14T22:49:29.887641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 5\n\nmodel.compile(\n  optimizer=keras.optimizers.Adam(1e-3),\n  loss=\"sparse_categorical_crossentropy\",\n  metrics=['accuracy'])\n\n\nSTEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size\n#STEP_SIZE_TEST=test_generator.n//test_generator.batch_size\nhistory = model.fit_generator(generator=train_generator,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=valid_generator,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T22:49:33.009062Z","iopub.execute_input":"2021-07-14T22:49:33.009389Z","iopub.status.idle":"2021-07-14T22:49:33.955367Z","shell.execute_reply.started":"2021-07-14T22:49:33.009359Z","shell.execute_reply":"2021-07-14T22:49:33.953175Z"},"trusted":true},"execution_count":null,"outputs":[]}]}