{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import math\nimport os\nimport cv2\nfrom PIL import Image\nimport numpy as np\nfrom keras import layers\nfrom keras.applications import DenseNet121\nfrom keras.applications import DenseNet201\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score\nimport scipy\nfrom tqdm import tqdm\nimport time\nfrom keras.models import load_model\nfrom keras.preprocessing import image ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets load binary classification model (NODR vs DR)"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"model = load_model('../input/dr-detection/dr_model.h5')\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Model  \ndr_model = Model(inputs=model.layers[0].layers[0].input, \n                 outputs=(model.layers[0].layers[-1].output)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def GetCAM(model,cmodel,img_path):\n    img = image.load_img(img_path, target_size=(224, 224))\n    x = image.img_to_array(img)\n    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor\n    x = np.expand_dims(x, axis=0)\n    pred_vec = model.predict(x)\n    pred=np.argmax(pred_vec)\n    print(img_path)\n    if pred==1:\n        print(\"==> DR\")\n    else:\n        print(\"==> NODR\")\n    last_conv_output = dr_model.predict(x)\n    last_conv_output = np.squeeze(last_conv_output)\n    # bilinear upsampling to resize each filtered image to size of original image \n    mat_for_mult = scipy.ndimage.zoom(last_conv_output, (32, 32, 1), order=1) \n    # dim: 224 x 224 x 1048\n    #print(mat_for_mult.shape)\n    # get fc layer weights\n    all_amp_layer_weights = model.layers[-1].get_weights()[0]\n    amp_layer_weights = all_amp_layer_weights[:, pred]\n    # get class activation map for object class that is predicted to be in the image\n    cam = np.dot(mat_for_mult.reshape((224*224, 1024)), amp_layer_weights).reshape(224,224) \n    # dim: 224 x 224\n    return cam\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cam=GetCAM(model,dr_model,\"../input/aptos2019-blindness-detection/train_images/0083ee8054ee.png\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(cam)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"im = cv2.resize(cv2.cvtColor(cv2.imread(\"../input/aptos2019-blindness-detection/train_images/0097f532ac9f.png\"), cv2.COLOR_BGR2RGB), (224, 224))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots()\nax.imshow(im, alpha=1)\nax.imshow(cam, cmap='jet', alpha=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(32, 32))\nnum_samples=2\n\nfor class_id in sorted(train_df['diagnosis'].unique()):\n    for i, (idx, row) in enumerate(train_df.loc[train_df['diagnosis'] == class_id].sample(num_samples).iterrows()):\n        ax = fig.add_subplot(5, num_samples, class_id *num_samples + i + 1, xticks=[], yticks=[])\n        im = cv2.resize(cv2.cvtColor(cv2.imread(f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"), cv2.COLOR_BGR2RGB), (224, 224))\n        cam=GetCAM(model,dr_model,f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\")\n        #plt.imshow(im)\n        ax.imshow(im, alpha=1)\n        ax.imshow(cam, cmap='jet', alpha=0.3)\n        ax.set_title(f'Label: {class_id}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}