{"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 cv2\nimport numpy as np \n\nimport pandas as pd\nimport tensorflow as tf\n\nimport keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import backend as K\nfrom keras import Input\nfrom keras.models import Model\nfrom keras.utils import *\nfrom keras.layers import *\n\nfrom tensorflow.keras import optimizers\n\nimport matplotlib.pyplot as plt\n\ntf.random.set_seed(2)\nnp.random.seed(0)\n\nimport os\nimport gc\n\nfrom keras.applications.efficientnet import EfficientNetB3","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:03:13.054736Z","iopub.execute_input":"2021-11-01T13:03:13.055561Z","iopub.status.idle":"2021-11-01T13:03:13.062433Z","shell.execute_reply.started":"2021-11-01T13:03:13.055523Z","shell.execute_reply":"2021-11-01T13:03:13.061416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 300\nBATCH_SIZE = 16","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:03:13.064518Z","iopub.execute_input":"2021-11-01T13:03:13.065173Z","iopub.status.idle":"2021-11-01T13:03:13.07322Z","shell.execute_reply.started":"2021-11-01T13:03:13.065071Z","shell.execute_reply":"2021-11-01T13:03:13.072551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): \n            return img \n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n            img = np.stack([img1,img2,img3],axis=-1)\n        return img\n    \ndef load_ben_color(image, sigmaX=10):\n    image = crop_image_from_gray(image).astype('uint8')\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image.astype('float32') / 255.\n\n'''\n    Preprocessing for ImageDataGenerator since ImageDataGenerator reads images in rgb mode, while opencv in bgr\n'''\ndef preprocessing(image, sigmaX=10):\n    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n    image = crop_image_from_gray(image).astype('uint8')\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image.astype('float32') / 255.","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:03:13.074542Z","iopub.execute_input":"2021-11-01T13:03:13.075128Z","iopub.status.idle":"2021-11-01T13:03:13.088954Z","shell.execute_reply.started":"2021-11-01T13:03:13.075093Z","shell.execute_reply":"2021-11-01T13:03:13.088248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Initialize EfficientNetB3\n    Output relu is relu rectifier with max_value of 4 to restrain predictions to proper range\n'''\ndef output_relu(x):\n    return K.relu(x, max_value=4)\n\nbase_model = EfficientNetB3(weights=None, include_top=False, input_shape=(IMG_SIZE,IMG_SIZE,3))\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dropout(0.4)(x)\nx = Dense(1, activation=output_relu, kernel_initializer='he_normal')(x)\nmodel = Model(inputs=base_model.input, outputs=x)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:03:13.091407Z","iopub.execute_input":"2021-11-01T13:03:13.091674Z","iopub.status.idle":"2021-11-01T13:03:17.930181Z","shell.execute_reply.started":"2021-11-01T13:03:13.091637Z","shell.execute_reply":"2021-11-01T13:03:17.929482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  model.load_weights('../input/pretrained-weights/model_b3_epochs_30_300.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:03:17.932441Z","iopub.execute_input":"2021-11-01T13:03:17.932845Z","iopub.status.idle":"2021-11-01T13:03:17.936568Z","shell.execute_reply.started":"2021-11-01T13:03:17.93281Z","shell.execute_reply":"2021-11-01T13:03:17.935895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntrain_id_codes = train_csv['id_code']\ntrain_labels = train_csv['diagnosis']\n\nfor i in range(len(train_id_codes)):\n    train_id_codes[i] = '../input/aptos2019-blindness-detection/train_images/{}.png'.format(train_id_codes[i])\n\ntest_csv = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\ntest_id_codes = test_csv['id_code']\ntest_pseudo_labels = np.empty(len(test_id_codes), dtype='float32')\nfor i in range(len(test_id_codes)):\n    test_id_codes[i] = '../input/aptos2019-blindness-detection/test_images/{}.png'.format(test_id_codes[i])\n    img = cv2.imread(test_id_codes[i])\n    img = load_ben_color(img)\n    X = np.array([img])\n    pred = model.predict(X)\n    test_pseudo_labels[i] = pred","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:03:17.937775Z","iopub.execute_input":"2021-11-01T13:03:17.938176Z","iopub.status.idle":"2021-11-01T13:07:40.85832Z","shell.execute_reply.started":"2021-11-01T13:03:17.938141Z","shell.execute_reply":"2021-11-01T13:07:40.857433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Float to integer prediction function. In this case round to nearest integer\n'''\ndef predict(X, coef=[0.5, 1.5, 2.5, 3.5]):\n        X_p = np.copy(X)\n        for i, pred in enumerate(X_p):\n            if pred < coef[0]:\n                X_p[i] = 0\n            elif pred >= coef[0] and pred < coef[1]:\n                X_p[i] = 1\n            elif pred >= coef[1] and pred < coef[2]:\n                X_p[i] = 2\n            elif pred >= coef[2] and pred < coef[3]:\n                X_p[i] = 3\n            else:\n                X_p[i] = 4\n        return X_p","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:40.859917Z","iopub.execute_input":"2021-11-01T13:07:40.860217Z","iopub.status.idle":"2021-11-01T13:07:40.867935Z","shell.execute_reply.started":"2021-11-01T13:07:40.860183Z","shell.execute_reply":"2021-11-01T13:07:40.866987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Round pseudo_labels\n'''\ntest_pseudo_labels = predict(test_pseudo_labels).astype('uint8')\nprint(test_pseudo_labels)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:40.869595Z","iopub.execute_input":"2021-11-01T13:07:40.869844Z","iopub.status.idle":"2021-11-01T13:07:40.886078Z","shell.execute_reply.started":"2021-11-01T13:07:40.869809Z","shell.execute_reply":"2021-11-01T13:07:40.885222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Create DataFrame for ImageDataGenerator\n    Combine training and test images with pseudo labels\n'''\nd = {}\nd['id_code'] = np.concatenate((train_id_codes, test_id_codes), axis=0)\nd['diagnosis'] = np.concatenate((train_labels, test_pseudo_labels), axis=0).astype('str')\ndf = pd.DataFrame(data=d)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:40.887522Z","iopub.execute_input":"2021-11-01T13:07:40.887861Z","iopub.status.idle":"2021-11-01T13:07:40.902655Z","shell.execute_reply.started":"2021-11-01T13:07:40.887829Z","shell.execute_reply":"2021-11-01T13:07:40.901963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Create Image Data Generator\n'''\ngen = ImageDataGenerator(preprocessing_function=preprocessing)\npseudo_datagen = gen.flow_from_dataframe(df, directory='.', x_col='id_code', y_col='diagnosis', target_size=(IMG_SIZE, IMG_SIZE), class_mode='sparse', batch_size=16)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:40.904071Z","iopub.execute_input":"2021-11-01T13:07:40.904313Z","iopub.status.idle":"2021-11-01T13:07:44.861866Z","shell.execute_reply.started":"2021-11-01T13:07:40.904282Z","shell.execute_reply":"2021-11-01T13:07:44.861107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    TPU Configs\n\n\n# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n'''","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:44.863327Z","iopub.execute_input":"2021-11-01T13:07:44.863762Z","iopub.status.idle":"2021-11-01T13:07:44.872476Z","shell.execute_reply.started":"2021-11-01T13:07:44.863722Z","shell.execute_reply":"2021-11-01T13:07:44.871548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# instantiating the model in the strategy scope creates the model on the TPU\n#with tpu_strategy.scope():\nmodel.compile(optimizer = optimizers.SGD(learning_rate=1e-3, momentum=0.9, nesterov=True, decay=1e-6), loss='mse')","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:44.873861Z","iopub.execute_input":"2021-11-01T13:07:44.874169Z","iopub.status.idle":"2021-11-01T13:07:44.897581Z","shell.execute_reply.started":"2021-11-01T13:07:44.874135Z","shell.execute_reply":"2021-11-01T13:07:44.896882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(pseudo_datagen, steps_per_epoch=len(pseudo_datagen), epochs=5)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:07:44.898653Z","iopub.execute_input":"2021-11-01T13:07:44.899111Z","iopub.status.idle":"2021-11-01T14:02:07.208543Z","shell.execute_reply.started":"2021-11-01T13:07:44.899073Z","shell.execute_reply":"2021-11-01T14:02:07.205959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Final prediction on testset\n'''\ntest_csv = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\nid_code = test_csv['id_code']\ntest_prediction = np.empty(len(id_code), dtype='float32')\nfor i in range(len(id_code)):\n    img = cv2.imread('../input/aptos2019-blindness-detection/test_images/{}.png'.format(id_code[i]))\n    img = load_ben_color(img)\n    X = np.array([img])\n    pred = model.predict(X)\n    test_prediction[i] = pred","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:02:07.212119Z","iopub.execute_input":"2021-11-01T14:02:07.212999Z","iopub.status.idle":"2021-11-01T14:06:03.561064Z","shell.execute_reply.started":"2021-11-01T14:02:07.212967Z","shell.execute_reply":"2021-11-01T14:06:03.560312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    Submission\n'''\nprediction = predict(test_prediction).astype('uint8')\ntest_csv['diagnosis'] = prediction\ntest_csv.to_csv(\"submission.csv\", index=False)\nunique, counts = np.unique(prediction, return_counts=True)\ntmp = dict(zip(unique, counts))\nprint(tmp)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:07:14.903527Z","iopub.execute_input":"2021-11-01T14:07:14.904093Z","iopub.status.idle":"2021-11-01T14:07:14.97858Z","shell.execute_reply.started":"2021-11-01T14:07:14.904045Z","shell.execute_reply":"2021-11-01T14:07:14.97748Z"},"trusted":true},"execution_count":null,"outputs":[]}]}