{"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":"markdown","source":"# Doctorado en estadística\n## Rodrigo Barrera","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from keras.layers import Input, Lambda, Dense\nfrom keras.models import Model\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom pathlib import Path\nimport os.path\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-29T01:09:39.486340Z","iopub.execute_input":"2022-12-29T01:09:39.486794Z","iopub.status.idle":"2022-12-29T01:09:45.612150Z","shell.execute_reply.started":"2022-12-29T01:09:39.486679Z","shell.execute_reply":"2022-12-29T01:09:45.611328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:09:45.613856Z","iopub.execute_input":"2022-12-29T01:09:45.614104Z","iopub.status.idle":"2022-12-29T01:09:45.726097Z","shell.execute_reply.started":"2022-12-29T01:09:45.614070Z","shell.execute_reply":"2022-12-29T01:09:45.725393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.species.value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:09:45.727619Z","iopub.execute_input":"2022-12-29T01:09:45.728175Z","iopub.status.idle":"2022-12-29T01:09:45.749828Z","shell.execute_reply.started":"2022-12-29T01:09:45.728138Z","shell.execute_reply":"2022-12-29T01:09:45.749060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = Path('../input/happywhaleimagessortedbyspecies/train_species_list')","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:09:47.664065Z","iopub.execute_input":"2022-12-29T01:09:47.664355Z","iopub.status.idle":"2022-12-29T01:09:47.668709Z","shell.execute_reply.started":"2022-12-29T01:09:47.664305Z","shell.execute_reply":"2022-12-29T01:09:47.668011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepaths = list(image_dir.glob(r'**/*.jpg'))\nlabels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1], filepaths))\n\nfilepaths = pd.Series(filepaths, name='Filepath').astype(str)\nlabels = pd.Series(labels, name='Label')\n\nimage_df = pd.concat([filepaths, labels], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:09:53.156983Z","iopub.execute_input":"2022-12-29T01:09:53.157543Z","iopub.status.idle":"2022-12-29T01:10:31.621292Z","shell.execute_reply.started":"2022-12-29T01:09:53.157501Z","shell.execute_reply":"2022-12-29T01:10:31.620311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, test_df = train_test_split(image_df, train_size=0.8, shuffle=True, random_state=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:31.622994Z","iopub.execute_input":"2022-12-29T01:10:31.623274Z","iopub.status.idle":"2022-12-29T01:10:31.634307Z","shell.execute_reply.started":"2022-12-29T01:10:31.623234Z","shell.execute_reply":"2022-12-29T01:10:31.633389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,\n)\n\ntest_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:31.637362Z","iopub.execute_input":"2022-12-29T01:10:31.637581Z","iopub.status.idle":"2022-12-29T01:10:31.916873Z","shell.execute_reply.started":"2022-12-29T01:10:31.637556Z","shell.execute_reply":"2022-12-29T01:10:31.916175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train_generator.flow_from_dataframe(\n    dataframe=train_df,\n    x_col='Filepath',\n    y_col='Label',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    shuffle=True,\n    seed=42,\n    subset='training'\n)\n\ntest_images = test_generator.flow_from_dataframe(\n    dataframe=test_df,\n    x_col='Filepath',\n    y_col='Label',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:31.918734Z","iopub.execute_input":"2022-12-29T01:10:31.919071Z","iopub.status.idle":"2022-12-29T01:10:54.039066Z","shell.execute_reply.started":"2022-12-29T01:10:31.919035Z","shell.execute_reply":"2022-12-29T01:10:54.038090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MobilenetV2","metadata":{}},{"cell_type":"code","source":"pretrained_model = tf.keras.applications.MobileNetV2(\n    input_shape=(224, 224, 3),\n    include_top=False,\n    weights='imagenet',\n    pooling='avg'\n)\n\npretrained_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:54.040251Z","iopub.execute_input":"2022-12-29T01:10:54.040522Z","iopub.status.idle":"2022-12-29T01:10:58.119056Z","shell.execute_reply.started":"2022-12-29T01:10:54.040487Z","shell.execute_reply":"2022-12-29T01:10:58.118309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\n\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:58.120402Z","iopub.execute_input":"2022-12-29T01:10:58.120643Z","iopub.status.idle":"2022-12-29T01:10:58.155221Z","shell.execute_reply.started":"2022-12-29T01:10:58.120609Z","shell.execute_reply":"2022-12-29T01:10:58.154608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:58.156522Z","iopub.execute_input":"2022-12-29T01:10:58.156762Z","iopub.status.idle":"2022-12-29T01:10:58.171275Z","shell.execute_reply.started":"2022-12-29T01:10:58.156720Z","shell.execute_reply":"2022-12-29T01:10:58.170627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = model.fit(\n    train_images,\n    validation_data=test_images,\n    epochs=5,\n    )","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:10:58.172619Z","iopub.execute_input":"2022-12-29T01:10:58.173008Z","iopub.status.idle":"2022-12-29T01:26:09.176203Z","shell.execute_reply.started":"2022-12-29T01:10:58.172975Z","shell.execute_reply":"2022-12-29T01:26:09.175399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Función de costo","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nfig.savefig('mnet1.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:26:09.178621Z","iopub.execute_input":"2022-12-29T01:26:09.178920Z","iopub.status.idle":"2022-12-29T01:26:09.465243Z","shell.execute_reply.started":"2022-12-29T01:26:09.178882Z","shell.execute_reply":"2022-12-29T01:26:09.464255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Precisión","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nfig.savefig('mnet2.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-29T01:26:09.466626Z","iopub.execute_input":"2022-12-29T01:26:09.467093Z","iopub.status.idle":"2022-12-29T01:26:09.762871Z","shell.execute_reply.started":"2022-12-29T01:26:09.467056Z","shell.execute_reply":"2022-12-29T01:26:09.761977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test_images, verbose=0)\nprint(\"Test Accuracy: {:.2f}%\".format(results[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:02:38.268085Z","iopub.execute_input":"2022-12-28T16:02:38.271198Z","iopub.status.idle":"2022-12-28T16:03:06.331814Z","shell.execute_reply.started":"2022-12-28T16:02:38.271118Z","shell.execute_reply":"2022-12-28T16:03:06.330844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_images), axis=1)\n\ncm = confusion_matrix(test_images.labels, predictions)\nclr = classification_report(test_images.labels, predictions, target_names=test_images.class_indices, zero_division=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:06.334225Z","iopub.execute_input":"2022-12-28T16:03:06.334763Z","iopub.status.idle":"2022-12-28T16:03:34.258862Z","shell.execute_reply.started":"2022-12-28T16:03:06.334719Z","shell.execute_reply":"2022-12-28T16:03:34.258113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Confusion Matrix","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nsns.heatmap(cm, annot=True, fmt='g', vmin=0, cmap='Blues', cbar=False)\nplt.xticks(ticks=np.arange(30) + 0.5, labels=test_images.class_indices, rotation=90)\nplt.yticks(ticks=np.arange(30) + 0.5, labels=test_images.class_indices, rotation=0)\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.title(\"Confusion Matrix\")\nplt.show()\nfig.savefig('mnet3.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:34.260043Z","iopub.execute_input":"2022-12-28T16:03:34.260318Z","iopub.status.idle":"2022-12-28T16:03:37.022515Z","shell.execute_reply.started":"2022-12-28T16:03:34.260283Z","shell.execute_reply":"2022-12-28T16:03:37.021590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Tabla de precisión","metadata":{}},{"cell_type":"code","source":"print(clr)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:37.024072Z","iopub.execute_input":"2022-12-28T16:03:37.024346Z","iopub.status.idle":"2022-12-28T16:03:37.031145Z","shell.execute_reply.started":"2022-12-28T16:03:37.024310Z","shell.execute_reply":"2022-12-28T16:03:37.030169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('mnet_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:37.032744Z","iopub.execute_input":"2022-12-28T16:03:37.033168Z","iopub.status.idle":"2022-12-28T16:03:37.362051Z","shell.execute_reply.started":"2022-12-28T16:03:37.033098Z","shell.execute_reply":"2022-12-28T16:03:37.361300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG19","metadata":{}},{"cell_type":"code","source":"pretrained_model = tf.keras.applications.VGG19(input_shape=(40,60,3),include_top=False,pooling='avg',classes=1000,classifier_activation=\"softmax\")\npretrained_model.trainable = False\ninputs = pretrained_model.input\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:37.363468Z","iopub.execute_input":"2022-12-28T16:03:37.363741Z","iopub.status.idle":"2022-12-28T16:03:37.744002Z","shell.execute_reply.started":"2022-12-28T16:03:37.363700Z","shell.execute_reply":"2022-12-28T16:03:37.743274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\n\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:37.745272Z","iopub.execute_input":"2022-12-28T16:03:37.745520Z","iopub.status.idle":"2022-12-28T16:03:37.773449Z","shell.execute_reply.started":"2022-12-28T16:03:37.745486Z","shell.execute_reply":"2022-12-28T16:03:37.772699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ajuste del modelo","metadata":{}},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nr = model.fit(\n    train_images,\n    validation_data=test_images,\n    epochs=5,\n    )\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:03:37.774578Z","iopub.execute_input":"2022-12-28T16:03:37.774894Z","iopub.status.idle":"2022-12-28T16:17:16.710268Z","shell.execute_reply.started":"2022-12-28T16:03:37.774858Z","shell.execute_reply":"2022-12-28T16:17:16.709547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nfig.savefig('vgg19_1.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:17:16.712049Z","iopub.execute_input":"2022-12-28T16:17:16.712654Z","iopub.status.idle":"2022-12-28T16:17:16.990397Z","shell.execute_reply.started":"2022-12-28T16:17:16.712615Z","shell.execute_reply":"2022-12-28T16:17:16.989450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nfig.savefig('vgg19_2.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:17:16.995560Z","iopub.execute_input":"2022-12-28T16:17:16.995934Z","iopub.status.idle":"2022-12-28T16:17:17.289814Z","shell.execute_reply.started":"2022-12-28T16:17:16.995898Z","shell.execute_reply":"2022-12-28T16:17:17.288930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test_images, verbose=0)\nprint(\"Test Accuracy: {:.2f}%\".format(results[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:17:17.291057Z","iopub.execute_input":"2022-12-28T16:17:17.291453Z","iopub.status.idle":"2022-12-28T16:17:48.806188Z","shell.execute_reply.started":"2022-12-28T16:17:17.291418Z","shell.execute_reply":"2022-12-28T16:17:48.805376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_images), axis=1)\n\ncm = confusion_matrix(test_images.labels, predictions)\nclr = classification_report(test_images.labels, predictions, target_names=test_images.class_indices, zero_division=0)\nprint(clr)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:17:48.807520Z","iopub.execute_input":"2022-12-28T16:17:48.808049Z","iopub.status.idle":"2022-12-28T16:18:15.693653Z","shell.execute_reply.started":"2022-12-28T16:17:48.808010Z","shell.execute_reply":"2022-12-28T16:18:15.692079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EfficientNetB7","metadata":{}},{"cell_type":"code","source":"\npretrained_model = tf.keras.applications.EfficientNetB7(input_shape=(40,60,3),include_top=False,pooling='avg')\npretrained_model.trainable = False\ninputs = pretrained_model.input\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:18:15.694801Z","iopub.execute_input":"2022-12-28T16:18:15.695063Z","iopub.status.idle":"2022-12-28T16:18:29.570730Z","shell.execute_reply.started":"2022-12-28T16:18:15.695027Z","shell.execute_reply":"2022-12-28T16:18:29.570004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\n\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nr = model.fit(\n    train_images,\n    validation_data=test_images,\n    epochs=5,\n    )","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:18:29.572008Z","iopub.execute_input":"2022-12-28T16:18:29.572274Z","iopub.status.idle":"2022-12-28T16:31:38.405561Z","shell.execute_reply.started":"2022-12-28T16:18:29.572238Z","shell.execute_reply":"2022-12-28T16:31:38.404846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test_images, verbose=0)\nprint(\"Test Accuracy: {:.2f}%\".format(results[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:31:38.407206Z","iopub.execute_input":"2022-12-28T16:31:38.407475Z","iopub.status.idle":"2022-12-28T16:32:09.860676Z","shell.execute_reply.started":"2022-12-28T16:31:38.407426Z","shell.execute_reply":"2022-12-28T16:32:09.859810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_images), axis=1)\n\ncm = confusion_matrix(test_images.labels, predictions)\nclr = classification_report(test_images.labels, predictions, target_names=test_images.class_indices, zero_division=0)\nprint(clr)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:32:09.862105Z","iopub.execute_input":"2022-12-28T16:32:09.862549Z","iopub.status.idle":"2022-12-28T16:32:38.112986Z","shell.execute_reply.started":"2022-12-28T16:32:09.862508Z","shell.execute_reply":"2022-12-28T16:32:38.112175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # Gráfica de precisión","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nfig.savefig('ef_1.svg', format='svg', dpi=1200)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nfig.savefig('ef_2.svg', format='svg', dpi=1200)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:32:38.114347Z","iopub.execute_input":"2022-12-28T16:32:38.114774Z","iopub.status.idle":"2022-12-28T16:32:38.314037Z","shell.execute_reply.started":"2022-12-28T16:32:38.114733Z","shell.execute_reply":"2022-12-28T16:32:38.313378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model = tf.keras.applications.VGG16(input_shape=(40,60,3),include_top=False,pooling='avg',classes=1000,classifier_activation=\"softmax\")\npretrained_model.trainable = False\ninputs = pretrained_model.input\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:32:38.315325Z","iopub.execute_input":"2022-12-28T16:32:38.315731Z","iopub.status.idle":"2022-12-28T16:32:39.011163Z","shell.execute_reply.started":"2022-12-28T16:32:38.315693Z","shell.execute_reply":"2022-12-28T16:32:39.010432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\n\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:32:39.012471Z","iopub.execute_input":"2022-12-28T16:32:39.012732Z","iopub.status.idle":"2022-12-28T16:32:39.040891Z","shell.execute_reply.started":"2022-12-28T16:32:39.012691Z","shell.execute_reply":"2022-12-28T16:32:39.040269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nr = model.fit(\n    train_images,\n    validation_data=test_images,\n    epochs=5,\n    )\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:32:39.042087Z","iopub.execute_input":"2022-12-28T16:32:39.042354Z","iopub.status.idle":"2022-12-28T16:46:23.289278Z","shell.execute_reply.started":"2022-12-28T16:32:39.042316Z","shell.execute_reply":"2022-12-28T16:46:23.288484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Función de costo","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nfig.savefig('vgg16_1.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:46:23.290896Z","iopub.execute_input":"2022-12-28T16:46:23.291269Z","iopub.status.idle":"2022-12-28T16:46:23.627496Z","shell.execute_reply.started":"2022-12-28T16:46:23.291229Z","shell.execute_reply":"2022-12-28T16:46:23.626340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nplt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nfig.savefig('vgg16_2.svg', format='svg', dpi=1200)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:46:23.634059Z","iopub.execute_input":"2022-12-28T16:46:23.638253Z","iopub.status.idle":"2022-12-28T16:46:23.946871Z","shell.execute_reply.started":"2022-12-28T16:46:23.638177Z","shell.execute_reply":"2022-12-28T16:46:23.945930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reporte de clasificación","metadata":{}},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_images), axis=1)\n\ncm = confusion_matrix(test_images.labels, predictions)\nclr = classification_report(test_images.labels, predictions, target_names=test_images.class_indices, zero_division=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:46:23.948265Z","iopub.execute_input":"2022-12-28T16:46:23.948524Z","iopub.status.idle":"2022-12-28T16:46:50.518804Z","shell.execute_reply.started":"2022-12-28T16:46:23.948486Z","shell.execute_reply":"2022-12-28T16:46:50.518024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T16:46:50.519939Z","iopub.execute_input":"2022-12-28T16:46:50.520198Z","iopub.status.idle":"2022-12-28T16:46:50.527783Z","shell.execute_reply.started":"2022-12-28T16:46:50.520162Z","shell.execute_reply":"2022-12-28T16:46:50.524577Z"},"trusted":true},"execution_count":null,"outputs":[]}]}