{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install keras==2.2.4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from __future__ import print_function\nimport numpy as np\nimport os\nimport sys\nfrom sklearn.model_selection import KFold, StratifiedKFold, train_test_split\nfrom sklearn.utils import shuffle\nimport keras\nfrom keras.models import Model, load_model\nimport tensorflow as tf\nfrom time import time\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential, Input\nfrom keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization, GlobalAveragePooling2D, Lambda\nfrom keras.layers import Conv2D, Conv3D, GlobalAveragePooling3D, Reshape\nfrom keras.callbacks import ModelCheckpoint, TensorBoard, ReduceLROnPlateau\nfrom keras.applications import MobileNetV2, MobileNet, Xception\nfrom sklearn.datasets import make_circles\nfrom sklearn.svm import SVC\nimport xgboost as xgb\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import precision_score\nfrom sklearn.metrics import recall_score\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.metrics import roc_auc_score, roc_curve\nfrom sklearn.metrics import confusion_matrix\nimport keras.backend as K\nimport matplotlib\nmatplotlib.use('pdf')\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = [\"MED_LYMPH_001\",\n\"MED_LYMPH_002\",\n\"MED_LYMPH_003\",\n\"MED_LYMPH_004\",\n\"MED_LYMPH_005\",\n\"MED_LYMPH_006\",\n\"MED_LYMPH_007\",\n\"MED_LYMPH_008\",\n\"MED_LYMPH_009\",\n\"MED_LYMPH_010\",\n\"MED_LYMPH_011\",\n\"MED_LYMPH_012\",\n\"MED_LYMPH_013\",\n\"MED_LYMPH_014\",\n\"MED_LYMPH_015\",\n\"MED_LYMPH_016\",\n\"MED_LYMPH_017\",\n\"MED_LYMPH_018\",\n\"MED_LYMPH_019\",\n\"MED_LYMPH_020\",\n\"MED_LYMPH_021\",\n\"MED_LYMPH_022\",\n\"MED_LYMPH_023\",\n\"MED_LYMPH_024\",\n\"MED_LYMPH_025\",\n\"MED_LYMPH_026\",\n\"MED_LYMPH_027\",\n\"MED_LYMPH_028\",\n\"MED_LYMPH_029\",\n\"MED_LYMPH_030\",\n\"MED_LYMPH_031\",\n\"MED_LYMPH_032\",\n\"MED_LYMPH_033\",\n\"MED_LYMPH_034\",\n\"MED_LYMPH_035\",\n\"MED_LYMPH_036\",\n\"MED_LYMPH_037\",\n\"MED_LYMPH_038\",\n\"MED_LYMPH_039\",\n\"MED_LYMPH_040\",\n\"MED_LYMPH_041\",\n\"MED_LYMPH_042\",\n\"MED_LYMPH_043\",\n\"MED_LYMPH_044\",\n\"MED_LYMPH_045\",\n\"MED_LYMPH_046\",\n\"MED_LYMPH_047\",\n\"MED_LYMPH_048\",\n\"MED_LYMPH_049\",\n\"MED_LYMPH_050\",\n\"MED_LYMPH_051\",\n\"MED_LYMPH_052\",\n\"MED_LYMPH_053\",\n\"MED_LYMPH_054\",\n\"MED_LYMPH_055\",\n\"MED_LYMPH_056\",\n\"MED_LYMPH_057\",\n\"MED_LYMPH_058\",\n\"MED_LYMPH_059\",\n\"MED_LYMPH_060\",\n\"MED_LYMPH_061\",\n\"MED_LYMPH_062\",\n\"MED_LYMPH_063\",\n\"MED_LYMPH_064\",\n\"MED_LYMPH_065\",\n\"MED_LYMPH_066\",\n\"MED_LYMPH_067\",\n\"MED_LYMPH_068\",\n\"MED_LYMPH_069\",\n\"MED_LYMPH_070\",\n\"MED_LYMPH_071\",\n\"MED_LYMPH_072\",\n\"MED_LYMPH_073\",\n\"MED_LYMPH_074\",\n\"MED_LYMPH_075\",\n\"MED_LYMPH_076\",\n\"MED_LYMPH_077\",\n\"MED_LYMPH_078\",\n\"MED_LYMPH_079\",\n\"MED_LYMPH_080\",\n\"MED_LYMPH_081\",\n\"MED_LYMPH_082\",\n\"MED_LYMPH_083\",\n\"MED_LYMPH_084\",\n\"MED_LYMPH_085\",\n\"MED_LYMPH_086\",\n\"MED_LYMPH_087\",\n\"MED_LYMPH_088\",\n\"MED_LYMPH_089\",\n\"MED_LYMPH_090\"]\nids.sort()\nids=np.array(ids)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(0).clf()\n\nnp.set_printoptions(threshold=sys.maxsize)\nseed=5\nnp.random.seed(seed)\n\ntrain_path=\"/kaggle/input/25dii/2.5D-II/train/\"\ntest_path=\"/kaggle/input/25dii/2.5D-II/test/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def check_units(y_true, y_pred):\n    if y_pred.shape[1] != 1:\n      y_pred = y_pred[:,1:2]\n      y_true = y_true[:,1:2]\n    return y_true, y_pred\n\ndef precision(y_true, y_pred):\n    y_true, y_pred = check_units(y_true, y_pred)\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef recall(y_true, y_pred):\n    y_true, y_pred = check_units(y_true, y_pred)\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef f1(y_true, y_pred):\n    def recall(y_true, y_pred):\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n        recall = true_positives / (possible_positives + K.epsilon())\n        return recall\n\n    def precision(y_true, y_pred):\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n        precision = true_positives / (predicted_positives + K.epsilon())\n        return precision\n    y_true, y_pred = check_units(y_true, y_pred)\n    precision = precision(y_true, y_pred)\n    recall = recall(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))\n\ndef acc1(y_true, y_pred):\n    acc1 = K.mean(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))\n    return acc1\n\ndef acc_rec(y_true, y_pred):\n    def recall(y_true, y_pred):\n        y_true, y_pred = check_units(y_true, y_pred)\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n        recall = true_positives / (possible_positives + K.epsilon())\n        return recall\n    \n    def acc1(y_true, y_pred):\n        acc1 = K.mean(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))\n        return acc1\n\n    acc1 = acc1(y_true, y_pred)\n    recall1 = recall(y_true, y_pred)\n    alpha = 0.5\n    return (alpha*acc1+(1-alpha)*recall1)\n\ndef f1_rec(y_true, y_pred):\n    def recall(y_true, y_pred):\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n        recall = true_positives / (possible_positives + K.epsilon())\n        return recall\n\n    def precision(y_true, y_pred):\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n        precision = true_positives / (predicted_positives + K.epsilon())\n        return precision\n    y_true, y_pred = check_units(y_true, y_pred)\n    precision = precision(y_true, y_pred)\n    recall = recall(y_true, y_pred)\n    f1 = 2*((precision*recall)/(precision+recall+K.epsilon()))\n    return (f1+recall)/2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kfold = KFold(n_splits=5, shuffle=True, random_state=seed)\n\naccuracy_list = []\nprecision_list = []\nrecall_list = []\nf1_score_list = []\ncohens_kappa_list = []\nROC_AUC_list = []\ntrue_negative = []\nfalse_negative = []\nfalse_positive = []\ntrue_positive = []\ncnt=0\n\nfor train, test in kfold.split(ids):\n    cnt=cnt+1\n    train_ids=ids[train]\n    test_ids=ids[test]\n    training_data=np.load(train_path+train_ids[0]+\".npy\", allow_pickle=True)\n    X=np.array([i[0] for i in training_data]).reshape(-1,32,32,12)\n    Y=np.array([i[1] for i in training_data])\n\n    training_data=np.load(test_path+test_ids[0]+\".npy\", allow_pickle=True)\n    x_test=np.array([i[0] for i in training_data]).reshape(-1,32,32,12)\n    y_test=np.array([i[1] for i in training_data])\n\n\n    for i in range(1, len(train_ids)):\n        training_data=np.load(train_path+train_ids[i]+\".npy\", allow_pickle=True)\n        X_temp=np.array([i[0] for i in training_data]).reshape(-1,32,32,12)\n        Y_temp=np.array([i[1] for i in training_data])\n        X=np.concatenate((X, X_temp))\n        Y=np.concatenate((Y, Y_temp))\n\n    for i in range(1, len(test_ids)):\n        training_data=np.load(test_path+test_ids[i]+\".npy\", allow_pickle=True)\n        x_test_temp=np.array([i[0] for i in training_data]).reshape(-1,32,32,12)\n        y_test_temp=np.array([i[1] for i in training_data])\n        x_test=np.concatenate((x_test, x_test_temp))\n        y_test=np.concatenate((y_test, y_test_temp))\n\n    X, Y=shuffle(X, Y, random_state=seed)\n    x_test, x_val, y_test, y_val=train_test_split(x_test, y_test, test_size=0.3, random_state=seed)\n\n    print()\n    print(X.shape, Y.shape)\n    print(x_val.shape, y_val.shape)\n    print(x_test.shape, y_test.shape)\n    print()\n\n    opt = keras.optimizers.Adam(lr=0.0001)\n    inp = Input(shape=(32, 32, 12))\n    x = Conv2D(3, 3, padding='same')(inp)\n    x = Lambda(lambda x: K.tf.image.resize_images(x, (224, 224)), input_shape=(32, 32, 3))(x)\n    base_model=MobileNetV2(weights='imagenet',include_top=False,input_shape=(224,224,3))(x) #imports the mobilenet model and discards the last 1000 neuron layer.\n\n    x=base_model\n    x=GlobalAveragePooling2D()(x)\n    x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.\n    x=Dropout(0.2)(x)\n    x=Dense(1024,activation='relu', name=\"SVM\")(x) #dense layer 2\n    x=Dropout(0.3)(x)\n    preds=Dense(2,activation='softmax')(x) #final layer with softmax activation\n\n    model=Model(inputs=inp,outputs=preds)\n    model.summary()\n\n    model_name = \"m1.h5\"\n    model_checkpoint = ModelCheckpoint(model_name, monitor='val_acc', verbose=1, save_best_only=True)\n    \n    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])\n    model.fit(X, Y, batch_size=32, epochs=15, callbacks=[model_checkpoint], validation_data=(x_val, y_val), shuffle=True)\n    \n    print()\n    print(\"#######################################################\")\n    print()\n\n    model_1 = load_model('m1.h5')\n    \n    yhat_probs=model_1.predict(x_test, verbose=1)\n    yhats=yhat_probs.argmax(axis=-1)\n    probs=yhat_probs[:, 1]\n    testy=y_test[:, 1]\n\n    # accuracy: (tp + tn) / (p + n)\n    accuracy = accuracy_score(testy, yhats)\n    print('Accuracy: %f' % accuracy)\n\n    # precision tp / (tp + fp)\n    precision = precision_score(testy, yhats)\n    print('Precision: %f' % precision)\n\n    # recall: tp / (tp + fn)\n    recall = recall_score(testy, yhats)\n    print('Recall: %f' % recall)\n\n    # f1: 2 tp / (2 tp + fp + fn)\n    f1 = f1_score(testy, yhats)\n    print('F1 score: %f' % f1)\n \n    # kappa\n    kappa = cohen_kappa_score(testy, yhats)\n    print('Cohens kappa: %f' % kappa)\n\n    # ROC AUC\n    auc = roc_auc_score(testy, probs)\n    print('ROC AUC: %f' % auc)\n\n    # confusion matrix\n    matrix = confusion_matrix(testy, yhats)\n    print(matrix)\n\n    fpr, tpr, thresh = roc_curve(testy, probs)\n    plt.plot(fpr, tpr, label=\"fold \"+str(cnt)+\", auc=\"+str(auc), linestyle='dashed')\n\n    true_negative.append(matrix[0][0])\n    false_negative.append(matrix[1][0])\n    false_positive.append(matrix[0][1])\n    true_positive.append(matrix[1][1])\n    precision_list.append(precision)\n    recall_list.append(recall)\n    f1_score_list.append(f1)\n    cohens_kappa_list.append(kappa)\n    ROC_AUC_list.append(auc)\n    accuracy_list.append(accuracy * 100)\n\n    print()\n    print(\"#######################################################\")\n    print()\n    \n    svm_training=[]\n    svm_testing=[]\n    intermediate_layer_model=Model(inputs=model.input, outputs=model.get_layer(\"SVM\").output)\n\n    intermediate_output1=intermediate_layer_model.predict(X)\n    for i in range(intermediate_output1.shape[0]):\n        svm_training.append([np.array(intermediate_output1[i]), np.array(Y[i])])\n\n    intermediate_output1=intermediate_layer_model.predict(x_test)\n    for i in range(intermediate_output1.shape[0]):\n        svm_testing.append([np.array(intermediate_output1[i]), np.array(y_test[i])])\n\n    np.save(\"training_\"+str(cnt)+\".npy\", svm_training)\n    np.save(\"testing_\"+str(cnt)+\".npy\", svm_testing)\n\nplt.legend(loc=0)\nplt.savefig(\"m1\")\nplt.close()\n\n\nprint(\"%.2f%% (+/- %.2f%%)\" % (np.mean(accuracy_list), np.std(accuracy_list)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(precision_list), np.std(precision_list)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(recall_list), np.std(recall_list)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(f1_score_list), np.std(f1_score_list)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(cohens_kappa_list), np.std(cohens_kappa_list)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(ROC_AUC_list), np.std(ROC_AUC_list)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(true_negative), np.std(true_negative)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(false_negative), np.std(false_negative)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(false_positive), np.std(false_positive)))\nprint(\"%.2f (+/- %.2f)\" % (np.mean(true_positive), np.std(true_positive)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}