{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\nimport pathlib\nimport shutil\nimport tensorflow as tf\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\nfrom keras.utils import to_categorical, image_dataset_from_directory\nfrom keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.layers import MaxPooling2D, GlobalMaxPooling2D, Activation\nfrom keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras import optimizers\nfrom keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.models import Model\nfrom keras.applications import VGG16\nfrom keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras import layers, losses, optimizers, applications","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-10T10:42:05.705249Z","iopub.execute_input":"2024-09-10T10:42:05.706168Z","iopub.status.idle":"2024-09-10T10:42:19.067916Z","shell.execute_reply.started":"2024-09-10T10:42:05.706126Z","shell.execute_reply":"2024-09-10T10:42:19.067102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Unzip train dataset\n!unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip\n#Unzip test dataset\n!unzip ../input/dogs-vs-cats-redux-kernels-edition/test.zip","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:19.069401Z","iopub.execute_input":"2024-09-10T10:42:19.069948Z","iopub.status.idle":"2024-09-10T10:42:39.266519Z","shell.execute_reply.started":"2024-09-10T10:42:19.069914Z","shell.execute_reply":"2024-09-10T10:42:39.265391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/working/train'\n\n# Crear subdirectorios para cada clase\nos.makedirs(os.path.join(base_dir, 'dogs'), exist_ok=True)\nos.makedirs(os.path.join(base_dir, 'cats'), exist_ok=True)\n\n# Mover las imágenes a los subdirectorios correspondientes\nfor filename in os.listdir(base_dir):\n    file_path = os.path.join(base_dir, filename)\n    if os.path.isfile(file_path):  \n        if filename.startswith('dog'):\n            shutil.move(file_path, os.path.join(base_dir, 'dogs', filename))\n        elif filename.startswith('cat'):\n            shutil.move(file_path, os.path.join(base_dir, 'cats', filename))","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:39.267871Z","iopub.execute_input":"2024-09-10T10:42:39.268173Z","iopub.status.idle":"2024-09-10T10:42:40.255477Z","shell.execute_reply.started":"2024-09-10T10:42:39.268141Z","shell.execute_reply":"2024-09-10T10:42:40.254547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create tensorflow dataset with image_dataset_from_directory\ndirectory = \"/kaggle/working/train\"\npath_for_data = pathlib.Path(directory)\n\ntrain_df = image_dataset_from_directory(path_for_data,\n                                        image_size = (128, 128),\n                                        validation_split = 0.3,\n                                        subset = \"training\",\n                                        shuffle = True,\n                                        batch_size = 25,\n                                        seed = 123)\n\nvalidation_df = image_dataset_from_directory(path_for_data,\n                                             image_size = (128, 128),\n                                             validation_split = 0.3,\n                                             subset = \"validation\",\n                                             shuffle = True,                                         \n                                             batch_size = 25,\n                                             seed = 123)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:40.257779Z","iopub.execute_input":"2024-09-10T10:42:40.258106Z","iopub.status.idle":"2024-09-10T10:42:45.358601Z","shell.execute_reply.started":"2024-09-10T10:42:40.258073Z","shell.execute_reply":"2024-09-10T10:42:45.357854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_batches = tf.data.experimental.cardinality(validation_df)\ntest_df = validation_df.take(validation_batches // 5)\nvalidation_df = validation_df.skip(validation_batches // 5)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:45.359717Z","iopub.execute_input":"2024-09-10T10:42:45.359989Z","iopub.status.idle":"2024-09-10T10:42:45.374978Z","shell.execute_reply.started":"2024-09-10T10:42:45.359960Z","shell.execute_reply":"2024-09-10T10:42:45.374230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Opciones de performance para dataset\n#Autotune es una constante en TensorFlow que se utiliza para optimizar automáticamente el rendimiento de las operaciones de entrada y salida\n#Prefetch permite que el preprocesamiento de los datos y la ejecución del modelo se realicen en paralelo. \n#Cache almacenar en caché los datos del dataset después de su primer acceso, de modo que las operaciones posteriores puedan acceder a los datos directamente\nautotune = tf.data.AUTOTUNE\nclass_names = train_df.class_names\n\ntrain_df = train_df.cache()\ntest_df = test_df.cache()\n\npf_train = train_df.prefetch(buffer_size = autotune)\npf_test = test_df.prefetch(buffer_size = autotune)\npf_val = validation_df.prefetch(buffer_size = autotune)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:45.376080Z","iopub.execute_input":"2024-09-10T10:42:45.376681Z","iopub.status.idle":"2024-09-10T10:42:45.393733Z","shell.execute_reply.started":"2024-09-10T10:42:45.376637Z","shell.execute_reply":"2024-09-10T10:42:45.392974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Aumentación de datos\n#No se usa image data generator sino capas de keras\ndata_augmentation = tf.keras.Sequential()\ndata_augmentation.add(layers.RandomRotation(0.3))\ndata_augmentation.add(layers.RandomFlip(\"horizontal_and_vertical\"))\ndata_augmentation.add(layers.RandomZoom(0.1))\ndata_augmentation.add(layers.RandomContrast(0.2))\ndata_augmentation.add(layers.RandomBrightness(0.2))","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:45.394803Z","iopub.execute_input":"2024-09-10T10:42:45.395118Z","iopub.status.idle":"2024-09-10T10:42:45.423634Z","shell.execute_reply.started":"2024-09-10T10:42:45.395080Z","shell.execute_reply":"2024-09-10T10:42:45.422900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Codigo para el modelo\nimage_size = (128, 128)\nimage_shape = image_size + (3,)\npreprocess_input = applications.resnet50.preprocess_input\nbase_model = applications.ResNet50(input_shape = image_shape, include_top = False, weights = 'imagenet')\nnclass = len(class_names)\nglobal_avg = layers.GlobalAveragePooling2D()\noutput_layer = layers.Dense(nclass, activation = 'softmax')\ninputs = tf.keras.Input(shape = image_shape)\nx = data_augmentation(inputs)\nx = preprocess_input(inputs)\nx = base_model(x)\nx = global_avg(x)\noutputs = output_layer(x)\n\nmodel = tf.keras.Model(inputs = inputs, outputs = outputs)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:45.424643Z","iopub.execute_input":"2024-09-10T10:42:45.424925Z","iopub.status.idle":"2024-09-10T10:42:50.208027Z","shell.execute_reply.started":"2024-09-10T10:42:45.424896Z","shell.execute_reply":"2024-09-10T10:42:50.207141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = optimizers.Adam(learning_rate = optimizers.schedules.CosineDecay(0.001, 500))\nloss = losses.SparseCategoricalCrossentropy()\nmodel.compile(optimizer = optimizer, loss = loss, metrics = ['accuracy'])\nhistory = model.fit(pf_train, validation_data = (pf_val), epochs = 3)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:42:50.209083Z","iopub.execute_input":"2024-09-10T10:42:50.209467Z","iopub.status.idle":"2024-09-10T10:51:11.421896Z","shell.execute_reply.started":"2024-09-10T10:42:50.209434Z","shell.execute_reply":"2024-09-10T10:51:11.421021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicciones\nimage_batch, label_batch = pf_test.as_numpy_iterator().next()\npred_labels = np.argmax(model.predict(image_batch), axis = 1)\nlab_and_pred = np.transpose(np.vstack((label_batch, pred_labels)))\nprint(lab_and_pred)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T10:51:11.425554Z","iopub.execute_input":"2024-09-10T10:51:11.426082Z","iopub.status.idle":"2024-09-10T10:51:14.619590Z","shell.execute_reply.started":"2024-09-10T10:51:11.426045Z","shell.execute_reply":"2024-09-10T10:51:14.618670Z"},"trusted":true},"execution_count":null,"outputs":[]}]}