{"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\nimport cv2\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport numpy as np\n#from matplotlib.pyplot import imshow\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-15T14:19:51.107826Z","iopub.execute_input":"2021-09-15T14:19:51.108137Z","iopub.status.idle":"2021-09-15T14:19:51.815054Z","shell.execute_reply.started":"2021-09-15T14:19:51.108042Z","shell.execute_reply":"2021-09-15T14:19:51.814046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN = '../input/prostate-cancer-grade-assessment/train_images/'\nMASKS = '../input/prostate-cancer-grade-assessment/train_label_masks/'\nOUT_ZIP = 'out_1.zip'\nOUT_MASKS = 'masks.zip'\nsz = 250   #900#400 #sz menor = mayor tamaño tile\nN = 49","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:19:51.81984Z","iopub.execute_input":"2021-09-15T14:19:51.820078Z","iopub.status.idle":"2021-09-15T14:19:51.828954Z","shell.execute_reply.started":"2021-09-15T14:19:51.820034Z","shell.execute_reply":"2021-09-15T14:19:51.827646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tile(img):#tile(img, mask):\n    result = []\n    shape = img.shape\n    #print(shape)\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                constant_values=255)\n#    mask = np.pad(mask,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n#                constant_values=0)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n #   mask = mask.reshape(mask.shape[0]//sz,sz,mask.shape[1]//sz,sz,3)\n  #  mask = mask.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    if len(img) < N:\n       # mask = np.pad(mask,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=0)\n        img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n    img = img[idxs]\n    #mask = mask[idxs]\n    for i in range(len(img)):\n        result.append({'img':img[i]})#result.append({'img':img[i], 'mask':mask[i], 'idx':i})\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:19:51.830935Z","iopub.execute_input":"2021-09-15T14:19:51.831521Z","iopub.status.idle":"2021-09-15T14:19:51.846884Z","shell.execute_reply.started":"2021-09-15T14:19:51.831465Z","shell.execute_reply":"2021-09-15T14:19:51.845769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def concat_tile(im_list_2d):\n    return cv2.vconcat([cv2.hconcat(im_list_h) for im_list_h in im_list_2d])","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:19:51.85086Z","iopub.execute_input":"2021-09-15T14:19:51.851851Z","iopub.status.idle":"2021-09-15T14:19:51.859017Z","shell.execute_reply.started":"2021-09-15T14:19:51.851803Z","shell.execute_reply":"2021-09-15T14:19:51.857624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tiles_num(tiles):\n    tile_num = []\n    tiles_total = 48\n    \n    tile_1 = cv2.resize(tiles[0], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_2 = cv2.resize(tiles[1], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_3 = cv2.resize(tiles[2], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_4 = cv2.resize(tiles[3], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_5 = cv2.resize(tiles[4], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_6 = cv2.resize(tiles[5], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_7 = cv2.resize(tiles[6], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_8 = cv2.resize(tiles[7], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_9 = cv2.resize(tiles[8], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_10 = cv2.resize(tiles[9], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_11 = cv2.resize(tiles[10], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_12 = cv2.resize(tiles[11], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_13 = cv2.resize(tiles[12], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_14 = cv2.resize(tiles[13], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_15 = cv2.resize(tiles[14], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_16 = cv2.resize(tiles[15], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_17 = cv2.resize(tiles[16], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_18 = cv2.resize(tiles[17], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_19 = cv2.resize(tiles[18], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_20 = cv2.resize(tiles[19], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_21 = cv2.resize(tiles[20], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_22 = cv2.resize(tiles[21], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_23 = cv2.resize(tiles[22], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_24 = cv2.resize(tiles[23], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_25 = cv2.resize(tiles[24], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_26 = cv2.resize(tiles[25], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_27 = cv2.resize(tiles[26], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_28 = cv2.resize(tiles[27], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_29 = cv2.resize(tiles[28], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_30 = cv2.resize(tiles[29], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_31 = cv2.resize(tiles[30], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_32 = cv2.resize(tiles[31], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_33 = cv2.resize(tiles[32], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_34 = cv2.resize(tiles[33], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_35 = cv2.resize(tiles[34], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_36 = cv2.resize(tiles[35], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_37 = cv2.resize(tiles[36], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_38 = cv2.resize(tiles[37], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_39 = cv2.resize(tiles[38], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_40 = cv2.resize(tiles[39], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_41 = cv2.resize(tiles[40], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_42 = cv2.resize(tiles[41], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_43 = cv2.resize(tiles[42], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_44 = cv2.resize(tiles[43], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_45 = cv2.resize(tiles[44], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_46 = cv2.resize(tiles[45], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_47 = cv2.resize(tiles[46], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_48 = cv2.resize(tiles[47], dsize=(0, 0), fx=0.5, fy=0.5)\n    tile_49 = cv2.resize(tiles[48], dsize=(0, 0), fx=0.5, fy=0.5)\n    \n    im_tiles = concat_tile([[tile_1, tile_2, tile_3, tile_4, tile_5, tile_6, tile_7],\n                       [tile_8, tile_9, tile_10, tile_11, tile_12, tile_13, tile_14],\n                       [tile_15, tile_16, tile_17, tile_18, tile_19, tile_20, tile_21],\n                       [tile_22, tile_23, tile_24, tile_25, tile_26, tile_27, tile_28],\n                       [tile_29, tile_30, tile_31, tile_32, tile_33, tile_34, tile_35],\n                       [tile_36, tile_37, tile_38, tile_39, tile_40, tile_41, tile_42],\n                       [tile_43, tile_44, tile_45, tile_46, tile_47, tile_48, tile_49]])\n    \n    return im_tiles ","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:19:51.861478Z","iopub.execute_input":"2021-09-15T14:19:51.86186Z","iopub.status.idle":"2021-09-15T14:19:51.902692Z","shell.execute_reply.started":"2021-09-15T14:19:51.861818Z","shell.execute_reply":"2021-09-15T14:19:51.901471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain=pd.read_csv('/kaggle/input/prostate-cancer-grade-assessment/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:22:49.91485Z","iopub.execute_input":"2021-09-15T14:22:49.915542Z","iopub.status.idle":"2021-09-15T14:22:49.961629Z","shell.execute_reply.started":"2021-09-15T14:22:49.915507Z","shell.execute_reply":"2021-09-15T14:22:49.960836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_id = [name for name in train['image_id']]","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:19:51.971864Z","iopub.execute_input":"2021-09-15T14:19:51.972152Z","iopub.status.idle":"2021-09-15T14:19:51.983424Z","shell.execute_reply.started":"2021-09-15T14:19:51.972126Z","shell.execute_reply":"2021-09-15T14:19:51.981838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(OUT_ZIP, 'w') as img_out:\n    #count=1\n    for name in tqdm(im_id[10250:]):\n        #print(count)\n        img = skimage.io.MultiImage(os.path.join(TRAIN,name+'.tiff'))[-1]\n        img = cv2.resize(img, dsize=(15000, 15000)) #menor dsize = menor numero representativas en tiles\n        tiles = tile(img)\n        img_concat = tiles_num(tiles)\n\n        img = cv2.imencode('.jpg',cv2.cvtColor(img_concat, cv2.COLOR_RGB2BGR))[1]\n        img_out.writestr(f'{name}.jpg', img)\n        #count=count+1","metadata":{"execution":{"iopub.status.busy":"2021-09-15T14:19:51.986034Z","iopub.execute_input":"2021-09-15T14:19:51.986376Z","iopub.status.idle":"2021-09-15T14:20:15.341019Z","shell.execute_reply.started":"2021-09-15T14:19:51.986335Z","shell.execute_reply":"2021-09-15T14:20:15.339343Z"},"trusted":true},"execution_count":null,"outputs":[]}]}