{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\nfrom keras.models import Sequential\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Flatten\nimport matplotlib.pyplot as plt\nfrom pydicom import dcmread\nimport tensorflow as tf\nimport cv2\nimport pandas as pd\npd.set_option('display.max_rows', 500)\npd.set_option('display.max_columns', 500)\npd.set_option('display.width', 1000)\npd.set_option('display.max_colwidth', 255)\nfrom sklearn.model_selection import train_test_split\n\n\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\ni=0\n\nfor dirname, _, filenames in os.walk('/kaggle/input/ranzcr-clip-catheter-line-classification/train'):\n    for filename in filenames: \n        i=i+1\n        \nprint(\"The no of images in Train Dir\",i)\ni=0\n\nfor dirname, _, filenames in os.walk('/kaggle/input/ranzcr-clip-catheter-line-classification/test'):\n    for filename in filenames: \n        i=i+1\n        \nprint(\"The no of images in Test Dir\",i)\ni=0\n\ntrain_data_dir = '/kaggle/input/ranzcr-clip-catheter-line-classification/train'\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BASE_DIR = \"/kaggle/input/ranzcr-clip-catheter-line-classification/\"\ndf_train = pd.read_csv(os.path.join(BASE_DIR, \"train.csv\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_data = []\n\nfiles =  os.listdir(train_data_dir)\nsorted_files =  sorted(files)\n\ndef create_training_data():\n    ii = 0\n    for img in sorted_files:\n        img_array = cv2.imread(os.path.join(train_data_dir,img), cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (25,25))\n        training_data.append(new_array)\n        \ncreate_training_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#X = np.array(training_data).reshape(-1,256,256,1)\nX = np.array(training_data)\n\ndf_train = df_train.sort_values(by=['StudyInstanceUID'])\ny = df_train.drop(columns=['StudyInstanceUID','PatientID'],axis=1)\ny = y.to_numpy()\n\nprint(X.shape)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = X.reshape(X.shape[0],25,25,1)\n\nclasses = ['ETT - Abnormal','ETT - Borderline','ETT - Normal','NGT - Abnormal','NGT - Borderline','NGT - Incompletely Imaged','NGT - Normal','CVC - Abnormal','CVC - Borderline','CVC - Normal','Swan Ganz Catheter Present']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)\n\nprint(X_train.shape,X_test.shape,y_train.shape,y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_filters = 20\nfiltersi = 5\npoolsi = 2\n\nmodel=Sequential()\nmodel.add(Conv2D(num_filters, filtersi, strides = (1,1), input_shape = (25,25,1)))\nmodel.add(MaxPooling2D(pool_size = poolsi))\nmodel.add(Flatten())\nmodel.add(Dense(10, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile('adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])\n\nmodel.fit(X_train, to_categorical(np.argmax(y_train,axis=1)), epochs = 30, verbose = 1, validation_data = (X_test,to_categorical(np.argmax(y_test, axis=1))),batch_size=1000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"score=model.evaluate(X_test,to_categorical(np.argmax(y_test,axis=1)),verbose=0)\nprint(\"test loss\",score[0])\nprint(\"test accuracy\",score[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict(X_test[:5])\nprint(to_categorical(np.argmax(predictions, axis=1)))\nprint(to_categorical(np.argmax(y_test, axis=1)))","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}