{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport torch\nimport torchvision\nfrom tensorflow.keras.optimizers import Adam\nfrom torchvision import transforms, datasets\nfrom torch.utils.data import Dataset\n\nfrom PIL import Image, ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True\nimport cv2\n\nfrom tqdm import tqdm_notebook as tqdm\nfrom sklearn.model_selection import train_test_split\nimport random\nimport time\nimport sys\nimport os\nimport math\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\npd.set_option('display.max_columns', None)\n%matplotlib inline\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:45:49.23697Z","iopub.execute_input":"2022-11-19T13:45:49.237645Z","iopub.status.idle":"2022-11-19T13:45:49.284369Z","shell.execute_reply.started":"2022-11-19T13:45:49.23761Z","shell.execute_reply":"2022-11-19T13:45:49.283398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata = pd.read_csv('../input/strip-ai-background-clot/metadata.csv')\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:45:51.011848Z","iopub.execute_input":"2022-11-19T13:45:51.012515Z","iopub.status.idle":"2022-11-19T13:45:51.125984Z","shell.execute_reply.started":"2022-11-19T13:45:51.012479Z","shell.execute_reply":"2022-11-19T13:45:51.125053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.drop(metadata[metadata['class'] == -1].index, inplace = True)\nmetadata.drop(metadata[metadata['class'] == 0].index, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:46:01.520887Z","iopub.execute_input":"2022-11-19T13:46:01.521387Z","iopub.status.idle":"2022-11-19T13:46:01.562118Z","shell.execute_reply.started":"2022-11-19T13:46:01.521344Z","shell.execute_reply":"2022-11-19T13:46:01.560878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpg\"\nmetadata[\"slice_id\"]=metadata[\"slice_id\"].apply(append_ext)\n\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:46:03.307169Z","iopub.execute_input":"2022-11-19T13:46:03.307589Z","iopub.status.idle":"2022-11-19T13:46:03.328976Z","shell.execute_reply.started":"2022-11-19T13:46:03.307548Z","shell.execute_reply":"2022-11-19T13:46:03.327968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata[\"target\"] = metadata[\"label\"].apply(lambda x : 1 if x==\"CE\" else 0)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:46:06.412359Z","iopub.execute_input":"2022-11-19T13:46:06.41271Z","iopub.status.idle":"2022-11-19T13:46:06.422126Z","shell.execute_reply.started":"2022-11-19T13:46:06.412679Z","shell.execute_reply":"2022-11-19T13:46:06.421166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:46:15.776562Z","iopub.execute_input":"2022-11-19T13:46:15.776932Z","iopub.status.idle":"2022-11-19T13:46:15.794269Z","shell.execute_reply.started":"2022-11-19T13:46:15.776901Z","shell.execute_reply":"2022-11-19T13:46:15.793299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nX_train = []\nrootdir = '../input/strip-ai-background-clot/positive/'\nfor file in tqdm(os.listdir(rootdir)):\n        d = os.path.join(rootdir, file)\n        print(d)\n        img = Image.open(d) # image extension *.png,*.jpg\n        new_width  = 128\n        new_height = 128\n        img = img.resize((new_width, new_height), Image.ANTIALIAS)\n        image = np.array(img)    \n        X_train.append(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:57:53.256331Z","iopub.execute_input":"2022-11-19T13:57:53.256685Z","iopub.status.idle":"2022-11-19T14:03:35.126489Z","shell.execute_reply.started":"2022-11-19T13:57:53.256654Z","shell.execute_reply":"2022-11-19T14:03:35.125467Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.array(X_train)\nY_train=metadata['target']\n\nx_train,x_test,y_train,y_test=train_test_split(X_train,Y_train,test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T14:03:59.881644Z","iopub.execute_input":"2022-11-19T14:03:59.881987Z","iopub.status.idle":"2022-11-19T14:04:01.128825Z","shell.execute_reply.started":"2022-11-19T14:03:59.881957Z","shell.execute_reply":"2022-11-19T14:04:01.12741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\nfrom keras.models import Sequential\nfrom tensorflow.keras.applications import DenseNet121\nfrom keras import layers\n\neffnet = DenseNet121(\n        weights='imagenet',\n        include_top=False,\n        input_shape=(128,128,3)\n)\n\nmodel = Sequential()\nmodel.add(effnet)\nmodel.add(layers.GlobalAveragePooling2D())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1024,activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))\n\nmodel.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(lr=0.001),\n        metrics=['accuracy'],\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T14:05:20.315695Z","iopub.execute_input":"2022-11-19T14:05:20.316072Z","iopub.status.idle":"2022-11-19T14:05:23.528407Z","shell.execute_reply.started":"2022-11-19T14:05:20.31604Z","shell.execute_reply":"2022-11-19T14:05:23.527364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checkpoint\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfilepath = \"CE_LLA.hdf5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')\ncallbacks_list = [checkpoint]","metadata":{"execution":{"iopub.status.busy":"2022-11-19T14:05:23.530077Z","iopub.execute_input":"2022-11-19T14:05:23.531247Z","iopub.status.idle":"2022-11-19T14:05:23.537797Z","shell.execute_reply.started":"2022-11-19T14:05:23.531207Z","shell.execute_reply":"2022-11-19T14:05:23.536446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.utils import compute_class_weight\n# train_classes = train_data.classes\n# class_weights = compute_class_weight(class_weight = \"balanced\", classes = np.unique(train_classes),y = train_classes)\n# class_weights = dict(zip(np.unique(train_classes), class_weights))\n# class_weights","metadata":{"execution":{"iopub.status.busy":"2022-11-19T14:04:08.887647Z","iopub.execute_input":"2022-11-19T14:04:08.888237Z","iopub.status.idle":"2022-11-19T14:04:08.900646Z","shell.execute_reply.started":"2022-11-19T14:04:08.888201Z","shell.execute_reply":"2022-11-19T14:04:08.89963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n                    x_train,\n                    y_train,\n                    batch_size=64,\n                    validation_data = (x_test,y_test),\n                    callbacks = callbacks_list,\n                    epochs=5)   ","metadata":{"execution":{"iopub.status.busy":"2022-11-19T14:05:23.540017Z","iopub.execute_input":"2022-11-19T14:05:23.540497Z","iopub.status.idle":"2022-11-19T14:09:52.190394Z","shell.execute_reply.started":"2022-11-19T14:05:23.540457Z","shell.execute_reply":"2022-11-19T14:09:52.189027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(10,5))\n# plt.plot(history.history['loss'])\n# plt.plot(history.history['val_loss'])\n# plt.title('Loss vs. epochs')\n# plt.ylabel('Loss')\n# plt.xlabel('Epoch')\n# plt.legend(['Training', 'Validation'], loc='upper right')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:38:20.882423Z","iopub.status.idle":"2022-11-19T13:38:20.882917Z","shell.execute_reply.started":"2022-11-19T13:38:20.882654Z","shell.execute_reply":"2022-11-19T13:38:20.882677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(10,5))\n# plt.plot(history.history['accuracy'])\n# plt.plot(history.history['val_accuracy'])\n# plt.title('accuracy vs. epochs')\n# plt.ylabel('accuracy')\n# plt.xlabel('Epoch')\n# plt.legend(['Training', 'Validation'], loc='upper right')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T13:38:20.884542Z","iopub.status.idle":"2022-11-19T13:38:20.885034Z","shell.execute_reply.started":"2022-11-19T13:38:20.884765Z","shell.execute_reply":"2022-11-19T13:38:20.884787Z"},"trusted":true},"execution_count":null,"outputs":[]}]}