{"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-22T08:21:54.416919Z","iopub.execute_input":"2022-03-22T08:21:54.417375Z","iopub.status.idle":"2022-03-22T08:21:55.194654Z","shell.execute_reply.started":"2022-03-22T08:21:54.417336Z","shell.execute_reply":"2022-03-22T08:21:55.193657Z"},"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-22T08:21:56.63012Z","iopub.execute_input":"2022-03-22T08:21:56.630751Z","iopub.status.idle":"2022-03-22T08:22:01.949523Z","shell.execute_reply.started":"2022-03-22T08:21:56.630693Z","shell.execute_reply":"2022-03-22T08:22:01.948676Z"},"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":"dataset_dir = '../input/vinbigdata-chest-xray-abnormalities-detection'","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:22:01.951115Z","iopub.execute_input":"2022-03-22T08:22:01.951604Z","iopub.status.idle":"2022-03-22T08:22:01.955305Z","shell.execute_reply.started":"2022-03-22T08:22:01.951537Z","shell.execute_reply":"2022-03-22T08:22:01.954401Z"},"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-22T08:22:01.95644Z","iopub.execute_input":"2022-03-22T08:22:01.956891Z","iopub.status.idle":"2022-03-22T08:22:01.974406Z","shell.execute_reply.started":"2022-03-22T08:22:01.95686Z","shell.execute_reply":"2022-03-22T08:22:01.973518Z"},"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[:1000]]\n# im = [dicom2array(path) for path in dicom_paths[:4]]\n# plot_imgs(im)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:22:02.44723Z","iopub.execute_input":"2022-03-22T08:22:02.447964Z","iopub.status.idle":"2022-03-22T08:41:39.212869Z","shell.execute_reply.started":"2022-03-22T08:22:02.447907Z","shell.execute_reply":"2022-03-22T08:41:39.210909Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Maybe, you can try some preprocess like equalize histogram. You can see the difference between before and after","metadata":{}},{"cell_type":"code","source":"\nimgs = [exposure.equalize_hist(img) for img in imgs]\nplot_imgs(imgs)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imgs","metadata":{"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-22T08:46:38.764725Z","iopub.execute_input":"2022-03-22T08:46:38.765266Z","iopub.status.idle":"2022-03-22T08:46:38.946609Z","shell.execute_reply.started":"2022-03-22T08:46:38.765225Z","shell.execute_reply":"2022-03-22T08:46:38.945484Z"},"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-22T08:46:44.451859Z","iopub.execute_input":"2022-03-22T08:46:44.452361Z","iopub.status.idle":"2022-03-22T08:46:44.583818Z","shell.execute_reply.started":"2022-03-22T08:46:44.45232Z","shell.execute_reply":"2022-03-22T08:46:44.582709Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_sorted.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:46:47.278994Z","iopub.execute_input":"2022-03-22T08:46:47.279389Z","iopub.status.idle":"2022-03-22T08:46:47.304941Z","shell.execute_reply.started":"2022-03-22T08:46:47.279349Z","shell.execute_reply":"2022-03-22T08:46:47.303925Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom as dicom","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:46:50.548294Z","iopub.execute_input":"2022-03-22T08:46:50.548935Z","iopub.status.idle":"2022-03-22T08:46:50.553079Z","shell.execute_reply.started":"2022-03-22T08:46:50.548896Z","shell.execute_reply":"2022-03-22T08:46:50.552186Z"},"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-22T08:46:52.309004Z","iopub.execute_input":"2022-03-22T08:46:52.309629Z","iopub.status.idle":"2022-03-22T08:46:52.317029Z","shell.execute_reply.started":"2022-03-22T08:46:52.309571Z","shell.execute_reply":"2022-03-22T08:46:52.316045Z"},"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-22T08:46:54.532782Z","iopub.execute_input":"2022-03-22T08:46:54.533154Z","iopub.status.idle":"2022-03-22T08:46:54.640397Z","shell.execute_reply.started":"2022-03-22T08:46:54.533122Z","shell.execute_reply":"2022-03-22T08:46:54.639492Z"},"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-22T08:46:55.671379Z","iopub.execute_input":"2022-03-22T08:46:55.671772Z","iopub.status.idle":"2022-03-22T08:46:57.71329Z","shell.execute_reply.started":"2022-03-22T08:46:55.671738Z","shell.execute_reply":"2022-03-22T08:46:57.712253Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(data_file)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:46:57.715756Z","iopub.execute_input":"2022-03-22T08:46:57.716233Z","iopub.status.idle":"2022-03-22T08:46:57.724068Z","shell.execute_reply.started":"2022-03-22T08:46:57.716184Z","shell.execute_reply":"2022-03-22T08:46:57.722712Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Image shape:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:46:59.059612Z","iopub.execute_input":"2022-03-22T08:46:59.059985Z","iopub.status.idle":"2022-03-22T08:46:59.06627Z","shell.execute_reply.started":"2022-03-22T08:46:59.059953Z","shell.execute_reply":"2022-03-22T08:46:59.064963Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:47:02.751438Z","iopub.execute_input":"2022-03-22T08:47:02.75185Z","iopub.status.idle":"2022-03-22T08:47:02.756818Z","shell.execute_reply.started":"2022-03-22T08:47:02.751816Z","shell.execute_reply":"2022-03-22T08:47:02.755697Z"},"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-22T08:47:04.359327Z","iopub.execute_input":"2022-03-22T08:47:04.35972Z","iopub.status.idle":"2022-03-22T08:47:04.884759Z","shell.execute_reply.started":"2022-03-22T08:47:04.359687Z","shell.execute_reply":"2022-03-22T08:47:04.883701Z"},"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-22T08:47:10.211422Z","iopub.execute_input":"2022-03-22T08:47:10.212164Z","iopub.status.idle":"2022-03-22T08:47:10.227118Z","shell.execute_reply.started":"2022-03-22T08:47:10.212099Z","shell.execute_reply":"2022-03-22T08:47:10.226243Z"},"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-22T08:47:37.369789Z","iopub.execute_input":"2022-03-22T08:47:37.370434Z","iopub.status.idle":"2022-03-22T08:48:50.912932Z","shell.execute_reply.started":"2022-03-22T08:47:37.370395Z","shell.execute_reply":"2022-03-22T08:48:50.911839Z"},"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-22T08:48:50.915188Z","iopub.execute_input":"2022-03-22T08:48:50.915489Z","iopub.status.idle":"2022-03-22T08:48:51.060919Z","shell.execute_reply.started":"2022-03-22T08:48:50.915461Z","shell.execute_reply":"2022-03-22T08:48:51.059976Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = []\nlabels = target_sorted['class_id'][0:1000]\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-22T08:48:51.062503Z","iopub.execute_input":"2022-03-22T08:48:51.062954Z","iopub.status.idle":"2022-03-22T08:48:51.070561Z","shell.execute_reply.started":"2022-03-22T08:48:51.062918Z","shell.execute_reply":"2022-03-22T08:48:51.069712Z"},"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-22T08:48:51.071887Z","iopub.execute_input":"2022-03-22T08:48:51.072205Z","iopub.status.idle":"2022-03-22T08:48:52.833781Z","shell.execute_reply.started":"2022-03-22T08:48:51.072164Z","shell.execute_reply":"2022-03-22T08:48:52.832523Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(imgs), len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:48:52.836584Z","iopub.execute_input":"2022-03-22T08:48:52.837053Z","iopub.status.idle":"2022-03-22T08:48:52.842635Z","shell.execute_reply.started":"2022-03-22T08:48:52.836987Z","shell.execute_reply":"2022-03-22T08:48:52.84168Z"},"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-22T08:55:43.235122Z","iopub.execute_input":"2022-03-22T08:55:43.235549Z","iopub.status.idle":"2022-03-22T08:55:44.253685Z","shell.execute_reply.started":"2022-03-22T08:55:43.235517Z","shell.execute_reply":"2022-03-22T08:55:44.252479Z"},"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-22T08:55:46.663Z","iopub.execute_input":"2022-03-22T08:55:46.663382Z","iopub.status.idle":"2022-03-22T08:55:47.177622Z","shell.execute_reply.started":"2022-03-22T08:55:46.663346Z","shell.execute_reply":"2022-03-22T08:55:47.176697Z"},"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-22T08:55:47.17896Z","iopub.execute_input":"2022-03-22T08:55:47.179387Z","iopub.status.idle":"2022-03-22T08:55:47.188038Z","shell.execute_reply.started":"2022-03-22T08:55:47.179355Z","shell.execute_reply":"2022-03-22T08:55:47.186945Z"},"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-22T08:55:50.822417Z","iopub.execute_input":"2022-03-22T08:55:50.82298Z","iopub.status.idle":"2022-03-22T08:55:51.45059Z","shell.execute_reply.started":"2022-03-22T08:55:50.822945Z","shell.execute_reply":"2022-03-22T08:55:51.449294Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_test.shape)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:55:53.302309Z","iopub.execute_input":"2022-03-22T08:55:53.302698Z","iopub.status.idle":"2022-03-22T08:55:53.310021Z","shell.execute_reply.started":"2022-03-22T08:55:53.302665Z","shell.execute_reply":"2022-03-22T08:55:53.308771Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_WIDTH=224\nIMG_HEIGHT=224\nIMG_DIM = (IMG_WIDTH, IMG_HEIGHT)\nBATCH_SIZE = 20\n#IMG_DIR = pathlib.Path('G:\\Github\\standford-dogs\\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-22T08:57:29.261914Z","iopub.execute_input":"2022-03-22T08:57:29.262602Z","iopub.status.idle":"2022-03-22T08:57:29.267409Z","shell.execute_reply.started":"2022-03-22T08:57:29.262551Z","shell.execute_reply":"2022-03-22T08:57:29.266498Z"},"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.2,\n                                             height_shift_range = 0.2,\n                                             zoom_range = 0.3,\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-22T08:57:32.742537Z","iopub.execute_input":"2022-03-22T08:57:32.74295Z","iopub.status.idle":"2022-03-22T08:57:32.750539Z","shell.execute_reply.started":"2022-03-22T08:57:32.742916Z","shell.execute_reply":"2022-03-22T08:57:32.749532Z"},"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-22T08:57:36.170521Z","iopub.execute_input":"2022-03-22T08:57:36.170936Z","iopub.status.idle":"2022-03-22T08:57:36.177921Z","shell.execute_reply.started":"2022-03-22T08:57:36.170903Z","shell.execute_reply":"2022-03-22T08:57:36.176733Z"},"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=20)\n\nvalidation_generator = data_generator_no_aug.flow(\n                                        X_test, Y_testHot,\n                                        batch_size=20)\n\n\n#nb_train = len(train_generator.filenames)\n#nb_val = len(validation_generator.filenames)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-22T08:57:39.20289Z","iopub.execute_input":"2022-03-22T08:57:39.203318Z","iopub.status.idle":"2022-03-22T08:57:39.610784Z","shell.execute_reply.started":"2022-03-22T08:57:39.203279Z","shell.execute_reply":"2022-03-22T08:57:39.609791Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nimport tensorflow.keras as keras\n\nvgg_base1 = VGG19(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT,IMG_WIDTH,3))","metadata":{"execution":{"iopub.status.busy":"2022-03-22T09:03:37.770315Z","iopub.execute_input":"2022-03-22T09:03:37.770811Z","iopub.status.idle":"2022-03-22T09:03:58.221863Z","shell.execute_reply.started":"2022-03-22T09:03:37.770769Z","shell.execute_reply":"2022-03-22T09:03:58.219429Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in vgg_base1.layers:\n    layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vgg_base1.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_base1)\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":{"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.003), \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=4,\n                        epochs = 200,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":"from tensorflow.keras.models import Model\nimport tensorflow.keras as keras\n\nvgg_base = VGG16(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT,IMG_WIDTH,3))\n\n# output = resnet.layers[-1].output\n# output = tf.keras.layers.Flatten()(output)\n# resnet = Model(resnet.input, output)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T09:01:09.602344Z","iopub.execute_input":"2022-03-22T09:01:09.603002Z","iopub.status.idle":"2022-03-22T09:01:29.92097Z","shell.execute_reply.started":"2022-03-22T09:01:09.602947Z","shell.execute_reply":"2022-03-22T09:01:29.916223Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in vgg_base.layers:\n    layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vgg_base.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_base)\nmodel.add(BatchNormalization())\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(1024, 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=4,\n                        epochs = 150,callbacks=[reduce_lr,early_stop],\n                          validation_steps=1,\n                        validation_data = validation_generator)","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)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-22T09:04:58.972769Z","iopub.execute_input":"2022-03-22T09:04:58.973171Z","iopub.status.idle":"2022-03-22T09:05:26.172784Z","shell.execute_reply.started":"2022-03-22T09:04:58.973138Z","shell.execute_reply":"2022-03-22T09:05:26.170132Z"},"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[-300:]","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[-300:]:\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='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-6,\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=4,\n                        epochs = 200,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":"# 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":{"trusted":true},"outputs":[],"execution_count":null}]}