{"cells":[{"metadata":{"_uuid":"d5f5166e-603e-461a-aa58-3f173cafe806","_cell_guid":"95f11c5b-07ea-4801-b510-389c9b610842","trusted":true},"cell_type":"markdown","source":"Dataset:\n[https://www.cell.com/cell/fulltext/S0092-8674(18)30154-5]\n[https://data.mendeley.com/datasets/rscbjbr9sj/2]","execution_count":null},{"metadata":{"_uuid":"dcb6d0c3-5056-40ee-ab75-f7d1a607f4ca","_cell_guid":"4efb4a6d-c572-44aa-be3c-aeca7581a5ef","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"import tensorflow as tf\nimport time\nimport matplotlib.pyplot as plt\nimport os, sys\nimport numpy as np\nimport cv2\n%matplotlib inline\n\n\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model, Sequential\nfrom keras import layers\nfrom keras.applications import *\nfrom keras.preprocessing.image import load_img\nimport random\n#from tensorflow.keras.applications import EfficientNetB7\n#1. Creating and compiling the model\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport itertools\nimport matplotlib.pyplot as plt\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom keras import optimizers\nimport tensorflow as tf\nimport time\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras import optimizers\nimport tensorflow as tf\nimport time\nfrom keras.preprocessing import image\nimport random\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.metrics import *\nimport itertools\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"83d4a4b3-836a-43f6-86f4-5a052f2e9980","_cell_guid":"63a4c58f-a689-42fb-be07-ccb3923beb35","trusted":true},"cell_type":"markdown","source":"> > Defining functions","execution_count":null},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"def get_model_transfer(original=\"\", unfreeze_last=4, last_activation='sigmoid', num_labels=2,\n              acc=['acc'], loss='categorical_crossentropy',optimizer=\"adam\", ):\n  \n\n    if original!=\"\":\n        for layer in original.layers[:-unfreeze_last]:\n            layer.trainable = False\n        \n        model = Sequential()\n        model.add(original)\n        model.add(layers.Dropout(0.5))\n        model.add(layers.Flatten())\n        model.add(layers.Dropout(0.5))\n        model.add(layers.Dense(num_labels, activation=last_activation))\n        model.compile(loss=loss,optimizer=optimizer,metrics=acc)\n        return model\n    else: \n        print(\"Please specify an original model !\")\n        return False\n\ndef train_model(model, train_gen, valid_gen, epochs=10, steps_per_epoch=100, my_callbacks=\"\"):\n    if isinstance(my_callbacks, str):\n        history = model.fit_generator(train_gen,\n                                  validation_data=valid_gen,\n                                  epochs=epochs,\n                                  validation_steps=valid_gen.samples/(valid_gen.batch_size*5),\n                                  verbose=1,\n                                  steps_per_epoch=steps_per_epoch)\n    else:\n        history = model.fit_generator(train_gen,\n                                  validation_data=valid_gen,\n                                  epochs=epochs,\n                                  validation_steps=valid_gen.samples/(valid_gen.batch_size*5),\n                                  steps_per_epoch=steps_per_epoch,\n                                  verbose=1,\n                                  callbacks=my_callbacks)\n        \n    return history\n\n\ndef show_accuracy(history, acc='acc', val_acc='val_acc', loss='loss', val_loss='val_loss'):\n    acc = history.history[acc]\n    val_acc = history.history[val_acc]\n    loss = history.history[loss]\n    val_loss = history.history[val_loss]\n    epochs = range(len(acc))\n\n\n    plt.figure(figsize=(15, 5))\n\n    plt.subplot(121)\n    line1, = plt.plot(epochs, acc,'b',label=\"Training acc\")\n    line2, = plt.plot(epochs, val_acc,'r',label=\"Validation acc\")\n    plt.title(\"Training and validation accuracy\")\n    plt.legend(handles=[line1, line2], loc='lower right')\n    plt.ylabel(\"Accuracy\")\n    plt.xlabel(\"Epochs\")\n\n    plt.subplot(122)\n    line3, = plt.plot(epochs, loss,'b',label=\"Training loss\")\n    line4, = plt.plot(epochs, val_loss,'r',label=\"Validation loss\")\n    plt.title(\"Training and validation loss\")\n    plt.legend(handles=[line3, line4], loc='upper right')\n    plt.ylabel(\"Loss\")\n    plt.xlabel(\"Epochs\")\n    plt.show()\n\ndef plot_confusion_matrix(validation_generator, predictions, normalize=False, title='Confusion matrix',\n                          classes=['PNEUMONIA', 'NORMAL'], cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    \n    Y_pred = predictions\n    #Confution Matrix and Classification Report\n    y_pred = np.argmax(Y_pred, axis=1)\n    cm=confusion_matrix(validation_generator.classes, y_pred)\n    \n    print('Classification Report')\n    print(classification_report(validation_generator.classes, y_pred, target_names=classes))\n    \n    \n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    \n#plot_confusion_matrix(cm=confusion_matrix(validation_generator.classes, y_pred), classes=target_names, title='Confusion Matrix')\n\ndef show_predictions(validation_datagen, directory='../input/chest-xray-pneumonia/chest_xray/chest_xray/test',\n                                 target_size=(224, 224),batchsize=10, class_mode='categorical', shuffle=False):\n    # Create a generator for prediction\n    validation_generator = validation_datagen.flow_from_directory(directory,target_size=target_size,\n                                                                batch_size=batchsize,class_mode=class_mode,shuffle=False)\n    # Get the filenames from the generator\n    fnames = validation_generator.filenames\n\n    # Get the ground truth from generator\n    ground_truth = validation_generator.classes\n\n    # Get the label to class mapping from the generator\n    label2index = validation_generator.class_indices\n\n    # Getting the mapping from class index to class label\n    idx2label = dict((v,k) for k,v in label2index.items())\n\n    # Get the predictions from the model using the generator\n    predictions = model.predict_generator(validation_generator,\n                                          steps=validation_generator.samples/validation_generator.batch_size,\n                                          verbose=1)\n    predicted_classes = np.argmax(predictions,axis=1)\n    #show predicted label of first image print(idx2label[np.argmax(predictions[0])])\n    #show original label of first image print(fnames[0].split('/')[0])\n\n    errors = np.where(predicted_classes != ground_truth)[0]\n    print(\"No of errors = {}/{}\".format(len(errors),validation_generator.samples))\n\n    n=0\n    plt.figure(figsize=(15,10))\n    for i in random.sample(range(len(np.argmax(predictions,axis=1))), 20):\n    #pred_class = np.argmax(predictions[errors[i]])\n    #pred_label = idx2label[pred_class]\n        ax = plt.subplot(5,4,n+1)\n        col = 'red' if i in errors else 'green'\n        original = load_img('{}/{}'.format('../input/chest-xray-pneumonia/chest_xray/chest_xray/test',fnames[i]))\n        plt.imshow(original)\n        plt.title('{} : {} : {:.3f}'.format(i,idx2label[np.argmax(predictions[i])], predictions[i][np.argmax(predictions[i])]), color=col)\n        plt.axis('off')\n        n+=1\n    plt.show()\n    return (validation_generator, predictions)\n    \n#print(os.environ)\ndef check_tpu_statue():\n    if 'TPU_NAME' not in os.environ:\n        return False\n    else:\n        return True\n#get_model_transfer(original=VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))).summary();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=10,width_shift_range=0.2,\n                                    height_shift_range=0.2,horizontal_flip=True,fill_mode='nearest')\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\n\n# Change the batchsize according to your system RAM\ntrain_batchsize = 100\nval_batchsize = 10\n\ntrain_generator = train_datagen.flow_from_directory('../input/chest-xray-pneumonia/chest_xray/chest_xray/train',\n                                            target_size=(224, 224), batch_size=train_batchsize, class_mode='categorical')\nvalidation_generator = validation_datagen.flow_from_directory('../input/chest-xray-pneumonia/chest_xray/chest_xray/test',\n                                target_size=(224, 224), batch_size=val_batchsize, class_mode='categorical', shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"F = [1,1,2,3]\nn = 3\nwhile (abs(F[n-2]/F[n-3]-F[n]/F[n-1])>10**(-16)):\n    u = F[n]+F[n-1]\n    F.extend([u])\n    n +=1\n    \nprint(F[n]/F[n-1])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**1. Transfer learning VGG16**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time()\n\ncheckpoint = ModelCheckpoint(\"vgg16_1.h5\", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')\n#reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, min_lr=0.0001, verbose=1,patience=5)\ncallbacks=[checkpoint,early]\n    \nsteps = (train_generator.samples/(train_generator.batch_size*5))\nmodel = get_model_transfer(original=VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)),\n                          optimizer=optimizers.RMSprop(lr=1e-4))\nhistory = train_model(model, train_generator, validation_generator, epochs=50, steps_per_epoch=steps, my_callbacks=callbacks)\nprint(\"Time of trainning is (hh:mm:ss): %s\" % time.strftime('%H:%M:%S', time.gmtime(time.time() - start_time)))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_accuracy(history, acc='acc', val_acc='val_acc', loss='loss', val_loss='val_loss')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_generator, predictions = show_predictions(validation_datagen, directory='../input/chest-xray-pneumonia/chest_xray/chest_xray/test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#binary_predictions = []\n#threshold = thresholds[np.argmax(precisions >= 0.85)]\n#for i in predictions:\n    #if i >= threshold:\n        #binary_predictions.append(1)\n    #else:\n       #binary_predictions.append(0) \n\n\nprint(\"[loss,  accuracy] = \",model.evaluate(val_generator))\n#print('Accuracy on testing set:', accuracy_score(binary_predictions, ground_truth))\n#print('Precision on testing set:', precision_score(binary_predictions, y_test))\n#print('Recall on testing set:', recall_score(binary_predictions, y_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_confusion_matrix(val_generator, predictions)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"2. *Transfer learning VGG19*","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time() \n\ncheckpoint = ModelCheckpoint(\"vgg19_1.h5\", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')\n#reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, min_lr=0.0001, verbose=1,patience=5)\ncallbacks=[checkpoint,early]    \n\nsteps = (train_generator.samples/(train_generator.batch_size*5))\nmodel = get_model_transfer(original=VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3)),\n                                  optimizer=optimizers.RMSprop(lr=1e-4))\nhistory = train_model(model, train_generator, validation_generator, epochs=50, steps_per_epoch=steps, my_callbacks=callbacks)\nprint(\"Time of trainning is (hh:mm:ss): %s\" % time.strftime('%H:%M:%S', time.gmtime(time.time() - start_time)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_accuracy(history, acc='acc', val_acc='val_acc', loss='loss', val_loss='val_loss')\nval_generator, predictions = show_predictions(validation_datagen, directory='../input/chest-xray-pneumonia/chest_xray/chest_xray/test')\nprint(\"[loss,  accuracy] = \",model.evaluate(val_generator))\nplot_confusion_matrix(val_generator, predictions)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**3. NASNetMobile**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time() \n\ncheckpoint = ModelCheckpoint(\"NASNetMobile.h5\", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')\n#reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, min_lr=0.0001, verbose=1,patience=5)\ncallbacks=[checkpoint,early]    \n\nsteps = (train_generator.samples/(train_generator.batch_size*5))\nmodel = get_model_transfer(original=VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3)),\n                                  optimizer=optimizers.RMSprop(lr=1e-4))\nhistory = train_model(model, train_generator, validation_generator, epochs=50, steps_per_epoch=steps, my_callbacks=callbacks)\nprint(\"Time of trainning is (hh:mm:ss): %s\" % time.strftime('%H:%M:%S', time.gmtime(time.time() - start_time)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_accuracy(history, acc='acc', val_acc='val_acc', loss='loss', val_loss='val_loss')\nval_generator, predictions = show_predictions(validation_datagen, directory='../input/chest-xray-pneumonia/chest_xray/chest_xray/test')\nprint(\"[loss,  accuracy] = \",model.evaluate(val_generator))\nplot_confusion_matrix(val_generator, predictions)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"4. ResNet152V2","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time() \n\ncheckpoint = ModelCheckpoint(\"ResNet152V2.h5\", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')\n#reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, min_lr=0.0001, verbose=1,patience=5)\ncallbacks=[checkpoint,early]    \n\nsteps = (train_generator.samples/(train_generator.batch_size*5))\nmodel = get_model_transfer(original=ResNet152V2(weights='imagenet', include_top=False, input_shape=(224, 224, 3)),\n                           optimizer=optimizers.RMSprop(lr=1e-4))\nhistory = train_model(model, train_generator, validation_generator, epochs=50, steps_per_epoch=steps, my_callbacks=callbacks)\nprint(\"Time of trainning is (hh:mm:ss): %s\" % time.strftime('%H:%M:%S', time.gmtime(time.time() - start_time)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_accuracy(history, acc='acc', val_acc='val_acc', loss='loss', val_loss='val_loss')\nval_generator, predictions = show_predictions(validation_datagen, directory='../input/chest-xray-pneumonia/chest_xray/chest_xray/test')\nprint(\"[loss,  accuracy] = \",model.evaluate(val_generator))\nplot_confusion_matrix(val_generator, predictions)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**5. InceptionResNetV2**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time() \n\ncheckpoint = ModelCheckpoint(\"InceptionResNetV2.h5\", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')\n#reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, min_lr=0.0001, verbose=1,patience=5)\ncallbacks=[checkpoint,early]    \n\nsteps = (train_generator.samples/(train_generator.batch_size*5))\nmodel = get_model_transfer(original=InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3)),\n                           optimizer=optimizers.RMSprop(lr=1e-4), unfreeze_last=10)\nhistory = train_model(model, train_generator, validation_generator, epochs=50, steps_per_epoch=steps, my_callbacks=callbacks)\nprint(\"Time of trainning is (hh:mm:ss): %s\" % time.strftime('%H:%M:%S', time.gmtime(time.time() - start_time)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_accuracy(history, acc='acc', val_acc='val_acc', loss='loss', val_loss='val_loss')\nval_generator, predictions = show_predictions(validation_datagen, directory='../input/chest-xray-pneumonia/chest_xray/chest_xray/test')\nprint(\"[loss,  accuracy] = \",model.evaluate(val_generator))\nplot_confusion_matrix(val_generator, predictions)","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}