{"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":"!pip install -Uq timm","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:37:15.477346Z","iopub.execute_input":"2022-11-10T10:37:15.477786Z","iopub.status.idle":"2022-11-10T10:37:26.646919Z","shell.execute_reply.started":"2022-11-10T10:37:15.477746Z","shell.execute_reply":"2022-11-10T10:37:26.645562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:18.2373Z","iopub.execute_input":"2022-11-13T09:33:18.237724Z","iopub.status.idle":"2022-11-13T09:33:19.571535Z","shell.execute_reply.started":"2022-11-13T09:33:18.237631Z","shell.execute_reply":"2022-11-13T09:33:19.570378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom glob import glob\nfrom pprint import pprint\nimport random\nimport cv2\nfrom joblib import Parallel, delayed\nfrom sklearn.metrics import accuracy_score\nfrom tqdm.notebook import tqdm\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom PIL import Image\nimport rasterio\nfrom sklearn.model_selection import train_test_split\n\n# import timm\nimport torch\nfrom torch import nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, utils\n\n\nfrom skimage.filters.rank import entropy\nfrom skimage.morphology import disk\nfrom skimage.color import rgb2hsv, rgb2gray, rgb2yuv\n\n# colors = ['#E7D5E8','#F9659B','#F69581','#F68FBB']\n# sns.palplot(sns.color_palette(colors))\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# Set Style\n# sns.set_style(\"whitegrid\")\n# sns.despine(left=True, bottom=True)\n\n# plt.rc('xtick',labelsize=11)\n# plt.rc('ytick',labelsize=11)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:22.888204Z","iopub.execute_input":"2022-11-13T09:33:22.888587Z","iopub.status.idle":"2022-11-13T09:33:24.31841Z","shell.execute_reply.started":"2022-11-13T09:33:22.888553Z","shell.execute_reply":"2022-11-13T09:33:24.317419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config = dict(\n    orig_train_dir = os.path.abspath('../input/mayo-clinic-strip-ai/train'),\n    orig_test_dir = os.path.abspath('../input/mayo-clinic-strip-ai/test'),\n    orig_other_dir = os.path.abspath('../input/mayo-clinic-strip-ai/other'),\n    orig_train_csv_path =  os.path.abspath('../input/mayo-clinic-strip-ai/train.csv'),\n    orig_test_csv_path = os.path.abspath('../input/mayo-clinic-strip-ai/test.csv'),\n    orig_sample_submission_path = os.path.abspath('../input/mayo-clinic-strip-ai/sample_submission.csv'),\n    orig_other_csv_path = os.path.abspath('../input/mayo-clinic-strip-ai/other.csv'),\n    \n    seed = 42,\n    device = 'cuda:0' if torch.cuda.is_available() else 'cpu',\n    \n    batch_size = 64,\n    num_epochs=5,\n    lr = 0.0003,\n    \n    use_wandb=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:24.320268Z","iopub.execute_input":"2022-11-13T09:33:24.321159Z","iopub.status.idle":"2022-11-13T09:33:24.350639Z","shell.execute_reply.started":"2022-11-13T09:33:24.321118Z","shell.execute_reply":"2022-11-13T09:33:24.349878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pprint(config)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:24.352104Z","iopub.execute_input":"2022-11-13T09:33:24.352813Z","iopub.status.idle":"2022-11-13T09:33:24.364227Z","shell.execute_reply.started":"2022-11-13T09:33:24.352773Z","shell.execute_reply":"2022-11-13T09:33:24.363382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed=42):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_everything(config['seed'])","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:24.367077Z","iopub.execute_input":"2022-11-13T09:33:24.367432Z","iopub.status.idle":"2022-11-13T09:33:24.380854Z","shell.execute_reply.started":"2022-11-13T09:33:24.367396Z","shell.execute_reply":"2022-11-13T09:33:24.379968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(config['orig_train_csv_path'])\ndisplay(train_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:24.382398Z","iopub.execute_input":"2022-11-13T09:33:24.382755Z","iopub.status.idle":"2022-11-13T09:33:24.403556Z","shell.execute_reply.started":"2022-11-13T09:33:24.382721Z","shell.execute_reply":"2022-11-13T09:33:24.402492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Contains annotations for images in the `train/` folder\n#### Coloums:\n- `image_id`: A unique identifier for this instance having the form `{patient_id}_{image_num}`. Corresponds to the image `{image_id}.tif`\n- `center_id`: Identifies the medical center where the slide was obtained.\n- `patient_id`: Identifies the patient from whom the slide was obtained.\n- `image_num`: Enumerates images of clots obtained from the same patient.\n- `label`: The etiology of the clot, either `CE` or `LAA`. This field is the classification target.","metadata":{}},{"cell_type":"code","source":"print(f'Number of Training Samples: {train_data.shape[0]}')\nprint(f'Are there any missing values?: {train_data.isnull().values.any()}')                                                ","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:24.405171Z","iopub.execute_input":"2022-11-13T09:33:24.405524Z","iopub.status.idle":"2022-11-13T09:33:24.411657Z","shell.execute_reply.started":"2022-11-13T09:33:24.405488Z","shell.execute_reply":"2022-11-13T09:33:24.410574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(config['orig_test_csv_path'])\ndisplay(test_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:24.413188Z","iopub.execute_input":"2022-11-13T09:33:24.413813Z","iopub.status.idle":"2022-11-13T09:33:24.428821Z","shell.execute_reply.started":"2022-11-13T09:33:24.413775Z","shell.execute_reply":"2022-11-13T09:33:24.428133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = pd.read_csv(config['orig_sample_submission_path'])\ndisplay(submission_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:26.437273Z","iopub.execute_input":"2022-11-13T09:33:26.437655Z","iopub.status.idle":"2022-11-13T09:33:26.452946Z","shell.execute_reply.started":"2022-11-13T09:33:26.43762Z","shell.execute_reply":"2022-11-13T09:33:26.451975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"supplementary_data = pd.read_csv(config['orig_other_csv_path'])\ndisplay(supplementary_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:27.103447Z","iopub.execute_input":"2022-11-13T09:33:27.103813Z","iopub.status.idle":"2022-11-13T09:33:27.119459Z","shell.execute_reply.started":"2022-11-13T09:33:27.103782Z","shell.execute_reply":"2022-11-13T09:33:27.118461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_to_label, label_to_id = {}, {}\n\nfor id, label in enumerate(train_data['label'].unique()):\n    label_to_id[label] = id\n    id_to_label[id] = label\n\ntrain_data['label'] = train_data['label'].replace(label_to_id)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:27.875017Z","iopub.execute_input":"2022-11-13T09:33:27.875409Z","iopub.status.idle":"2022-11-13T09:33:27.883996Z","shell.execute_reply.started":"2022-11-13T09:33:27.875377Z","shell.execute_reply":"2022-11-13T09:33:27.882827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 🔎 Exploratory data analysis","metadata":{}},{"cell_type":"code","source":"type_distribution = train_data['label'].value_counts()\n# plt.figure(figsize=(20, 5))\nsns.barplot(x=type_distribution.values, y=list(id_to_label.values()))\nplt.title('Class Distribution', fontsize=25)\nplt.xlabel('Frequency', fontsize=25)\nplt.ylabel('Label', fontsize=25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:29.347803Z","iopub.execute_input":"2022-11-13T09:33:29.349048Z","iopub.status.idle":"2022-11-13T09:33:29.563974Z","shell.execute_reply.started":"2022-11-13T09:33:29.349004Z","shell.execute_reply":"2022-11-13T09:33:29.562369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"center_distribution = train_data['center_id'].value_counts().sort_values(ignore_index=True)\n# plt.figure(figsize=(20, 5))\nsns.barplot(x=center_distribution.index, y=center_distribution.values)\nplt.title('CenterID Distribution', fontsize=25)\nplt.xlabel('Center ID', fontsize=25)\nplt.ylabel('Frequency', fontsize=25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:31.14084Z","iopub.execute_input":"2022-11-13T09:33:31.141761Z","iopub.status.idle":"2022-11-13T09:33:31.373763Z","shell.execute_reply.started":"2022-11-13T09:33:31.141726Z","shell.execute_reply":"2022-11-13T09:33:31.372867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 🔎 Visualization","metadata":{}},{"cell_type":"code","source":"positive_df = pd.DataFrame(os.listdir('../input/strip-ai-background-clot/positive'), columns=['filename'])\npositive_df['filepath'] = positive_df['filename'].apply(lambda x: os.path.join('../input/strip-ai-background-clot/positive', x))\npositive_df['label'] = 1\npositive_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:33.302493Z","iopub.execute_input":"2022-11-13T09:33:33.302939Z","iopub.status.idle":"2022-11-13T09:33:33.341157Z","shell.execute_reply.started":"2022-11-13T09:33:33.302884Z","shell.execute_reply":"2022-11-13T09:33:33.339362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"negative_df = pd.DataFrame(os.listdir('../input/strip-ai-background-clot/negative'), columns=['filename'])\nnegative_df['filepath'] = negative_df['filename'].apply(lambda x: os.path.join('../input/strip-ai-background-clot/negative', x))\nnegative_df['label'] = 0\nnegative_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:33.413141Z","iopub.execute_input":"2022-11-13T09:33:33.413524Z","iopub.status.idle":"2022-11-13T09:33:33.449978Z","shell.execute_reply.started":"2022-11-13T09:33:33.413491Z","shell.execute_reply":"2022-11-13T09:33:33.448983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.concat([positive_df, negative_df]).sample(frac=1)\nprint(data.shape)\ndisplay(data.head(10))","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:35.197444Z","iopub.execute_input":"2022-11-13T09:33:35.197816Z","iopub.status.idle":"2022-11-13T09:33:35.217497Z","shell.execute_reply.started":"2022-11-13T09:33:35.197782Z","shell.execute_reply":"2022-11-13T09:33:35.216401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_background(filepath, threshold=10, display=False, ax=None):\n    image = cv2.imread(filepath)\n    h, w, _ = image.shape\n    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]\n\n    pixels = cv2.countNonZero(thresh)\n    ratio = (pixels/(h * w)) * 100\n    #print('Pixel ratio: {:.2f}%'.format(ratio))\n    roi = 0\n    if ratio >= threshold:\n        roi = 1\n    \n    if display and ax is not None:\n        ax.imshow(thresh)\n        ax.set_title('Mostly Background' if not roi else 'Contains region of interest', fontsize=14)\n        ax.axis('off')\n    \n    return roi","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:36.42978Z","iopub.execute_input":"2022-11-13T09:33:36.43123Z","iopub.status.idle":"2022-11-13T09:33:36.439138Z","shell.execute_reply.started":"2022-11-13T09:33:36.431182Z","shell.execute_reply":"2022-11-13T09:33:36.438143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def threshold_checker(filepath):\n    shawl_image = rgb2gray(imread(filepath))\n    \n    scaled_entropy = shawl_gray / shawl_gray.max()\n    entropy_image = entropy(scaled_entropy, disk(6))\n#     print(entropy_image)\n    scaled_entropy = entropy_image / entropy_image.max()\n    \n    mask = scaled_entropy > 0.05\n    mask_int = mask.astype(int)\n#     image = cv2.imread(filepath)\n#     thresholds =  np.arange(0.1,1.1,0.1)\n#     image_gray = rgb2gray(image)\n#     entropy_image = entropy(image_gray, disk(6))\n#     scaled_entropy = entropy_image / entropy_image.max()\n#     print(scaled_entropy)\n    \n    roi = 0\n    if np.average(mask_int)>=0.01:\n        roi = 1\n    return roi","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:37.501488Z","iopub.execute_input":"2022-11-13T09:33:37.502394Z","iopub.status.idle":"2022-11-13T09:33:37.50924Z","shell.execute_reply.started":"2022-11-13T09:33:37.502343Z","shell.execute_reply":"2022-11-13T09:33:37.508002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import skimage.measure\nfrom skimage.io import imread, imshow\ndef threshold_checker1(filepath):\n    shawl_gray = rgb2gray(imread(filepath))\n    scaled_entropy = shawl_gray / shawl_gray.max()\n#     scaled_entropy = shawl_gray / shawl_gray.max()\n#     entropy_image = entropy(scaled_entropy, disk(3))\n#     scaled_entropy = entropy_image / entropy_image.max()\n   \n#     mask = scaled_entropy > 0.4\n#     mask_int = mask.astype(int)\n#     image=imread(filepath)\n    entropy = skimage.measure.shannon_entropy(scaled_entropy)\n    roi = 1\n    if entropy<=1.5:\n        roi = 0\n    return roi","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:38.746621Z","iopub.execute_input":"2022-11-13T09:33:38.747333Z","iopub.status.idle":"2022-11-13T09:33:38.834972Z","shell.execute_reply.started":"2022-11-13T09:33:38.747295Z","shell.execute_reply":"2022-11-13T09:33:38.834061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=2, ncols=6, figsize=(20,6))\nplt.suptitle(\"Samples\", fontsize = 16)\n\nfor i in range(0, 2*6):\n    image = cv2.imread(data['filepath'].values[i])\n    \n    x = i // 6\n    y = i % 6\n    axes[x, y].imshow(image, cmap=plt.cm.bone)\n    #axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:39.843363Z","iopub.execute_input":"2022-11-13T09:33:39.844066Z","iopub.status.idle":"2022-11-13T09:33:42.518671Z","shell.execute_reply.started":"2022-11-13T09:33:39.844029Z","shell.execute_reply":"2022-11-13T09:33:42.517823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['filepath'].values[np.random.randint(0, len(data))]","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:42.520152Z","iopub.execute_input":"2022-11-13T09:33:42.520463Z","iopub.status.idle":"2022-11-13T09:33:42.529956Z","shell.execute_reply.started":"2022-11-13T09:33:42.520433Z","shell.execute_reply":"2022-11-13T09:33:42.528882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fpath = '../input/strip-ai-background-clot/positive/008e5c_0-imgslice.25.3.jpg'","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:43.030361Z","iopub.execute_input":"2022-11-13T09:33:43.030742Z","iopub.status.idle":"2022-11-13T09:33:43.035025Z","shell.execute_reply.started":"2022-11-13T09:33:43.03071Z","shell.execute_reply":"2022-11-13T09:33:43.034047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from skimage.io import imread, imshow\n# shawl_gray = rgb2gray(imread(fpath))\n# plt.figure(num=None, figsize=(8, 6), dpi=80)\n# imshow(shawl_gray);","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:43.869989Z","iopub.execute_input":"2022-11-13T09:33:43.871196Z","iopub.status.idle":"2022-11-13T09:33:43.876366Z","shell.execute_reply.started":"2022-11-13T09:33:43.871141Z","shell.execute_reply":"2022-11-13T09:33:43.875089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# entropy_image = entropy(shawl_gray, disk(5))\n# print(entropy_image)\n# plt.figure(num=None, figsize=(8, 6), dpi=80)\n# imshow(entropy_image, cmap = 'magma');","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:46.167735Z","iopub.execute_input":"2022-11-13T09:33:46.16813Z","iopub.status.idle":"2022-11-13T09:33:46.172851Z","shell.execute_reply.started":"2022-11-13T09:33:46.168093Z","shell.execute_reply":"2022-11-13T09:33:46.171807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaled_entropy = shawl_gray / shawl_gray.max()\n# entropy_image = entropy(scaled_entropy, disk(6))\n# scaled_entropy = entropy_image / entropy_image.max()\n# # print(scaled_entropy)\n# # print(scaled_entropy.shape)\n# print(np.average(scaled_entropy))\n# # mask = scaled_entropy > 0.5\n# # # print(mask.sum())\n# # mask_int = mask.astype(int)\n# # print(mask_int)\n# # plt.figure(num=None, figsize=(8, 6), dpi=80)\n# # imshow(mask_int, cmap = 'gray');","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:46.175165Z","iopub.execute_input":"2022-11-13T09:33:46.175953Z","iopub.status.idle":"2022-11-13T09:33:46.1829Z","shell.execute_reply.started":"2022-11-13T09:33:46.175891Z","shell.execute_reply":"2022-11-13T09:33:46.182028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaled_entropy = shawl_gray / shawl_gray.max()\n# entropy_image = entropy(scaled_entropy, disk(6))\n# scaled_entropy = entropy_image / entropy_image.max()\n# mask = scaled_entropy > 0.8\n# plt.figure(num=None, figsize=(8, 6), dpi=80)\n# imshow(shawl_gray * mask, cmap = 'gray');","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:46.185866Z","iopub.execute_input":"2022-11-13T09:33:46.186175Z","iopub.status.idle":"2022-11-13T09:33:46.192427Z","shell.execute_reply.started":"2022-11-13T09:33:46.186149Z","shell.execute_reply":"2022-11-13T09:33:46.191569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image = cv2.imread(filepath)\n# thresholds =  np.arange(0.1,1.1,0.1)\n# image_gray = rgb2gray(image)\n# entropy_image = entropy(image_gray, disk(6))\n# scaled_entropy = entropy_image / entropy_image.max()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:47.534294Z","iopub.execute_input":"2022-11-13T09:33:47.534661Z","iopub.status.idle":"2022-11-13T09:33:47.539566Z","shell.execute_reply.started":"2022-11-13T09:33:47.53463Z","shell.execute_reply":"2022-11-13T09:33:47.538593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, axes = plt.subplots(nrows=5, ncols=8, figsize=(40,25))\n\n# for i in range(0, 5*8):\n#     x = i // 8\n#     y = i % 8\n#     threshold_checker(data['filepath'].values[np.random.randint(0, len(data))])","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:50.24087Z","iopub.execute_input":"2022-11-13T09:33:50.241985Z","iopub.status.idle":"2022-11-13T09:33:50.246996Z","shell.execute_reply.started":"2022-11-13T09:33:50.241913Z","shell.execute_reply":"2022-11-13T09:33:50.245767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preds = Parallel(n_jobs=-1)(delayed(check_background)(data['filepath'].values[i], 5) for i in tqdm(range(len(data))))\n# print(f'Accuracy: {accuracy_score(preds, data[\"label\"].values)}')","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:51.158317Z","iopub.execute_input":"2022-11-13T09:33:51.158913Z","iopub.status.idle":"2022-11-13T09:33:51.163814Z","shell.execute_reply.started":"2022-11-13T09:33:51.158841Z","shell.execute_reply":"2022-11-13T09:33:51.162674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = pd.DataFrame(glob('../input/mayo-clinic-1024*[!test]/train/*.jpg'), columns=['filepath'])\n\n\n# df['image_id'] = df['filepath'].apply(lambda x: x.split('/')[-1].split('-')[0])\n# df = df.merge(train_data, on='image_id', how='left')\n# df = df.drop(['center_id', 'image_num', 'patient_id'], axis=1)\n\n# df = df[~df['image_id'].isin([\"008e5c_0\",\"00c058_0\",\"01adc5_0\"])]\n# df = df.drop(['image_id'], axis=1)\n# df","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:51.251002Z","iopub.execute_input":"2022-11-13T09:33:51.251754Z","iopub.status.idle":"2022-11-13T09:33:51.258207Z","shell.execute_reply.started":"2022-11-13T09:33:51.251713Z","shell.execute_reply":"2022-11-13T09:33:51.257052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# is_background = Parallel(n_jobs=-1)(delayed(threshold_checker1)(df['filepath'].values[i]) for i in tqdm(range(len(df)//10, 2*len(df)//10)))\n# df['is_background'] = is_background\n# df.to_csv('dataset.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:54.269645Z","iopub.execute_input":"2022-11-13T09:33:54.270038Z","iopub.status.idle":"2022-11-13T09:33:54.274247Z","shell.execute_reply.started":"2022-11-13T09:33:54.270003Z","shell.execute_reply":"2022-11-13T09:33:54.273268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data = pd.read_csv('../input/stroke-blood-clot-classification/dataset.csv')\n# display(data.head())\n# # data = df","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:54.854852Z","iopub.execute_input":"2022-11-13T09:33:54.855981Z","iopub.status.idle":"2022-11-13T09:33:54.860655Z","shell.execute_reply.started":"2022-11-13T09:33:54.855906Z","shell.execute_reply":"2022-11-13T09:33:54.859457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"part1 = pd.read_csv('../input/filtereddata/part1.csv')\npart2 = pd.read_csv('../input/filtereddata/part2.csv')\npart3 = pd.read_csv('../input/filtereddata/part3.csv')\ndata = pd.concat([part1, part2, part3])","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:55.361183Z","iopub.execute_input":"2022-11-13T09:33:55.361561Z","iopub.status.idle":"2022-11-13T09:33:56.002662Z","shell.execute_reply.started":"2022-11-13T09:33:55.361529Z","shell.execute_reply":"2022-11-13T09:33:56.001643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:57.945449Z","iopub.execute_input":"2022-11-13T09:33:57.946527Z","iopub.status.idle":"2022-11-13T09:33:57.95934Z","shell.execute_reply.started":"2022-11-13T09:33:57.946482Z","shell.execute_reply":"2022-11-13T09:33:57.958234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Percentage of Background Images in sliced dataset: {:.3f}'.format(data[data[\"is_background\"] == 0].count()[0] / len(data) * 100))","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:33:59.145938Z","iopub.execute_input":"2022-11-13T09:33:59.146569Z","iopub.status.idle":"2022-11-13T09:33:59.206118Z","shell.execute_reply.started":"2022-11-13T09:33:59.146532Z","shell.execute_reply":"2022-11-13T09:33:59.205094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"data['filepath'][4]","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:34:01.004017Z","iopub.execute_input":"2022-11-13T09:34:01.00472Z","iopub.status.idle":"2022-11-13T09:34:01.032966Z","shell.execute_reply.started":"2022-11-13T09:34:01.004676Z","shell.execute_reply":"2022-11-13T09:34:01.031794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['label'][4]","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:34:02.02766Z","iopub.execute_input":"2022-11-13T09:34:02.028467Z","iopub.status.idle":"2022-11-13T09:34:02.039393Z","shell.execute_reply.started":"2022-11-13T09:34:02.028418Z","shell.execute_reply":"2022-11-13T09:34:02.038281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lets visualize some images for sanity check if is_background are correct or not","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, axes = plt.subplots(nrows=3, ncols=6, figsize=(40,25))\nplt.suptitle(\"Background patches\", fontsize = 16)\n\nbackground_images = data[data[\"is_background\"] == 0]\n\nfor i in range(0, 3*6):\n    x = i // 6\n    y = i % 6\n    image = cv2.imread(background_images.sample(1)['filepath'].values[0])\n    axes[x, y].imshow(image, cmap=plt.cm.bone)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:34:03.915898Z","iopub.execute_input":"2022-11-13T09:34:03.91662Z","iopub.status.idle":"2022-11-13T09:34:08.261972Z","shell.execute_reply.started":"2022-11-13T09:34:03.916582Z","shell.execute_reply":"2022-11-13T09:34:08.260984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=3, ncols=6, figsize=(40,25))\nplt.suptitle(\"Non-Background patches\", fontsize = 16)\n\nroi_images = data[data[\"is_background\"] == 1]\n\nfor i in range(0, 3*6):\n    x = i // 6\n    y = i % 6\n    image = cv2.imread(roi_images.sample(1)['filepath'].values[0])\n    axes[x, y].imshow(image, cmap=plt.cm.bone)\n    axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:34:08.263891Z","iopub.execute_input":"2022-11-13T09:34:08.264534Z","iopub.status.idle":"2022-11-13T09:34:11.98586Z","shell.execute_reply.started":"2022-11-13T09:34:08.264497Z","shell.execute_reply":"2022-11-13T09:34:11.984488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.loc[data['is_background'] != 0]\nprint(data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:34:11.98808Z","iopub.execute_input":"2022-11-13T09:34:11.988876Z","iopub.status.idle":"2022-11-13T09:34:12.00652Z","shell.execute_reply.started":"2022-11-13T09:34:11.988835Z","shell.execute_reply":"2022-11-13T09:34:12.005732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type_distribution = data['label'].value_counts()\nplt.figure(figsize=(20, 5))\nsns.barplot(x=type_distribution.values, y=list(id_to_label.values()))\nplt.title('Class Distribution', fontsize=25)\nplt.xlabel('Frequency', fontsize=25)\nplt.ylabel('Label', fontsize=25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:34:12.008838Z","iopub.execute_input":"2022-11-13T09:34:12.009657Z","iopub.status.idle":"2022-11-13T09:34:12.217065Z","shell.execute_reply.started":"2022-11-13T09:34:12.009606Z","shell.execute_reply":"2022-11-13T09:34:12.215937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ClinicDataset(Dataset):\n    def __init__(self, df, transforms=None, is_test=False):\n        self.df = df\n        self.transforms = transforms\n        self.is_test = is_test\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        image = Image.open(self.df['filepath'].values[idx])\n\n        if transforms is not None:\n            image = self.transforms(image)\n        \n        if self.is_test:\n            return image\n        \n        label = torch.tensor(self.df['label'].values[idx], dtype=torch.float)\n\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:35:45.950807Z","iopub.execute_input":"2022-11-13T09:35:45.951774Z","iopub.status.idle":"2022-11-13T09:35:45.959676Z","shell.execute_reply.started":"2022-11-13T09:35:45.951721Z","shell.execute_reply":"2022-11-13T09:35:45.958648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.models as models\n# net = models.efficientnet_b4(pretrained=False)\n# net.classifier[1] = nn.Linear(in_features=1792, out_features=2, bias=True)\n# net.classifier[1] = nn.Linear(in_features=1280, out_features=2, bias=True)\n# num_ftrs = net.fc.in_features\n# net.fc = nn.Linear(num_ftrs, 2)\nnet = models.mobilenet_v2(pretrained=True)\nnet.classifier[1] = nn.Linear(1280, 2)\n# net.fc = nn.Linear(512, 2)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:35:46.926738Z","iopub.execute_input":"2022-11-13T09:35:46.927121Z","iopub.status.idle":"2022-11-13T09:35:47.031375Z","shell.execute_reply.started":"2022-11-13T09:35:46.927091Z","shell.execute_reply":"2022-11-13T09:35:47.030371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.AdamW(net.parameters(), lr=config['lr'])\nscheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=config['num_epochs'])\n# loss_fn = torch.nn.BCELoss()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:35:55.239981Z","iopub.execute_input":"2022-11-13T09:35:55.240413Z","iopub.status.idle":"2022-11-13T09:35:55.248454Z","shell.execute_reply.started":"2022-11-13T09:35:55.240378Z","shell.execute_reply":"2022-11-13T09:35:55.247527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda'\nnet = net.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:35:57.655864Z","iopub.execute_input":"2022-11-13T09:35:57.65649Z","iopub.status.idle":"2022-11-13T09:35:59.175374Z","shell.execute_reply.started":"2022-11-13T09:35:57.656455Z","shell.execute_reply":"2022-11-13T09:35:59.174361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(os.listdir('../input/mayo-clinic-1024x1024-sliced-jpg-test-set/test'), columns=['filename'])\ntest_df['filepath'] = test_df['filename'].apply(lambda x: os.path.join('../input/mayo-clinic-1024x1024-sliced-jpg-test-set/test', x))\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:00.621294Z","iopub.execute_input":"2022-11-13T09:36:00.62166Z","iopub.status.idle":"2022-11-13T09:36:00.641492Z","shell.execute_reply.started":"2022-11-13T09:36:00.621627Z","shell.execute_reply":"2022-11-13T09:36:00.640587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['filepath'][0]","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:01.525593Z","iopub.execute_input":"2022-11-13T09:36:01.526685Z","iopub.status.idle":"2022-11-13T09:36:01.5348Z","shell.execute_reply.started":"2022-11-13T09:36:01.526641Z","shell.execute_reply":"2022-11-13T09:36:01.533602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ClinicTestDataset(Dataset):\n    def __init__(self, df, transforms=None, is_test=False):\n        self.df = df\n        self.transforms = transforms\n        self.is_test = is_test\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        image = Image.open(self.df['filepath'].values[idx])\n        name = self.df['filename'].values[idx][:6]\n        label = torch.tensor(self.df['label'].values[idx], dtype=torch.int64)\n\n        if transforms is not None:\n            image = self.transforms(image)\n        \n        if self.is_test:\n            return image\n\n        return image, name, label","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:02.783379Z","iopub.execute_input":"2022-11-13T09:36:02.783745Z","iopub.status.idle":"2022-11-13T09:36:02.792627Z","shell.execute_reply.started":"2022-11-13T09:36:02.783713Z","shell.execute_reply":"2022-11-13T09:36:02.791569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(data, test_size=0.2)\n\nprint(train_df.shape, valid_df.shape)\n\ndata_transforms = {\n    'train': transforms.Compose([\n        transforms.Resize((224, 224)),\n#         transforms.RandomHorizontalFlip(),\n#         transforms.CenterCrop(10),\n#         transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),\n        transforms.ToTensor(),\n        \n    ]),\n    'valid': transforms.Compose([\n        transforms.Resize((224, 224)),\n        transforms.ToTensor(),\n    ])\n}\n\ntrain_dataset = ClinicDataset(train_df, transforms=data_transforms['train'])\nvalid_dataset = ClinicDataset(valid_df, transforms=data_transforms['valid'])\ntest_dataset = ClinicTestDataset(test_df, transforms=data_transforms['valid'])\n\ntrain_dataloader = DataLoader(train_dataset, batch_size=config['batch_size'], shuffle=True, drop_last=True)\nvalid_dataloader = DataLoader(valid_dataset, batch_size=config['batch_size'], shuffle=True, drop_last=True)\ntest_dataloader = DataLoader(test_dataset, batch_size=config['batch_size'], shuffle=True, drop_last=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:02.845338Z","iopub.execute_input":"2022-11-13T09:36:02.847253Z","iopub.status.idle":"2022-11-13T09:36:02.869871Z","shell.execute_reply.started":"2022-11-13T09:36:02.847222Z","shell.execute_reply":"2022-11-13T09:36:02.868845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = []\ntrain_accuracies = []","metadata":{"execution":{"iopub.status.busy":"2022-11-08T09:34:59.654777Z","iopub.execute_input":"2022-11-08T09:34:59.655891Z","iopub.status.idle":"2022-11-08T09:34:59.661598Z","shell.execute_reply.started":"2022-11-08T09:34:59.655845Z","shell.execute_reply":"2022-11-08T09:34:59.660105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(epoch):\n    print('\\nEpoch: %d' % epoch)\n    global net\n    net.train()\n    train_loss = 0\n    train_correct = 0\n    train_total = 0\n    for batch_idx, (inputs, targets) in enumerate(tqdm(train_dataloader)):\n#         print(inputs.shape)\n#         print(inputs)\n#         print(targets.shape)\n#         print(targets)\n        targets = targets.type(torch. int64)\n        inputs, targets = inputs.to(device), targets.to(device)\n        optimizer.zero_grad()\n        outputs = net(inputs)\n        loss = criterion(outputs, targets)\n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n        _, predicted = outputs.max(1)\n        train_total += targets.size(0)\n        train_correct += predicted.eq(targets).sum().item()\n    train_losses.append(train_loss/(batch_idx+1))\n    train_accuracies.append(100. * train_correct/train_total)\n    print('train loss:', train_losses[-1])\n    print('train Accuracy:' , train_accuracies[-1])\n        # progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'\n        #              % (train_loss/(batch_idx+1), 100.*train_correct/train_total, train_correct, train_total))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T09:35:00.05481Z","iopub.execute_input":"2022-11-08T09:35:00.055166Z","iopub.status.idle":"2022-11-08T09:35:00.064466Z","shell.execute_reply.started":"2022-11-08T09:35:00.055135Z","shell.execute_reply":"2022-11-08T09:35:00.063369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_losses = []\nval_accuracies = []","metadata":{"execution":{"iopub.status.busy":"2022-11-08T09:35:00.555953Z","iopub.execute_input":"2022-11-08T09:35:00.556417Z","iopub.status.idle":"2022-11-08T09:35:00.561642Z","shell.execute_reply.started":"2022-11-08T09:35:00.556384Z","shell.execute_reply":"2022-11-08T09:35:00.560308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def val():\n    # print('\\nEpoch: %d' % epoch)\n    global net\n    net.eval()\n    val_loss = 0\n    val_correct = 0\n    val_total = 0\n    for batch_idx, (inputs, targets) in enumerate(tqdm(valid_dataloader)):\n        # print(inputs.shape)\n        # print(inputs)\n        # print(targets.shape)\n        # print(targets)\n        targets = targets.type(torch. int64)\n        inputs, targets = inputs.to(device), targets.to(device)\n        # optimizer.zero_grad()\n        outputs = net(inputs)\n        loss = criterion(outputs, targets)\n        # loss.backward()\n        # optimizer.step()\n\n        val_loss += loss.item()\n        _, predicted = outputs.max(1)\n        val_total += targets.size(0)\n        val_correct += predicted.eq(targets).sum().item()\n    val_losses.append(val_loss/(batch_idx+1))\n    val_accuracies.append(100. * val_correct/val_total)\n    print('val loss:', val_losses[-1])\n    print('val Accuracy:' , val_accuracies[-1])\n        # progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'\n        #              % (train_loss/(batch_idx+1), 100.*train_correct/train_total, train_correct, train_total))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T09:35:00.940961Z","iopub.execute_input":"2022-11-08T09:35:00.941642Z","iopub.status.idle":"2022-11-08T09:35:00.950401Z","shell.execute_reply.started":"2022-11-08T09:35:00.941609Z","shell.execute_reply":"2022-11-08T09:35:00.949314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import time\n# for epoch in range(0, config['num_epochs']):\n#     t1 = time.time()\n#     train(epoch)\n#     t2 = time.time()\n# #     print(\"Epoch training time: \", t2-t1)\n#     t3 = time.time()\n#     val()\n#     t4 = time.time()\n#     torch.save(net.state_dict(), 'ckpt-{}.pth'.format(epoch))\n# #     print(\"Validation time: \", t4-t3)\n#     scheduler.step()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = models.mobilenet_v2()\nnew_model.classifier[1] = nn.Linear(1280, 2)\nnew_model.load_state_dict(torch.load('../input/mobilenetmodel/ckpt-4.pth'))","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:11.660668Z","iopub.execute_input":"2022-11-13T09:36:11.661251Z","iopub.status.idle":"2022-11-13T09:36:11.818632Z","shell.execute_reply.started":"2022-11-13T09:36:11.661206Z","shell.execute_reply":"2022-11-13T09:36:11.81766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load('../input/mobilenetmodel/ckpt-4.pth')","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:13.666872Z","iopub.execute_input":"2022-11-13T09:36:13.67036Z","iopub.status.idle":"2022-11-13T09:36:13.727496Z","shell.execute_reply.started":"2022-11-13T09:36:13.670308Z","shell.execute_reply":"2022-11-13T09:36:13.726554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net.load_state_dict(checkpoint)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:14.758388Z","iopub.execute_input":"2022-11-13T09:36:14.758855Z","iopub.status.idle":"2022-11-13T09:36:14.796053Z","shell.execute_reply.started":"2022-11-13T09:36:14.758813Z","shell.execute_reply":"2022-11-13T09:36:14.794978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:15.69213Z","iopub.execute_input":"2022-11-13T09:36:15.692538Z","iopub.status.idle":"2022-11-13T09:36:15.697731Z","shell.execute_reply.started":"2022-11-13T09:36:15.692503Z","shell.execute_reply":"2022-11-13T09:36:15.696197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_losses = []\ntest_accuracies = []","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:18.185016Z","iopub.execute_input":"2022-11-13T09:36:18.18569Z","iopub.status.idle":"2022-11-13T09:36:18.191104Z","shell.execute_reply.started":"2022-11-13T09:36:18.185654Z","shell.execute_reply":"2022-11-13T09:36:18.190101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:18.328976Z","iopub.execute_input":"2022-11-13T09:36:18.329635Z","iopub.status.idle":"2022-11-13T09:36:18.336586Z","shell.execute_reply.started":"2022-11-13T09:36:18.329601Z","shell.execute_reply":"2022-11-13T09:36:18.335627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['filepath']","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:18.500748Z","iopub.execute_input":"2022-11-13T09:36:18.501087Z","iopub.status.idle":"2022-11-13T09:36:18.510952Z","shell.execute_reply.started":"2022-11-13T09:36:18.501058Z","shell.execute_reply":"2022-11-13T09:36:18.509827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:21.970746Z","iopub.execute_input":"2022-11-13T09:36:21.97145Z","iopub.status.idle":"2022-11-13T09:36:21.983469Z","shell.execute_reply.started":"2022-11-13T09:36:21.971411Z","shell.execute_reply":"2022-11-13T09:36:21.982717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_background = Parallel(n_jobs=-1)(delayed(threshold_checker1)(test_df['filepath'].values[i]) for i in tqdm(range(len(test_df))))","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:36:24.125407Z","iopub.execute_input":"2022-11-13T09:36:24.125784Z","iopub.status.idle":"2022-11-13T09:38:27.426911Z","shell.execute_reply.started":"2022-11-13T09:36:24.125745Z","shell.execute_reply":"2022-11-13T09:38:27.425772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# is_background = Parallel(n_jobs=-1)(delayed(threshold_checker1)(test_df['filepath'].values[i]) for i in tqdm(range(len(test_df)))\ntest_df['is_background'] = is_background\ntest_df.to_csv('test_dataset.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:27.429835Z","iopub.execute_input":"2022-11-13T09:38:27.430587Z","iopub.status.idle":"2022-11-13T09:38:27.447274Z","shell.execute_reply.started":"2022-11-13T09:38:27.430542Z","shell.execute_reply":"2022-11-13T09:38:27.446417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:39.909664Z","iopub.execute_input":"2022-11-13T09:38:39.910049Z","iopub.status.idle":"2022-11-13T09:38:39.921611Z","shell.execute_reply.started":"2022-11-13T09:38:39.910017Z","shell.execute_reply":"2022-11-13T09:38:39.920357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data = {'008e5c': 0, '01adc5':1, '00c058':1}\ntest_df['label'] = test_df['filename'].apply(lambda x: label_data[x[:6]])","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:41.258574Z","iopub.execute_input":"2022-11-13T09:38:41.258966Z","iopub.status.idle":"2022-11-13T09:38:41.266967Z","shell.execute_reply.started":"2022-11-13T09:38:41.25891Z","shell.execute_reply":"2022-11-13T09:38:41.265721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[test_df[\"is_background\"]==1]","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:42.281437Z","iopub.execute_input":"2022-11-13T09:38:42.281798Z","iopub.status.idle":"2022-11-13T09:38:42.299802Z","shell.execute_reply.started":"2022-11-13T09:38:42.281763Z","shell.execute_reply":"2022-11-13T09:38:42.298901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = ClinicTestDataset(test_df[test_df[\"is_background\"]==1], transforms=data_transforms['valid'])","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:43.063513Z","iopub.execute_input":"2022-11-13T09:38:43.063912Z","iopub.status.idle":"2022-11-13T09:38:43.070224Z","shell.execute_reply.started":"2022-11-13T09:38:43.063875Z","shell.execute_reply":"2022-11-13T09:38:43.06917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:45.124231Z","iopub.execute_input":"2022-11-13T09:38:45.124592Z","iopub.status.idle":"2022-11-13T09:38:45.131724Z","shell.execute_reply.started":"2022-11-13T09:38:45.124562Z","shell.execute_reply":"2022-11-13T09:38:45.130405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:46.055739Z","iopub.execute_input":"2022-11-13T09:38:46.056145Z","iopub.status.idle":"2022-11-13T09:38:46.061048Z","shell.execute_reply.started":"2022-11-13T09:38:46.05611Z","shell.execute_reply":"2022-11-13T09:38:46.059989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_dataloader.dataset)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:47.869026Z","iopub.execute_input":"2022-11-13T09:38:47.869385Z","iopub.status.idle":"2022-11-13T09:38:47.876294Z","shell.execute_reply.started":"2022-11-13T09:38:47.869354Z","shell.execute_reply":"2022-11-13T09:38:47.875284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = []\n# test_dataloader.dataset.df['label'].loc[8]\nfor i in range(1204):\n    y_test.append(test_dataloader.dataset.df.iloc[i]['label'])\n","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:49.503143Z","iopub.execute_input":"2022-11-13T09:38:49.503507Z","iopub.status.idle":"2022-11-13T09:38:49.634319Z","shell.execute_reply.started":"2022-11-13T09:38:49.503476Z","shell.execute_reply":"2022-11-13T09:38:49.633359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicte = []","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:50.579437Z","iopub.execute_input":"2022-11-13T09:38:50.579909Z","iopub.status.idle":"2022-11-13T09:38:50.585043Z","shell.execute_reply.started":"2022-11-13T09:38:50.579866Z","shell.execute_reply":"2022-11-13T09:38:50.58398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = new_model.to(device)\nnew_model.eval()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:51.761004Z","iopub.execute_input":"2022-11-13T09:38:51.761367Z","iopub.status.idle":"2022-11-13T09:38:51.785355Z","shell.execute_reply.started":"2022-11-13T09:38:51.761338Z","shell.execute_reply":"2022-11-13T09:38:51.784259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch_idx, (input, name, label) in enumerate(tqdm(test_dataloader)):\n    outputs = new_model(input.to(device))\n    _, predicted = outputs.max(1)\n    predicte.append([predicted.item(), name, label.item()])","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:38:52.793831Z","iopub.execute_input":"2022-11-13T09:38:52.79456Z","iopub.status.idle":"2022-11-13T09:39:30.047146Z","shell.execute_reply.started":"2022-11-13T09:38:52.794523Z","shell.execute_reply":"2022-11-13T09:39:30.046114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predicte)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:39:43.129151Z","iopub.execute_input":"2022-11-13T09:39:43.129507Z","iopub.status.idle":"2022-11-13T09:39:43.137149Z","shell.execute_reply.started":"2022-11-13T09:39:43.129478Z","shell.execute_reply":"2022-11-13T09:39:43.136141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicte[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:39:44.535647Z","iopub.execute_input":"2022-11-13T09:39:44.53604Z","iopub.status.idle":"2022-11-13T09:39:44.54304Z","shell.execute_reply.started":"2022-11-13T09:39:44.536006Z","shell.execute_reply":"2022-11-13T09:39:44.54197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum_d = 0\nfor i in predicte:\n    if i[0] == 0:\n        sum_d+=1\nsum_d","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:39:44.943695Z","iopub.execute_input":"2022-11-13T09:39:44.94442Z","iopub.status.idle":"2022-11-13T09:39:44.951021Z","shell.execute_reply.started":"2022-11-13T09:39:44.944383Z","shell.execute_reply":"2022-11-13T09:39:44.949986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum_d = 0\nfor i in predicte:\n    if i[0] == 1:\n        sum_d+=1\nsum_d","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:39:45.820379Z","iopub.execute_input":"2022-11-13T09:39:45.820739Z","iopub.status.idle":"2022-11-13T09:39:45.831033Z","shell.execute_reply.started":"2022-11-13T09:39:45.820708Z","shell.execute_reply":"2022-11-13T09:39:45.829894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count1=0\ncount2=0\ncount3=0\nnum1=0\nnum2=0\nnum3=0\nfor i in predicte:\n    if i[1][0] == '008e5c':\n        count1+=1\n        if i[0] == i[2]:\n            num1+=1\n    elif i[1][0] == '01adc5':\n        count2+=1\n        if i[0] == i[2]:\n            num2+=1 \n    else:\n        count3+=1\n        if i[0] == i[2]:\n            num3+=1 \n        ","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:01.906555Z","iopub.execute_input":"2022-11-13T09:40:01.906939Z","iopub.status.idle":"2022-11-13T09:40:01.915079Z","shell.execute_reply.started":"2022-11-13T09:40:01.906891Z","shell.execute_reply":"2022-11-13T09:40:01.914095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy of {}:\".format('008e5c'))\nprint(100 * num1/count1)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:03.524297Z","iopub.execute_input":"2022-11-13T09:40:03.524664Z","iopub.status.idle":"2022-11-13T09:40:03.530292Z","shell.execute_reply.started":"2022-11-13T09:40:03.524633Z","shell.execute_reply":"2022-11-13T09:40:03.529351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy of {}:\".format('01adc5'))\nprint(100 * num2/count2)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:04.783537Z","iopub.execute_input":"2022-11-13T09:40:04.783914Z","iopub.status.idle":"2022-11-13T09:40:04.790224Z","shell.execute_reply.started":"2022-11-13T09:40:04.78388Z","shell.execute_reply":"2022-11-13T09:40:04.78899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy of {}:\".format('00c058'))\nprint(100 * num3/count3)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:06.576294Z","iopub.execute_input":"2022-11-13T09:40:06.57666Z","iopub.status.idle":"2022-11-13T09:40:06.582262Z","shell.execute_reply.started":"2022-11-13T09:40:06.576629Z","shell.execute_reply":"2022-11-13T09:40:06.581246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = (num1+num2+num3)/(count1+count2+count3)\nprint(\"Total percentage:\",res*100)","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:12.996504Z","iopub.execute_input":"2022-11-13T09:40:12.996864Z","iopub.status.idle":"2022-11-13T09:40:13.003036Z","shell.execute_reply.started":"2022-11-13T09:40:12.996832Z","shell.execute_reply":"2022-11-13T09:40:13.001965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = [predicte[i][2] for i in range(1204)]\ny_test1 = [predicte[i][2] for i in range(1204) if predicte[i][1][0] == '008e5c']\ny_test2 = [predicte[i][2] for i in range(1204) if predicte[i][1][0] == '01adc5']\ny_test3 = [predicte[i][2] for i in range(1204) if predicte[i][1][0] == '00c058']","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:16.04916Z","iopub.execute_input":"2022-11-13T09:40:16.049529Z","iopub.status.idle":"2022-11-13T09:40:16.057538Z","shell.execute_reply.started":"2022-11-13T09:40:16.049496Z","shell.execute_reply":"2022-11-13T09:40:16.055852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = [predicte[i][0] for i in range(1204)]\ny_predict1 = [predicte[i][0] for i in range(1204) if predicte[i][1][0] == '008e5c']\ny_predict2 = [predicte[i][0] for i in range(1204) if predicte[i][1][0] == '01adc5']\ny_predict3 = [predicte[i][0] for i in range(1204) if predicte[i][1][0] == '00c058']","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:27.347678Z","iopub.execute_input":"2022-11-13T09:40:27.348089Z","iopub.status.idle":"2022-11-13T09:40:27.356293Z","shell.execute_reply.started":"2022-11-13T09:40:27.348054Z","shell.execute_reply":"2022-11-13T09:40:27.355155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prob = new_model.predict_proba()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:46:56.174571Z","iopub.execute_input":"2022-11-13T09:46:56.175592Z","iopub.status.idle":"2022-11-13T09:46:56.182062Z","shell.execute_reply.started":"2022-11-13T09:46:56.175542Z","shell.execute_reply":"2022-11-13T09:46:56.181056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn import metrics\n# import matplotlib.pyplot as plt\n# fpr, tpr, _ = metrics.roc_curve(y_test, y_predict)\n# auc = metrics.auc(fpr, tpr)\n\n# #create ROC curve\n# plt.plot(fpr,tpr,label=\"AUC=\"+str(auc))\n# plt.ylabel('True Positive Rate')\n# plt.xlabel('False Positive Rate')\n# plt.legend(loc=2)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:40:29.50921Z","iopub.execute_input":"2022-11-13T09:40:29.509585Z","iopub.status.idle":"2022-11-13T09:40:29.927864Z","shell.execute_reply.started":"2022-11-13T09:40:29.509555Z","shell.execute_reply":"2022-11-13T09:40:29.926881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nimport matplotlib.pyplot as plt\nfpr, tpr, _ = metrics.roc_curve(y_test3, y_predict3)\nauc = metrics.auc(fpr, tpr)\nprint(fpr)\n#create ROC curve\nplt.plot(fpr,tpr,label=\"AUC=\"+str(auc))\nplt.ylabel('True Positive Rate')\nplt.xlabel('False Positive Rate')\nplt.legend(loc=2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-13T09:50:17.82815Z","iopub.execute_input":"2022-11-13T09:50:17.828511Z","iopub.status.idle":"2022-11-13T09:50:18.013245Z","shell.execute_reply.started":"2022-11-13T09:50:17.828482Z","shell.execute_reply":"2022-11-13T09:50:18.012177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}