{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-09-24T08:07:06.307101Z","iopub.execute_input":"2024-09-24T08:07:06.307439Z","iopub.status.idle":"2024-09-24T08:07:08.176396Z","shell.execute_reply.started":"2024-09-24T08:07:06.307401Z","shell.execute_reply":"2024-09-24T08:07:08.175187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n\nimport cv2 as cv\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:08:25.369348Z","iopub.execute_input":"2024-09-24T08:08:25.369903Z","iopub.status.idle":"2024-09-24T08:08:45.37597Z","shell.execute_reply.started":"2024-09-24T08:08:25.369856Z","shell.execute_reply":"2024-09-24T08:08:45.375197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unzip_file(zip_path, extract_to):\n    # Ensure the extract_to directory exists\n    if not os.path.exists(extract_to):\n        os.makedirs(extract_to)\n    \n    # Open the zip file in read mode\n    with zipfile.ZipFile(zip_path, 'r') as zip_ref:\n        # Extract all the contents to the specified directory\n        zip_ref.extractall(extract_to)\n\n# /kaggle/input/data-science-bowl-2018/stage1_test.zip\n# /kaggle/input/data-science-bowl-2018/stage1_train.zip\n# /kaggle/input/data-science-bowl-2018/stage1_sample_submission.csv.zip\nzip_path = '/kaggle/input/data-science-bowl-2018/stage1_sample_submission.csv.zip'  # Specify zip file path \nextract_to = './stage1_sample_submission.csv'  # Specify the path for extract the files\n\nunzip_file(zip_path, extract_to)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:13:45.942661Z","iopub.execute_input":"2024-09-24T08:13:45.943491Z","iopub.status.idle":"2024-09-24T08:13:45.951124Z","shell.execute_reply.started":"2024-09-24T08:13:45.943448Z","shell.execute_reply":"2024-09-24T08:13:45.9503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids=next(os.walk('/kaggle/working/stage1_test'))[1]\ntrain_ids=next(os.walk('/kaggle/working/stage1_train'))[1]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:29.463503Z","iopub.execute_input":"2024-09-24T08:14:29.464263Z","iopub.status.idle":"2024-09-24T08:14:29.469769Z","shell.execute_reply.started":"2024-09-24T08:14:29.464222Z","shell.execute_reply":"2024-09-24T08:14:29.468902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids[:3]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:35.264977Z","iopub.execute_input":"2024-09-24T08:14:35.265703Z","iopub.status.idle":"2024-09-24T08:14:35.271787Z","shell.execute_reply.started":"2024-09-24T08:14:35.265662Z","shell.execute_reply":"2024-09-24T08:14:35.270722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids[:3]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:35.764519Z","iopub.execute_input":"2024-09-24T08:14:35.764907Z","iopub.status.idle":"2024-09-24T08:14:35.771352Z","shell.execute_reply.started":"2024-09-24T08:14:35.76487Z","shell.execute_reply":"2024-09-24T08:14:35.770108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH=128\nIMG_HEIGHT=128\nIMG_CHANNELS=3","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:37.980358Z","iopub.execute_input":"2024-09-24T08:14:37.981231Z","iopub.status.idle":"2024-09-24T08:14:37.985161Z","shell.execute_reply.started":"2024-09-24T08:14:37.98119Z","shell.execute_reply":"2024-09-24T08:14:37.984273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=np.zeros((len(train_ids),IMG_HEIGHT,IMG_WIDTH,IMG_CHANNELS),dtype=np.uint8)\ny_train=np.zeros((len(train_ids),IMG_HEIGHT,IMG_WIDTH,1),dtype=np.bool_)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:38.698531Z","iopub.execute_input":"2024-09-24T08:14:38.699289Z","iopub.status.idle":"2024-09-24T08:14:38.704389Z","shell.execute_reply.started":"2024-09-24T08:14:38.699247Z","shell.execute_reply":"2024-09-24T08:14:38.70343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:41.714762Z","iopub.execute_input":"2024-09-24T08:14:41.715507Z","iopub.status.idle":"2024-09-24T08:14:41.721377Z","shell.execute_reply.started":"2024-09-24T08:14:41.715467Z","shell.execute_reply":"2024-09-24T08:14:41.720537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# resizing traing image and mask \nnum=0\nfor i in train_ids:\n    path='/kaggle/working/stage1_train/'+i+'/images/'+i+'.png'\n#     print(path)\n    img=cv.cvtColor(cv.imread(path),cv.COLOR_BGR2RGB)\n    img=cv.resize(img,(IMG_HEIGHT,IMG_WIDTH))\n    x_train[num]=img\n    mask_paths = next(os.walk(f'/kaggle/working/stage1_train/{i}/masks/'))[2]\n\n    # Load the first mask in grayscale\n    temp_img0 = cv.imread(f'/kaggle/working/stage1_train/{i}/masks/{mask_paths[0]}', cv.IMREAD_GRAYSCALE)\n\n    # Loop over the rest of the masks and add them to the initial mask\n    for mask_path in mask_paths[1:]:\n        # Load each mask in grayscale\n        temp_img1 = cv.imread(f'/kaggle/working/stage1_train/{i}/masks/{mask_path}', cv.IMREAD_GRAYSCALE)\n\n        # Ensure the dimensions of both images are the same\n        if temp_img0.shape == temp_img1.shape:\n            # Add the current mask to the combined mask\n            temp_img0 = cv.add(temp_img0, temp_img1)\n\n    # Optionally, threshold the final mask for binary segmentation\n    _, temp_img0 = cv.threshold(temp_img0, 127, 255, cv.THRESH_BINARY)\n    y_train[num]=np.reshape(cv.resize(temp_img0,(IMG_HEIGHT,IMG_WIDTH)),(128,128,1))\n    num+=1","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:14:48.755239Z","iopub.execute_input":"2024-09-24T08:14:48.755592Z","iopub.status.idle":"2024-09-24T08:15:09.430658Z","shell.execute_reply.started":"2024-09-24T08:14:48.755559Z","shell.execute_reply":"2024-09-24T08:15:09.429855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j,i in zip(x_train[:5],y_train[:5]):\n    plt.subplot(1,2,1)\n    plt.imshow(i,cmap='gray')\n    \n    plt.subplot(1,2,2)\n    plt.imshow(j)\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:15:19.379624Z","iopub.execute_input":"2024-09-24T08:15:19.380469Z","iopub.status.idle":"2024-09-24T08:15:21.136635Z","shell.execute_reply.started":"2024-09-24T08:15:19.380431Z","shell.execute_reply":"2024-09-24T08:15:21.135697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs =tf.keras.layers.Input((IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS))\n\nscale=tf.keras.layers.Lambda(lambda x: x/255)(inputs)\nc1= tf.keras.layers.Conv2D(16,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(scale) # kernel_initializer it's define kernel values or wights of kernel\nc1= tf.keras.layers.Dropout(0.1)(c1)\nc1=tf.keras.layers.Conv2D(16,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c1)\np1=tf.keras.layers.MaxPooling2D((2,2))(c1)\n\n\nc2= tf.keras.layers.Conv2D(32,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(p1) # kernel_initializer it's define kernel values or wights of kernel\nc2= tf.keras.layers.Dropout(0.1)(c2)\nc2=tf.keras.layers.Conv2D(32,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c2)\np2=tf.keras.layers.MaxPooling2D((2,2))(c2)\n\nc3= tf.keras.layers.Conv2D(64,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(p2) # kernel_initializer it's define kernel values or wights of kernel\nc3= tf.keras.layers.Dropout(0.2)(c3)\nc3=tf.keras.layers.Conv2D(64,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c3)\np3=tf.keras.layers.MaxPooling2D((2,2))(c3)\n\nc4= tf.keras.layers.Conv2D(128,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(p3) # kernel_initializer it's define kernel values or wights of kernel\nc4= tf.keras.layers.Dropout(0.2)(c4)\nc4=tf.keras.layers.Conv2D(128,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c4)\np4=tf.keras.layers.MaxPooling2D((2,2))(c4)\n\nc5= tf.keras.layers.Conv2D(256,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(p4) # kernel_initializer it's define kernel values or wights of kernel\nc5= tf.keras.layers.Dropout(0.3)(c5)\nc5=tf.keras.layers.Conv2D(256,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c5)\n\nu6=tf.keras.layers.Conv2DTranspose(128,(2,2),strides=(2,2),padding='same')(c5)\nu6=tf.keras.layers.concatenate([u6,c4])\nc6=tf.keras.layers.Conv2D(128,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(u6)\nc6=tf.keras.layers.Dropout(0.2)(c6)\nc6=tf.keras.layers.Conv2D(128,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c6)\n\nu7=tf.keras.layers.Conv2DTranspose(64,(2,2),strides=(2,2),padding='same')(c6)\nu7=tf.keras.layers.concatenate([u7,c3])\nc7=tf.keras.layers.Conv2D(64,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(u7)\nc7=tf.keras.layers.Dropout(0.2)(c7)\nc7=tf.keras.layers.Conv2D(64,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c7)\n\nu8=tf.keras.layers.Conv2DTranspose(32,(2,2),strides=(2,2),padding='same')(c7)\nu8=tf.keras.layers.concatenate([u8,c2])\nc8=tf.keras.layers.Conv2D(32,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(u8)\nc8=tf.keras.layers.Dropout(0.1)(c8)\nc8=tf.keras.layers.Conv2D(32,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c8)\n\nu9=tf.keras.layers.Conv2DTranspose(16,(2,2),strides=(2,2),padding='same')(c8)\nu9=tf.keras.layers.concatenate([u9,c1])\nc9=tf.keras.layers.Conv2D(16,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(u9)\nc9=tf.keras.layers.Dropout(0.1)(c9)\nc9=tf.keras.layers.Conv2D(16,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c9)\n\noutputs=tf.keras.layers.Conv2D(1,(1,1),activation='sigmoid')(c9)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:15:41.063135Z","iopub.execute_input":"2024-09-24T08:15:41.064052Z","iopub.status.idle":"2024-09-24T08:15:42.505038Z","shell.execute_reply.started":"2024-09-24T08:15:41.064006Z","shell.execute_reply":"2024-09-24T08:15:42.503886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=tf.keras.Model(inputs=[inputs],outputs=[outputs])\nmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:15:42.948332Z","iopub.execute_input":"2024-09-24T08:15:42.948763Z","iopub.status.idle":"2024-09-24T08:15:43.052803Z","shell.execute_reply.started":"2024-09-24T08:15:42.94872Z","shell.execute_reply":"2024-09-24T08:15:43.051888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks=[\n    tf.keras.callbacks.EarlyStopping(patience=2,monitor='val_loss'),\n    tf.keras.callbacks.TensorBoard(log_dir='logs'),\n    tf.keras.callbacks.ModelCheckpoint('model_.keras',verbose=1,save_best_only=True)\n]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:15:47.010297Z","iopub.execute_input":"2024-09-24T08:15:47.011049Z","iopub.status.idle":"2024-09-24T08:15:47.016017Z","shell.execute_reply.started":"2024-09-24T08:15:47.011007Z","shell.execute_reply":"2024-09-24T08:15:47.015032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=model.fit(x_train,y_train,validation_split=0.1,batch_size=16,epochs=25,callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:16:33.779604Z","iopub.execute_input":"2024-09-24T08:16:33.780114Z","iopub.status.idle":"2024-09-24T08:17:44.568346Z","shell.execute_reply.started":"2024-09-24T08:16:33.780073Z","shell.execute_reply":"2024-09-24T08:17:44.567382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=x_train[np.random.randint(0,len(train_ids),1)[0]]\nplt.imshow(test)\nplt.show()\nprediction=model.predict(np.reshape(test,(1,128,128,3)))\nplt.imshow(prediction.reshape(128,128,1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-24T08:45:47.815648Z","iopub.execute_input":"2024-09-24T08:45:47.81645Z","iopub.status.idle":"2024-09-24T08:45:48.36075Z","shell.execute_reply.started":"2024-09-24T08:45:47.816402Z","shell.execute_reply":"2024-09-24T08:45:48.359797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}