{"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":"markdown","source":"# 1. Dicom to Numpy array","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport pydicom\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt\nfrom skimage import exposure\nimport cv2\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-28T03:17:20.325944Z","iopub.execute_input":"2022-03-28T03:17:20.326359Z","iopub.status.idle":"2022-03-28T03:17:20.334187Z","shell.execute_reply.started":"2022-03-28T03:17:20.326326Z","shell.execute_reply":"2022-03-28T03:17:20.333215Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\nmodel_file = \"../input/vinbigdata-chest-xray-abnormalities-detection/train.csv\"\nwith open(model_file,'wb') as f:\npickle.dump(class_id, f)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T03:18:02.600372Z","iopub.execute_input":"2022-03-28T03:18:02.601083Z","iopub.status.idle":"2022-03-28T03:18:02.610281Z","shell.execute_reply.started":"2022-03-28T03:18:02.601023Z","shell.execute_reply":"2022-03-28T03:18:02.608771Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport cv2\nfrom PIL import Image \nimport pathlib\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:29:15.62505Z","iopub.execute_input":"2022-03-28T02:29:15.62561Z","iopub.status.idle":"2022-03-28T02:29:22.08122Z","shell.execute_reply.started":"2022-03-28T02:29:15.62555Z","shell.execute_reply":"2022-03-28T02:29:22.080159Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"All images in dataset are DICOM format. So we need to convert data from DICOM to numpy array. Original dicom2array function in [raddar's notebook](https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way)","metadata":{}},{"cell_type":"code","source":"import pickle\n\n\n\n# open a file, where you ant to store the data\nfile = open('train.csv', 'wb')\n\n# dump information to that file\npickle.dump(class_id, file)\n\n# close the file\nfile.close()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:28:13.375705Z","iopub.execute_input":"2022-03-28T02:28:13.376943Z","iopub.status.idle":"2022-03-28T02:28:13.403847Z","shell.execute_reply.started":"2022-03-28T02:28:13.37687Z","shell.execute_reply":"2022-03-28T02:28:13.402208Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset_dir = '../input/vinbigdata-chest-xray-abnormalities-detection'","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:29:22.084454Z","iopub.execute_input":"2022-03-28T02:29:22.084942Z","iopub.status.idle":"2022-03-28T02:29:22.089462Z","shell.execute_reply.started":"2022-03-28T02:29:22.084894Z","shell.execute_reply":"2022-03-28T02:29:22.088497Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:29:22.090668Z","iopub.execute_input":"2022-03-28T02:29:22.091259Z","iopub.status.idle":"2022-03-28T02:29:22.104192Z","shell.execute_reply.started":"2022-03-28T02:29:22.091221Z","shell.execute_reply":"2022-03-28T02:29:22.103248Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    # VOI LUT (if available by DICOM device) is used to\n    # transform raw DICOM data to \"human-friendly\" view\n#     print(dicom)\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n        \n    \ndef plot_img(img, size=(7, 7), is_rgb=True, title=\"\", cmap='gray'):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n\n\ndef plot_imgs(imgs, cols=4, size=7, is_rgb=True, title=\"\", cmap='gray', img_size=(500,500)):\n    rows = len(imgs)//cols + 1\n    fig = plt.figure(figsize=(cols*size, rows*size))\n    for i, img in enumerate(imgs):\n        if img_size is not None:\n            img = cv2.resize(img, img_size)\n        fig.add_subplot(rows, cols, i+1)\n        plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n    \n# def draw_bboxes(img, boxes, thickness=10, color=(255, 0, 0), img_size=(500,500)):\n#     img_copy = img.copy()\n#     if len(img_copy.shape) == 2:\n#         img_copy = np.stack([img_copy, img_copy, img_copy], axis=-1)\n#     for box in boxes:\n#         img_copy = cv2.rectangle(\n#             img_copy,\n#             (int(box[0]), int(box[1])),\n#             (int(box[2]), int(box[3])),\n#             color, thickness)\n#     if img_size is not None:\n#         img_copy = cv2.resize(img_copy, img_size)\n#     return img_copy","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-03-28T02:29:22.796972Z","iopub.execute_input":"2022-03-28T02:29:22.79774Z","iopub.status.idle":"2022-03-28T02:29:22.818204Z","shell.execute_reply.started":"2022-03-28T02:29:22.797685Z","shell.execute_reply":"2022-03-28T02:29:22.816854Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import glob\ndicom_paths = sorted(glob.glob(f'{dataset_dir}/train/*.dicom'))\nimgs = [dicom2array(path) for path in dicom_paths[:1200]]\n# im = [dicom2array(path) for path in dicom_paths[:4]]\n# plot_imgs(im)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:29:24.85546Z","iopub.execute_input":"2022-03-28T02:29:24.856141Z","iopub.status.idle":"2022-03-28T02:53:22.759977Z","shell.execute_reply.started":"2022-03-28T02:29:24.856101Z","shell.execute_reply":"2022-03-28T02:53:22.758862Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = pd.read_csv(\"../input/vinbigdata-chest-xray-abnormalities-detection/train.csv\")\ntarget.class_id","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:55:50.302469Z","iopub.execute_input":"2022-03-28T02:55:50.303392Z","iopub.status.idle":"2022-03-28T02:55:50.562611Z","shell.execute_reply.started":"2022-03-28T02:55:50.303313Z","shell.execute_reply":"2022-03-28T02:55:50.561267Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_sorted=target.sort_values(\n     by=\"image_id\",\n     ascending=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:55:53.829184Z","iopub.execute_input":"2022-03-28T02:55:53.829942Z","iopub.status.idle":"2022-03-28T02:55:53.976831Z","shell.execute_reply.started":"2022-03-28T02:55:53.829871Z","shell.execute_reply":"2022-03-28T02:55:53.975018Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_sorted.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:55:56.79808Z","iopub.execute_input":"2022-03-28T02:55:56.798466Z","iopub.status.idle":"2022-03-28T02:55:56.823597Z","shell.execute_reply.started":"2022-03-28T02:55:56.798432Z","shell.execute_reply":"2022-03-28T02:55:56.821993Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom as dicom","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:55:59.524245Z","iopub.execute_input":"2022-03-28T02:55:59.525161Z","iopub.status.idle":"2022-03-28T02:55:59.530651Z","shell.execute_reply.started":"2022-03-28T02:55:59.525102Z","shell.execute_reply":"2022-03-28T02:55:59.528957Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:56:00.66316Z","iopub.execute_input":"2022-03-28T02:56:00.663608Z","iopub.status.idle":"2022-03-28T02:56:00.674496Z","shell.execute_reply.started":"2022-03-28T02:56:00.663571Z","shell.execute_reply":"2022-03-28T02:56:00.67319Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:20.390229Z","iopub.execute_input":"2022-03-28T02:57:20.390734Z","iopub.status.idle":"2022-03-28T02:57:20.503742Z","shell.execute_reply.started":"2022-03-28T02:57:20.390671Z","shell.execute_reply":"2022-03-28T02:57:20.502762Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idnum = 2\nimage_id = train_data.loc[idnum, 'image_id']\ndata_file = dicom.dcmread(path+'train/'+image_id+'.dicom')\nimg = data_file.pixel_array","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:21.383602Z","iopub.execute_input":"2022-03-28T02:57:21.384057Z","iopub.status.idle":"2022-03-28T02:57:23.448139Z","shell.execute_reply.started":"2022-03-28T02:57:21.383928Z","shell.execute_reply":"2022-03-28T02:57:23.447196Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(data_file)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:23.95905Z","iopub.execute_input":"2022-03-28T02:57:23.959488Z","iopub.status.idle":"2022-03-28T02:57:23.968568Z","shell.execute_reply.started":"2022-03-28T02:57:23.959454Z","shell.execute_reply":"2022-03-28T02:57:23.96674Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\n\n\n# open a file, where you ant to store the data\nfile = open('data_file', 'wb')\n\n# dump information to that file\npickle.dump(image_id, file)\n\n# close the file\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-03-28T03:09:52.906116Z","iopub.execute_input":"2022-03-28T03:09:52.906595Z","iopub.status.idle":"2022-03-28T03:09:52.914464Z","shell.execute_reply.started":"2022-03-28T03:09:52.906558Z","shell.execute_reply":"2022-03-28T03:09:52.912996Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Image shape:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:25.618448Z","iopub.execute_input":"2022-03-28T02:57:25.619052Z","iopub.status.idle":"2022-03-28T02:57:25.626465Z","shell.execute_reply.started":"2022-03-28T02:57:25.618979Z","shell.execute_reply":"2022-03-28T02:57:25.625228Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:26.998717Z","iopub.execute_input":"2022-03-28T02:57:26.999454Z","iopub.status.idle":"2022-03-28T02:57:27.003869Z","shell.execute_reply.started":"2022-03-28T02:57:26.999415Z","shell.execute_reply":"2022-03-28T02:57:27.002803Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bbox = [train_data.loc[idnum, 'x_min'],\n        train_data.loc[idnum, 'y_min'],\n        train_data.loc[idnum, 'x_max'],\n        train_data.loc[idnum, 'y_max']]\nfig, ax = plt.subplots(1, 1, figsize=(20, 4))\nax.imshow(img, cmap='gray')\np = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                 bbox[2]-bbox[0],\n                                 bbox[3]-bbox[1],\n                                 ec='r', fc='none', lw=2.)\nax.add_patch(p)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:27.939229Z","iopub.execute_input":"2022-03-28T02:57:27.939704Z","iopub.status.idle":"2022-03-28T02:57:28.485581Z","shell.execute_reply.started":"2022-03-28T02:57:27.939658Z","shell.execute_reply":"2022-03-28T02:57:28.484688Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_train_data(idx_list):\n    fig, axs = plt.subplots(1, 3, figsize=(15, 10))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    for i in range(3):\n        image_id = train_data.loc[idx_list[i], 'image_id']\n        data_file = dicom.dcmread(path+'train/'+image_id+'.dicom')\n        img = data_file.pixel_array\n        axs[i].imshow(img, cmap='gray')\n        axs[i].set_title(train_data.loc[idx_list[i], 'class_name'])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n        if train_data.loc[idx_list[i], 'class_name'] != 'No finding':\n            bbox = [train_data.loc[idx_list[i], 'x_min'],\n                    train_data.loc[idx_list[i], 'y_min'],\n                    train_data.loc[idx_list[i], 'x_max'],\n                    train_data.loc[idx_list[i], 'y_max']]\n            p = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                             bbox[2]-bbox[0],\n                                             bbox[3]-bbox[1],\n                                             ec='r', fc='none', lw=2.)\n            axs[i].add_patch(p)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:28.729505Z","iopub.execute_input":"2022-03-28T02:57:28.729886Z","iopub.status.idle":"2022-03-28T02:57:28.744267Z","shell.execute_reply.started":"2022-03-28T02:57:28.729851Z","shell.execute_reply":"2022-03-28T02:57:28.74329Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for num in range(15):\n    idx_list = train_data[train_data['class_id']==num][0:3].index.values\n    plot_train_data(idx_list)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:57:29.486551Z","iopub.execute_input":"2022-03-28T02:57:29.487046Z","iopub.status.idle":"2022-03-28T02:58:42.801226Z","shell.execute_reply.started":"2022-03-28T02:57:29.486982Z","shell.execute_reply":"2022-03-28T02:58:42.800343Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for num in range(15):\n    print(train_data[train_data['class_id']==num])","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:58:42.802866Z","iopub.execute_input":"2022-03-28T02:58:42.803384Z","iopub.status.idle":"2022-03-28T02:58:42.949093Z","shell.execute_reply.started":"2022-03-28T02:58:42.803334Z","shell.execute_reply":"2022-03-28T02:58:42.948099Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = []\nlabels = target_sorted['class_id'][0:1200]\nprint(labels)\n# for i in range(len(labels)):\n#     if labels[i]==3:\n#         print(labels[i])","metadata":{"execution":{"iopub.status.busy":"2022-03-28T02:58:42.950606Z","iopub.execute_input":"2022-03-28T02:58:42.950978Z","iopub.status.idle":"2022-03-28T02:58:42.959115Z","shell.execute_reply.started":"2022-03-28T02:58:42.950945Z","shell.execute_reply":"2022-03-28T02:58:42.958135Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\n\n\n# open a file, where you ant to store the data\nfile = open('labels', 'wb')\n\n# dump information to that file\npickle.dump(labels, file)\n\n# close the file\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-03-28T03:08:19.449515Z","iopub.execute_input":"2022-03-28T03:08:19.450389Z","iopub.status.idle":"2022-03-28T03:08:19.458441Z","shell.execute_reply.started":"2022-03-28T03:08:19.450326Z","shell.execute_reply":"2022-03-28T03:08:19.457412Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(3):\n    plt.imshow(imgs[i])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:02.932304Z","iopub.execute_input":"2022-03-27T18:36:02.932971Z","iopub.status.idle":"2022-03-27T18:36:04.694573Z","shell.execute_reply.started":"2022-03-27T18:36:02.932919Z","shell.execute_reply":"2022-03-27T18:36:04.693299Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(imgs), len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:06.162891Z","iopub.execute_input":"2022-03-27T18:36:06.163313Z","iopub.status.idle":"2022-03-27T18:36:06.170125Z","shell.execute_reply.started":"2022-03-27T18:36:06.163273Z","shell.execute_reply":"2022-03-27T18:36:06.168747Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sklearn \nfrom keras.utils.np_utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, Y_train, Y_test = train_test_split(imgs, labels, test_size=0.2)\n\n# Reduce Sample Size for DeBugging\n# X_train = np.array(X_train[0:5000]) \n# Y_train = np.array(Y_train[0:5000])\n# X_test = np.array(X_test[0:2000]) \n# Y_test = np.array(Y_test[0:2000])\n\n# Encode labels to hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])\nY_trainHot = to_categorical(Y_train, num_classes = 15)\nY_testHot = to_categorical(Y_test, num_classes = 15)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:07.640221Z","iopub.execute_input":"2022-03-27T18:36:07.640755Z","iopub.status.idle":"2022-03-27T18:36:08.482049Z","shell.execute_reply.started":"2022-03-27T18:36:07.640714Z","shell.execute_reply":"2022-03-27T18:36:08.480828Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\nX_tr = np.array([cv2.resize(image, (224, 224)) for image in X_train])\nX_te = np.array([cv2.resize(image, (224, 224)) for image in X_test])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:09.094586Z","iopub.execute_input":"2022-03-27T18:36:09.095029Z","iopub.status.idle":"2022-03-27T18:36:09.773106Z","shell.execute_reply.started":"2022-03-27T18:36:09.094987Z","shell.execute_reply":"2022-03-27T18:36:09.771916Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_tr = np.expand_dims(X_tr, -1)\nX_te = np.expand_dims(X_te, -1)\nprint(X_tr.shape)\nX_te.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:11.478141Z","iopub.execute_input":"2022-03-27T18:36:11.478577Z","iopub.status.idle":"2022-03-27T18:36:11.487398Z","shell.execute_reply.started":"2022-03-27T18:36:11.478538Z","shell.execute_reply":"2022-03-27T18:36:11.486215Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = np.repeat(X_tr, repeats = 3, axis = -1)\nX_test = np.repeat(X_te, repeats = 3, axis = -1)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:12.458192Z","iopub.execute_input":"2022-03-27T18:36:12.458581Z","iopub.status.idle":"2022-03-27T18:36:13.299077Z","shell.execute_reply.started":"2022-03-27T18:36:12.458548Z","shell.execute_reply":"2022-03-27T18:36:13.297778Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_test.shape)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:13.304187Z","iopub.execute_input":"2022-03-27T18:36:13.304578Z","iopub.status.idle":"2022-03-27T18:36:13.316646Z","shell.execute_reply.started":"2022-03-27T18:36:13.304542Z","shell.execute_reply":"2022-03-27T18:36:13.315497Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_WIDTH=224\nIMG_HEIGHT=224\nIMG_DIM = (IMG_WIDTH, IMG_HEIGHT)\nBATCH_SIZE = 32\n#IMG_DIR = pathlib.Path('G:\\Github\\standford-dplt.plot(history.history[\"loss\"],c = \"purple\")\nplt.plot(history.history[\"val_loss\"],c = \"orange\")\nplt.title(\"Loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\n#plt.show()ogs\\cropped')\n#TRAIN_DIR = 'G:/Github/standford-dogs/cropped/train'\n#VAL_DIR = 'G:/Github/standford-dogs/cropped/validation'","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:28.687026Z","iopub.execute_input":"2022-03-27T18:36:28.687435Z","iopub.status.idle":"2022-03-27T18:36:29.108267Z","shell.execute_reply.started":"2022-03-27T18:36:28.687401Z","shell.execute_reply":"2022-03-27T18:36:29.106225Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_WIDTH=224\nIMG_HEIGHT=224\nIMG_DIM = (IMG_WIDTH, IMG_HEIGHT)\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:44.070527Z","iopub.execute_input":"2022-03-27T18:36:44.07092Z","iopub.status.idle":"2022-03-27T18:36:44.076656Z","shell.execute_reply.started":"2022-03-27T18:36:44.070887Z","shell.execute_reply":"2022-03-27T18:36:44.075402Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg19 import preprocess_input\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Specify the values for all arguments to data_generator_with_aug.\n# add zoom and vertical flip -> dogs are dogs no matter it is position and facing\ndata_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input,\n                                             horizontal_flip = True,\n                                             width_shift_range = 0.1,\n                                             height_shift_range = 0.1,\n                                             zoom_range = 0.1,\n                                             rotation_range = 0.2,\n                                             vertical_flip = True\n                                                )\n            \ndata_generator_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:51.169583Z","iopub.execute_input":"2022-03-27T18:36:51.170179Z","iopub.status.idle":"2022-03-27T18:36:51.178084Z","shell.execute_reply.started":"2022-03-27T18:36:51.17014Z","shell.execute_reply":"2022-03-27T18:36:51.177013Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(X_train), len(Y_trainHot))\nprint(len(X_test), len(Y_testHot))","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:52.487092Z","iopub.execute_input":"2022-03-27T18:36:52.487512Z","iopub.status.idle":"2022-03-27T18:36:52.493362Z","shell.execute_reply.started":"2022-03-27T18:36:52.487472Z","shell.execute_reply":"2022-03-27T18:36:52.492396Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator = data_generator_with_aug.flow(\n                                        X_train,Y_trainHot,\n                                        batch_size=32)\n\nvalidation_generator = data_generator_no_aug.flow(\n                                        X_test, Y_testHot,\n                                        batch_size=32)\n\n\n#nb_train = len(train_generator.filenames)\n#nb_val = len(validation_generator.filenames)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:36:53.702707Z","iopub.execute_input":"2022-03-27T18:36:53.703183Z","iopub.status.idle":"2022-03-27T18:36:54.065269Z","shell.execute_reply.started":"2022-03-27T18:36:53.703137Z","shell.execute_reply":"2022-03-27T18:36:54.064025Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nimport tensorflow.keras as keras\n\nvgg_base19 = VGG19(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT,IMG_WIDTH,3))","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:37:03.937527Z","iopub.execute_input":"2022-03-27T18:37:03.937962Z","iopub.status.idle":"2022-03-27T18:37:06.61181Z","shell.execute_reply.started":"2022-03-27T18:37:03.937911Z","shell.execute_reply":"2022-03-27T18:37:06.610593Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in vgg_base19.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:39:18.768431Z","iopub.execute_input":"2022-03-27T18:39:18.76889Z","iopub.status.idle":"2022-03-27T18:39:18.775493Z","shell.execute_reply.started":"2022-03-27T18:39:18.768852Z","shell.execute_reply":"2022-03-27T18:39:18.774348Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vgg_base19.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:39:19.288095Z","iopub.execute_input":"2022-03-27T18:39:19.288766Z","iopub.status.idle":"2022-03-27T18:39:19.304175Z","shell.execute_reply.started":"2022-03-27T18:39:19.288725Z","shell.execute_reply":"2022-03-27T18:39:19.303034Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout, BatchNormalization\n\nnum_classes = 15\n\nmodel = Sequential()\nmodel.add(vgg_base9)\nmodel.add(BatchNormalization())\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:39:19.99183Z","iopub.execute_input":"2022-03-27T18:39:19.992338Z","iopub.status.idle":"2022-03-27T18:39:20.216134Z","shell.execute_reply.started":"2022-03-27T18:39:19.992283Z","shell.execute_reply":"2022-03-27T18:39:20.214899Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"adam = tf.keras.optimizers.Adam(learning_rate=0.0001)\n\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, mode='min',\n                                              restore_best_weights=False\n                                              )\n\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', mode = 'min',\n                                   factor=0.3,\n                                   patience=5,\n                                   verbose=1,\n                                   min_delta=1e-3,min_lr = 1e-5,\n                                   )","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:39:23.209402Z","iopub.execute_input":"2022-03-27T18:39:23.209823Z","iopub.status.idle":"2022-03-27T18:39:23.21848Z","shell.execute_reply.started":"2022-03-27T18:39:23.209789Z","shell.execute_reply":"2022-03-27T18:39:23.217184Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001), \n              loss = 'categorical_crossentropy', \n              metrics=['accuracy']) #,tfa.metrics.F1Score(num_classes=num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:39:23.777068Z","iopub.execute_input":"2022-03-27T18:39:23.777506Z","iopub.status.idle":"2022-03-27T18:39:24.108317Z","shell.execute_reply.started":"2022-03-27T18:39:23.777464Z","shell.execute_reply":"2022-03-27T18:39:24.107146Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit_generator(train_generator,steps_per_epoch=8,\n                        epochs = 500,callbacks=[reduce_lr,early_stop],\n                          validation_steps=1,\n                        validation_data = validation_generator)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T18:39:25.696713Z","iopub.execute_input":"2022-03-27T18:39:25.697121Z","iopub.status.idle":"2022-03-27T19:10:56.739105Z","shell.execute_reply.started":"2022-03-27T18:39:25.697086Z","shell.execute_reply":"2022-03-27T19:10:56.738053Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"],c = \"purple\")\nplt.plot(history.history[\"val_loss\"],c = \"orange\")\nplt.title(\"Loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:11:42.270149Z","iopub.execute_input":"2022-03-27T19:11:42.2709Z","iopub.status.idle":"2022-03-27T19:11:42.471026Z","shell.execute_reply.started":"2022-03-27T19:11:42.270844Z","shell.execute_reply":"2022-03-27T19:11:42.469794Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"],c = \"purple\")\nplt.plot(history.history[\"val_accuracy\"],c = \"orange\")\nplt.title(\"Accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:11:50.579111Z","iopub.execute_input":"2022-03-27T19:11:50.579514Z","iopub.status.idle":"2022-03-27T19:11:50.787717Z","shell.execute_reply.started":"2022-03-27T19:11:50.579481Z","shell.execute_reply":"2022-03-27T19:11:50.786707Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score1 = model.evaluate(X_test, Y_testHot ,verbose=1)\nprint('Test Loss:', score1[0])\nprint('Test accuracy:', score1[1])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:11:57.874894Z","iopub.execute_input":"2022-03-27T19:11:57.875298Z","iopub.status.idle":"2022-03-27T19:13:18.292051Z","shell.execute_reply.started":"2022-03-27T19:11:57.875264Z","shell.execute_reply":"2022-03-27T19:13:18.291077Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import f1_score, roc_curve, auc, precision_score, accuracy_score, confusion_matrix\n\n#Confusion Matrix\nY_pred = model.predict(X_test)\nY_pred.shape\nY_pred = np.argmax(Y_pred, axis=1)\nY_pred1 = Y_pred\nY_true = np.argmax(Y_testHot, axis=1)\n\ncm = confusion_matrix(Y_true, Y_pred)\n#tn, fp, fn, tp = confusion_matrix(Y_true, Y_pred).ravel()\n#sp = tn/(tn+fp)\n#sn = tp/(tp+fn)\nprint(cm)\n#plt.figure(figsize=(8, 8))\n#ax = sns.heatmap(cm, cmap=plt.cm.Greens, annot=True, square=True,Y_true,Y_pred1)\n#ax.set_ylabel('Actual',fontsize=20)\n#ax.set_xlabel('Predicted',fontsize=20)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:16:51.908658Z","iopub.execute_input":"2022-03-27T19:16:51.909122Z","iopub.status.idle":"2022-03-27T19:18:12.315148Z","shell.execute_reply.started":"2022-03-27T19:16:51.909085Z","shell.execute_reply":"2022-03-27T19:18:12.31344Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fp = cm.sum(axis=0) - np.diag(cm)  \nfn = cm.sum(axis=1) - np.diag(cm)\ntp = np.diag(cm)\ntn = cm.sum() - (fp + fn + tp)\n\nprint(fp,fn,tp,tn)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:11.100144Z","iopub.execute_input":"2022-03-27T19:20:11.100613Z","iopub.status.idle":"2022-03-27T19:20:11.110202Z","shell.execute_reply.started":"2022-03-27T19:20:11.10057Z","shell.execute_reply":"2022-03-27T19:20:11.109283Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 0\nprint(fp[idx], fn[idx], tp[idx], tn[idx])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:47.656214Z","iopub.execute_input":"2022-03-27T19:20:47.656853Z","iopub.status.idle":"2022-03-27T19:20:47.663707Z","shell.execute_reply.started":"2022-03-27T19:20:47.656792Z","shell.execute_reply":"2022-03-27T19:20:47.662496Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 1","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:48.980097Z","iopub.execute_input":"2022-03-27T19:20:48.98074Z","iopub.status.idle":"2022-03-27T19:20:48.985738Z","shell.execute_reply.started":"2022-03-27T19:20:48.980686Z","shell.execute_reply":"2022-03-27T19:20:48.984804Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 2","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:50.084094Z","iopub.execute_input":"2022-03-27T19:20:50.084487Z","iopub.status.idle":"2022-03-27T19:20:50.089517Z","shell.execute_reply.started":"2022-03-27T19:20:50.084453Z","shell.execute_reply":"2022-03-27T19:20:50.088326Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy_score(Y_true, Y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:50.808556Z","iopub.execute_input":"2022-03-27T19:20:50.808991Z","iopub.status.idle":"2022-03-27T19:20:50.816959Z","shell.execute_reply.started":"2022-03-27T19:20:50.808946Z","shell.execute_reply":"2022-03-27T19:20:50.81588Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"acc = np.diag(cm).sum() / cm.sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:53.343287Z","iopub.execute_input":"2022-03-27T19:20:53.343681Z","iopub.status.idle":"2022-03-27T19:20:53.349199Z","shell.execute_reply.started":"2022-03-27T19:20:53.343647Z","shell.execute_reply":"2022-03-27T19:20:53.34807Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\n\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T19:20:57.043883Z","iopub.execute_input":"2022-03-27T19:20:57.044314Z","iopub.status.idle":"2022-03-27T19:20:58.605214Z","shell.execute_reply.started":"2022-03-27T19:20:57.044274Z","shell.execute_reply":"2022-03-27T19:20:58.604113Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vgg_base19(X_train[:960], Y_trainHot[:960], X_test[:240], Y_testHot[:240],class_weight,8,15)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T20:05:20.250742Z","iopub.execute_input":"2022-03-27T20:05:20.251334Z","iopub.status.idle":"2022-03-27T20:05:20.293481Z","shell.execute_reply.started":"2022-03-27T20:05:20.251298Z","shell.execute_reply":"2022-03-27T20:05:20.291983Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, vmin=0.0, vmax=100.0, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\n\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#cm_df = pd.DataFrame(cm,\n                     #index = ['SETOSA','VERSICOLR','VIRGINICA'], \n                     #columns = ['SETOSA','VERSICOLR','VIRGINICA'])","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nimport tensorflow.keras as keras\n\nvgg_base16 = VGG16(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT,IMG_WIDTH,3))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in vgg_base16.layers:\n    layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vgg_base16.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout, BatchNormalization\n\nnum_classes = 15\n\nmodel = Sequential()\nmodel.add(vgg_base16)\nmodel.add(BatchNormalization())\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"adam = tf.keras.optimizers.Adam(learning_rate=0.0001)\n\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, mode='min',\n                                              restore_best_weights=False\n                                              )\n\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', mode = 'min',\n                                   factor=0.3,\n                                   patience=5,\n                                   verbose=1,\n                                   min_delta=1e-3,min_lr = 1e-7,\n                                   )","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.001), \n              loss = 'categorical_crossentropy', \n              metrics=['accuracy']) #,tfa.metrics.F1Score(num_classes=num_classes)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit_generator(train_generator,steps_per_epoch=8,\n                        epochs = 500,callbacks=[early_stop,reduce_lr],\n                          validation_steps=1,\n                        validation_data = validation_generator)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"],c = \"purple\")\nplt.plot(history.history[\"val_loss\"],c = \"orange\")\nplt.title(\"Loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"],c = \"purple\")\nplt.plot(history.history[\"val_accuracy\"],c = \"orange\")\nplt.title(\"Accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score1 = model.evaluate(X_test, Y_testHot ,verbose=1)\nprint('Test Loss:', score1[0])\nprint('Test accuracy:', score1[1])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import f1_score, roc_curve, auc, precision_score, accuracy_score, confusion_matrix\n\n#Confusion Matrix\nY_pred = model.predict(X_test)\nY_pred.shape\nY_pred = np.argmax(Y_pred, axis=1)\nY_pred1 = Y_pred\nY_true = np.argmax(Y_testHot, axis=1)\n\ncm = confusion_matrix(Y_true, Y_pred)\n#tn, fp, fn, tp = confusion_matrix(Y_true, Y_pred).ravel()\n#sp = tn/(tn+fp)\n#sn = tp/(tp+fn)\nprint(cm)\n#plt.figure(figsize=(8, 8))\n#ax = sns.heatmap(cm, cmap=plt.cm.Greens, annot=True, square=True,Y_true,Y_pred1)\n#ax.set_ylabel('Actual',fontsize=20)\n#ax.set_xlabel('Predicted',fontsize=20)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fp = cm.sum(axis=0) - np.diag(cm)  \nfn = cm.sum(axis=1) - np.diag(cm)\ntp = np.diag(cm)\ntn = cm.sum() - (fp + fn + tp)\n\nprint(fp,fn,tp,tn)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 0\nprint(fp[idx], fn[idx], tp[idx], tn[idx])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 1","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 2","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy_score(Y_true, Y_pred)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"acc = np.diag(cm).sum() / cm.sum()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\n\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, vmin=0.0, vmax=100.0, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, vmin=0.0, vmax=100.0, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\n\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionResNetV2\nfrom tensorflow.keras.applications.inception_resnet_v2 import preprocess_input\nfrom tensorflow.keras.models import Model\nimport tensorflow.keras as keras\n\nresnet = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT,IMG_WIDTH,3),pooling='avg')\n\noutput = resnet.layers[-1].output\noutput = tf.keras.layers.Flatten()(output)\nresnet = Model(resnet.input, output)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nres_name = []\nfor layer in resnet.layers:\n    res_name.append(layer.name)\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"res_name[-333:]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"set_trainable = False\nfor layer in resnet.layers:\n    if layer.name in res_name[-333:]:\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nresnet.summary()\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout\n\nnum_classes = 15\n\nmodel = Sequential()\nmodel.add(resnet)\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1500, activation='relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(num_classes, activation='sigmoid'))\n\nmodel.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"adam = tf.keras.optimizers.Adam(learning_rate=0.0001)\n\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, mode='min',\n                                              restore_best_weights=False\n                                              )\n\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', mode = 'min',\n                                   factor=0.3,\n                                   patience=5,\n                                   verbose=1,\n                                   min_delta=1e-3,min_lr = 1e-5,\n                                   )\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer = adam, \n              loss = 'categorical_crossentropy', \n              metrics=['accuracy',tfa.metrics.F1Score(num_classes=num_classes)])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit_generator(train_generator,steps_per_epoch=8,\n                        epochs = 500,callbacks=[early_stop,reduce_lr],\n                          validation_steps=1,\n                        validation_data = validation_generator)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"],c = \"purple\")\nplt.plot(history.history[\"val_loss\"],c = \"orange\")\nplt.title(\"Loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"],c = \"purple\")\nplt.plot(history.history[\"val_accuracy\"],c = \"orange\")\nplt.title(\"Accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.legend([\"train\", \"val\"])\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score1 = model.evaluate(X_test, Y_testHot ,verbose=1)\nprint('Test Loss:', score1[0])\nprint('Test accuracy:', score1[1])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import f1_score, roc_curve, auc, precision_score, accuracy_score, confusion_matrix\n\n#Confusion Matrix\nY_pred = model.predict(X_test)\nY_pred.shape\nY_pred = np.argmax(Y_pred, axis=1)\nY_pred1 = Y_pred\nY_true = np.argmax(Y_testHot, axis=1)\n\ncm = confusion_matrix(Y_true, Y_pred)\n#tn, fp, fn, tp = confusion_matrix(Y_true, Y_pred).ravel()\n#sp = tn/(tn+fp)\n#sn = tp/(tp+fn)\nprint(cm)\n#plt.figure(figsize=(8, 8))\n#ax = sns.heatmap(cm, cmap=plt.cm.Greens, annot=True, square=True,Y_true,Y_pred1)\n#ax.set_ylabel('Actual',fontsize=20)\n#ax.set_xlabel('Predicted',fontsize=20)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fp = cm.sum(axis=0) - np.diag(cm)  \nfn = cm.sum(axis=1) - np.diag(cm)\ntp = np.diag(cm)\ntn = cm.sum() - (fp + fn + tp)\n\nprint(fp,fn,tp,tn)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 0\nprint(fp[idx], fn[idx], tp[idx], tn[idx])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 1","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 2","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"idx = 3","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy_score(Y_true, Y_pred)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"acc = np.diag(cm).sum() / cm.sum()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\n\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport pandas as pd\n\nimport seaborn as sns\nimport math\n\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\nimport matplotlib as mpl\nsum = cm.sum()\ncm = cm * 100.0 / ( 1.0 * sum )\n\nmpl.style.use('seaborn')\n\ndf_cm = pd.DataFrame(cm, \n    index = [i for i in range(cm.shape[0])],\n    columns = [i for i in range(cm.shape[1])])\n\nfig = plt.figure()\n\nplt.clf()\n\nax = fig.add_subplot(111)\nax.set_aspect(1)\n\ncmap = sns.cubehelix_palette(light=1, as_cmap=True)\n\nres = sn.heatmap(df_cm, annot=True, vmin=0.0, vmax=100.0, fmt='.2f', cmap=cmap)\n\nres.invert_yaxis()\n\n#plt.yticks([0.5,1.5,2.5], [ '0', '1', '2'],va='center')\n\nplt.title('Confusion Matrix')\n\nplt.savefig('confusion_matrix_1.png', dpi=100, bbox_inches='tight' )\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=300, verbose=0)\n# evaluate the model\ntrain_acc = model.evaluate(X_train, Y_trainHot, verbose=0)\ntest_acc = model.evaluate(X_test, Y_testHot, verbose=0)\nprint('Train: %.3f, Test: %.3f' % (train_acc, test_acc))","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n# accuracy: (tp + tn) / (p + n)\naccuracy = accuracy_score(testy, yhat_classes)\nprint('Accuracy: %f' % accuracy)\n# precision tp / (tp + fp)\nprecision = precision_score(testy, yhat_classes)\nprint('Precision: %f' % precision)\n# recall: tp / (tp + fn)\nrecall = recall_score(testy, yhat_classes)\nprint('Recall: %f' % recall)\n# f1: 2 tp / (2 tp + fp + fn)\nf1 = f1_score(testy, yhat_classes)\nprint('F1 score: %f' % f1)","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training & validation accuracy values\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null}]}