{"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport keras\nfrom tqdm import tqdm\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom cv2 import cv2\nfrom PIL import Image\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom keras.layers import Dense, Dropout, Flatten, Input \nfrom keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\nfrom keras.preprocessing import image\nfrom keras.utils import plot_model\nfrom keras.models import Model\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.pooling import MaxPooling2D\nfrom numpy import array\nfrom glob import glob\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/diabetic-retinopathy-detection/trainLabels.csv.zip')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"targets_series = pd.Series(df_train['level'])\none_hot = pd.get_dummies(targets_series, sparse = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"targets_series[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"one_hot[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"one_hot_labels = np.asarray(one_hot)\none_hot_labelsY = np.asarray(targets_series)\none_hot_labelsY[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"im_size1 = 786\nim_size2 = 786\nx_train = []\ny_train = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = 0 \nfor f, breed in tqdm(df_train.values):\n    print(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = df_train[:1000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#this is a OpenCV implementation\ni = 0 \nfor f, breed in tqdm(df_train.values):\n    if type(cv2.imread('/storage/train/{}.jpeg'.format(f)))==type(None):\n        continue\n    else:\n        img = cv2.imread('/storage/train/{}.jpeg'.format(f))\n        label = one_hot_labels[i]\n        x_train.append(cv2.resize(img, (im_size1, im_size2)))\n        y_train.append(label)\n        i += 1\nnp.save('x_train2',x_train)\nnp.save('y_train2',y_train)\nprint('Done')\n\ni=0\nfor f, breed in tqdm(df_test.values):\n    try:\n        img = image.load_img(('/storage/train/{}.jpeg'.format(f)), target_size=(786, 786))\n        arr = image.img_to_array(img)\n        label = one_hot_labelsY[i]\n        x_train.append(arr)\n        y_train.append(label)\n        i += 1 \n    except:\n        pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"SOURCE_IMAGES = \"../input/diabetic-retinopathy-detection\"\n\nimages = glob(os.path.join(SOURCE_IMAGES, \"*.jpeg\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images[0:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# First five images paths\nimages[0:100]\n#SOURCE_IMAGES[0:100]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\nReturns two arrays: \n    x is an array of resized images\n    y is an array of labels\n\"\"\"\n\nx = [] # images as arrays\ny = [] # labels Infiltration or Not_infiltration\nWIDTH = 128\nHEIGHT = 128\ni=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img in images:\n    base = os.path.basename(img)\n    base = base.replace(\".jpeg\", \"\")\n    finding = labels[\"level\"][labels[\"image\"] == base].values[0]\n    # Read and resize image\n    imgg = load_img(img)\n    img_change = imgg.resize((WIDTH,HEIGHT))\n    x.append(np.asarray(img_change)) #.transpose(1, 0, 2))\n    #full_size_image = cv2.imread(img)\n    #x.append(cv2.resize(full_size_image, (WIDTH,HEIGHT), interpolation=cv2.INTER_CUBIC))\n    print(i)\n    # Labels\n    y.append(finding)\n    i=i+1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(type(x))\nprint(type(y))\n\n#img = plt.imshow(x[1])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Set it up as a dataframe if you like\ndf = pd.DataFrame()\ndf[\"labels\"]=y\ndf[\"images\"]=x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.savez(\"x_images_arrays\", x)\nnp.savez(\"y_labels_arrays\", y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls -1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}