{"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 os\nos.listdir(r'/kaggle/input/robot-segmentation')","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:11.824000Z","iopub.execute_input":"2023-04-03T10:37:11.824974Z","iopub.status.idle":"2023-04-03T10:37:11.867309Z","shell.execute_reply.started":"2023-04-03T10:37:11.824929Z","shell.execute_reply":"2023-04-03T10:37:11.864642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import normalize\nimport os\nimport cv2\nfrom PIL import Image\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom sklearn.preprocessing import MinMaxScaler\nfrom keras.optimizers import Adam\nimport glob\n     ","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:11.873432Z","iopub.execute_input":"2023-04-03T10:37:11.874181Z","iopub.status.idle":"2023-04-03T10:37:20.090493Z","shell.execute_reply.started":"2023-04-03T10:37:11.874141Z","shell.execute_reply":"2023-04-03T10:37:20.089400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_names = glob.glob(\"/kaggle/input/robot-segmentation/RobotSegmentation/images/*.PNG\")\nimage_names.sort()\n     \n\nimage_names_subset = image_names\n     \n\nimages = [cv2.imread(img, 0) for img in image_names_subset]\n     \n\nimage_dataset = np.array(images)\nimage_dataset = np.expand_dims(image_dataset, axis = 3)\nmask_names = glob.glob(\"/kaggle/input/robot-segmentation/RobotSegmentation/SegmentationClass/*.png\")\nmask_names.sort()\nmask_names_subset = mask_names\nmasks = [cv2.imread(mask, 0) for mask in mask_names_subset]\nmask_dataset = np.array(masks)\nmask_dataset[mask_dataset > 0.3] = 255\nmask_dataset = np.expand_dims(mask_dataset, axis = 3)\n\nprint(\"Image data shape is: \", image_dataset.shape)\nprint(\"Mask data shape is: \", mask_dataset.shape)\nprint(\"Max pixel value in image is: \", image_dataset.max())\nprint(\"Labels in the mask are : \", np.unique(mask_dataset))","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:20.092860Z","iopub.execute_input":"2023-04-03T10:37:20.094040Z","iopub.status.idle":"2023-04-03T10:37:30.694926Z","shell.execute_reply.started":"2023-04-03T10:37:20.093997Z","shell.execute_reply":"2023-04-03T10:37:30.693639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Normalize images\nimage_dataset = image_dataset /255.  #Can also normalize or scale using MinMax scaler\n#Do not normalize masks, just rescale to 0 to 1.\nmask_dataset = mask_dataset /255.  #PIxel values will be 0 or 1\nfrom sklearn.model_selection import train_test_split\n# first split the data into train and test sets (80-20 split)\nX_train1, X_test, y_train1, y_test = train_test_split(image_dataset, mask_dataset, test_size=0.1, random_state=42)\n\n# then split the train set into train and validation sets (70-30 split)\nX_train, X_valid, y_train, y_valid = train_test_split(X_train1, y_train1, test_size=0.1, random_state=42)\n     \n\n\n     ","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:30.699536Z","iopub.execute_input":"2023-04-03T10:37:30.699839Z","iopub.status.idle":"2023-04-03T10:37:31.642371Z","shell.execute_reply.started":"2023-04-03T10:37:30.699809Z","shell.execute_reply":"2023-04-03T10:37:31.641258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:31.644112Z","iopub.execute_input":"2023-04-03T10:37:31.644486Z","iopub.status.idle":"2023-04-03T10:37:31.653474Z","shell.execute_reply.started":"2023-04-03T10:37:31.644445Z","shell.execute_reply":"2023-04-03T10:37:31.649275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sanity check, view few mages\nimport random\n\nimage_number = random.randint(0, len(X_train)-1)\nplt.figure(figsize=(12, 6))\nplt.subplot(121)\nplt.imshow(X_train[5,:,:,0], cmap='gray')\nplt.subplot(122)\nplt.imshow(y_train[5,:,:,0], cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:31.655539Z","iopub.execute_input":"2023-04-03T10:37:31.656027Z","iopub.status.idle":"2023-04-03T10:37:32.042168Z","shell.execute_reply.started":"2023-04-03T10:37:31.655997Z","shell.execute_reply":"2023-04-03T10:37:32.041089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building Unet by dividing encoder and decoder into blocks\n\nfrom keras.models import Model\nfrom keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda\nfrom keras.optimizers import Adam\nfrom keras.layers import Activation, MaxPool2D, Concatenate\n\n\ndef conv_block(input, num_filters):\n    x = Conv2D(num_filters, 3, padding=\"same\")(input)\n    x = BatchNormalization()(x)   #Not in the original network. \n    x = Activation(\"relu\")(x)\n\n    x = Conv2D(num_filters, 3, padding=\"same\")(x)\n    x = BatchNormalization()(x)  #Not in the original network\n    x = Activation(\"relu\")(x)\n\n    return x\n\n#Encoder block: Conv block followed by maxpooling\n\n\ndef encoder_block(input, num_filters):\n    x = conv_block(input, num_filters)\n    p = MaxPool2D((2, 2))(x)\n    return x, p   \n\n#Decoder block\n#skip features gets input from encoder for concatenation\n\ndef decoder_block(input, skip_features, num_filters):\n    x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding=\"same\")(input)\n    x = Concatenate()([x, skip_features])\n    x = conv_block(x, num_filters)\n    return x\n\n#Build Unet using the blocks\ndef build_unet(input_shape, n_classes):\n    inputs = Input(input_shape)\n\n    s1, p1 = encoder_block(inputs, 64)\n    s2, p2 = encoder_block(p1, 128)\n    s3, p3 = encoder_block(p2, 256)\n    s4, p4 = encoder_block(p3, 512)\n\n    b1 = conv_block(p4, 1024) #Bridge\n\n    d1 = decoder_block(b1, s4, 512)\n    d2 = decoder_block(d1, s3, 256)\n    d3 = decoder_block(d2, s2, 128)\n    d4 = decoder_block(d3, s1, 64)\n\n    if n_classes == 1:  #Binary\n      activation = 'sigmoid'\n    else:\n      activation = 'softmax'\n\n    outputs = Conv2D(n_classes, 1, padding=\"same\", activation=activation)(d4)  #Change the activation based on n_classes\n    print(activation)\n\n    model = Model(inputs, outputs, name=\"U-Net\")\n    return model\n     \n\nIMG_HEIGHT = image_dataset.shape[1]\nIMG_WIDTH  = image_dataset.shape[2]\nIMG_CHANNELS = image_dataset.shape[3]\n\ninput_shape = (IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS)\n     \n\nmodel = build_unet(input_shape, n_classes=1)\nmodel.compile(optimizer=Adam(learning_rate = 1e-3), loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:32.043937Z","iopub.execute_input":"2023-04-03T10:37:32.044411Z","iopub.status.idle":"2023-04-03T10:37:35.423102Z","shell.execute_reply.started":"2023-04-03T10:37:32.044369Z","shell.execute_reply":"2023-04-03T10:37:35.421940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, \n                    batch_size = 8, \n                    verbose=1, \n                    epochs=25, \n                    validation_data=(X_test, y_test), \n                    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:37:35.424650Z","iopub.execute_input":"2023-04-03T10:37:35.425054Z","iopub.status.idle":"2023-04-03T10:53:42.702658Z","shell.execute_reply.started":"2023-04-03T10:37:35.425013Z","shell.execute_reply":"2023-04-03T10:53:42.701632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = history.history\nprint(history_dict.keys())","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:53:42.708601Z","iopub.execute_input":"2023-04-03T10:53:42.708926Z","iopub.status.idle":"2023-04-03T10:53:42.714614Z","shell.execute_reply.started":"2023-04-03T10:53:42.708894Z","shell.execute_reply":"2023-04-03T10:53:42.713439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot the training and validation accuracy and loss at each epoch\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(loss) + 1)\nplt.plot(epochs, loss, 'y', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nplt.plot(epochs, acc, 'y', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T10:54:38.614599Z","iopub.execute_input":"2023-04-03T10:54:38.615601Z","iopub.status.idle":"2023-04-03T10:54:39.054807Z","shell.execute_reply.started":"2023-04-03T10:54:38.615560Z","shell.execute_reply":"2023-04-03T10:54:39.053813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)\ny_pred_thresholded = y_pred > 0.5","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.metrics import MeanIoU\n     \n\nn_classes = 2\nIOU_keras = MeanIoU(num_classes=n_classes)  \nIOU_keras.update_state(y_pred_thresholded, y_test)\nprint(\"Mean IoU =\", IOU_keras.result().numpy())\n     \n\n\n     ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nthreshold = 0.5\ntest_img_number = random.randint(0, len(X_test)-1)\ntest_img = X_test[test_img_number]\nground_truth=y_test[test_img_number]\ntest_img_input=np.expand_dims(test_img, 0)\nprint(test_img_input.shape)\nprediction = (model.predict(test_img_input)[0,:,:,0] > 0.5).astype(np.uint8)\nprint(prediction.shape)\n\nplt.figure(figsize=(16, 8))\nplt.subplot(231)\nplt.title('Testing Image')\nplt.imshow(test_img[:,:,0], cmap='gray')\nplt.subplot(232)\nplt.title('Testing Label')\nplt.imshow(ground_truth[:,:,0], cmap='gray')\nplt.subplot(233)\nplt.title('Prediction on test image')\nplt.imshow(prediction, cmap='gray')\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_names1 = glob.glob(\"/kaggle/input/test-image/*.PNG\")\nimage_names1.sort()\n     \n\nimage_names_subset1 = image_names1\nimages1 = []\n\nfor img in image_names_subset1:\n    j = cv2.imread(img, 0) \n    i= np.transpose( j, (1, 0))\n\n    images1.append(i)\nimage_dataset1 = np.array(images1)\nimage_dataset1 = np.expand_dims(image_dataset1, axis = 3)\nprint(\"Image data shape is: \", image_dataset1.shape)\nimage_dataset1 = image_dataset1 /255. ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nthreshold = 0.5\ntest_img_number1 = random.randint(0, len(image_dataset1)-1)\ntest_img = image_dataset1[test_img_number1]\n#ground_truth=y_test[test_img_number]\ntest_img_input=np.expand_dims(test_img, 0)\nprint(test_img_input.shape)\n\nprediction = (model.predict(test_img_input)[0,:,:,0] > 0.5).astype(np.uint8)\n\n\nplt.figure(figsize=(16, 8))\nplt.subplot(231)\nplt.title('Testing Image')\nplt.imshow(test_img[:,:,0], cmap='gray')\nplt.subplot(232)\nplt.title('Prediction on test image')\nplt.imshow(prediction, cmap='gray')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/BEST.hdf5')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}