{"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":"code","source":"!pip install nb_black==1.0.5 --quiet\n\n%load_ext lab_black","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-02T08:17:30.064020Z","iopub.execute_input":"2023-11-02T08:17:30.064317Z","iopub.status.idle":"2023-11-02T08:17:46.682712Z","shell.execute_reply.started":"2023-11-02T08:17:30.064292Z","shell.execute_reply":"2023-11-02T08:17:46.681852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 0- Imports","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout\n\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\n\n# Allocate memory on the GPU only as needed\nconfig = tf.compat.v1.ConfigProto()\nconfig.gpu_options.allow_growth = True\nsess = tf.compat.v1.Session(config=config)\n\n# Constants\nSEED = 42\nAUTOTUNE = tf.data.AUTOTUNE\nBATCH_SIZE = 16","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-11-02T08:32:56.811915Z","iopub.execute_input":"2023-11-02T08:32:56.812469Z","iopub.status.idle":"2023-11-02T08:33:07.166943Z","shell.execute_reply.started":"2023-11-02T08:32:56.812438Z","shell.execute_reply":"2023-11-02T08:33:07.166091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1- Read The Data","metadata":{}},{"cell_type":"code","source":"tr_csv = pd.read_csv(\n    \"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/train.csv\"\n)\nts_csv = pd.read_csv(\n    \"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/test.csv\"\n)\n\ntr_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:07.168733Z","iopub.execute_input":"2023-11-02T08:33:07.169435Z","iopub.status.idle":"2023-11-02T08:33:07.227286Z","shell.execute_reply.started":"2023-11-02T08:33:07.169399Z","shell.execute_reply":"2023-11-02T08:33:07.226301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shuffle the data\ntr_csv = tr_csv.sample(frac=1).reset_index(drop=True)\n\n# split train data for validation\nxtr, xval, ytr, yval = train_test_split(\n    tr_csv.file.values,\n    tr_csv.glasses.values,\n    test_size=0.2,\n    stratify=tr_csv.glasses.values,\n    random_state=SEED,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:07.228318Z","iopub.execute_input":"2023-11-02T08:33:07.228581Z","iopub.status.idle":"2023-11-02T08:33:07.257888Z","shell.execute_reply.started":"2023-11-02T08:33:07.228557Z","shell.execute_reply":"2023-11-02T08:33:07.257140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess_image(path):\n    \"\"\"\n    Reads jpeg image as tensor with appropiate size\n    \"\"\"\n    img = tf.io.read_file(\n        \"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/\" + path\n    )\n    img = tf.io.decode_jpeg(img, channels=3)\n    return tf.image.resize(img, [512, 512])\n\n\ndef load_and_preprocess_from_path_label(path, label):\n    return load_and_preprocess_image(path), label\n\n\ntr_ds = tf.data.Dataset.from_tensor_slices((xtr, ytr))\nval_ds = tf.data.Dataset.from_tensor_slices((xval, yval))\nts_ds = tf.data.Dataset.from_tensor_slices(ts_csv.file.values)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:07.260529Z","iopub.execute_input":"2023-11-02T08:33:07.261173Z","iopub.status.idle":"2023-11-02T08:33:07.350682Z","shell.execute_reply.started":"2023-11-02T08:33:07.261133Z","shell.execute_reply":"2023-11-02T08:33:07.349909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create train, validation and test datasets\ntr = (\n    tr_ds.map(load_and_preprocess_from_path_label, num_parallel_calls=AUTOTUNE)\n    .batch(batch_size=BATCH_SIZE, num_parallel_calls=AUTOTUNE)\n    .prefetch(buffer_size=AUTOTUNE)\n)\nval = (\n    val_ds.map(load_and_preprocess_from_path_label, num_parallel_calls=AUTOTUNE)\n    .batch(batch_size=BATCH_SIZE, num_parallel_calls=AUTOTUNE)\n    .prefetch(buffer_size=AUTOTUNE)\n)\nts = (\n    ts_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)\n    .batch(batch_size=BATCH_SIZE, num_parallel_calls=AUTOTUNE)\n    .prefetch(buffer_size=AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:07.351701Z","iopub.execute_input":"2023-11-02T08:33:07.351968Z","iopub.status.idle":"2023-11-02T08:33:07.501204Z","shell.execute_reply.started":"2023-11-02T08:33:07.351944Z","shell.execute_reply":"2023-11-02T08:33:07.500246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2- Build Model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(tf.keras.layers.Rescaling(scale=1.0 / 255))\n\nbase_pretrainedmodel = tf.keras.applications.ResNet50(\n    include_top=False,\n    input_shape=(512, 512, 3),\n    pooling=\"avg\",\n    classes=2,\n    weights=\"imagenet\",\n)\n\n# for layer in base_pretrainedmodel.layers:\n#     layer.trainable = False\n\nmodel.add(base_pretrainedmodel)\nmodel.add(Flatten())\nmodel.add(Dense(512, activation=\"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation=\"sigmoid\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:07.502451Z","iopub.execute_input":"2023-11-02T08:33:07.502795Z","iopub.status.idle":"2023-11-02T08:33:14.302667Z","shell.execute_reply.started":"2023-11-02T08:33:07.502763Z","shell.execute_reply":"2023-11-02T08:33:14.301854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build([None, 512, 512, 3])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:14.303743Z","iopub.execute_input":"2023-11-02T08:33:14.303992Z","iopub.status.idle":"2023-11-02T08:33:15.042901Z","shell.execute_reply.started":"2023-11-02T08:33:14.303969Z","shell.execute_reply":"2023-11-02T08:33:15.041987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(), loss=\"binary_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:15.044283Z","iopub.execute_input":"2023-11-02T08:33:15.045052Z","iopub.status.idle":"2023-11-02T08:33:15.068662Z","shell.execute_reply.started":"2023-11-02T08:33:15.045012Z","shell.execute_reply":"2023-11-02T08:33:15.067915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3- Train Model","metadata":{}},{"cell_type":"code","source":"EarlyStopping_cb = tf.keras.callbacks.EarlyStopping(monitor=\"val_loss\", patience=3)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:15.069738Z","iopub.execute_input":"2023-11-02T08:33:15.070026Z","iopub.status.idle":"2023-11-02T08:33:15.075898Z","shell.execute_reply.started":"2023-11-02T08:33:15.069999Z","shell.execute_reply":"2023-11-02T08:33:15.074907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(tr, epochs=10, validation_data=val, callbacks = [EarlyStopping_cb])","metadata":{"execution":{"iopub.status.busy":"2023-11-02T08:33:15.078260Z","iopub.execute_input":"2023-11-02T08:33:15.079054Z","iopub.status.idle":"2023-11-02T08:34:32.568479Z","shell.execute_reply.started":"2023-11-02T08:33:15.079023Z","shell.execute_reply":"2023-11-02T08:34:32.566993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T05:08:23.779144Z","iopub.execute_input":"2023-10-31T05:08:23.779950Z","iopub.status.idle":"2023-10-31T05:08:23.826001Z","shell.execute_reply.started":"2023-10-31T05:08:23.779917Z","shell.execute_reply":"2023-10-31T05:08:23.824749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T05:08:25.484061Z","iopub.execute_input":"2023-10-31T05:08:25.484870Z","iopub.status.idle":"2023-10-31T05:08:25.533621Z","shell.execute_reply.started":"2023-10-31T05:08:25.484837Z","shell.execute_reply":"2023-10-31T05:08:25.532257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4- Predict and Submit","metadata":{}},{"cell_type":"code","source":"yhat = model.predict(ts)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"id\": ts_csv.id, \"glasses\": yhat.reshape(-1)})\n\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}