{"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":"import keras\nimport random\nimport os\n\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\n\nstrategy = tf.distribute.MirroredStrategy()\nprint('DEVICES AVAILABLE: {}'.format(strategy.num_replicas_in_sync))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-05T15:02:32.908018Z","iopub.execute_input":"2023-11-05T15:02:32.908377Z","iopub.status.idle":"2023-11-05T15:02:32.918629Z","shell.execute_reply.started":"2023-11-05T15:02:32.908348Z","shell.execute_reply":"2023-11-05T15:02:32.917712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.273022Z","iopub.execute_input":"2023-11-05T15:02:33.273402Z","iopub.status.idle":"2023-11-05T15:02:33.287602Z","shell.execute_reply.started":"2023-11-05T15:02:33.273372Z","shell.execute_reply":"2023-11-05T15:02:33.286683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\n\ntmp = df.groupby(['label'])['image_id'].nunique().plot(kind='pie', autopct=\"%0.2f\")\nplt.gca().get_yaxis().set_visible(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.289267Z","iopub.execute_input":"2023-11-05T15:02:33.28955Z","iopub.status.idle":"2023-11-05T15:02:33.428183Z","shell.execute_reply.started":"2023-11-05T15:02:33.289527Z","shell.execute_reply":"2023-11-05T15:02:33.426689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\n\ntmp = df.groupby(['is_tma'])['image_id'].nunique().plot(kind='pie', autopct=\"%0.2f\")\nplt.gca().get_yaxis().set_visible(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.430211Z","iopub.execute_input":"2023-11-05T15:02:33.430768Z","iopub.status.idle":"2023-11-05T15:02:33.583269Z","shell.execute_reply.started":"2023-11-05T15:02:33.430724Z","shell.execute_reply":"2023-11-05T15:02:33.581811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASS = {j: i for i, j in enumerate(list(set(df.label.to_numpy())))}\nCLASS","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.585438Z","iopub.execute_input":"2023-11-05T15:02:33.586313Z","iopub.status.idle":"2023-11-05T15:02:33.597293Z","shell.execute_reply.started":"2023-11-05T15:02:33.586264Z","shell.execute_reply":"2023-11-05T15:02:33.595821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.601266Z","iopub.execute_input":"2023-11-05T15:02:33.602321Z","iopub.status.idle":"2023-11-05T15:02:33.614791Z","shell.execute_reply.started":"2023-11-05T15:02:33.602272Z","shell.execute_reply":"2023-11-05T15:02:33.613227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def filters(x):\n    arr = [0 for i in range(len(CLASS))]\n    \n    arr[CLASS[x]] = 1 \n    \n    return arr\n    \ndf.label = df.label.apply(filters)\ndf.is_tma = df.is_tma.apply(lambda x: 1 if x else 0)\ndf.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.616858Z","iopub.execute_input":"2023-11-05T15:02:33.617569Z","iopub.status.idle":"2023-11-05T15:02:33.65249Z","shell.execute_reply.started":"2023-11-05T15:02:33.617524Z","shell.execute_reply":"2023-11-05T15:02:33.651034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name= []\n\nfor dirname, _, filenames in os.walk('/kaggle/input/UBC-OCEAN/train_thumbnails'):\n    for filename in filenames:\n        image = os.path.join(dirname, filename)\n        \n        index, index_2 = image.index('s/')+2, image.index('_thumbnail.png')\n        \n        image_name.append((image, int(image[index:index_2])))\n\nprint('GOAL')","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.654389Z","iopub.execute_input":"2023-11-05T15:02:33.655425Z","iopub.status.idle":"2023-11-05T15:02:33.669219Z","shell.execute_reply.started":"2023-11-05T15:02:33.655375Z","shell.execute_reply":"2023-11-05T15:02:33.668342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name[:10]","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.675147Z","iopub.execute_input":"2023-11-05T15:02:33.675437Z","iopub.status.idle":"2023-11-05T15:02:33.681439Z","shell.execute_reply.started":"2023-11-05T15:02:33.675413Z","shell.execute_reply":"2023-11-05T15:02:33.680475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name = sorted(image_name, key=lambda x: x[1])\nimage_name[:10]","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.683995Z","iopub.execute_input":"2023-11-05T15:02:33.684437Z","iopub.status.idle":"2023-11-05T15:02:33.692507Z","shell.execute_reply.started":"2023-11-05T15:02:33.684405Z","shell.execute_reply":"2023-11-05T15:02:33.691565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name = list(map(lambda x: x[0], image_name))\nimage_name[:10]","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.693643Z","iopub.execute_input":"2023-11-05T15:02:33.693984Z","iopub.status.idle":"2023-11-05T15:02:33.704029Z","shell.execute_reply.started":"2023-11-05T15:02:33.693953Z","shell.execute_reply":"2023-11-05T15:02:33.703309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"srez = min(len(image_name), len(df.label.to_numpy()))\nsrez","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.705364Z","iopub.execute_input":"2023-11-05T15:02:33.705633Z","iopub.status.idle":"2023-11-05T15:02:33.714205Z","shell.execute_reply.started":"2023-11-05T15:02:33.70561Z","shell.execute_reply":"2023-11-05T15:02:33.713308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_label  = df.drop(['image_id', 'image_width', 'image_height', 'is_tma'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.7153Z","iopub.execute_input":"2023-11-05T15:02:33.71556Z","iopub.status.idle":"2023-11-05T15:02:33.723855Z","shell.execute_reply.started":"2023-11-05T15:02:33.715537Z","shell.execute_reply":"2023-11-05T15:02:33.72287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_label.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.728959Z","iopub.execute_input":"2023-11-05T15:02:33.729686Z","iopub.status.idle":"2023-11-05T15:02:33.741905Z","shell.execute_reply.started":"2023-11-05T15:02:33.72965Z","shell.execute_reply":"2023-11-05T15:02:33.74107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(image_name[:srez], \n                                                  [i for i in df_label.label.to_numpy()][:srez], \n                                                  test_size=.2)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.744108Z","iopub.execute_input":"2023-11-05T15:02:33.744448Z","iopub.status.idle":"2023-11-05T15:02:33.751345Z","shell.execute_reply.started":"2023-11-05T15:02:33.744424Z","shell.execute_reply":"2023-11-05T15:02:33.75047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train), len(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.752406Z","iopub.execute_input":"2023-11-05T15:02:33.752745Z","iopub.status.idle":"2023-11-05T15:02:33.766331Z","shell.execute_reply.started":"2023-11-05T15:02:33.752718Z","shell.execute_reply":"2023-11-05T15:02:33.765333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_train = tf.data.Dataset.from_tensor_slices((X_train, y_train))\n\ndataset_val = tf.data.Dataset.from_tensor_slices((X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.767499Z","iopub.execute_input":"2023-11-05T15:02:33.767764Z","iopub.status.idle":"2023-11-05T15:02:33.789064Z","shell.execute_reply.started":"2023-11-05T15:02:33.767741Z","shell.execute_reply":"2023-11-05T15:02:33.788318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(dataset_train))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:02:33.791453Z","iopub.execute_input":"2023-11-05T15:02:33.791724Z","iopub.status.idle":"2023-11-05T15:02:33.802877Z","shell.execute_reply.started":"2023-11-05T15:02:33.7917Z","shell.execute_reply":"2023-11-05T15:02:33.80198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BUFFER_SIZE = 10000\nBATCH_SIZE = 16\nIMG_WIDTH = 224\nIMG_HEIGHT = 224\n\ndef norm(img):\n    img = tf.cast(img, tf.float32)\n    \n    mean, std = tf.math.reduce_mean(img), tf.math.reduce_std(img)\n\n    img = tf.image.per_image_standardization(img)\n    \n    img = (img*mean) / std\n\n    return img\n\ndef resize_image(img):\n    img = tf.image.resize(\n        img, (IMG_WIDTH, IMG_HEIGHT), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR\n    )\n\n    return img\n\ndef load(image_file):\n    image = tf.io.read_file(image_file)\n    image = tf.io.decode_jpeg(image)\n\n    input_image = tf.cast(image, tf.float32)\n\n    return input_image\n\ndef preprocess_image_train(image, y):\n    image = resize_image(load(image))\n    \n    image = norm(image)\n\n    return image, y\n\n\ndataset_norm = lambda x: x.map(preprocess_image_train, num_parallel_calls=tf.data.AUTOTUNE)\\\n    .shuffle(BUFFER_SIZE).batch(BATCH_SIZE)\n\ntrain_dataset = dataset_norm(dataset_train)\nval_dataset = dataset_norm(dataset_val)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:12:30.836766Z","iopub.execute_input":"2023-11-05T15:12:30.837793Z","iopub.status.idle":"2023-11-05T15:12:31.102256Z","shell.execute_reply.started":"2023-11-05T15:12:30.837749Z","shell.execute_reply":"2023-11-05T15:12:31.101358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model = keras.Sequential([\n        keras.Input((IMG_WIDTH, IMG_HEIGHT, 3)),\n        hub.KerasLayer('/kaggle/input/vision-transformer/tensorflow2/vit-b32-classification/1', trainable=True),\n        keras.layers.Dropout(.3),\n        keras.layers.Dense(256, activation='relu'),\n        keras.layers.Dropout(.3),\n        keras.layers.Dense(len(CLASS), activation='sigmoid')\n    ])\n\noptim = tf.keras.optimizers.experimental.Adam(3e-4)\n\nmodel.compile(optimizer=optim, \n              loss=keras.losses.BinaryCrossentropy(), \n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:14:28.700774Z","iopub.execute_input":"2023-11-05T15:14:28.701368Z","iopub.status.idle":"2023-11-05T15:14:33.033637Z","shell.execute_reply.started":"2023-11-05T15:14:28.701336Z","shell.execute_reply":"2023-11-05T15:14:33.032628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset, epochs=10, validation_data=val_dataset)\n\nplt.plot(history.history['loss'])\nplt.plot(history.history[f\"val_loss\"])\n\nplt.title(\"loss\")\n\nplt.ylabel(\"loss\")\nplt.xlabel(\"epoch\")\n\nplt.legend([\"train\", \"validation\"], loc=\"upper left\")\nplt.show()\n\nmodel.save_weights(\"ucb_ocean_checkpoint.weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:14:37.159799Z","iopub.execute_input":"2023-11-05T15:14:37.160227Z","iopub.status.idle":"2023-11-05T15:22:03.567836Z","shell.execute_reply.started":"2023-11-05T15:14:37.160192Z","shell.execute_reply":"2023-11-05T15:22:03.566757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = norm(resize_image(load('/kaggle/input/UBC-OCEAN/test_thumbnails/41_thumbnail.png')))\n\ntest = model(np.array([img.numpy()]))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:09.035571Z","iopub.execute_input":"2023-11-05T15:22:09.036591Z","iopub.status.idle":"2023-11-05T15:22:13.039333Z","shell.execute_reply.started":"2023-11-05T15:22:09.036553Z","shell.execute_reply":"2023-11-05T15:22:13.038226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outpute = list(CLASS.keys())[test.numpy().argmax()]\n\nplt.imshow(img)\nplt.title(outpute)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:13.235157Z","iopub.execute_input":"2023-11-05T15:22:13.235562Z","iopub.status.idle":"2023-11-05T15:22:13.554071Z","shell.execute_reply.started":"2023-11-05T15:22:13.23553Z","shell.execute_reply":"2023-11-05T15:22:13.553182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/UBC-OCEAN/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:16.49125Z","iopub.execute_input":"2023-11-05T15:22:16.491608Z","iopub.status.idle":"2023-11-05T15:22:16.498928Z","shell.execute_reply.started":"2023-11-05T15:22:16.49158Z","shell.execute_reply":"2023-11-05T15:22:16.498085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:18.099496Z","iopub.execute_input":"2023-11-05T15:22:18.099871Z","iopub.status.idle":"2023-11-05T15:22:18.108775Z","shell.execute_reply.started":"2023-11-05T15:22:18.09984Z","shell.execute_reply":"2023-11-05T15:22:18.107642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.loc[0, 'label'] = outpute","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:19.59643Z","iopub.execute_input":"2023-11-05T15:22:19.59732Z","iopub.status.idle":"2023-11-05T15:22:19.601524Z","shell.execute_reply.started":"2023-11-05T15:22:19.597287Z","shell.execute_reply":"2023-11-05T15:22:19.600547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=None)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:20.642986Z","iopub.execute_input":"2023-11-05T15:22:20.643751Z","iopub.status.idle":"2023-11-05T15:22:20.649525Z","shell.execute_reply.started":"2023-11-05T15:22:20.643714Z","shell.execute_reply":"2023-11-05T15:22:20.648552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-05T15:22:21.80447Z","iopub.execute_input":"2023-11-05T15:22:21.805151Z","iopub.status.idle":"2023-11-05T15:22:21.815748Z","shell.execute_reply.started":"2023-11-05T15:22:21.805118Z","shell.execute_reply":"2023-11-05T15:22:21.814649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}