{"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":"\n!pip install -Uqq fastbook\nimport fastbook\nfastbook.setup_book()\n \nfrom fastai.vision.all import *\nfrom fastbook import *\nfrom IPython.display import clear_output\n\nmatplotlib.rc('image', cmap='Greys')\nclear_output(wait=False)\n# https://github.com/mmichelli/CV_2_Fast_AI","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-21T13:42:34.565878Z","iopub.execute_input":"2021-06-21T13:42:34.566273Z","iopub.status.idle":"2021-06-21T13:42:40.855784Z","shell.execute_reply.started":"2021-06-21T13:42:34.566238Z","shell.execute_reply":"2021-06-21T13:42:40.854643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can share code here:\n#### https://github.com/mmichelli/CV_2_Fast_AI","metadata":{"execution":{"iopub.status.busy":"2021-06-19T18:06:05.818707Z","iopub.execute_input":"2021-06-19T18:06:05.819116Z","iopub.status.idle":"2021-06-19T18:06:05.825468Z","shell.execute_reply.started":"2021-06-19T18:06:05.819073Z","shell.execute_reply":"2021-06-19T18:06:05.82412Z"}}},{"cell_type":"code","source":"#Get Data\npath = Path('../input/understanding_cloud_organization')\nimages = Path(path/'train_images')\ntrain_CSV = path/'train.csv'","metadata":{"execution":{"iopub.status.busy":"2021-06-21T13:10:54.419484Z","iopub.execute_input":"2021-06-21T13:10:54.419785Z","iopub.status.idle":"2021-06-21T13:10:54.424749Z","shell.execute_reply.started":"2021-06-21T13:10:54.419755Z","shell.execute_reply":"2021-06-21T13:10:54.423682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load data","metadata":{"execution":{"iopub.status.busy":"2021-06-17T06:06:26.678599Z","iopub.execute_input":"2021-06-17T06:06:26.679092Z","iopub.status.idle":"2021-06-17T06:06:28.823709Z","shell.execute_reply.started":"2021-06-17T06:06:26.679052Z","shell.execute_reply":"2021-06-17T06:06:28.822507Z"}}},{"cell_type":"code","source":"#Prep DataFrame\ntrain  = pd.read_csv(train_CSV)\n#Transform the csv file to Image name and label\ntrain['Image_name'] = train['Image_Label'].apply(lambda x: x.split('_')[0])\ntrain['Label_name'] = train['Image_Label'].apply(lambda x: x.split('_')[1])\ntrain.drop('Image_Label',axis=1,inplace=True)\ntrain = train.pivot('Image_name','Label_name','EncodedPixels')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-21T13:15:54.235478Z","iopub.execute_input":"2021-06-21T13:15:54.236176Z","iopub.status.idle":"2021-06-21T13:15:56.204810Z","shell.execute_reply.started":"2021-06-21T13:15:54.236122Z","shell.execute_reply":"2021-06-21T13:15:56.203509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(images/'0011165.jpg') ","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:09:20.886931Z","iopub.execute_input":"2021-06-21T14:09:20.887457Z","iopub.status.idle":"2021-06-21T14:09:20.893981Z","shell.execute_reply.started":"2021-06-21T14:09:20.887422Z","shell.execute_reply":"2021-06-21T14:09:20.892860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rleToMask(rleString,height,width):\n  rows,cols = height,width\n  rleNumbers = [int(numstring) for numstring in rleString.split(' ')]\n  rlePairs = np.array(rleNumbers).reshape(-1,2)\n  img = np.zeros(rows*cols,dtype=np.uint8)\n  mask_one_dim = torch.zeros(height*width)\n  for start_pixel, num_pixels in rlePairs:\n    mask_one_dim[start_pixel-1: start_pixel-1 + num_pixels] = 1\n  img = mask_one_dim.reshape(rows,cols).T\n  return img\n  \n  \n","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:28:58.789509Z","iopub.execute_input":"2021-06-21T14:28:58.790209Z","iopub.status.idle":"2021-06-21T14:28:58.796301Z","shell.execute_reply.started":"2021-06-21T14:28:58.790141Z","shell.execute_reply":"2021-06-21T14:28:58.795539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.loc[train.index[55]]","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:45:30.479116Z","iopub.execute_input":"2021-06-21T14:45:30.479677Z","iopub.status.idle":"2021-06-21T14:45:30.485928Z","shell.execute_reply.started":"2021-06-21T14:45:30.479642Z","shell.execute_reply":"2021-06-21T14:45:30.485283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h,w = img.shape\nimg = Image.open(images/train.index[55]) \nt_image = tensor(img)\nmask = ","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:50:52.960945Z","iopub.execute_input":"2021-06-21T14:50:52.961336Z","iopub.status.idle":"2021-06-21T14:50:53.047695Z","shell.execute_reply.started":"2021-06-21T14:50:52.961291Z","shell.execute_reply":"2021-06-21T14:50:53.046819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_image = TensorImage(t_image)\n\n_, ax = plt.subplots(1, 1, figsize=(15, 10))\nt_image.show(ctx=ax)\nTensorMask(rleToMask(train.loc[train.index[55]]['Fish'], w, h )).show(  ctx=ax)\nTensorMask(rleToMask(train.loc[train.index[55]]['Sugar'], w, h )).show(  ctx=ax)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:53:14.669939Z","iopub.execute_input":"2021-06-21T14:53:14.670334Z","iopub.status.idle":"2021-06-21T14:53:15.575050Z","shell.execute_reply.started":"2021-06-21T14:53:14.670298Z","shell.execute_reply":"2021-06-21T14:53:15.573927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}