{"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 matplotlib.pyplot as plt\nimport tensorflow as tf\nimport os\nimport cv2\nfrom glob import glob\nfrom openslide import OpenSlide\nimport numpy as np\nimport tifffile as tiff\nimport warnings\nimport pandas as pd\nfrom PIL import Image\nimport openslide\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom IPython.display import clear_output\nImage.MAX_IMAGE_PIXELS = None","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:10.758351Z","iopub.execute_input":"2022-08-21T11:31:10.759205Z","iopub.status.idle":"2022-08-21T11:31:16.836202Z","shell.execute_reply.started":"2022-08-21T11:31:10.759162Z","shell.execute_reply":"2022-08-21T11:31:16.835051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = '/kaggle/input/mayo-clinic-strip-ai/'\ntrain_folder = os.path.join(base_path,'train')\ntest_folder = os.path.join(base_path,'test')\ntrain_image_list = os.listdir(train_folder)\ntest_image_list = os.listdir(test_folder)\ntrain_csv = pd.read_csv(os.path.join(base_path,'train.csv'))\ntest_csv = pd.read_csv(os.path.join(base_path,'test.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:19.309121Z","iopub.execute_input":"2022-08-21T11:31:19.309968Z","iopub.status.idle":"2022-08-21T11:31:19.375555Z","shell.execute_reply.started":"2022-08-21T11:31:19.309929Z","shell.execute_reply":"2022-08-21T11:31:19.374454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.pie(train_csv['label'].value_counts(), labels = train_csv['label'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:20.20483Z","iopub.execute_input":"2022-08-21T11:31:20.205833Z","iopub.status.idle":"2022-08-21T11:31:20.325996Z","shell.execute_reply.started":"2022-08-21T11:31:20.205786Z","shell.execute_reply":"2022-08-21T11:31:20.324694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting the categorical labels to a numeric value\nlabels = LabelEncoder()\npoints = labels.fit_transform(train_csv['label'])\ntrain_csv.drop(['label'],axis=1)\ntrain_csv = train_csv.assign(label=points)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:21.031442Z","iopub.execute_input":"2022-08-21T11:31:21.031982Z","iopub.status.idle":"2022-08-21T11:31:21.041436Z","shell.execute_reply.started":"2022-08-21T11:31:21.031948Z","shell.execute_reply":"2022-08-21T11:31:21.040461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_csv.head(10))\nprint(test_csv.head(10))","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:21.923902Z","iopub.execute_input":"2022-08-21T11:31:21.924258Z","iopub.status.idle":"2022-08-21T11:31:21.936827Z","shell.execute_reply.started":"2022-08-21T11:31:21.924225Z","shell.execute_reply":"2022-08-21T11:31:21.935788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_images(image_path, image_list):\n    image_data = []\n    for i in range(len(image_list)):\n        image_file = os.path.join(image_path, image_list[i])\n        image = OpenSlide(image_file)\n        image_data.append(np.array(image.read_region((0,0),0,(224,224))))\n    image_data = np.array(image_data)\n    return image_data","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:22.796056Z","iopub.execute_input":"2022-08-21T11:31:22.796761Z","iopub.status.idle":"2022-08-21T11:31:22.802982Z","shell.execute_reply.started":"2022-08-21T11:31:22.796723Z","shell.execute_reply":"2022-08-21T11:31:22.802004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = resize_images(train_folder, train_image_list)\ntest_image = resize_images(test_folder, test_image_list)\ntrain_labels = train_csv['label'].values","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:23.95317Z","iopub.execute_input":"2022-08-21T11:31:23.953952Z","iopub.status.idle":"2022-08-21T11:31:47.684663Z","shell.execute_reply.started":"2022-08-21T11:31:23.953913Z","shell.execute_reply":"2022-08-21T11:31:47.683683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_image.shape)\nprint(train_labels.shape)\nprint(test_image.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:47.686456Z","iopub.execute_input":"2022-08-21T11:31:47.68682Z","iopub.status.idle":"2022-08-21T11:31:47.693966Z","shell.execute_reply.started":"2022-08-21T11:31:47.686786Z","shell.execute_reply":"2022-08-21T11:31:47.692257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:47.69559Z","iopub.execute_input":"2022-08-21T11:31:47.69675Z","iopub.status.idle":"2022-08-21T11:31:47.712006Z","shell.execute_reply.started":"2022-08-21T11:31:47.696714Z","shell.execute_reply":"2022-08-21T11:31:47.71099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Splitting the test and the train data\nX_train, X_test, y_train, y_test = train_test_split(train_image, train_labels, test_size = 0.2)\nprint(X_train.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:47.714959Z","iopub.execute_input":"2022-08-21T11:31:47.71572Z","iopub.status.idle":"2022-08-21T11:31:47.763007Z","shell.execute_reply.started":"2022-08-21T11:31:47.715685Z","shell.execute_reply":"2022-08-21T11:31:47.761975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Building the CNN model for classification\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(224,224,4)))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(2))","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:47.764576Z","iopub.execute_input":"2022-08-21T11:31:47.764946Z","iopub.status.idle":"2022-08-21T11:31:50.481558Z","shell.execute_reply.started":"2022-08-21T11:31:47.76491Z","shell.execute_reply":"2022-08-21T11:31:50.480556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:31:50.482937Z","iopub.execute_input":"2022-08-21T11:31:50.484811Z","iopub.status.idle":"2022-08-21T11:31:50.492167Z","shell.execute_reply.started":"2022-08-21T11:31:50.484771Z","shell.execute_reply":"2022-08-21T11:31:50.490368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class trainingplot(tf.keras.callbacks.Callback):\n    \n    def on_train_begin(self, logs={}):\n        self.acc = []\n    def on_epoch_end(self, epoch, logs={}):\n        acc = logs.get('accuracy')\n        self.acc.append(acc)\n        \n        if epoch > 0 and epoch %1 == 0:\n            clear_output(wait=True)\n            N = np.arange(0, len(self.acc))\n            plt.figure(figsize=(10,3))\n            plt.title(\"accuracy over epoch\")\n            plt.plot(N, self.acc)\n            plt.show()\n\nplotaccuracy = trainingplot()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:32:05.504936Z","iopub.execute_input":"2022-08-21T11:32:05.505918Z","iopub.status.idle":"2022-08-21T11:32:05.523834Z","shell.execute_reply.started":"2022-08-21T11:32:05.505859Z","shell.execute_reply":"2022-08-21T11:32:05.519477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])\n\nhistory = model.fit(X_train, y_train, batch_size=10,epochs=100,validation_data = (X_test, y_test),callbacks=[plotaccuracy], verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T11:32:09.38018Z","iopub.execute_input":"2022-08-21T11:32:09.381025Z","iopub.status.idle":"2022-08-21T11:33:41.180531Z","shell.execute_reply.started":"2022-08-21T11:32:09.380965Z","shell.execute_reply":"2022-08-21T11:33:41.179523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}