{"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 matplotlib.pyplot as plt\nimport cv2\nimport numpy as np\nimport pandas as pd\n\ndef print_cell(image, title, index):\n    plt.subplot(1,10,index)\n    plt.imshow(image)\n    plt.title(title)\n    plt.axis('off')\n    \ndef build_image_names(image_id: str) -> list:\n    # mt is the mitchondria\n    mt = f'/kaggle/input/hpa-single-cell-image-classification/train/{image_id}_red.png'\n    \n    # er is the endoplasmic reticulum\n    er = f'/kaggle/input/hpa-single-cell-image-classification/train/{image_id}_yellow.png'\n    \n    # nu is the nuclei\n    nu = f'/kaggle/input/hpa-single-cell-image-classification/train/{image_id}_blue.png'\n    \n    # high is the protein\n    high = f'/kaggle/input/hpa-single-cell-image-classification/train/{image_id}_green.png'\n    \n    return [mt], [er], [nu], [high], [[mt], [er], [nu]],\n\ndef get_images(image_id):\n    mt, er, nu, high, images = build_image_names(image_id=image_id)\n    \n    blue = plt.imread(nu[0])\n    \n    green = plt.imread(high[0])\n    \n    red = plt.imread(mt[0])\n    \n    yellow = plt.imread(er[0])\n  \n    return blue, green, red, yellow","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I have been having problems with only a few images that end up crashing my notebooks when I compile with the entire dataset. One example is below. I debugged it back to how I am stacking the model in the code, but I do not know why it is having this issue. I can continue to by-pass these images, but this one is the smaller classes. Any thoughts on why this 3072 size image does not stack correctly and the subsequent one stacks as expected. ","metadata":{}},{"cell_type":"code","source":"def normalization(array):\n    a = (array - array.min())/(array.max()-array.min())\n    return a","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfpath = '../input/hpa-single-cell-image-classification/train.csv'\ntraindf = pd.read_csv(dfpath)\ntraindf['ID'] = traindf['ID'].str.strip()\nnames = np.array(traindf['ID'])\ncount = 0\nfor image_id in names:\n    try:\n        blue, green, red, yellow = get_images(image_id)\n    except TypeError:\n        print(f'Could not open: {image_id}')\n    if blue.shape[0] == 3072 and count <= 10:\n        print(image_id, blue.shape[0])\n        count+=1\n    else:\n        break","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As a suggestion from [Satwik](https://www.kaggle.com/p4rallax) I looked at the pixel values and have added some normalization and rescaling algorythms for both images. The first image improves with the normalization to 255, rescaling to RGB at 255,3,255 but normalization to 1 or as is are still difficult to decifer. The 2nd image is best as is or normalized to 1 but is not quite right normalized to 255 or rescaled to RGB at 255,3,255. \n\n# Am I missing something in how image '7930e80e-bbc8-11e8-b2bc-ac1f6b6435d0' is presented in comparison the image '0b252bcc-bbc8-11e8-b2bc-ac1f6b6435d0'? Revised to '5e22a522-bb99-11e8-b2b9-ac1f6b6435d0' which is also a 3072 image. ","metadata":{}},{"cell_type":"code","source":"image_id = '7930e80e-bbc8-11e8-b2bc-ac1f6b6435d0' \n\nmt, er, nu, high, images = build_image_names(image_id=image_id)\n    \n# Protein model image\nblue = plt.imread(nu[0])\ngreen = plt.imread(high[0])\nred = plt.imread(mt[0])\nyellow = plt.imread(er[0])\n\nRGB = np.stack((red,green,blue), axis = 2)\nRGB_norm = np.stack((normalization(red), normalization(green), normalization(blue)), axis = 2)\nRGB_255norm = np.stack((normalization(red)*255, normalization(green)*255, normalization(blue)*255), axis = 2)\nRGB_Satwik = np.stack((normalization(red)*255, normalization(green)*3, normalization(blue)*255), axis = 2)\nRYB = np.stack(((red-124)/(1/(.0167*255)) ,yellow,blue), axis = 2)\n\n##NORMALIZE = {\"mean\": [124 / 255, NA, NA], \"std\": [1 / (0.0167 * 255), ] * 3}\n\nplt.figure(figsize=(40,4)) \nprint_cell(RGB, 'RGB stack', 1)\nprint_cell(RGB_norm, 'RGB stack - normalize to 1', 2)\nprint_cell(RGB_255norm, 'RGB stack - normalize to 255', 3)\nprint_cell(RGB_Satwik, 'RGB stack - 255,3,255 rescale', 4)\nplt.tight_layout() \nplt.show()\n\nplt.figure(figsize=(40,4)) \nprint_cell(RYB, 'RYB stack from cell segmentator normalization', 1)\nprint_cell(red, 'red', 2)\nprint_cell(blue, 'blue', 3) \nprint_cell(green, 'Green cells', 4) \nplt.tight_layout() \nplt.show()\n\nprint(f'stacked image has: {RGB.shape}, only blue / nuclei has: {blue.shape}')\nprint(f'Average pixel in blue: {blue.mean()}, red: {red.mean()}, green: {green.mean()}')\nprint(f'Max pixel in blue: {blue.max()}, red: {red.max()}, green: {green.max()}')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = '60b57878-bb99-11e8-b2b9-ac1f6b6435d0' \n\nmt, er, nu, high, images = build_image_names(image_id=image_id)\n    \n# Protein model image\nblue = plt.imread(nu[0])\ngreen = plt.imread(high[0])\nred = plt.imread(mt[0])\nyellow = plt.imread(er[0])\n\nRGB = np.stack((red,green,blue), axis = 2)\nRGB_norm = np.stack((normalization(red), normalization(green), normalization(blue)), axis = 2)\nRGB_255norm = np.stack((normalization(red)*255, normalization(green)*255, normalization(blue)*255), axis = 2)\nRGB_Satwik = np.stack((normalization(red)*255, normalization(green)*3, normalization(blue)*255), axis = 2)\nRYB = np.stack(((red-124)/(1/(.0167*255)) ,yellow,blue), axis = 2)\n\n##NORMALIZE = {\"mean\": [124 / 255, NA, NA], \"std\": [1 / (0.0167 * 255), ] * 3}\n\nplt.figure(figsize=(40,4)) \nprint_cell(RGB, 'RGB stack', 1)\nprint_cell(RGB_norm, 'RGB stack - normalize to 1', 2)\nprint_cell(RGB_255norm, 'RGB stack - normalize to 255', 3)\nprint_cell(RGB_Satwik, 'RGB stack - 255,3,255 rescale', 4)\nplt.tight_layout() \nplt.show()\n\nplt.figure(figsize=(40,4)) \nprint_cell(RYB, 'RYB stack from cell segmentator normalization', 1)\nprint_cell(red, 'red', 2)\nprint_cell(blue, 'blue', 3) \nprint_cell(green, 'Green cells', 4) \nplt.tight_layout() \nplt.show()\n\nprint(f'stacked image has: {RGB.shape}, only blue / nuclei has: {blue.shape}')\nprint(f'Average pixel in blue: {blue.mean()}, red: {red.mean()}, green: {green.mean()}')\nprint(f'Max pixel in blue: {blue.max()}, red: {red.max()}, green: {green.max()}')","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}