{"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":"**PLEASE UPVOTE: https://www.kaggle.com/code/lucasvw/0-11-simplest-possible-solution-submit-testmask**","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\nimport warnings\nwarnings.simplefilter('ignore')\n\ntest_path = Path('/kaggle/input/vesuvius-challenge-ink-detection/test')\nPath.BASE_PATH = test_path\n\nimages_test_a_random_order = get_files(test_path / 'a' / 'surface_volume', extensions='.tif')[0]\nprint(f'\\nTYPE(IMAGES_TEST_A_RANDOM_ORDER): {type(images_test_a_random_order)}')\nprint(f'IMAGES_TEST_A_RANDOM_ORDER: {images_test_a_random_order}')\n\nimages_test_b_random_order = get_files(test_path / 'b' / 'surface_volume', extensions='.tif')[0]\nprint(f'\\nTYPE(IMAGES_TEST_B_RANDOM_ORDER): {type(images_test_b_random_order)}')\nprint(f'IMAGES_TEST_B_RANDOM_ORDER: {images_test_b_random_order}')","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:18.172768Z","iopub.execute_input":"2023-05-21T14:50:18.173203Z","iopub.status.idle":"2023-05-21T14:50:24.781600Z","shell.execute_reply.started":"2023-05-21T14:50:18.173170Z","shell.execute_reply":"2023-05-21T14:50:24.779093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A\n\nimg_a_05 = Image.open(images_test_a_random_order)\nimg_a_05_np_arr = np.array(img_a_05)\ntransform_norm = A.Normalize(mean = [0], std = [1])\ntransformed_norm = transform_norm(image=img_a_05_np_arr)\nimg_a_05_norm = transformed_norm['image']\nplt.subplots(2, 1, figsize=(16, 8))\nplt.subplot(2, 1, 1).set_title('Original Image')\nplt.imshow(img_a_05, cmap='gray')\nplt.subplot(2, 1, 2).set_title('Normalized Image')\nplt.imshow(img_a_05_norm, cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:24.784124Z","iopub.execute_input":"2023-05-21T14:50:24.784651Z","iopub.status.idle":"2023-05-21T14:50:29.928604Z","shell.execute_reply.started":"2023-05-21T14:50:24.784588Z","shell.execute_reply":"2023-05-21T14:50:29.927475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'IMG_A_05_NORM.DTYPE: {img_a_05_norm.dtype}')\nprint(f'IMG_A_05_NORM.MAX(): {img_a_05_norm.max()}')\nprint(f'IMG_A_05_NORM.MIN(): {img_a_05_norm.min()}')","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:29.931121Z","iopub.execute_input":"2023-05-21T14:50:29.931888Z","iopub.status.idle":"2023-05-21T14:50:29.957299Z","shell.execute_reply.started":"2023-05-21T14:50:29.931851Z","shell.execute_reply":"2023-05-21T14:50:29.955827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_b_05 = Image.open(images_test_b_random_order)\nimg_b_05_np_arr = np.array(img_b_05)\ntransform_norm = A.Normalize(mean = [0], std = [1])\ntransformed_norm = transform_norm(image=img_b_05_np_arr)\nimg_b_05_norm = transformed_norm['image']\nplt.subplots(1, 2, figsize=(10, 5))\nplt.subplot(1, 2, 1).set_title('Original Image')\nplt.imshow(img_b_05, cmap='gray')\nplt.subplot(1, 2, 2).set_title('Normalized Image')\nplt.imshow(img_b_05_norm, cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:29.961248Z","iopub.execute_input":"2023-05-21T14:50:29.961894Z","iopub.status.idle":"2023-05-21T14:50:34.912087Z","shell.execute_reply.started":"2023-05-21T14:50:29.961854Z","shell.execute_reply":"2023-05-21T14:50:34.910726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'IMG_B_05_NORM.DTYPE: {img_b_05_norm.dtype}')\nprint(f'IMG_B_05_NORM.MAX(): {img_b_05_norm.max()}')\nprint(f'IMG_B_05_NORM.MIN(): {img_b_05_norm.min()}')","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:34.913809Z","iopub.execute_input":"2023-05-21T14:50:34.915020Z","iopub.status.idle":"2023-05-21T14:50:34.951122Z","shell.execute_reply.started":"2023-05-21T14:50:34.914981Z","shell.execute_reply":"2023-05-21T14:50:34.949734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_test_a_b = []\nimages_test_a_b.append(images_test_a_random_order)\nimages_test_a_b.append(images_test_b_random_order)\nprint(f'TYPE(IMAGES_TEST_A_B): {type(images_test_a_b)}')\nprint(f'LEN(IMAGES_TEST_A_B): {len(images_test_a_b)}')\nprint(f'IMAGES_TEST_A_B: {images_test_a_b}')","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:34.952425Z","iopub.execute_input":"2023-05-21T14:50:34.952829Z","iopub.status.idle":"2023-05-21T14:50:34.961091Z","shell.execute_reply.started":"2023-05-21T14:50:34.952796Z","shell.execute_reply":"2023-05-21T14:50:34.959608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_test_a_b[0].parent.parent.name","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:34.962771Z","iopub.execute_input":"2023-05-21T14:50:34.963247Z","iopub.status.idle":"2023-05-21T14:50:34.980302Z","shell.execute_reply.started":"2023-05-21T14:50:34.963213Z","shell.execute_reply":"2023-05-21T14:50:34.978883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_test_a_b[1].parent.parent.name","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:34.981811Z","iopub.execute_input":"2023-05-21T14:50:34.982247Z","iopub.status.idle":"2023-05-21T14:50:34.995311Z","shell.execute_reply.started":"2023-05-21T14:50:34.982218Z","shell.execute_reply":"2023-05-21T14:50:34.994024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_length_encoding(np_array_image):\n    \n    print(f'NP_ARRAY_IMAGE:\\n{np_array_image}')\n    print(f'TYPE(NP_ARRAY_IMAGE): {type(np_array_image)}')\n    print(f'NP.DTYPE(NP_ARRAY_IMAGE.DTYPE): {np_array_image.dtype}')\n    \n    print(f'NP_ARRAY_IMAGE.SHAPE: {np_array_image.shape}\\n')\n    print(f'NP_ARRAY_IMAGE PIXELS: {np_array_image.shape[0] * np_array_image.shape[1]}')\n    \n    unique, counts = np.unique(np_array_image, return_counts=True)\n    print('unique, counts = np.unique(np_array_image, return_counts=True)')\n    print(f'UNIQUE:{unique}')\n    print(f'TYPE(UNIQUE):{type(unique)}')\n    print(f'LEN(UNIQUE):{len(unique)}')\n    print(f'COUNTS:{counts}')\n    print(f'TYPE(COUNTS):{type(counts)}')\n    print(f'LEN(COUNTS):{len(counts)}\\n')\n    \n    flatten_image = np.where(np_array_image.flatten() > 0., 1., 0.).astype(np.uint8)\n    print('flatten_image = np.where(np_array_image.flatten() > 0, 1, 0).astype(np.uint8)')\n    print(f'FLATTEN_IMAGE: {flatten_image}')\n    unique_f, counts_f = np.unique(flatten_image, return_counts=True)\n    print(f'UNIQUE_F:{unique_f}')\n    print(f'COUNTS_F:{counts_f}\\n')\n    \n    start_border_black_array_indices = np.array((flatten_image[:-1] == 0) & (flatten_image[1:] == 1))\n    print('start_border_black_array_indices = np.array((flatten_image[:-1] == 0) & (flatten_image[1:] == 1))')\n    print(f'START_BORDER_BLACK_ARRAY_INDICES: {start_border_black_array_indices}')\n    unique_s, counts_s = np.unique(start_border_black_array_indices, return_counts=True)\n    print(f'UNIQUE_S:{unique_s}')\n    print(f'COUNTS_S:{counts_s}')\n    print(f'LEN(START_BORDER_WHITE_ARRAY_INDICES): {len(start_border_black_array_indices)}\\n')\n    \n    \n    end_border_white_array_indices = np.array((flatten_image[:-1] == 1) & (flatten_image[1:] == 0))\n    print('end_border_white_array_indices = np.array((flatten_image[:-1] == 1) & (flatten_image[1:] == 0))')\n    print(f'END_BORDER_WHITE_ARRAY_INDICES: {end_border_white_array_indices}')\n    unique_e, counts_e = np.unique(end_border_white_array_indices, return_counts=True)\n    print(f'UNIQUE_E:{unique_e}')\n    print(f'COUNTS_E:{counts_e}\\n')\n    print(f'LEN(END_BORDER_WHITE_ARRAY_INDICES): {len(end_border_white_array_indices)}\\n')\n    \n    start_border_white_image_indices = np.where(start_border_black_array_indices)[0] + 2\n    print('start_border_white_image_indices = np.where(start_border_black_array_indices)[0] + 2')\n    print(f'START_BORDER_WHITE_IMAGE_INDICES: {start_border_white_image_indices}')\n    unique_si, counts_si = np.unique(start_border_white_image_indices, return_counts=True)\n    print('unique_si, counts_si = np.unique(start_border_white_image_indices, return_counts=True)')\n    print(f'UNIQUE_SI:{unique_si}')\n    print(f'LEN(UNIQUE_SI):{len(unique_si)}')\n    print(f'COUNTS_SI:{counts_si}')\n    print(f'LEN(COUNTS_SI):{len(counts_si)}\\n')\n    \n    end_border_black_image_indices = np.where(end_border_white_array_indices)[0] + 2\n    print('end_border_black_image_indices = np.where(end_border_white_array_indices)[0] + 2')\n    print(f'END_BORDER_BLACK_IMAGE_INDICES: {end_border_black_image_indices}')\n    unique_ei, counts_ei = np.unique(end_border_black_image_indices, return_counts=True)\n    print('unique_ei, counts_ei = np.unique(end_border_black_image_indices, return_counts=True)')\n    print(f'UNIQUE_EI:{unique_ei}')\n    print(f'LEN(UNIQUE_EI):{len(unique_ei)}')\n    print(f'COUNTS_EI:{counts_ei}')\n    print(f'LEN(COUNTS_EI):{len(counts_ei)}\\n')\n    \n    white_pixels_lengths = end_border_black_image_indices - start_border_white_image_indices\n    print('white_pixels_lengths = end_border_black_image_indices - start_border_white_image_indices')\n    \n    print(f'WHITE_PIXELS_LENGTHS: {white_pixels_lengths}')\n    \n    unique_l, counts_l = np.unique(white_pixels_lengths, return_counts=True)\n    print(f'UNIQUE_L:{unique_l}')\n    print(f'COUNTS_L:{counts_l}')\n    print(f'LEN(WHITE_PIXELS_LENGTHS): {len(white_pixels_lengths)}\\n')\n    \n    rle = ' '.join(map(str, sum(zip(start_border_white_image_indices, white_pixels_lengths), ())))\n    print('rle = ' '.join(map(str, sum(zip(start_border_white_image_indices, white_pixels_lengths), ())))')\n    print(f'RLE:\\n{rle}\\n')\n    print('=' * 60)\n    \n    return rle","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:34.997284Z","iopub.execute_input":"2023-05-21T14:50:34.997685Z","iopub.status.idle":"2023-05-21T14:50:35.019149Z","shell.execute_reply.started":"2023-05-21T14:50:34.997644Z","shell.execute_reply":"2023-05-21T14:50:35.017961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = defaultdict(list)\n\nfor i, image_test_a_b in enumerate(images_test_a_b):\n    \n    img_a_b = Image.open(image_test_a_b)\n    img_a_b_np_arr = np.array(img_a_b)\n    transform_norm = A.Normalize(mean = [0], std = [1])\n    transformed_norm = transform_norm(image=img_a_b_np_arr)\n    img_a_b_norm = transformed_norm['image']\n\n    submission['Id'].append(image_test_a_b.parent.parent.name)\n    submission['Predicted'].append(run_length_encoding(np.array(img_a_b_norm)))","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:35.023411Z","iopub.execute_input":"2023-05-21T14:50:35.023808Z","iopub.status.idle":"2023-05-21T14:50:43.312529Z","shell.execute_reply.started":"2023-05-21T14:50:35.023774Z","shell.execute_reply":"2023-05-21T14:50:43.311221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame.from_dict(submission)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:43.313908Z","iopub.execute_input":"2023-05-21T14:50:43.314261Z","iopub.status.idle":"2023-05-21T14:50:43.355252Z","shell.execute_reply.started":"2023-05-21T14:50:43.314232Z","shell.execute_reply":"2023-05-21T14:50:43.354064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:50:43.357126Z","iopub.execute_input":"2023-05-21T14:50:43.357489Z","iopub.status.idle":"2023-05-21T14:50:43.381077Z","shell.execute_reply.started":"2023-05-21T14:50:43.357458Z","shell.execute_reply":"2023-05-21T14:50:43.379625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}