{"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":"Entrenamiento JPG","metadata":{}},{"cell_type":"code","source":"import tensorflow.keras as keras","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:21:45.313975Z","iopub.execute_input":"2023-06-22T08:21:45.314338Z","iopub.status.idle":"2023-06-22T08:21:45.382762Z","shell.execute_reply.started":"2023-06-22T08:21:45.314301Z","shell.execute_reply":"2023-06-22T08:21:45.375351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-binary/data/train')","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:21:48.196261Z","iopub.execute_input":"2023-06-22T08:21:48.196624Z","iopub.status.idle":"2023-06-22T08:22:37.266395Z","shell.execute_reply.started":"2023-06-22T08:21:48.196591Z","shell.execute_reply":"2023-06-22T08:22:37.265406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-binary/data/valid')","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:22:37.268195Z","iopub.execute_input":"2023-06-22T08:22:37.268579Z","iopub.status.idle":"2023-06-22T08:29:09.873113Z","shell.execute_reply.started":"2023-06-22T08:22:37.268547Z","shell.execute_reply":"2023-06-22T08:29:09.872095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-binary/data/test', shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:29:09.87455Z","iopub.execute_input":"2023-06-22T08:29:09.874904Z","iopub.status.idle":"2023-06-22T08:38:44.125187Z","shell.execute_reply.started":"2023-06-22T08:29:09.874865Z","shell.execute_reply":"2023-06-22T08:38:44.124397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout , BatchNormalization\n# from tensorflow.keras.preprocessing import Rescaling\n\nmodel = Sequential()\n# model.add(Rescaling(scale = 1/255, input_shape = ( 256, 256, 3)))\nmodel.add(Conv2D(32 , (3,3) , padding = 'same' , activation = 'relu' , input_shape = ( 256, 256, 3)))\n\n\nmodel.add(MaxPool2D((2,2)))\nmodel.add(Conv2D(64 , (3,3), activation = 'relu'))\n\n\n\nmodel.add(MaxPool2D((2,2)))\nmodel.add(Conv2D(64 , (3,3), activation = 'relu'))\n\n\nmodel.add(MaxPool2D((2,2) ))\nmodel.add(Conv2D(128 , (3,3), activation = 'relu'))\n\n\n\nmodel.add(MaxPool2D((2,2)))\nmodel.add(Conv2D(256 , (3,3), activation = 'relu'))\n\n\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(units = 128 , activation = 'relu'))\nmodel.add(Dense(units = 1 , activation = 'sigmoid')) #clasificación binaria","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:38:44.126617Z","iopub.execute_input":"2023-06-22T08:38:44.12696Z","iopub.status.idle":"2023-06-22T08:38:44.242443Z","shell.execute_reply.started":"2023-06-22T08:38:44.126921Z","shell.execute_reply":"2023-06-22T08:38:44.241703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = keras.losses.BinaryCrossentropy()\nmodel.compile(loss=loss, \n              optimizer = keras.optimizers.Adam(1e-3),\n              metrics=[keras.metrics.AUC(),keras.metrics.Precision(),\n                       keras.metrics.Recall()])","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:38:44.245778Z","iopub.execute_input":"2023-06-22T08:38:44.246141Z","iopub.status.idle":"2023-06-22T08:38:44.404842Z","shell.execute_reply.started":"2023-06-22T08:38:44.246097Z","shell.execute_reply":"2023-06-22T08:38:44.404058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n      x = training,\n      epochs=10,\n      validation_data=validation,\n      verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T08:38:44.406461Z","iopub.execute_input":"2023-06-22T08:38:44.406802Z","iopub.status.idle":"2023-06-22T12:47:11.7373Z","shell.execute_reply.started":"2023-06-22T08:38:44.406764Z","shell.execute_reply":"2023-06-22T12:47:11.73634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/model-simple.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-22T12:47:11.741823Z","iopub.execute_input":"2023-06-22T12:47:11.744034Z","iopub.status.idle":"2023-06-22T12:47:11.841112Z","shell.execute_reply.started":"2023-06-22T12:47:11.743985Z","shell.execute_reply":"2023-06-22T12:47:11.839879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T12:47:11.842803Z","iopub.execute_input":"2023-06-22T12:47:11.843169Z","iopub.status.idle":"2023-06-22T13:45:25.082276Z","shell.execute_reply.started":"2023-06-22T12:47:11.84313Z","shell.execute_reply":"2023-06-22T13:45:25.080671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"test loss, test auc, test precision, test recall:\", results)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:45:25.083984Z","iopub.execute_input":"2023-06-22T13:45:25.085182Z","iopub.status.idle":"2023-06-22T13:45:25.090416Z","shell.execute_reply.started":"2023-06-22T13:45:25.085148Z","shell.execute_reply":"2023-06-22T13:45:25.089468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:45:25.091609Z","iopub.execute_input":"2023-06-22T13:45:25.092369Z","iopub.status.idle":"2023-06-22T14:10:47.545903Z","shell.execute_reply.started":"2023-06-22T13:45:25.092326Z","shell.execute_reply":"2023-06-22T14:10:47.544818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ny_pred_binary = np.squeeze(np.round(y_pred)).astype(int)\ny_pred_binary","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:10:47.547587Z","iopub.execute_input":"2023-06-22T14:10:47.547992Z","iopub.status.idle":"2023-06-22T14:10:47.558296Z","shell.execute_reply.started":"2023-06-22T14:10:47.54795Z","shell.execute_reply":"2023-06-22T14:10:47.557175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_proba = model.predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:10:47.560007Z","iopub.execute_input":"2023-06-22T14:10:47.560737Z","iopub.status.idle":"2023-06-22T14:25:28.572307Z","shell.execute_reply.started":"2023-06-22T14:10:47.560667Z","shell.execute_reply":"2023-06-22T14:25:28.571343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_proba[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:25:28.573734Z","iopub.execute_input":"2023-06-22T14:25:28.574085Z","iopub.status.idle":"2023-06-22T14:25:28.58276Z","shell.execute_reply.started":"2023-06-22T14:25:28.574049Z","shell.execute_reply":"2023-06-22T14:25:28.581903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}