{"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":87323,"databundleVersionId":9934427,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Librerias en general\nimport tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport pandas as pd\nimport csv\n\n#Cosas especificas\nfrom tensorflow.keras.models import Model,Sequential\nfrom tensorflow.keras.layers import *","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:28:38.429937Z","iopub.execute_input":"2024-12-08T03:28:38.430282Z","iopub.status.idle":"2024-12-08T03:28:50.744271Z","shell.execute_reply.started":"2024-12-08T03:28:38.430246Z","shell.execute_reply":"2024-12-08T03:28:50.743426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Path del csv\nfilename = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv\"\n#Path de las imagenes de entrenamiento\npath_images = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train/\"\n\n#Aqui guardamos el nombre de cada imagen\nimg_names = []\n#Aqui se guardan los datos para entrenar\ntarget = []\nwith open(filename,\"r\") as file:\n    reader = csv.reader(file,delimiter=\",\")\n    for row in reader:\n        if not row[0]==\"image\":\n            img_names.append(row[0])\n            target.append(1 if row[1]==\"real\" else 0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:28:53.719008Z","iopub.execute_input":"2024-12-08T03:28:53.720094Z","iopub.status.idle":"2024-12-08T03:28:53.733185Z","shell.execute_reply.started":"2024-12-08T03:28:53.720043Z","shell.execute_reply":"2024-12-08T03:28:53.732130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Abre cualquier imagen y la normaliza\ndef open_image(path,filename):\n    img = tf.cast(tf.image.decode_jpeg(tf.io.read_file(path+\"/\"+filename)),tf.float32)\n    img = img/127.5-1\n    return img\n\n#Funcion para abrir el set de entrenamiento\n#Se hace así para optimizar el calculo\n@tf.function()\ndef open_train_image(filename,value):\n    img = open_image(path_images,filename)\n    return img,value\n\n#Abrimos una imagen de prueba\nidx = 100\nimg = open_image(path_images,img_names[2])\nprint(tf.shape(img))\nplt.imshow((img+1)/2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:28:55.640775Z","iopub.execute_input":"2024-12-08T03:28:55.641121Z","iopub.status.idle":"2024-12-08T03:28:57.059016Z","shell.execute_reply.started":"2024-12-08T03:28:55.641087Z","shell.execute_reply":"2024-12-08T03:28:57.058083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices((img_names,target))\ndataset = dataset.map(open_train_image,num_parallel_calls=tf.data.AUTOTUNE)\ndataset = dataset.batch(16).shuffle(1024)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:28:59.675309Z","iopub.execute_input":"2024-12-08T03:28:59.676048Z","iopub.status.idle":"2024-12-08T03:28:59.777443Z","shell.execute_reply.started":"2024-12-08T03:28:59.676011Z","shell.execute_reply":"2024-12-08T03:28:59.776444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for img,v in dataset.take(1):\n    print(tf.shape(img))\n    print(v)\n    plt.imshow((img[0]+1)*0.5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:29:02.296009Z","iopub.execute_input":"2024-12-08T03:29:02.296640Z","iopub.status.idle":"2024-12-08T03:29:12.251230Z","shell.execute_reply.started":"2024-12-08T03:29:02.296600Z","shell.execute_reply":"2024-12-08T03:29:12.250267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def Net():\n    inp = Input(shape=[720,1280,3]) #Modulo para la entrada\n    \n    layers = [\n        Conv2D(8,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)), #Esto reduce a la mitad la imagen de salida\n        Conv2D(16,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)),\n        Conv2D(32,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)),\n        Conv2D(64,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)),\n        Conv2D(128,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)),\n        Conv2D(256,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)),\n        Conv2D(512,(4,4),padding=\"same\",activation=\"relu\"),\n        MaxPooling2D(pool_size=(2,2)),\n        Flatten(),\n        Dense(128,activation=\"relu\"),\n        Dense(64,activation=\"relu\"), #Para disminuir el cuello de botella\n        Dense(1,activation=\"sigmoid\")\n    ]\n    x = inp\n    for layer in layers:\n        x = layer(x)\n    return Model(inp,x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:42:02.144298Z","iopub.execute_input":"2024-12-08T03:42:02.144714Z","iopub.status.idle":"2024-12-08T03:42:02.153123Z","shell.execute_reply.started":"2024-12-08T03:42:02.144675Z","shell.execute_reply":"2024-12-08T03:42:02.152158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Net()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:42:03.685855Z","iopub.execute_input":"2024-12-08T03:42:03.686836Z","iopub.status.idle":"2024-12-08T03:42:03.782286Z","shell.execute_reply.started":"2024-12-08T03:42:03.686788Z","shell.execute_reply":"2024-12-08T03:42:03.781397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:42:03.784089Z","iopub.execute_input":"2024-12-08T03:42:03.784511Z","iopub.status.idle":"2024-12-08T03:42:03.817035Z","shell.execute_reply.started":"2024-12-08T03:42:03.784448Z","shell.execute_reply":"2024-12-08T03:42:03.816211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4,beta_1=0.5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:15:06.773592Z","iopub.execute_input":"2024-12-08T04:15:06.773942Z","iopub.status.idle":"2024-12-08T04:15:06.782095Z","shell.execute_reply.started":"2024-12-08T04:15:06.773913Z","shell.execute_reply":"2024-12-08T04:15:06.781198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ckpt_path = \"/kaggle/working/checkpoints/\"\nckpt_prefix = os.path.join(ckpt_path,\"ckpt\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:15:09.288046Z","iopub.execute_input":"2024-12-08T04:15:09.288866Z","iopub.status.idle":"2024-12-08T04:15:09.292797Z","shell.execute_reply.started":"2024-12-08T04:15:09.288832Z","shell.execute_reply":"2024-12-08T04:15:09.291915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"checkpoint = tf.train.Checkpoint(\n    model = model,\n    optimizer = optimizer\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:15:09.914730Z","iopub.execute_input":"2024-12-08T04:15:09.915056Z","iopub.status.idle":"2024-12-08T04:15:09.920631Z","shell.execute_reply.started":"2024-12-08T04:15:09.915028Z","shell.execute_reply":"2024-12-08T04:15:09.919666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"checkpoint.save(ckpt_prefix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:15:10.959072Z","iopub.execute_input":"2024-12-08T04:15:10.959726Z","iopub.status.idle":"2024-12-08T04:15:11.059560Z","shell.execute_reply.started":"2024-12-08T04:15:10.959689Z","shell.execute_reply":"2024-12-08T04:15:11.058242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss_object = tf.keras.losses.BinaryCrossentropy(from_logits=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:43:08.705420Z","iopub.execute_input":"2024-12-08T03:43:08.705831Z","iopub.status.idle":"2024-12-08T03:43:08.710177Z","shell.execute_reply.started":"2024-12-08T03:43:08.705798Z","shell.execute_reply":"2024-12-08T03:43:08.709203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"@tf.function()\ndef train_step(img,value):\n    with tf.GradientTape() as tape:\n        pred = model(img)\n        loss = loss_object(value,pred)\n        grads = tape.gradient(loss,model.trainable_variables)\n    optimizer.apply_gradients(zip(grads,model.trainable_variables))\n\n    return loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:43:10.922201Z","iopub.execute_input":"2024-12-08T03:43:10.922539Z","iopub.status.idle":"2024-12-08T03:43:10.927961Z","shell.execute_reply.started":"2024-12-08T03:43:10.922507Z","shell.execute_reply":"2024-12-08T03:43:10.926909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs = 1\nfor epoch in range(epochs):\n    print(\"Epoch {}/{}\".format(epoch+1,epochs))\n    lh = []\n    for img,c in dataset:\n        ls = train_step(img,c)\n        lh.append(ls)\n        print(\".\",end=\"\")\n    print()\n    print(\"Loss: {}\".format(sum(lh)/len(lh)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T03:51:34.583975Z","iopub.execute_input":"2024-12-08T03:51:34.584331Z","iopub.status.idle":"2024-12-08T03:51:44.337589Z","shell.execute_reply.started":"2024-12-08T03:51:34.584299Z","shell.execute_reply":"2024-12-08T03:51:44.336594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!touch submission.csv ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:12:25.454831Z","iopub.execute_input":"2024-12-08T04:12:25.455842Z","iopub.status.idle":"2024-12-08T04:12:26.532188Z","shell.execute_reply.started":"2024-12-08T04:12:25.455800Z","shell.execute_reply":"2024-12-08T04:12:26.531132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path_test = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test/\"\ntest_names = os.listdir(path_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:12:27.330476Z","iopub.execute_input":"2024-12-08T04:12:27.330895Z","iopub.status.idle":"2024-12-08T04:12:27.336181Z","shell.execute_reply.started":"2024-12-08T04:12:27.330858Z","shell.execute_reply":"2024-12-08T04:12:27.335251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"submission.csv\",\"w\") as file:\n    writer = csv.writer(file)\n    writer.writerow([\"image\",\"label\"])\n    for name in test_names:\n        img = open_image(path_test,name)\n        pred = model(tf.expand_dims(img,0))\n        pred_np = np.array(pred)\n        pred_np = np.squeeze(pred_np)\n        rounded_output = int(pred_np.round())\n        writer.writerow([name,rounded_output])\n\n    file.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T04:12:28.913372Z","iopub.execute_input":"2024-12-08T04:12:28.913748Z","iopub.status.idle":"2024-12-08T04:12:32.643706Z","shell.execute_reply.started":"2024-12-08T04:12:28.913714Z","shell.execute_reply":"2024-12-08T04:12:32.642940Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}