{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install  -qq ../input/staintools/spams-2.6-cp37-cp37m-linux_x86_64.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install  -qq ../input/staintools/staintools-2.1.0-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport PIL\nfrom IPython.display import Image, display\nfrom tqdm.notebook import tqdm\nimport cv2\nfrom skimage import io\nimport spams\nimport staintools\nfrom staintools.reinhard_color_normalizer import ReinhardColorNormalizer\nfrom PIL import Image\nimport os\nimport shutil\nprint(os.listdir('/kaggle/input/prostate-cancer-grade-assessment'))\nimport zipfile\n\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Location of the training images\ndata_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'\nmask_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_label_masks/'\n\n# Location of training labels\npds = pd.read_csv('/kaggle/input/prostate-cancer-grade-assessment/train.csv')\nprint(pds.head())\n\nsample = pd.read_csv('../input/prostate-cancer-grade-assessment/sample_submission.csv')\nOUT_TRAIN = 'train.zip'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 256\nSEQ_LEN = 36\nSLIDE = 1\nScaling = 1\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define some Utils","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_patches_train(img_path, num_patches, img_size):\n    p_size = img_size\n    img = io.MultiImage(img_path)[SLIDE] / 255\n    pad0, pad1 = (p_size - img.shape[0] % p_size) % p_size, (p_size - img.shape[1] % p_size) % p_size\n    img = np.pad(\n        img,\n        [\n            [pad0 // 2, pad0 - pad0 // 2], \n            [pad1 // 2, pad1 - pad1 // 2], \n            [0, 0]\n        ],\n        constant_values=1\n    )\n    img = img.reshape(img.shape[0] // p_size, p_size, img.shape[1] // p_size, p_size, 3)\n    img = img.transpose(0, 2, 1, 3, 4).reshape(-1, p_size, p_size, 3)\n    if len(img) < num_patches:\n        img = np.pad(\n            img, \n            [\n                [0, num_patches - len(img)],\n                [0, 0],\n                [0, 0],\n                [0, 0]\n            ],\n            constant_values=1\n        )\n    idxs = np.argsort(img.reshape(img.shape[0], -1).sum(-1))[:num_patches]\n    return np.array(img[idxs])\n\n\ndef glue_to_one_trian(imgs_seq, img_size, num_patches, normalizer=False ):\n    side = int(np.sqrt(num_patches))\n    img_glue = np.zeros((img_size * side, img_size * side, 3), dtype=np.float32)\n    for i, ptch in enumerate(imgs_seq):\n        x = i // side\n        y = i % side\n        img_glue[x * img_size : (x + 1) * img_size, \n                 y * img_size : (y + 1) * img_size, :] = ptch\n    \n    if normalizer:\n        to_transform = staintools.LuminosityStandardizer.standardize((img_glue*255).astype(\"uint8\"))\n        img_norm = normalizer.transform(to_transform) \n    else:\n        img_norm = None\n    return img_norm, img_glue","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Stain Tools","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Read data\n#target = staintools.read_image('../input/staintools/target.png')\nimg_path = data_dir + '0005f7aaab2800f6170c399693a96917.tiff'\nimg_patches = get_patches_train(img_path, SEQ_LEN, IMG_SIZE)\n_,target= glue_to_one_trian(img_patches, IMG_SIZE, SEQ_LEN)\ntarget = staintools.LuminosityStandardizer.standardize((target*255).astype(\"uint8\"))\n\n# Stain normalize\n#normalizer = staintools.StainNormalizer(method='vahadane')\nnormalizer = ReinhardColorNormalizer()\nnormalizer.fit(target)\nimg_path = data_dir + '78fa6eadfc403f3440ef91db24d387b6.tiff'\nimg_patches = get_patches_train(img_path, SEQ_LEN, IMG_SIZE)\nimg_norm, img_glue = glue_to_one_trian(img_patches, IMG_SIZE, SEQ_LEN , normalizer)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#fig, axs = plt.subplots(1,3, figsize=(20, 10))\n#axs[0].imshow(target)\n#axs[1].imshow(img_norm)\n#axs[2].imshow(img_glue)\n#plt.show","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#plt.figure(figsize=(40, 20))\n#plt.imshow(img_norm)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\npds = pd.read_csv('/kaggle/input/prostate-cancer-grade-assessment/train.csv')\ndata_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'\nsave_dir1 = \"/kaggle/train_images/\"\nif os.path.exists(save_dir1):\n    shutil.rmtree(save_dir1)\nos.makedirs(save_dir1, exist_ok=True)\nwith zipfile.ZipFile(OUT_TRAIN, 'w') as img_out:\n    for im_id in tqdm(pds['image_id'][0:4000]):\n        img_path = os.path.join(data_dir,im_id+\".tiff\")\n        if os.path.exists(img_path):\n            img_patches = get_patches_train(img_path, SEQ_LEN, IMG_SIZE)\n            #img = (glue_to_one_trian(img_patches, IMG_SIZE, SEQ_LEN, normalizer)*255).astype(\"uint8\")\n            img, _ = glue_to_one_trian(img_patches, IMG_SIZE, SEQ_LEN , normalizer)\n            SIZE_Final1 = int(Scaling * img.shape[0])\n            SIZE_Final2 = int(Scaling * img.shape[1])\n            img = cv2.resize(img, (SIZE_Final1,SIZE_Final2))\n            save_path = save_dir1 + im_id + '.tiff'\n            #cv2.imwrite(save_path, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))\n            #Image.fromarray(img).save(im_id+'.jpeg')\n            img = cv2.imencode('.jpeg',cv2.cvtColor(img, cv2.COLOR_RGB2BGR))[1]\n            img_out.writestr(f'{im_id}.jpeg', img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"#fig, axs = plt.subplots(1,2, figsize=(20, 10))\n#im384 = io.imread(os.path.join(save_dir1,os.listdir(save_dir1)[0]))\n#axs[0].imshow(im384)\n#im980 = io.imread(os.path.join(save_dir2,os.listdir(save_dir2)[0]))\n#axs[1].imshow(im980)\n#plt.show\n#print(im384.shape)\n#print(im980.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"#!tar -czf train_images.tar.gz ../train_images/*.tiff","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"#!tar -czf train_images_960.tar.gz ../train_images_960/*.tiff","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}