{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-27T10:59:11.469896Z","iopub.execute_input":"2023-05-27T10:59:11.471041Z","iopub.status.idle":"2023-05-27T10:59:11.576244Z","shell.execute_reply.started":"2023-05-27T10:59:11.470999Z","shell.execute_reply":"2023-05-27T10:59:11.575332Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport cv2\nimport numpy as np\nfrom tensorflow.keras import layers, Sequential, utils, preprocessing\nimport imageio","metadata":{"execution":{"iopub.status.busy":"2023-05-27T10:59:26.514862Z","iopub.execute_input":"2023-05-27T10:59:26.515225Z","iopub.status.idle":"2023-05-27T10:59:35.769260Z","shell.execute_reply.started":"2023-05-27T10:59:26.515197Z","shell.execute_reply":"2023-05-27T10:59:35.768284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/labels')\nos.mkdir('/kaggle/working/gifs')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gifs(input):\n    for k in range(3):\n        gif = []\n        tiff_dir = f'/kaggle/input/vesuvius-challenge-ink-detection/train/{k+1}/surface_volume/'\n        print(f'Getting Images from {k+1}',end = \" \")\n        for i in sorted(os.listdir(tiff_dir)):\n            img = cv2.imread(f'{tiff_dir}{i}')\n            img = cv2.resize(img, (input,input))\n            gif.append(img)\n        print(len(gif))\n        img_label = cv2.imread(f'/kaggle/input/vesuvius-challenge-ink-detection/train/{k+1}/inklabels.png')\n        img_label = cv2.resize(img_label, (input,input))\n        cv2.imwrite(f'/kaggle/working/labels/inklabels_{k+1}.png', img_label)\n        print('Making the GIF')\n        with imageio.get_writer(f'/kaggle/working/gifs/{k+1}.gif',mode='I') as writer:\n            for frame in gif:\n                writer.append_data(frame)\n        print('GIF made')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:12:29.575740Z","iopub.execute_input":"2023-05-27T11:12:29.576141Z","iopub.status.idle":"2023-05-27T11:12:29.585665Z","shell.execute_reply.started":"2023-05-27T11:12:29.576109Z","shell.execute_reply":"2023-05-27T11:12:29.584495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inp = 256\ngifs(inp)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:12:38.046942Z","iopub.execute_input":"2023-05-27T11:12:38.047521Z","iopub.status.idle":"2023-05-27T11:19:46.972781Z","shell.execute_reply.started":"2023-05-27T11:12:38.047489Z","shell.execute_reply":"2023-05-27T11:19:46.971719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = [[],[],[]]\ny = []\nfor i in range(3):\n    gif = cv2.VideoCapture(f'/kaggle/working/gifs/{i+1}.gif')\n    while True:\n        ret, frame = gif.read()\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n        if not ret:\n            break\n        x[i].append(frame)\n        if(len(x[i])==64):\n            break\n\nx = np.array(x)\n\nfor i in range(3):\n    label = cv2.imread(f'/kaggle/working/labels/inklabels_{i+1}.png')\n    label = cv2.cvtColor(label, cv2.COLOR_BGR2GRAY)\n    label = label/255\n    y.append(label)\n\ny = np.array(y, dtype = bool)\ny.shape, y.dtype, y.max(),y.min(), x.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:21:34.817427Z","iopub.execute_input":"2023-05-27T11:21:34.817775Z","iopub.status.idle":"2023-05-27T11:21:35.050006Z","shell.execute_reply.started":"2023-05-27T11:21:34.817748Z","shell.execute_reply":"2023-05-27T11:21:35.048886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ndef create_unet_model(input_shape, num_classes):\n    inputs = tf.keras.Input(shape=input_shape)\n    conv1 = tf.keras.layers.Conv3D(64, 3, activation='relu', padding='same')(inputs)\n    conv1 = tf.keras.layers.Conv3D(64, 3, activation='relu', padding='same')(conv1)\n    pool1 = tf.keras.layers.MaxPooling3D(pool_size=(2, 2, 1))(conv1)\n\n    conv2 = tf.keras.layers.Conv3D(128, 3, activation='relu', padding='same')(pool1)\n    conv2 = tf.keras.layers.Conv3D(128, 3, activation='relu', padding='same')(conv2)\n    pool2 = tf.keras.layers.MaxPooling3D(pool_size=(2, 2, 1))(conv2)\n\n    up3 = tf.keras.layers.Conv3DTranspose(64, 2, strides=(2, 2, 1), padding='same')(conv2)\n    concat3 = tf.keras.layers.concatenate([up3, conv1], axis=-1)  # Concatenate along the channel dimension\n    conv3 = tf.keras.layers.Conv3D(64, 3, activation='relu', padding='same')(concat3)\n    conv3 = tf.keras.layers.Conv3D(64, 3, activation='relu', padding='same')(conv3)\n\n    reshape3 = tf.keras.layers.Reshape((conv3.shape[2], conv3.shape[2], conv3.shape[1] * conv3.shape[4]))(conv3)\n\n    conv_projection = tf.keras.layers.Conv2D(64, 1, activation='relu')(reshape3)\n\n    outputs = tf.keras.layers.Conv2D(num_classes, 1, activation='sigmoid')(conv_projection)\n\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:22:13.476039Z","iopub.execute_input":"2023-05-27T11:22:13.476899Z","iopub.status.idle":"2023-05-27T11:22:13.489432Z","shell.execute_reply.started":"2023-05-27T11:22:13.476859Z","shell.execute_reply":"2023-05-27T11:22:13.488492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_unet_model((64,inp,inp,1), num_classes=1)\nmodel.compile(\n    loss = 'binary_crossentropy',\n    optimizer = 'adam',\n    metrics=['accuracy']\n)\ntf.keras.utils.plot_model(model, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:22:26.453497Z","iopub.execute_input":"2023-05-27T11:22:26.453853Z","iopub.status.idle":"2023-05-27T11:22:31.988290Z","shell.execute_reply.started":"2023-05-27T11:22:26.453824Z","shell.execute_reply":"2023-05-27T11:22:31.987341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x,y, batch_size=1, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:22:48.421727Z","iopub.execute_input":"2023-05-27T11:22:48.422138Z"},"trusted":true},"execution_count":null,"outputs":[]}]}