{"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        \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":"2021-07-26T07:46:44.538627Z","iopub.execute_input":"2021-07-26T07:46:44.538995Z","iopub.status.idle":"2021-07-26T07:46:44.549952Z","shell.execute_reply.started":"2021-07-26T07:46:44.538914Z","shell.execute_reply":"2021-07-26T07:46:44.549021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\n\nimport pydicom as dicom\nimport os\nimport cv2\nimport PIL # optional\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:46:44.551655Z","iopub.execute_input":"2021-07-26T07:46:44.552039Z","iopub.status.idle":"2021-07-26T07:46:49.943193Z","shell.execute_reply.started":"2021-07-26T07:46:44.552000Z","shell.execute_reply":"2021-07-26T07:46:49.942160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image preprocessing ","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:46:49.948744Z","iopub.execute_input":"2021-07-26T07:46:49.951012Z","iopub.status.idle":"2021-07-26T07:46:49.957666Z","shell.execute_reply.started":"2021-07-26T07:46:49.950968Z","shell.execute_reply":"2021-07-26T07:46:49.956619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_type = pd.read_csv('/kaggle/input/decease-type-files/decease_type.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:46:49.963215Z","iopub.execute_input":"2021-07-26T07:46:49.966131Z","iopub.status.idle":"2021-07-26T07:46:50.023240Z","shell.execute_reply.started":"2021-07-26T07:46:49.966086Z","shell.execute_reply":"2021-07-26T07:46:50.022477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_type","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:46:50.027729Z","iopub.execute_input":"2021-07-26T07:46:50.029854Z","iopub.status.idle":"2021-07-26T07:46:50.072812Z","shell.execute_reply.started":"2021-07-26T07:46:50.029813Z","shell.execute_reply":"2021-07-26T07:46:50.071956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir Bbox_256x256","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:46:50.077001Z","iopub.execute_input":"2021-07-26T07:46:50.079544Z","iopub.status.idle":"2021-07-26T07:46:50.768032Z","shell.execute_reply.started":"2021-07-26T07:46:50.079505Z","shell.execute_reply":"2021-07-26T07:46:50.767004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crp_file_name = []\nfor i in np.arange(D_type.shape[0]):\n    r = D_type.iloc[i]\n    im1 = tf.keras.preprocessing.image.load_img(r.filename)\n    ary = tf.keras.preprocessing.image.img_to_array(im1)\n    jt =tf.keras.preprocessing.image.array_to_img(ary[r.ymin:r.ymax,r.xmin:r.xmax]) # [ymin:Ymax,xmin:xmax]\n    hp = jt.resize((256,256))\n    hp.save('/kaggle/working/Bbox_256x256/'+ f'{r.xmin}_'+ f'{r.ymin}_'+f'{r.xmax}_'+ f'{r.ymax}'+'.png')\n    crp_file_name.append('/kaggle/working/Bbox_256x256/'+ f'{r.xmin}_'+ f'{r.ymin}_'+f'{r.xmax}_'+ f'{r.ymax}'+'.png')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:46:50.769618Z","iopub.execute_input":"2021-07-26T07:46:50.769957Z","iopub.status.idle":"2021-07-26T07:49:25.857533Z","shell.execute_reply.started":"2021-07-26T07:46:50.769909Z","shell.execute_reply":"2021-07-26T07:49:25.856700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_type['crp_file_name'] = crp_file_name\nD_type","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:25.860250Z","iopub.execute_input":"2021-07-26T07:49:25.860611Z","iopub.status.idle":"2021-07-26T07:49:25.883378Z","shell.execute_reply.started":"2021-07-26T07:49:25.860575Z","shell.execute_reply":"2021-07-26T07:49:25.882292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ! zip \"/kaggle/working/Bbox_256x256.zip\" \"/kaggle/working/\" ","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:25.885930Z","iopub.execute_input":"2021-07-26T07:49:25.886351Z","iopub.status.idle":"2021-07-26T07:49:26.531921Z","shell.execute_reply.started":"2021-07-26T07:49:25.886312Z","shell.execute_reply":"2021-07-26T07:49:26.531012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_type[['crp_file_name','class']]","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:26.533488Z","iopub.execute_input":"2021-07-26T07:49:26.533820Z","iopub.status.idle":"2021-07-26T07:49:26.552305Z","shell.execute_reply.started":"2021-07-26T07:49:26.533783Z","shell.execute_reply":"2021-07-26T07:49:26.551569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.get_dummies(D_type[['class']])","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:26.553607Z","iopub.execute_input":"2021-07-26T07:49:26.553968Z","iopub.status.idle":"2021-07-26T07:49:26.574126Z","shell.execute_reply.started":"2021-07-26T07:49:26.553923Z","shell.execute_reply":"2021-07-26T07:49:26.573195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Just taking only the required columns from the trainstudy dataframe\nkj = D_type[['crp_file_name','class']]\nkt = pd.get_dummies(D_type[['class']])\nfea = pd.concat([kt,kj],axis=1)\nfea.columns = ['Atypical', 'Indeterminate','Typical', 'crp_file_name','class']\nfea","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:26.575427Z","iopub.execute_input":"2021-07-26T07:49:26.575808Z","iopub.status.idle":"2021-07-26T07:49:26.599559Z","shell.execute_reply.started":"2021-07-26T07:49:26.575765Z","shell.execute_reply":"2021-07-26T07:49:26.598802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_trn,X_val,Y_trn,Y_val = train_test_split(fea,fea[['class']],test_size=0.15,random_state=45)\n\n#X_trn\nX_val","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:26.600817Z","iopub.execute_input":"2021-07-26T07:49:26.601170Z","iopub.status.idle":"2021-07-26T07:49:26.792871Z","shell.execute_reply.started":"2021-07-26T07:49:26.601136Z","shell.execute_reply":"2021-07-26T07:49:26.791521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-Trained Model Activation ","metadata":{}},{"cell_type":"code","source":"img_size = 256\nimg_depth = 3\nmodel = tf.keras.applications.efficientnet.EfficientNetB7(include_top=False, #Do not include FC layer at the end\n                                          input_shape=(img_size,img_size, img_depth),\n                                          weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:26.794323Z","iopub.execute_input":"2021-07-26T07:49:26.794921Z","iopub.status.idle":"2021-07-26T07:49:35.872339Z","shell.execute_reply.started":"2021-07-26T07:49:26.794882Z","shell.execute_reply":"2021-07-26T07:49:35.870926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Set pre-trained model layers to not trainable\nfor layer in model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:35.873980Z","iopub.execute_input":"2021-07-26T07:49:35.874307Z","iopub.status.idle":"2021-07-26T07:49:35.904073Z","shell.execute_reply.started":"2021-07-26T07:49:35.874272Z","shell.execute_reply":"2021-07-26T07:49:35.903288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get Output layer of Pre0trained model\nx = model.output\n\n#Flatten the output to feed to Dense layer\nx = tf.keras.layers.Flatten()(x)\n\n#Add one Dense layer\nx = tf.keras.layers.Dense(200, activation='relu')(x)\n\n#Add output layer\nprediction = tf.keras.layers.Dense(3,activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:35.905316Z","iopub.execute_input":"2021-07-26T07:49:35.905703Z","iopub.status.idle":"2021-07-26T07:49:35.932386Z","shell.execute_reply.started":"2021-07-26T07:49:35.905667Z","shell.execute_reply":"2021-07-26T07:49:35.931605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using Keras Model class\nfinal_model = tf.keras.models.Model(inputs=model.input, #Pre-trained model input as input layer\n                                    outputs=prediction) #Output layer added","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:35.933496Z","iopub.execute_input":"2021-07-26T07:49:35.933805Z","iopub.status.idle":"2021-07-26T07:49:35.986584Z","shell.execute_reply.started":"2021-07-26T07:49:35.933770Z","shell.execute_reply":"2021-07-26T07:49:35.985833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:35.987726Z","iopub.execute_input":"2021-07-26T07:49:35.988104Z","iopub.status.idle":"2021-07-26T07:49:36.013984Z","shell.execute_reply.started":"2021-07-26T07:49:35.988064Z","shell.execute_reply":"2021-07-26T07:49:36.013254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image data augumentataion \ntrn_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.,\n                                                fill_mode=\"nearest\",horizontal_flip=True,\n                                                #validation_split=0.2,\n                                                preprocessing_function=tf.keras.applications.efficientnet.preprocess_input)\n\n\nval_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.,\n                                                fill_mode=\"nearest\",horizontal_flip=True,\n                                                #validation_split=0.2,\n                                                preprocessing_function=tf.keras.applications.efficientnet.preprocess_input)\n\ntest_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:36.015176Z","iopub.execute_input":"2021-07-26T07:49:36.015661Z","iopub.status.idle":"2021-07-26T07:49:36.022286Z","shell.execute_reply.started":"2021-07-26T07:49:36.015622Z","shell.execute_reply":"2021-07-26T07:49:36.021363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_trn","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:36.023993Z","iopub.execute_input":"2021-07-26T07:49:36.024467Z","iopub.status.idle":"2021-07-26T07:49:36.046825Z","shell.execute_reply.started":"2021-07-26T07:49:36.024388Z","shell.execute_reply":"2021-07-26T07:49:36.046021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create train and test generator\nbatchsize = 128\ntrain_generator =  trn_gen.flow_from_dataframe (X_trn,\n                                                x_col=\"crp_file_name\",\n                                                y_col=['Atypical', 'Indeterminate', 'Typical'],\n                                                target_size=(256, 256),\n                                                color_mode=\"rgb\",\n                                            #classes=['Negative','Typical','Indeterminate','Atypical'], \n                                                class_mode=\"raw\",\n                                                batch_size=batchsize,\n                                                shuffle=True,\n                                                seed=20,\n                                                ) #batchsize can be changed\n\nval_generator = val_gen.flow_from_dataframe (X_val,\n                                            x_col=\"crp_file_name\",\n                                            y_col=['Atypical', 'Indeterminate','Typical'],\n                                            target_size=(256, 256),\n                                            color_mode=\"rgb\",\n                                            #classes=['Negative','Typical','Indeterminate','Atypical'], \n                                            class_mode=\"raw\",\n                                            batch_size=batchsize,\n                                            shuffle=True,\n                                            seed=20,\n                                            )","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:36.048264Z","iopub.execute_input":"2021-07-26T07:49:36.048619Z","iopub.status.idle":"2021-07-26T07:49:36.123215Z","shell.execute_reply.started":"2021-07-26T07:49:36.048585Z","shell.execute_reply":"2021-07-26T07:49:36.122234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.fit_generator(train_generator, \n                          epochs=10,\n                          steps_per_epoch= X_trn.shape[0]//batchsize,\n                          validation_data=val_generator,\n                          validation_steps = X_val.shape[0]//batchsize,\n                          verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:36.124679Z","iopub.execute_input":"2021-07-26T07:49:36.125018Z","iopub.status.idle":"2021-07-26T08:00:21.217112Z","shell.execute_reply.started":"2021-07-26T07:49:36.124982Z","shell.execute_reply":"2021-07-26T08:00:21.216303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nprint(len(model.layers))\nfor layer in model.layers[150:]:\n    layer.trainable =  True \n'''","metadata":{"execution":{"iopub.status.busy":"2021-07-26T08:00:21.220578Z","iopub.execute_input":"2021-07-26T08:00:21.220837Z","iopub.status.idle":"2021-07-26T08:00:21.226612Z","shell.execute_reply.started":"2021-07-26T08:00:21.220812Z","shell.execute_reply":"2021-07-26T08:00:21.225725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}