{"cells":[{"metadata":{},"cell_type":"markdown","source":"This kernel is a Basic visualization and some data exploratory \nThanks to https://www.kaggle.com/puremath86/visualization-starter\nThe example on the images grids \n\n\nSome key take away \n1. Number of images is low.\n2. As others mention, there is no balance between classes. \n3. See the Histograms and active areas, the photos are not occupying the same dynamic  (meaning the retina area versus the padding background) area, I am not sure this will impact the models, but it is worth thinking about  when resizing the images. "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image\nimport seaborn as sns; sns.set_style(\"white\")\nimport random\nimport cv2\nimport time \nfrom tqdm import tqdm, tqdm_notebook\n\n\n \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read Meta Data \nRead the train CSV which contains the image ID and the labels "},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(\"../input/train.csv\")\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = df_train['diagnosis'].value_counts().plot(kind='bar',\n                                    figsize=(14,8),\n                                    title=\"Count Diagnosis Labels  \")\nax.set_xlabel(\"Diagnossis Label\")\nax.set_ylabel(\"Frequency\")\nax.set_xticklabels( labels = list(('No DR', 'Moderate', 'Mild', 'Proliferative DR', 'Severe')),rotation=45)\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read images size"},{"metadata":{"trusted":true},"cell_type":"code","source":"%time\nPATH = \"../input/train_images\"\nimage_size_list=[]\nimages_files = os.listdir(PATH)\nfor image in images_files :\n    image_size_list.append(Image.open(os.path.join(PATH, image)).size)\n ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_size = np.array(image_size_list)\nimages_area =  images_size[:,0] * images_size[:,1]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DF = pd.DataFrame(images_size,columns=['Width','Height'])\nDF.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = DF['Width'].value_counts().plot(kind='bar',\n                                    figsize=(14,8),\n                                    title=\"Image's Width    \")\nax.set_xlabel(\"Width\")\nax.set_ylabel(\"Frequency\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = DF['Height'].value_counts().plot(kind='bar',\n                                    figsize=(14,8),\n                                    title=\"Image's Height    \")\nax.set_xlabel(\"Height\")\nax.set_ylabel(\"Frequency\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#This function show a gride of images, and heir histogram \n\ndef show_images_with_Histograms(images, cols = 2, titles = None):\n   \n    n_images = len(images)\n    nrows = int(n_images/cols)\n    fig, ax = plt.subplots(nrows, 2*  cols )\n    \n    assert((titles is None)or (len(images) == len(titles)))\n    \n    if titles is None: titles = ['Image (%d)' % i for i in range(1,n_images + 1)]\n    \n    row = 0\n    col = 0 \n    for n, (image, title) in enumerate(zip(images, titles)):\n        \n        if image.ndim == 2:\n            plt.gray()\n        ax[row,col].imshow(image)\n        ax[row,col].set_title(title,fontsize=50)\n        \n        col +=1 \n        if col == 2 * cols : \n            col =0\n            row +=1\n        \n        img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        \n        ax[row,col].hist(img_gray.ravel(),30,[0,256])\n       \n        col +=1 \n        if col == 2 * cols : \n            col =0\n            row +=1\n            \n    fig.set_size_inches(np.array(fig.get_size_inches()) * n_images  )\n    #plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"SEED = 1234\nPATH = \"../input/train_images\"\nimgs = []\ntitles = []\n\nfor i in range(12):\n    #plt.figure(figsize=(5,5))\n    random.seed(SEED + i)\n    id = random.choice(os.listdir(PATH))\n    id_code = id.split(\".\")[0]\n    imgs.append(np.asarray(Image.open(os.path.join(PATH, id))))\n    titles.append(\" \".join([str(\"Label =\"),str((df_train.loc[df_train.id_code == id_code, 'diagnosis']).item()),str(\"Image id is:\"),str(id_code)]))\n   \n\nshow_images_with_Histograms(imgs, cols = 2, titles = titles)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Calculate the Active area \nCalculate the Active area of the image\nCalculate the percentage of the area that the retina occupy (without the black padding) from the entire image area "},{"metadata":{"trusted":true},"cell_type":"code","source":"#Calculate the Active area of the image \nThrshold = 50 \ndef Calc_Active_Area(img):\n\n    img = np.where(img < 50, 0, img) \n        \n    # Mask of non-black pixels (assuming image has a single channel).\n    mask = img > 0\n\n    # Coordinates of non-black pixels.\n    coords = np.argwhere(mask)\n\n    # Bounding box of non-black pixels.\n    y0, x0 = coords.min(axis=0)\n    y1 , x1 = coords.max(axis=0) + 1   # slices are exclusive at the top\n    \n    active_area = (abs(x1-x0) * abs(y1-y0)) / (img.shape[0] * img.shape[1])*100\n    \n    return active_area","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%time\nPATH = \"../input/train_images\"\nimages_active_list=[]\n\n\ntk0 = tqdm_notebook(list(images_files))\n\n\nfor  i , image in enumerate(tk0) :\n    \n    \n    img = cv2.imread(os.path.join(PATH, image))\n    \n    img_gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)\n    active_area = Calc_Active_Area(np.asarray(img_gray))\n    images_active_list.append(active_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DFActiveList  = pd.DataFrame(images_active_list,columns=['ActiveArea'])\nDFActiveList.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bins = [0, 1, 5, 10, 20,30,40, 50,60,70,80,85,90,95,99, 100]\nDFActiveList['binned'] = pd.cut(DFActiveList['ActiveArea'], bins)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = DFActiveList['binned'].value_counts().plot(kind='bar',\n                                    figsize=(14,8),\n                                    title=\"Image's Active Area %    \")\nax.set_xlabel(\"% Active Area\")\nax.set_ylabel(\"Frequency\")\nlabels = ax.get_xticklabels()\nax.set_xticklabels(labels = labels ,rotation=45)\n\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}