{"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":"markdown","source":"# Tried the CRF post processing ","metadata":{"id":"BPM3qb0hZ5JF","_kg_hide-input":false,"execution":{"iopub.status.busy":"2023-06-07T15:20:05.512751Z","iopub.execute_input":"2023-06-07T15:20:05.513663Z","iopub.status.idle":"2023-06-07T15:21:15.729705Z","shell.execute_reply.started":"2023-06-07T15:20:05.513621Z","shell.execute_reply":"2023-06-07T15:21:15.728517Z"}}},{"cell_type":"markdown","source":"**Umm facing little issues would be glad if some one helps out**","metadata":{}},{"cell_type":"code","source":"!pip install cython\n!pip install git+https://github.com/lucasb-eyer/pydensecrf.git","metadata":{"execution":{"iopub.status.busy":"2023-06-07T15:33:40.322285Z","iopub.execute_input":"2023-06-07T15:33:40.322684Z","iopub.status.idle":"2023-06-07T15:34:19.637886Z","shell.execute_reply.started":"2023-06-07T15:33:40.322655Z","shell.execute_reply":"2023-06-07T15:34:19.636507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pydensecrf.densecrf as dcrf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-07T15:21:18.619827Z","iopub.execute_input":"2023-06-07T15:21:18.620372Z","iopub.status.idle":"2023-06-07T15:21:18.637923Z","shell.execute_reply.started":"2023-06-07T15:21:18.620321Z","shell.execute_reply":"2023-06-07T15:21:18.636471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pydensecrf.utils import unary_from_labels, create_pairwise_bilateral, create_pairwise_gaussian","metadata":{"id":"flisaLaGZ5JI","execution":{"iopub.status.busy":"2023-06-07T15:21:21.187684Z","iopub.execute_input":"2023-06-07T15:21:21.188505Z","iopub.status.idle":"2023-06-07T15:21:21.194182Z","shell.execute_reply.started":"2023-06-07T15:21:21.188468Z","shell.execute_reply":"2023-06-07T15:21:21.193001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"id":"YL2pgaSiZ5JJ","execution":{"iopub.status.busy":"2023-06-07T15:21:21.801207Z","iopub.execute_input":"2023-06-07T15:21:21.801589Z","iopub.status.idle":"2023-06-07T15:21:21.974175Z","shell.execute_reply.started":"2023-06-07T15:21:21.801558Z","shell.execute_reply":"2023-06-07T15:21:21.973014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.color import gray2rgb\nfrom skimage.color import rgb2gray\nimport matplotlib.pyplot as plt\n# from osgeo import gdal\n%matplotlib inline","metadata":{"id":"e86-5mibZ5JJ","execution":{"iopub.status.busy":"2023-06-07T15:29:17.536333Z","iopub.execute_input":"2023-06-07T15:29:17.536786Z","iopub.status.idle":"2023-06-07T15:29:17.545321Z","shell.execute_reply.started":"2023-06-07T15:29:17.536750Z","shell.execute_reply":"2023-06-07T15:29:17.543999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3> Get DataSet</h3> ","metadata":{"id":"0zmM_lFWZ5JJ"}},{"cell_type":"code","source":"X=cv2.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png\")\ny=cv2.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\")\n# out=cv2.imread(\"C:\\Aashish\\Assignments\\Machine Learning\\CRF - DataSet\\20_actual.png\")","metadata":{"id":"HVcuvh1wZ5JL","execution":{"iopub.status.busy":"2023-06-07T15:37:44.662979Z","iopub.execute_input":"2023-06-07T15:37:44.664501Z","iopub.status.idle":"2023-06-07T15:37:46.017854Z","shell.execute_reply.started":"2023-06-07T15:37:44.664427Z","shell.execute_reply":"2023-06-07T15:37:46.016513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3> Perform CRF</h3>","metadata":{"id":"3lObVX6fZ5JL"}},{"cell_type":"code","source":"\"\"\"\nFunction which returns the labelled image after applying CRF\n\n\"\"\"\n\n#Original_image = Image which has to labelled\n#Annotated image = Which has been labelled by some technique( FCN in this case)\n#Output_image = Name of the final output image after applying CRF\n#Use_2d = boolean variable \n#if use_2d = True specialised 2D fucntions will be applied\n#else Generic functions will be applied\n\ndef crf(original_image, annotated_image,output_image, use_2d = True):\n    \n    # Converting annotated image to RGB if it is Gray scale\n    if(len(annotated_image.shape)<3):\n        annotated_image = gray2rgb(annotated_image).astype(np.uint32)\n    \n    cv2.imwrite(\"testing2.png\",annotated_image)\n    annotated_image = annotated_image.astype(np.uint32)\n    #Converting the annotations RGB color to single 32 bit integer\n    annotated_label = annotated_image[:,:,0].astype(np.uint32) + (annotated_image[:,:,1]<<8).astype(np.uint32) + (annotated_image[:,:,2]<<16).astype(np.uint32)\n    \n    # Convert the 32bit integer color to 0,1, 2, ... labels.\n    colors, labels = np.unique(annotated_label, return_inverse=True)\n    \n    #Creating a mapping back to 32 bit colors\n    colorize = np.empty((len(colors), 3), np.uint8)\n    colorize[:,0] = (colors & 0x0000FF)\n    colorize[:,1] = (colors & 0x00FF00) >> 8\n    colorize[:,2] = (colors & 0xFF0000) >> 16\n    \n    #Gives no of class labels in the annotated image\n    n_labels = len(set(labels.flat)) \n    \n    print(\"No of labels in the Image are \")\n    print(n_labels)\n    \n    \n    #Setting up the CRF model\n    if use_2d :\n        d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], n_labels)\n\n        # get unary potentials (neg log probability)\n        U = unary_from_labels(labels, n_labels, gt_prob=0.90, zero_unsure=False)\n        d.setUnaryEnergy(U)\n\n        # This adds the color-independent term, features are the locations only.\n        d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,\n                          normalization=dcrf.NORMALIZE_SYMMETRIC)\n\n        # This adds the color-dependent term, i.e. features are (x,y,r,g,b).\n        d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image,\n                           compat=10,\n                           kernel=dcrf.DIAG_KERNEL,\n                           normalization=dcrf.NORMALIZE_SYMMETRIC)\n        \n    #Run Inference for 5 steps \n    Q = d.inference(5)\n\n    # Find out the most probable class for each pixel.\n    MAP = np.argmax(Q, axis=0)\n\n    # Convert the MAP (labels) back to the corresponding colors and save the image.\n    # Note that there is no \"unknown\" here anymore, no matter what we had at first.\n    MAP = colorize[MAP,:]\n    cv2.imwrite(output_image,MAP.reshape(original_image.shape))\n    return MAP.reshape(original_image.shape)","metadata":{"id":"Up8s077sZ5JM","execution":{"iopub.status.busy":"2023-06-07T15:37:47.688355Z","iopub.execute_input":"2023-06-07T15:37:47.689180Z","iopub.status.idle":"2023-06-07T15:37:47.704988Z","shell.execute_reply.started":"2023-06-07T15:37:47.689123Z","shell.execute_reply":"2023-06-07T15:37:47.703526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crfimage = crf(X,y,\"out.png\")","metadata":{"id":"ZQc6REv5Z5JN","outputId":"3e80ee55-800a-4239-a7ad-2f146530c6ff","execution":{"iopub.status.busy":"2023-06-07T15:37:48.052535Z","iopub.execute_input":"2023-06-07T15:37:48.052933Z","iopub.status.idle":"2023-06-07T15:39:40.808732Z","shell.execute_reply.started":"2023-06-07T15:37:48.052900Z","shell.execute_reply":"2023-06-07T15:39:40.807640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**If u replace the above y with the output mask then u will be able to get the post processing look**","metadata":{}},{"cell_type":"code","source":"plt.subplot(1,2,1)\nplt.imshow(y)\nplt.subplot(1,2,2)\nplt.imshow(crfimage)","metadata":{"id":"ULXxdraCZ5JO","outputId":"3b12fd2f-62cc-4c0d-9128-c6aa8a216926","execution":{"iopub.status.busy":"2023-06-07T15:39:40.810653Z","iopub.execute_input":"2023-06-07T15:39:40.811164Z","iopub.status.idle":"2023-06-07T15:39:54.997815Z","shell.execute_reply.started":"2023-06-07T15:39:40.811123Z","shell.execute_reply":"2023-06-07T15:39:54.996717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"ZslbuIPCZ5JO","collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}