{"cells":[{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"import os\nimport cv2\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport numpy as np\nimport pandas as pd"},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[],"source":"get_user = os.environ.get('USER', 'KAGGLE')\n\nif get_user == 'KAGGLE':\n    my_env = 'KAGGLE'\nelif get_user == 'jupyter':\n    my_env = 'GCP'\nelif get_user == 'user':\n    my_env = 'LOCAL'\nelse:\n    my_env = None\n    \nassert my_env is not None    \n\nenv_input_fn = {\n    'KAGGLE': '../input/prostate-cancer-grade-assessment/',\n    'LOCAL':  '../data/',\n    'GCP':    '../../',\n}\n\ninput_fn = env_input_fn[my_env]"},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[],"source":"train_df = pd.read_csv(input_fn + 'train.csv')"},{"cell_type":"markdown","metadata":{},"source":"### Tiling Code"},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[],"source":"TRAIN = input_fn + 'train_images/'\nMASKS = input_fn + 'train_label_masks/'\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'\nsz = 128\nN = 16"},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[],"source":"def tile(img, mask):\n    result = []\n    shape = img.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], 'mask':mask[i], 'idx':i})\n    return result"},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[],"source":"x_tot,x2_tot = [],[]\nnames = [name[:-10] for name in os.listdir(MASKS)]\n\nimg_fns = os.listdir(TRAIN)\n\nimg_fn = img_fns[0]\nmask_fn = img_fn.split('.')[0] +'_mask.tiff'\n\nimg_fn, mask_fn"},{"cell_type":"code","execution_count":22,"metadata":{},"outputs":[],"source":"img = skimage.io.MultiImage(TRAIN + img_fn)\nmask = skimage.io.MultiImage(MASKS + mask_fn)"},{"cell_type":"markdown","metadata":{},"source":"##### The problem section:\n\n```python\n-> 2486             raise ValueError(\"cannot decompress %s\" % self.compression)\n   2487         if 'sample_format' in self.tags:\n   2488             tag = self.tags['sample_format']\n\nValueError: cannot decompress jpeg\n\n```"},{"cell_type":"code","execution_count":35,"metadata":{"collapsed":true},"outputs":[],"source":"img2 = img[-1]\nmask2 = mask[-1]"},{"cell_type":"code","execution_count":19,"metadata":{"collapsed":true},"outputs":[],"source":"ret = tile(img, mask)"},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":""}],"metadata":{"kernelspec":{"display_name":"Python [conda env:fastai2]","language":"python","name":"conda-env-fastai2-py"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.6"}},"nbformat":4,"nbformat_minor":2}