{"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":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport csv\nimport os\nimport os.path as op\nimport random\nfrom tqdm import tqdm\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\n\n# import torch, torchvision\n# import torch.nn as nn\n# import torch.nn.functional as F\n# import torch.optim as optim\n# from torchvision import datasets, transforms\n# from torch.utils.data.dataset import Dataset   \n# torch.backends.cudnn.benchmark=True\n\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 \nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow import keras\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:43:41.463861Z","iopub.execute_input":"2022-03-18T20:43:41.464111Z","iopub.status.idle":"2022-03-18T20:43:41.478535Z","shell.execute_reply.started":"2022-03-18T20:43:41.464083Z","shell.execute_reply":"2022-03-18T20:43:41.477659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training/Test Folders\ndata_dir = '../input/covidx-cxr2'\ntrain_folder = data_dir + '/train'\ntest_folder = data_dir + '/test'","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:19:54.889775Z","iopub.execute_input":"2022-03-18T20:19:54.89004Z","iopub.status.idle":"2022-03-18T20:19:54.894353Z","shell.execute_reply.started":"2022-03-18T20:19:54.890011Z","shell.execute_reply":"2022-03-18T20:19:54.893378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/covidx-cxr2/train.txt', sep=\" \", header=None)\ntrain_df.columns=['patient id', 'filename', 'class', 'data source']\ntrain_df=train_df.drop(['patient id', 'data source'], axis=1 )","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:19:56.68596Z","iopub.execute_input":"2022-03-18T20:19:56.686694Z","iopub.status.idle":"2022-03-18T20:19:56.779538Z","shell.execute_reply.started":"2022-03-18T20:19:56.686658Z","shell.execute_reply":"2022-03-18T20:19:56.77876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/covidx-cxr2/test.txt', sep=\" \", header=None)\ntest_df.columns=['id', 'filename', 'class', 'data source' ]\ntest_df=test_df.drop(['id', 'data source'], axis=1 )","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:19:58.780686Z","iopub.execute_input":"2022-03-18T20:19:58.780938Z","iopub.status.idle":"2022-03-18T20:19:58.794279Z","shell.execute_reply.started":"2022-03-18T20:19:58.78091Z","shell.execute_reply":"2022-03-18T20:19:58.793607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/covidx-cxr2/train/'  #directory path\ntest_path = '../input/covidx-cxr2/test/'","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:20:00.300327Z","iopub.execute_input":"2022-03-18T20:20:00.300837Z","iopub.status.idle":"2022-03-18T20:20:00.304842Z","shell.execute_reply.started":"2022-03-18T20:20:00.3008Z","shell.execute_reply":"2022-03-18T20:20:00.304059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"negative  = train_df[train_df['class']=='negative']   #negative values in class column\npositive = train_df[train_df['class']=='positive']  #positive values in class column\n\nfrom sklearn.utils import resample\n\ndf_majority_downsampled = resample(negative, replace = True, n_samples = 2158) \n\ntrain_df = pd.concat([positive, df_majority_downsampled])\n\nfrom sklearn.utils import shuffle\n\ntrain_df = shuffle(train_df) # shuffling so that there is particular sequence","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:20:02.519594Z","iopub.execute_input":"2022-03-18T20:20:02.520304Z","iopub.status.idle":"2022-03-18T20:20:02.54613Z","shell.execute_reply.started":"2022-03-18T20:20:02.520238Z","shell.execute_reply":"2022-03-18T20:20:02.545361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:20:11.631801Z","iopub.execute_input":"2022-03-18T20:20:11.632059Z","iopub.status.idle":"2022-03-18T20:20:11.644177Z","shell.execute_reply.started":"2022-03-18T20:20:11.632031Z","shell.execute_reply":"2022-03-18T20:20:11.643522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train_df, train_size=0.9, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:20:14.607244Z","iopub.execute_input":"2022-03-18T20:20:14.607939Z","iopub.status.idle":"2022-03-18T20:20:14.616432Z","shell.execute_reply.started":"2022-03-18T20:20:14.607903Z","shell.execute_reply":"2022-03-18T20:20:14.615557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255.,rotation_range = 40, width_shift_range = 0.2, height_shift_range = 0.2, \n                                   shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True, vertical_flip =True)\ntest_datagen = ImageDataGenerator(rescale = 1.0/255.)\n\n#Now fit the them to get the images from directory (name of the images are given in dataframe) with augmentation\n\n\ntrain_gen = train_datagen.flow_from_dataframe(dataframe = train_df, directory=train_path, x_col='filename', \n                                              y_col='class', target_size=(224,224), batch_size=64, \n                                               class_mode='binary')\nvalid_gen = test_datagen.flow_from_dataframe(dataframe = valid_df, directory=train_path, x_col='filename',\n                                             y_col='class', target_size=(224,224), batch_size=64, \n                                            class_mode='binary')\ntest_gen = test_datagen.flow_from_dataframe(dataframe = test_df, directory=test_path, x_col='filename', \n                                            y_col='class', target_size=(224,224), batch_size=64,\n                                             class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:21:00.467837Z","iopub.execute_input":"2022-03-18T20:21:00.468383Z","iopub.status.idle":"2022-03-18T20:21:09.503384Z","shell.execute_reply.started":"2022-03-18T20:21:00.468344Z","shell.execute_reply":"2022-03-18T20:21:09.502611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagenet_resnet_model = ResNet50(weights='imagenet', input_shape=(224,224,3))\n#Remove output layer for default classifier\noutput = imagenet_resnet_model.layers[-2].output\nresnet_model = Model(imagenet_resnet_model.input, output)\n# print(resnet_model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:21:12.220547Z","iopub.execute_input":"2022-03-18T20:21:12.220813Z","iopub.status.idle":"2022-03-18T20:21:16.803513Z","shell.execute_reply.started":"2022-03-18T20:21:12.220784Z","shell.execute_reply":"2022-03-18T20:21:16.802732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagenet_mobilenet_model = MobileNetV2(include_top=False, weights='imagenet')\n#Remove output layer for default classifier\noutput = imagenet_mobilenet_model.layers[-2].output\nmobilenet_model = Model(imagenet_mobilenet_model.input, output)\n# print(mobilenet_model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:21:39.44335Z","iopub.execute_input":"2022-03-18T20:21:39.444058Z","iopub.status.idle":"2022-03-18T20:21:40.581368Z","shell.execute_reply.started":"2022-03-18T20:21:39.44402Z","shell.execute_reply":"2022-03-18T20:21:40.580654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.compile(optimizer = keras.optimizers.Adam(learning_rate=0.001),\n                     loss = 'binary_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:22:23.663068Z","iopub.execute_input":"2022-03-18T20:22:23.663686Z","iopub.status.idle":"2022-03-18T20:22:23.69491Z","shell.execute_reply.started":"2022-03-18T20:22:23.663646Z","shell.execute_reply":"2022-03-18T20:22:23.694263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.compile(optimizer = keras.optimizers.Adam(learning_rate=0.001),\n                     loss = 'binary_crossentropy')\ncallbacks = [\n    tf.keras.callbacks.ModelCheckpoint(\"covid_classifier_model.h5\", save_best_only=True, verbose = 0),\n    tf.keras.callbacks.EarlyStopping(patience=3, monitor='val_loss', verbose=1),\n    tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, verbose=1)\n]\n\nhistory = resnet_model.fit(train_gen, \n                    validation_data=valid_gen, epochs=20, \n                    callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2022-03-18T20:44:10.293214Z","iopub.execute_input":"2022-03-18T20:44:10.293812Z","iopub.status.idle":"2022-03-18T22:52:04.774595Z","shell.execute_reply.started":"2022-03-18T20:44:10.293776Z","shell.execute_reply":"2022-03-18T22:52:04.772928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.load_weights('./covid_classifier_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-03-18T22:55:32.903915Z","iopub.execute_input":"2022-03-18T22:55:32.904197Z","iopub.status.idle":"2022-03-18T22:55:33.239929Z","shell.execute_reply.started":"2022-03-18T22:55:32.904159Z","shell.execute_reply":"2022-03-18T22:55:33.239121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.save_weights(\"./\")","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:41.978744Z","iopub.execute_input":"2022-03-17T20:27:41.979248Z","iopub.status.idle":"2022-03-17T20:27:42.981419Z","shell.execute_reply.started":"2022-03-17T20:27:41.979213Z","shell.execute_reply":"2022-03-17T20:27:42.980577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.evaluate(test_gen)","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:34:26.208975Z","iopub.execute_input":"2022-03-17T20:34:26.209734Z","iopub.status.idle":"2022-03-17T20:35:14.271027Z","shell.execute_reply.started":"2022-03-17T20:34:26.209603Z","shell.execute_reply":"2022-03-17T20:35:14.270219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = (resnet_model.predict(test_gen)>0.5).astype(\"int32\")\n\npreds","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:39:55.607717Z","iopub.execute_input":"2022-03-17T20:39:55.607971Z","iopub.status.idle":"2022-03-17T20:40:23.023901Z","shell.execute_reply.started":"2022-03-17T20:39:55.607943Z","shell.execute_reply":"2022-03-17T20:40:23.023009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_clients = 20\nnum_selected = 6\nnum_rounds = 150\nepochs = 5\nbatch_size = 32\nshared_layers = 10","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.14609Z","iopub.status.idle":"2022-03-17T20:27:31.146688Z","shell.execute_reply.started":"2022-03-17T20:27:31.146443Z","shell.execute_reply":"2022-03-17T20:27:31.14647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_client_models(num_clients):\n    resnet_clients = []\n    mobilenet_clients = []\n    for client in num_clients:\n        resnet_clients.append(ResNet50(include_top=True, weights='imagenet'))\n        mobilenet_clients.append(MobileNetV2(include_top=True, weights='imagenet'))\n    return resnet_clients, mobilenet_clients","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.147885Z","iopub.status.idle":"2022-03-17T20:27:31.14845Z","shell.execute_reply.started":"2022-03-17T20:27:31.148218Z","shell.execute_reply":"2022-03-17T20:27:31.148243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_global_models():\n    resnet_model = ResNet50(include_top=True, weights='imagenet')\n    mobilenet_model = MobileNetV2(include_top=True, weights='imagenet')\n    return resnet_model, mobilenet_model","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.1497Z","iopub.status.idle":"2022-03-17T20:27:31.150384Z","shell.execute_reply.started":"2022-03-17T20:27:31.150067Z","shell.execute_reply":"2022-03-17T20:27:31.150097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Start by actually applying the algorithm","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.151455Z","iopub.status.idle":"2022-03-17T20:27:31.152005Z","shell.execute_reply.started":"2022-03-17T20:27:31.151771Z","shell.execute_reply":"2022-03-17T20:27:31.151797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Training\ndef train_clients(client_list, input_data):\n    for model in client_list:\n        model.fit(input_data[0], input_data[1])\n    \n    pass","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.15314Z","iopub.status.idle":"2022-03-17T20:27:31.153701Z","shell.execute_reply.started":"2022-03-17T20:27:31.153467Z","shell.execute_reply":"2022-03-17T20:27:31.153493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Update\ndef update_global(client_list):\n    input_layer = []\n    output_layer = []\n    for client in client_list:\n        input_layer.append(client.layers[0])\n    pass","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.154783Z","iopub.status.idle":"2022-03-17T20:27:31.155347Z","shell.execute_reply.started":"2022-03-17T20:27:31.155096Z","shell.execute_reply":"2022-03-17T20:27:31.155122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get model info from clients\ndef get_client_models(client_list):\n    \n    \n    pass","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.156425Z","iopub.status.idle":"2022-03-17T20:27:31.156967Z","shell.execute_reply.started":"2022-03-17T20:27:31.156736Z","shell.execute_reply":"2022-03-17T20:27:31.156762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Pass global model info to client\ndef update_clients(global_model):\n    \n    pass","metadata":{"execution":{"iopub.status.busy":"2022-03-17T20:27:31.158067Z","iopub.status.idle":"2022-03-17T20:27:31.158641Z","shell.execute_reply.started":"2022-03-17T20:27:31.158409Z","shell.execute_reply":"2022-03-17T20:27:31.158434Z"},"trusted":true},"execution_count":null,"outputs":[]}]}