{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom glob import glob\nfrom tqdm import tqdm\nimport cv2\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-03T11:30:30.01447Z","iopub.execute_input":"2022-09-03T11:30:30.015108Z","iopub.status.idle":"2022-09-03T11:30:30.034392Z","shell.execute_reply.started":"2022-09-03T11:30:30.015071Z","shell.execute_reply":"2022-09-03T11:30:30.033164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:51:52.894023Z","iopub.execute_input":"2022-09-03T11:51:52.894358Z","iopub.status.idle":"2022-09-03T11:51:52.901775Z","shell.execute_reply.started":"2022-09-03T11:51:52.894334Z","shell.execute_reply":"2022-09-03T11:51:52.900557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = '../input/tiff-to-jpeg/train'\nfilename = os.listdir(TRAIN_PATH)\nIMG_SIZE = 100\nplt.figure(figsize=(20,20))\n\nfor i in range(1, 7):\n    img_array = cv2.imread(os.path.join(TRAIN_PATH, filename[i]))\n    #resize_image = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n    #print(resize_image)\n    plt.subplot(3,3,i)\n    image = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)\n    plt.axis('off')\n    plt.imshow(image)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-03T03:15:17.125374Z","iopub.execute_input":"2022-09-03T03:15:17.126069Z","iopub.status.idle":"2022-09-03T03:15:36.686724Z","shell.execute_reply.started":"2022-09-03T03:15:17.126026Z","shell.execute_reply":"2022-09-03T03:15:36.685538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/test.csv\")\nother_df = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/other.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:10:24.374325Z","iopub.execute_input":"2022-09-03T11:10:24.374688Z","iopub.status.idle":"2022-09-03T11:10:24.410856Z","shell.execute_reply.started":"2022-09-03T11:10:24.374662Z","shell.execute_reply":"2022-09-03T11:10:24.408192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:10:39.039391Z","iopub.execute_input":"2022-09-03T11:10:39.039801Z","iopub.status.idle":"2022-09-03T11:10:39.060014Z","shell.execute_reply.started":"2022-09-03T11:10:39.039771Z","shell.execute_reply":"2022-09-03T11:10:39.057262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = glob('../input/tiff-to-jpeg/train/*')","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:12:10.001284Z","iopub.execute_input":"2022-09-03T11:12:10.001682Z","iopub.status.idle":"2022-09-03T11:12:10.056092Z","shell.execute_reply.started":"2022-09-03T11:12:10.001656Z","shell.execute_reply":"2022-09-03T11:12:10.054747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = list()\nfor files in image_files:    \n    image_id.append(os.path.splitext( os.path.basename(files))[0])","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:17:42.256071Z","iopub.execute_input":"2022-09-03T11:17:42.256423Z","iopub.status.idle":"2022-09-03T11:17:42.262527Z","shell.execute_reply.started":"2022-09-03T11:17:42.256387Z","shell.execute_reply":"2022-09-03T11:17:42.261224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df = train_df[train_df['image_id'].isin(image_id)]","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:20:15.702674Z","iopub.execute_input":"2022-09-03T11:20:15.703105Z","iopub.status.idle":"2022-09-03T11:20:15.708936Z","shell.execute_reply.started":"2022-09-03T11:20:15.703078Z","shell.execute_reply":"2022-09-03T11:20:15.707742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:20:28.446596Z","iopub.execute_input":"2022-09-03T11:20:28.448029Z","iopub.status.idle":"2022-09-03T11:20:28.455069Z","shell.execute_reply.started":"2022-09-03T11:20:28.447965Z","shell.execute_reply":"2022-09-03T11:20:28.454331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_count = new_train_df.groupby('label')['label'].count()","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:21:23.689632Z","iopub.execute_input":"2022-09-03T11:21:23.689957Z","iopub.status.idle":"2022-09-03T11:21:23.69734Z","shell.execute_reply.started":"2022-09-03T11:21:23.689933Z","shell.execute_reply":"2022-09-03T11:21:23.694683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_count","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:21:28.407745Z","iopub.execute_input":"2022-09-03T11:21:28.40871Z","iopub.status.idle":"2022-09-03T11:21:28.417045Z","shell.execute_reply.started":"2022-09-03T11:21:28.408683Z","shell.execute_reply":"2022-09-03T11:21:28.415348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:40:35.403857Z","iopub.execute_input":"2022-09-03T11:40:35.405067Z","iopub.status.idle":"2022-09-03T11:40:35.417499Z","shell.execute_reply.started":"2022-09-03T11:40:35.405005Z","shell.execute_reply":"2022-09-03T11:40:35.416688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label = new_train_df.loc[new_train_df['image_id'] == '037300_0', ['label']]","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:41:09.961929Z","iopub.execute_input":"2022-09-03T11:41:09.962379Z","iopub.status.idle":"2022-09-03T11:41:09.972166Z","shell.execute_reply.started":"2022-09-03T11:41:09.962346Z","shell.execute_reply":"2022-09-03T11:41:09.969739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = list()\ny= list()\nIMG_SIZE = 250\nfor i in tqdm(image_id):\n    label = new_train_df.loc[new_train_df['image_id'] == i,['label']]\n    img_array = cv2.imread(f'../input/tiff-to-jpeg/train/{i}.jpg', cv2.IMREAD_GRAYSCALE)\n    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n    X.append(new_array) \n    y.append(label['label'].iloc[0])\n    #print(f'../input/tiff-to-jpeg/train/{label}.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:46:42.17164Z","iopub.execute_input":"2022-09-03T11:46:42.172097Z","iopub.status.idle":"2022-09-03T11:46:56.767108Z","shell.execute_reply.started":"2022-09-03T11:46:42.172065Z","shell.execute_reply":"2022-09-03T11:46:56.765912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, x_test, y_train, y_test = train_test_split(X,y,test_size = 0.1, random_state = 50)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:49:20.679114Z","iopub.execute_input":"2022-09-03T11:49:20.679459Z","iopub.status.idle":"2022-09-03T11:49:20.686241Z","shell.execute_reply.started":"2022-09-03T11:49:20.679432Z","shell.execute_reply":"2022-09-03T11:49:20.684739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:49:34.474693Z","iopub.execute_input":"2022-09-03T11:49:34.47513Z","iopub.status.idle":"2022-09-03T11:49:34.48242Z","shell.execute_reply.started":"2022-09-03T11:49:34.475102Z","shell.execute_reply":"2022-09-03T11:49:34.480997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:52:24.330786Z","iopub.execute_input":"2022-09-03T11:52:24.331826Z","iopub.status.idle":"2022-09-03T11:52:24.338678Z","shell.execute_reply.started":"2022-09-03T11:52:24.331777Z","shell.execute_reply":"2022-09-03T11:52:24.337421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(256, (3,3), input_shape=(250,250,1)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(256, (3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors\n\nmodel.add(Dense(64))\nmodel.add(Dense(1))\n\nmodel.add(Activation('sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:52:57.280544Z","iopub.execute_input":"2022-09-03T11:52:57.281149Z","iopub.status.idle":"2022-09-03T11:52:57.853069Z","shell.execute_reply.started":"2022-09-03T11:52:57.281122Z","shell.execute_reply":"2022-09-03T11:52:57.851628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'],)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:53:11.438645Z","iopub.execute_input":"2022-09-03T11:53:11.439778Z","iopub.status.idle":"2022-09-03T11:53:11.449272Z","shell.execute_reply.started":"2022-09-03T11:53:11.439721Z","shell.execute_reply":"2022-09-03T11:53:11.448029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size=32, epochs=15, validation_data = (x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-09-03T11:53:34.920406Z","iopub.execute_input":"2022-09-03T11:53:34.921077Z","iopub.status.idle":"2022-09-03T11:53:34.949786Z","shell.execute_reply.started":"2022-09-03T11:53:34.921039Z","shell.execute_reply":"2022-09-03T11:53:34.948517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}