{"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":"markdown","source":"Problem statement\n\nRegression prediction of covid","metadata":{}},{"cell_type":"markdown","source":"Import libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:35.923372Z","iopub.execute_input":"2023-06-09T14:48:35.923662Z","iopub.status.idle":"2023-06-09T14:48:37.198506Z","shell.execute_reply.started":"2023-06-09T14:48:35.923635Z","shell.execute_reply":"2023-06-09T14:48:37.197337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Get files","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:37.204316Z","iopub.execute_input":"2023-06-09T14:48:37.206749Z","iopub.status.idle":"2023-06-09T14:48:40.125974Z","shell.execute_reply.started":"2023-06-09T14:48:37.206713Z","shell.execute_reply":"2023-06-09T14:48:40.125057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read files","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/covid-final-exam/train.csv', header=None)\ntrain.columns = ['Image', 'Prediction']\ntest = pd.read_csv('/kaggle/input/covid-final-exam/test.csv')\nsample = pd.read_csv('/kaggle/input/covid-final-exam/sample.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.126937Z","iopub.execute_input":"2023-06-09T14:48:40.127295Z","iopub.status.idle":"2023-06-09T14:48:40.169004Z","shell.execute_reply.started":"2023-06-09T14:48:40.127264Z","shell.execute_reply":"2023-06-09T14:48:40.168080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.173865Z","iopub.execute_input":"2023-06-09T14:48:40.176019Z","iopub.status.idle":"2023-06-09T14:48:40.196382Z","shell.execute_reply.started":"2023-06-09T14:48:40.175981Z","shell.execute_reply":"2023-06-09T14:48:40.195405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.201055Z","iopub.execute_input":"2023-06-09T14:48:40.203530Z","iopub.status.idle":"2023-06-09T14:48:40.215279Z","shell.execute_reply.started":"2023-06-09T14:48:40.203496Z","shell.execute_reply":"2023-06-09T14:48:40.214217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.217240Z","iopub.execute_input":"2023-06-09T14:48:40.218350Z","iopub.status.idle":"2023-06-09T14:48:40.234210Z","shell.execute_reply.started":"2023-06-09T14:48:40.218286Z","shell.execute_reply":"2023-06-09T14:48:40.233213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now in the filepaths column create the full path to the image file\ntrain_image_dir=r'/kaggle/input/covid-final-exam/Train'\ntrain_imglist=sorted(os.listdir(train_image_dir))\ntrain_paths=[]\nfor i in range (len(train_imglist)):\n    train_paths.append(os.path.join(train_image_dir, train_imglist[i]))    \nprint (train_paths[0])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.236654Z","iopub.execute_input":"2023-06-09T14:48:40.237049Z","iopub.status.idle":"2023-06-09T14:48:40.256991Z","shell.execute_reply.started":"2023-06-09T14:48:40.237010Z","shell.execute_reply":"2023-06-09T14:48:40.256000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now in the filepaths column create the full path to the image file\ntest_image_dir=r'/kaggle/input/covid-final-exam/Test'\ntest_imglist=sorted(os.listdir(test_image_dir))\ntest_paths=[]\nfor i in range (len(test_imglist)):\n    test_paths.append(os.path.join(test_image_dir, test_imglist[i]))    \nprint (test_paths[0])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.261003Z","iopub.execute_input":"2023-06-09T14:48:40.261413Z","iopub.status.idle":"2023-06-09T14:48:40.278455Z","shell.execute_reply.started":"2023-06-09T14:48:40.261371Z","shell.execute_reply":"2023-06-09T14:48:40.277519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now in the train filepaths column insert the full filepath\ntrain['filepaths']=train_paths\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.282387Z","iopub.execute_input":"2023-06-09T14:48:40.282965Z","iopub.status.idle":"2023-06-09T14:48:40.306447Z","shell.execute_reply.started":"2023-06-09T14:48:40.282928Z","shell.execute_reply":"2023-06-09T14:48:40.305423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now in the train_anno filepaths column insert the full filepath\ntest['filepaths']=test_paths\ntest","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.312677Z","iopub.execute_input":"2023-06-09T14:48:40.313089Z","iopub.status.idle":"2023-06-09T14:48:40.335085Z","shell.execute_reply.started":"2023-06-09T14:48:40.313045Z","shell.execute_reply":"2023-06-09T14:48:40.333980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show image\nimport PIL # We will import the packages at \"use-time (just for this kernel)\n\nPIL.Image.open(\"/kaggle/input/covid-final-exam/Train/1110.png\")","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.338916Z","iopub.execute_input":"2023-06-09T14:48:40.340565Z","iopub.status.idle":"2023-06-09T14:48:40.384853Z","shell.execute_reply.started":"2023-06-09T14:48:40.340529Z","shell.execute_reply":"2023-06-09T14:48:40.383895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show image\nimport PIL # We will import the packages at \"use-time (just for this kernel)\n\nPIL.Image.open(\"/kaggle/input/covid-final-exam/Test/3350.png\")","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.389107Z","iopub.execute_input":"2023-06-09T14:48:40.391291Z","iopub.status.idle":"2023-06-09T14:48:40.430409Z","shell.execute_reply.started":"2023-06-09T14:48:40.391255Z","shell.execute_reply":"2023-06-09T14:48:40.429277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analyse prediction","metadata":{}},{"cell_type":"code","source":"sns.displot(train['Prediction'], kde=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:40.434571Z","iopub.execute_input":"2023-06-09T14:48:40.436851Z","iopub.status.idle":"2023-06-09T14:48:41.037635Z","shell.execute_reply.started":"2023-06-09T14:48:40.436814Z","shell.execute_reply":"2023-06-09T14:48:41.036279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Prediction'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:41.040845Z","iopub.execute_input":"2023-06-09T14:48:41.041228Z","iopub.status.idle":"2023-06-09T14:48:41.052924Z","shell.execute_reply.started":"2023-06-09T14:48:41.041196Z","shell.execute_reply":"2023-06-09T14:48:41.051797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_multi = train[\"Prediction\"].value_counts() > 1\nfiltered = train[train[\"Prediction\"].isin(is_multi[is_multi].index)]\ntrain = filtered\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:41.054511Z","iopub.execute_input":"2023-06-09T14:48:41.055126Z","iopub.status.idle":"2023-06-09T14:48:41.075643Z","shell.execute_reply.started":"2023-06-09T14:48:41.055092Z","shell.execute_reply":"2023-06-09T14:48:41.073862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split dataset","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# lets create a test data frame by splitting the train dataframe into a train dataframe and a test data frame\ntrain_df, val_df=train_test_split(train, train_size=.8, shuffle=True, random_state=42,stratify=train['Prediction'])\nprint ('train_df length: ', len(train_df), '  val_df length: ', len(val_df), '  test length: ', len(test))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:41.077327Z","iopub.execute_input":"2023-06-09T14:48:41.077750Z","iopub.status.idle":"2023-06-09T14:48:41.251967Z","shell.execute_reply.started":"2023-06-09T14:48:41.077713Z","shell.execute_reply":"2023-06-09T14:48:41.250863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Tensorflow","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Activation,Dropout,Conv2D, MaxPooling2D,BatchNormalization\nfrom tensorflow.keras.optimizers import Adam, Adamax\nfrom tensorflow.keras.metrics import categorical_crossentropy\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import backend as K","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:41.254382Z","iopub.execute_input":"2023-06-09T14:48:41.255030Z","iopub.status.idle":"2023-06-09T14:48:49.182454Z","shell.execute_reply.started":"2023-06-09T14:48:41.254990Z","shell.execute_reply":"2023-06-09T14:48:49.181408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the train set is balance and has an adequate number of images for classification\n# lets create generators for the three dataframes\nimg_size=(224,224) # image size\nbs=40 # batch size\nt_gen=ImageDataGenerator(horizontal_flip=True) # generator for training\ngen=ImageDataGenerator() # generator for validation and test \ntrain_gen=t_gen.flow_from_dataframe(train_df, x_col='filepaths', y_col='Prediction', target_size=img_size,\n                                   color_mode='rgb',batch_size=bs, shuffle=True, seed=123,\n                                   class_mode= 'raw')\nval_gen=gen.flow_from_dataframe(val_df, x_col='filepaths', y_col='Prediction', target_size=img_size,\n                                   color_mode='rgb',batch_size=bs, shuffle=False, seed=123,\n                                   class_mode= 'raw')\ntest_gen=gen.flow_from_dataframe(test, x_col='filepaths', y_col='Prediction', target_size=img_size,\n                                   color_mode='rgb',batch_size=bs, shuffle=False, seed=123,\n                                   class_mode= 'raw')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:49.184638Z","iopub.execute_input":"2023-06-09T14:48:49.185795Z","iopub.status.idle":"2023-06-09T14:48:51.017275Z","shell.execute_reply.started":"2023-06-09T14:48:49.185759Z","shell.execute_reply":"2023-06-09T14:48:51.016212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I will use transfer learning using the EfficientNetB0 model\n#class_count=2 # number of classes\nlr= .001 # learning rate\nimg_shape=(224,224,3) # shape of color images\nbase_model=tf.keras.applications.EfficientNetV2B0(include_top=False, weights=\"imagenet\",\n                                                   input_shape=img_shape, pooling='max')        \nbase_model.trainable=True\nx=base_model.output\nx=BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)\nx = Dense(256, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),\n                bias_regularizer=regularizers.l1(0.006) ,activation='relu')(x)\nx=Dropout(rate=.4, seed=123)(x)       \noutput=Dense(1, activation='linear')(x)\nmodel=Model(inputs=base_model.input, outputs=output)\nmodel.compile(Adamax(learning_rate=lr), loss='MeanSquaredError', metrics=['mse'])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:51.018691Z","iopub.execute_input":"2023-06-09T14:48:51.019072Z","iopub.status.idle":"2023-06-09T14:48:56.957972Z","shell.execute_reply.started":"2023-06-09T14:48:51.019035Z","shell.execute_reply":"2023-06-09T14:48:56.956987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lets define 2 callbacks , reduce learning rate on plateau and early stopping\nrlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor=\"val_loss\", factor=0.4,  patience=2,\n                                            verbose=1)\nestop=tf.keras.callbacks.EarlyStopping(monitor=\"val_loss\", patience=4,restore_best_weights=True)\ncallbacks=[rlronp, estop]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:56.959557Z","iopub.execute_input":"2023-06-09T14:48:56.959892Z","iopub.status.idle":"2023-06-09T14:48:56.968299Z","shell.execute_reply.started":"2023-06-09T14:48:56.959849Z","shell.execute_reply":"2023-06-09T14:48:56.967308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now lets train the model\nepochs=10\nhistory=model.fit(x=train_gen,   epochs=epochs, verbose=1, callbacks=callbacks,  validation_data=val_gen,\n                   validation_steps=None,  shuffle=True,  initial_epoch=0) # train the model","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:48:56.969848Z","iopub.execute_input":"2023-06-09T14:48:56.970216Z","iopub.status.idle":"2023-06-09T14:52:34.088154Z","shell.execute_reply.started":"2023-06-09T14:48:56.970185Z","shell.execute_reply":"2023-06-09T14:52:34.087097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).plot(figsize=(8,5))\nplt.grid(True)\nplt.gca().set_ylim(0,1000)\nplt.show","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:34.089755Z","iopub.execute_input":"2023-06-09T14:52:34.090231Z","iopub.status.idle":"2023-06-09T14:52:34.434109Z","shell.execute_reply.started":"2023-06-09T14:52:34.090193Z","shell.execute_reply":"2023-06-09T14:52:34.433213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = (model.predict(val_gen))\ny_pred = y_pred.flatten()\ny_pred","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:34.435579Z","iopub.execute_input":"2023-06-09T14:52:34.435928Z","iopub.status.idle":"2023-06-09T14:52:38.018060Z","shell.execute_reply.started":"2023-06-09T14:52:34.435893Z","shell.execute_reply":"2023-06-09T14:52:38.016999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = model.evaluate(val_gen)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:38.019596Z","iopub.execute_input":"2023-06-09T14:52:38.020232Z","iopub.status.idle":"2023-06-09T14:52:40.268052Z","shell.execute_reply.started":"2023-06-09T14:52:38.020195Z","shell.execute_reply":"2023-06-09T14:52:40.267021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(y_pred, kde=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:40.269527Z","iopub.execute_input":"2023-06-09T14:52:40.269900Z","iopub.status.idle":"2023-06-09T14:52:40.710128Z","shell.execute_reply.started":"2023-06-09T14:52:40.269839Z","shell.execute_reply":"2023-06-09T14:52:40.709233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on test set","metadata":{}},{"cell_type":"code","source":"predictions = (model.predict(test_gen))\npredictions = predictions.flatten()\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:40.711730Z","iopub.execute_input":"2023-06-09T14:52:40.712092Z","iopub.status.idle":"2023-06-09T14:52:49.750098Z","shell.execute_reply.started":"2023-06-09T14:52:40.712058Z","shell.execute_reply":"2023-06-09T14:52:49.748944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(predictions,kde=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:49.751637Z","iopub.execute_input":"2023-06-09T14:52:49.752507Z","iopub.status.idle":"2023-06-09T14:52:50.205249Z","shell.execute_reply.started":"2023-06-09T14:52:49.752464Z","shell.execute_reply":"2023-06-09T14:52:50.204230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'Image': sample['Image'], 'Prediction':predictions})\nsubmission.to_csv('submission.csv', index=False)\nsubmission = pd.read_csv('submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-06-09T14:52:50.210245Z","iopub.execute_input":"2023-06-09T14:52:50.210558Z","iopub.status.idle":"2023-06-09T14:52:50.233271Z","shell.execute_reply.started":"2023-06-09T14:52:50.210532Z","shell.execute_reply":"2023-06-09T14:52:50.232348Z"},"trusted":true},"execution_count":null,"outputs":[]}]}