{"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-26T03:25:53.971461Z","iopub.execute_input":"2021-07-26T03:25:53.971936Z","iopub.status.idle":"2021-07-26T03:25:53.983721Z","shell.execute_reply.started":"2021-07-26T03:25:53.971805Z","shell.execute_reply":"2021-07-26T03:25:53.982494Z"},"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-26T03:25:53.985413Z","iopub.execute_input":"2021-07-26T03:25:53.98579Z","iopub.status.idle":"2021-07-26T03:25:59.342519Z","shell.execute_reply.started":"2021-07-26T03:25:53.985753Z","shell.execute_reply":"2021-07-26T03:25:59.341556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image preprocessing ","metadata":{"execution":{"iopub.status.busy":"2021-07-26T03:25:59.344369Z","iopub.execute_input":"2021-07-26T03:25:59.344691Z","iopub.status.idle":"2021-07-26T03:25:59.352004Z","shell.execute_reply.started":"2021-07-26T03:25:59.344663Z","shell.execute_reply":"2021-07-26T03:25:59.351141Z"},"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-26T03:25:59.353756Z","iopub.execute_input":"2021-07-26T03:25:59.354141Z","iopub.status.idle":"2021-07-26T03:25:59.404617Z","shell.execute_reply.started":"2021-07-26T03:25:59.3541Z","shell.execute_reply":"2021-07-26T03:25:59.403734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_type","metadata":{"execution":{"iopub.status.busy":"2021-07-26T03:25:59.405834Z","iopub.execute_input":"2021-07-26T03:25:59.406169Z","iopub.status.idle":"2021-07-26T03:25:59.438416Z","shell.execute_reply.started":"2021-07-26T03:25:59.406135Z","shell.execute_reply":"2021-07-26T03:25:59.437399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir Bbox_256x256","metadata":{"execution":{"iopub.status.busy":"2021-07-26T03:25:59.439875Z","iopub.execute_input":"2021-07-26T03:25:59.440286Z","iopub.status.idle":"2021-07-26T03:26:00.143564Z","shell.execute_reply.started":"2021-07-26T03:25:59.440247Z","shell.execute_reply":"2021-07-26T03:26:00.142463Z"},"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-26T03:26:00.147198Z","iopub.execute_input":"2021-07-26T03:26:00.147476Z","iopub.status.idle":"2021-07-26T03:28:28.579562Z","shell.execute_reply.started":"2021-07-26T03:26:00.147446Z","shell.execute_reply":"2021-07-26T03:28:28.578691Z"},"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-26T03:28:28.582363Z","iopub.execute_input":"2021-07-26T03:28:28.582702Z","iopub.status.idle":"2021-07-26T03:28:28.615582Z","shell.execute_reply.started":"2021-07-26T03:28:28.582667Z","shell.execute_reply":"2021-07-26T03:28:28.613589Z"},"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-26T03:28:28.617954Z","iopub.execute_input":"2021-07-26T03:28:28.618455Z","iopub.status.idle":"2021-07-26T03:28:28.647076Z","shell.execute_reply.started":"2021-07-26T03:28:28.618409Z","shell.execute_reply":"2021-07-26T03:28:28.646275Z"},"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-26T03:28:28.648419Z","iopub.execute_input":"2021-07-26T03:28:28.648812Z","iopub.status.idle":"2021-07-26T03:28:28.821547Z","shell.execute_reply.started":"2021-07-26T03:28:28.648773Z","shell.execute_reply":"2021-07-26T03:28:28.820794Z"},"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-26T03:28:28.822877Z","iopub.execute_input":"2021-07-26T03:28:28.823274Z","iopub.status.idle":"2021-07-26T03:28:38.034641Z","shell.execute_reply.started":"2021-07-26T03:28:28.823235Z","shell.execute_reply":"2021-07-26T03:28:38.033758Z"},"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-26T03:28:38.036661Z","iopub.execute_input":"2021-07-26T03:28:38.036944Z","iopub.status.idle":"2021-07-26T03:28:38.066928Z","shell.execute_reply.started":"2021-07-26T03:28:38.036907Z","shell.execute_reply":"2021-07-26T03:28:38.066217Z"},"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-26T03:28:38.06808Z","iopub.execute_input":"2021-07-26T03:28:38.06862Z","iopub.status.idle":"2021-07-26T03:28:38.095976Z","shell.execute_reply.started":"2021-07-26T03:28:38.068581Z","shell.execute_reply":"2021-07-26T03:28:38.095267Z"},"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-26T03:28:38.097327Z","iopub.execute_input":"2021-07-26T03:28:38.097675Z","iopub.status.idle":"2021-07-26T03:28:38.150009Z","shell.execute_reply.started":"2021-07-26T03:28:38.09764Z","shell.execute_reply":"2021-07-26T03:28:38.149172Z"},"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-26T03:28:38.151378Z","iopub.execute_input":"2021-07-26T03:28:38.15186Z","iopub.status.idle":"2021-07-26T03:28:38.184492Z","shell.execute_reply.started":"2021-07-26T03:28:38.151826Z","shell.execute_reply":"2021-07-26T03:28:38.183573Z"},"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-26T03:28:38.187623Z","iopub.execute_input":"2021-07-26T03:28:38.187877Z","iopub.status.idle":"2021-07-26T03:28:38.194953Z","shell.execute_reply.started":"2021-07-26T03:28:38.187853Z","shell.execute_reply":"2021-07-26T03:28:38.194218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_trn","metadata":{"execution":{"iopub.status.busy":"2021-07-26T03:28:38.196401Z","iopub.execute_input":"2021-07-26T03:28:38.196801Z","iopub.status.idle":"2021-07-26T03:28:38.213678Z","shell.execute_reply.started":"2021-07-26T03:28:38.196757Z","shell.execute_reply":"2021-07-26T03:28:38.212742Z"},"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-26T03:28:38.215101Z","iopub.execute_input":"2021-07-26T03:28:38.215485Z","iopub.status.idle":"2021-07-26T03:28:38.291377Z","shell.execute_reply.started":"2021-07-26T03:28:38.215447Z","shell.execute_reply":"2021-07-26T03:28:38.29045Z"},"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-26T03:28:38.292738Z","iopub.execute_input":"2021-07-26T03:28:38.293104Z","iopub.status.idle":"2021-07-26T03:39:27.137582Z","shell.execute_reply.started":"2021-07-26T03:28:38.293067Z","shell.execute_reply":"2021-07-26T03:39:27.136762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.save('/kaggle/working/model_decease_type_classification.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T03:41:26.615767Z","iopub.execute_input":"2021-07-26T03:41:26.616143Z","iopub.status.idle":"2021-07-26T03:41:28.812443Z","shell.execute_reply.started":"2021-07-26T03:41:26.616107Z","shell.execute_reply":"2021-07-26T03:41:28.811428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.save('/kaggle/working/model_decease_type_classification_1')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T03:42:03.265375Z","iopub.execute_input":"2021-07-26T03:42:03.26594Z","iopub.status.idle":"2021-07-26T03:43:43.947308Z","shell.execute_reply.started":"2021-07-26T03:42:03.26588Z","shell.execute_reply":"2021-07-26T03:43:43.946374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ! zip \"/kaggle/working/Decease_type.zip\" \"/kaggle/working/model_decease_type_classification_1\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/model_decease_type_classification_1')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/Bbox_256x256')","metadata":{},"execution_count":null,"outputs":[]}]}