{"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":"none","dataSources":[{"sourceId":84511,"databundleVersionId":9468663,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-10-15T15:07:18.305393Z","iopub.execute_input":"2024-10-15T15:07:18.305955Z","iopub.status.idle":"2024-10-15T15:07:18.321503Z","shell.execute_reply.started":"2024-10-15T15:07:18.305903Z","shell.execute_reply":"2024-10-15T15:07:18.319972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.load(\"/kaggle/input/dl-itba-cifar-100-2024-q-1/x_train.npy\")/255.\nx_test = np.load(\"/kaggle/input/dl-itba-cifar-100-2024-q-1/x_test.npy\")/255.\ny_train_coarse = np.load(\"/kaggle/input/dl-itba-cifar-100-2024-q-1/y_train_coarse.npy\")\ny_train_fine = np.load(\"/kaggle/input/dl-itba-cifar-100-2024-q-1/y_train_fine.npy\")","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:08:27.598798Z","iopub.execute_input":"2024-10-15T15:08:27.599320Z","iopub.status.idle":"2024-10-15T15:08:28.443105Z","shell.execute_reply.started":"2024-10-15T15:08:27.599272Z","shell.execute_reply":"2024-10-15T15:08:28.441901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-10-15T14:41:48.602049Z","iopub.execute_input":"2024-10-15T14:41:48.602492Z","iopub.status.idle":"2024-10-15T14:41:49.436150Z","shell.execute_reply.started":"2024-10-15T14:41:48.602447Z","shell.execute_reply":"2024-10-15T14:41:49.434686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nwith open(\"/kaggle/input/dl-itba-cifar-100-2024-q-1/fine_label_names.pck\", \"rb\") as f:\n    labels_fine = pickle.load(f)\nwith open(\"/kaggle/input/dl-itba-cifar-100-2024-q-1/coarse_label_names.pck\", \"rb\") as f:\n    labels_coarse = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:09:01.612552Z","iopub.execute_input":"2024-10-15T15:09:01.613553Z","iopub.status.idle":"2024-10-15T15:09:01.621990Z","shell.execute_reply.started":"2024-10-15T15:09:01.613489Z","shell.execute_reply":"2024-10-15T15:09:01.620612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_val, y_train, y_val = train_test_split(x_train, y_train_fine, test_size=0.1, stratify=y_train_fine)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:09:07.943329Z","iopub.execute_input":"2024-10-15T15:09:07.943834Z","iopub.status.idle":"2024-10-15T15:09:08.373966Z","shell.execute_reply.started":"2024-10-15T15:09:07.943778Z","shell.execute_reply":"2024-10-15T15:09:08.372567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\n%load_ext tensorboard","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:09:17.505648Z","iopub.execute_input":"2024-10-15T15:09:17.506164Z","iopub.status.idle":"2024-10-15T15:09:17.515508Z","shell.execute_reply.started":"2024-10-15T15:09:17.506117Z","shell.execute_reply":"2024-10-15T15:09:17.514057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_bis = x_train[:,:,::-1,:]\nplt.figure(figsize=(1,1))\nplt.imshow(x_train_bis[2])","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:12:33.683698Z","iopub.execute_input":"2024-10-15T15:12:33.684223Z","iopub.status.idle":"2024-10-15T15:12:33.913556Z","shell.execute_reply.started":"2024-10-15T15:12:33.684173Z","shell.execute_reply":"2024-10-15T15:12:33.912049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train[2])","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:12:40.098909Z","iopub.execute_input":"2024-10-15T15:12:40.099370Z","iopub.status.idle":"2024-10-15T15:12:40.107461Z","shell.execute_reply.started":"2024-10-15T15:12:40.099326Z","shell.execute_reply":"2024-10-15T15:12:40.105692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_fine[50]","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:12:44.707943Z","iopub.execute_input":"2024-10-15T15:12:44.708439Z","iopub.status.idle":"2024-10-15T15:12:44.719425Z","shell.execute_reply.started":"2024-10-15T15:12:44.708390Z","shell.execute_reply":"2024-10-15T15:12:44.717845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_dup = np.concatenate([x_train, x_train_bis],axis=0)\ny_train_dup = np.concatenate([y_train, y_train], axis=0)\nx_train_bis = x_train_dup.copy()\nx_train_bis_bis = x_train_dup.copy()\nx_train_bis[:,:,1:,:] =x_train_dup[:,:,:-1,:]\nx_train_bis_bis[:,:,:-1,:] =x_train_dup[:,:,1:,:]\nx_train_dup = np.concatenate([x_train_dup, x_train_bis, x_train_bis_bis],axis=0)\ny_train_dup = np.concatenate([y_train_dup, y_train_dup, y_train_dup], axis=0)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-15T14:42:29.963966Z","iopub.execute_input":"2024-10-15T14:42:29.964418Z","iopub.status.idle":"2024-10-15T14:42:37.509333Z","shell.execute_reply.started":"2024-10-15T14:42:29.964372Z","shell.execute_reply":"2024-10-15T14:42:37.507863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_dup.shape, y_train_dup.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-15T14:42:37.511234Z","iopub.execute_input":"2024-10-15T14:42:37.511620Z","iopub.status.idle":"2024-10-15T14:42:37.519429Z","shell.execute_reply.started":"2024-10-15T14:42:37.511579Z","shell.execute_reply":"2024-10-15T14:42:37.518138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Me fijo para una categoría de grano grueso, cuáles son las categorías de grano fino\ncoarse = 10\nprint(f\"Grano grueso: {labels_coarse[coarse]}\")\nfor fine in np.unique(y_train_fine[y_train_coarse == coarse]):\n    print(f\"Grano fino: {labels_fine[fine]}\")","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:11:51.280748Z","iopub.execute_input":"2024-10-15T15:11:51.281270Z","iopub.status.idle":"2024-10-15T15:11:51.289508Z","shell.execute_reply.started":"2024-10-15T15:11:51.281220Z","shell.execute_reply":"2024-10-15T15:11:51.288094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization, Flatten, Conv2D, MaxPool2D, GlobalAveragePooling2D, Activation\nfrom tensorflow.keras.optimizers import SGD, Adam, RMSprop\nfrom tensorflow.keras.regularizers import l1, l2\nfrom tensorflow.keras.constraints import MaxNorm\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, TensorBoard","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Flatten(input_shape=(32,32,3)))\n#model.add(Dense(10000, activation=\"relu\"))\nmodel.add(Dense(100, activation=\"softmax\"))\nmodel.compile(loss=\"sparse_categorical_crossentropy\", metrics = [\"accuracy\"], optimizer = Adam(learning_rate=0.001))","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:18:47.668465Z","iopub.execute_input":"2024-10-15T15:18:47.668973Z","iopub.status.idle":"2024-10-15T15:18:47.708349Z","shell.execute_reply.started":"2024-10-15T15:18:47.668925Z","shell.execute_reply":"2024-10-15T15:18:47.707127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:18:48.967797Z","iopub.execute_input":"2024-10-15T15:18:48.969047Z","iopub.status.idle":"2024-10-15T15:18:48.997279Z","shell.execute_reply.started":"2024-10-15T15:18:48.968976Z","shell.execute_reply":"2024-10-15T15:18:48.996067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rlrop = ReduceLROnPlateau(\n    monitor = \"val_accuracy\",\n    factor = 0.5,\n    patience = 3,\n    verbose = 1,\n    min_lr = 1e-5\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:19:04.081321Z","iopub.execute_input":"2024-10-15T15:19:04.081921Z","iopub.status.idle":"2024-10-15T15:19:04.090627Z","shell.execute_reply.started":"2024-10-15T15:19:04.081869Z","shell.execute_reply":"2024-10-15T15:19:04.088909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mc = ModelCheckpoint(\n    \"best_weights.weights.h5\",\n    monitor = \"val_accuracy\",\n    verbose = 1,\n    save_best_only = True,\n    save_weights_only = True,\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:19:07.247514Z","iopub.execute_input":"2024-10-15T15:19:07.248003Z","iopub.status.idle":"2024-10-15T15:19:07.255369Z","shell.execute_reply.started":"2024-10-15T15:19:07.247955Z","shell.execute_reply":"2024-10-15T15:19:07.253750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(\n    monitor = \"val_accuracy\",\n    patience = 10, \n    verbose = 1,\n    restore_best_weights = True,\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:19:09.826488Z","iopub.execute_input":"2024-10-15T15:19:09.827257Z","iopub.status.idle":"2024-10-15T15:19:09.834705Z","shell.execute_reply.started":"2024-10-15T15:19:09.827197Z","shell.execute_reply":"2024-10-15T15:19:09.832956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tb = TensorBoard(\n    log_dir=\"logs\",\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:19:20.102847Z","iopub.execute_input":"2024-10-15T15:19:20.103380Z","iopub.status.idle":"2024-10-15T15:19:20.109513Z","shell.execute_reply.started":"2024-10-15T15:19:20.103332Z","shell.execute_reply":"2024-10-15T15:19:20.108187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indexes = list(range(len(x_train_dup)))\nnp.random.shuffle(indexes)\nx_train_dup = x_train_dup[indexes]\ny_train_dup = y_train_dup[indexes]\n","metadata":{"execution":{"iopub.status.busy":"2024-10-15T14:43:42.747955Z","iopub.execute_input":"2024-10-15T14:43:42.748432Z","iopub.status.idle":"2024-10-15T14:43:45.650202Z","shell.execute_reply.started":"2024-10-15T14:43:42.748382Z","shell.execute_reply":"2024-10-15T14:43:45.648859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x = x_train, \n    y= y_train, \n    batch_size=32, \n    epochs=2, \n    validation_data = (x_val, y_val), \n    callbacks= [\n        rlrop,\n        es,\n        mc\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:19:47.741003Z","iopub.execute_input":"2024-10-15T15:19:47.742030Z","iopub.status.idle":"2024-10-15T15:20:05.448379Z","shell.execute_reply.started":"2024-10-15T15:19:47.741973Z","shell.execute_reply":"2024-10-15T15:20:05.447099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir logs","metadata":{"execution":{"iopub.status.busy":"2024-10-15T14:48:23.517380Z","iopub.execute_input":"2024-10-15T14:48:23.517853Z","iopub.status.idle":"2024-10-15T14:48:24.784097Z","shell.execute_reply.started":"2024-10-15T14:48:23.517807Z","shell.execute_reply":"2024-10-15T14:48:24.782508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"])\nplt.plot(history.history[\"val_loss\"])","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:20:40.665530Z","iopub.execute_input":"2024-10-15T15:20:40.666695Z","iopub.status.idle":"2024-10-15T15:20:40.912171Z","shell.execute_reply.started":"2024-10-15T15:20:40.666635Z","shell.execute_reply":"2024-10-15T15:20:40.910689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"])\nplt.plot(history.history[\"val_accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:21:04.060856Z","iopub.execute_input":"2024-10-15T15:21:04.061369Z","iopub.status.idle":"2024-10-15T15:21:04.329706Z","shell.execute_reply.started":"2024-10-15T15:21:04.061321Z","shell.execute_reply":"2024-10-15T15:21:04.328136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%tensorboard --logdir logs","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:21:14.719015Z","iopub.execute_input":"2024-10-15T15:21:14.719499Z","iopub.status.idle":"2024-10-15T15:21:14.739316Z","shell.execute_reply.started":"2024-10-15T15:21:14.719453Z","shell.execute_reply":"2024-10-15T15:21:14.737621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test).argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:21:27.826002Z","iopub.execute_input":"2024-10-15T15:21:27.826493Z","iopub.status.idle":"2024-10-15T15:21:29.166926Z","shell.execute_reply.started":"2024-10-15T15:21:27.826442Z","shell.execute_reply":"2024-10-15T15:21:29.165492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(predictions, columns=[\"Label\"])","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:21:46.006054Z","iopub.execute_input":"2024-10-15T15:21:46.007607Z","iopub.status.idle":"2024-10-15T15:21:46.016558Z","shell.execute_reply.started":"2024-10-15T15:21:46.007538Z","shell.execute_reply":"2024-10-15T15:21:46.014902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:21:50.132274Z","iopub.execute_input":"2024-10-15T15:21:50.132713Z","iopub.status.idle":"2024-10-15T15:21:50.167411Z","shell.execute_reply.started":"2024-10-15T15:21:50.132670Z","shell.execute_reply":"2024-10-15T15:21:50.165906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.index.name = \"Id\"","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:22:35.338912Z","iopub.execute_input":"2024-10-15T15:22:35.339440Z","iopub.status.idle":"2024-10-15T15:22:35.345082Z","shell.execute_reply.started":"2024-10-15T15:22:35.339393Z","shell.execute_reply":"2024-10-15T15:22:35.343906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-15T15:22:37.900569Z","iopub.execute_input":"2024-10-15T15:22:37.901090Z","iopub.status.idle":"2024-10-15T15:22:37.926478Z","shell.execute_reply.started":"2024-10-15T15:22:37.901037Z","shell.execute_reply":"2024-10-15T15:22:37.924666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}