{"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":"import tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom keras.metrics import Precision, Recall\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import classification_report, accuracy_score\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn import svm\nimport xgboost as xgb\nimport seaborn as sns\nimport cv2\nimport pathlib\nimport os","metadata":{"id":"5Y0tGBy6zpoV"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus: \n    tf.config.experimental.set_memory_growth(gpu, True)\ntf.config.list_physical_devices('GPU')","metadata":{"id":"tcGvW6Io1Z4Z","outputId":"b3ec81b2-f861-467d-a832-8546dbd1c882"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Authenticate and mount Google Drive\nfrom google.colab import auth\nauth.authenticate_user()\nfrom google.colab import drive\ndrive.mount('/content/drive')","metadata":{"id":"Trbc28QH5Ehh","outputId":"c36171ea-26c5-40c8-ef5a-e80fd9325a9c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir_path = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images'\nlabels = pd.read_csv('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv')","metadata":{"id":"0oU5Jwvs1gK5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Credit to https://www.kaggle.com/code/kaledhoshme/classification-foliar-diseases-in-apple-trees for this technique\n#For regular Alexnet\ndiagnosis = labels['patient_overall']\ndir_labels = labels['StudyInstanceUID']\n\n","metadata":{"id":"0kKeKnQlh9w5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images_id_csv)","metadata":{"id":"EUlbysl-lt95","outputId":"3b04b6c9-7916-40c6-f361-3cfb744420f3"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for regular MCC model\n\nfrom PIL import Image\n\nimages = []\n\nfor index, values in enumerate(images_id_csv):\n    img = cv2.imread(os.path.join(f'{images_dir_path}/Train_{index}.jpg'))\n    img_array = Image.fromarray(img, 'RGB')\n    img_array = img_array.resize((227,227))\n    img = np.array(img_array)\n    images.append(img)\n","metadata":{"id":"19M5Xhw0w-cR"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\n\nfor index, values in enumerate(images_id_csv):\n    if (index < len(images)):\n      labels.append([healthy_csv[index], multiple_diseases_csv[index], rust_csv[index],\n                  scab_csv[index]])\n    \nlabels = np.array(labels)\n","metadata":{"id":"zMYEYmb9XRL0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nfor i in range(16):\n  plt.subplot(4, 4, i + 1)\n  plt.imshow(images[i])\n  plt.title(labels[i])\nplt.show()","metadata":{"id":"OFErc3eGGAoJ","outputId":"c505dd54-d955-4bbc-ded9-f4a41e96e635"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Image Augmentation\n\nimages_after_aug = []\nlabels_after_aug = []\n\naugmentor = tf.keras.preprocessing.image.ImageDataGenerator(\n        rotation_range=0.3,\n        zoom_range=0.2,\n        horizontal_flip=True,\n        vertical_flip=True,\n        shear_range=0.2,\n        width_shift_range=0.15,\n        height_shift_range=0.15,\n        rescale=None)\n\nfor index, image in enumerate(images):\n  for i in range(9):\n      img = augmentor.flow(np.reshape(image, (1, 227, 227, 3))).next()\n      images_after_aug.append(np.reshape(img, (227, 227, 3)))\n      labels_after_aug.append(labels[index])\n\nlabels = np.array(labels_after_aug)\nimages = np.array(images_after_aug)","metadata":{"id":"DPsOWXxkGe_e"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\n\nimages = np.array(images)\n\n# convert integer values of X into floats\nimages = images.astype(np.float32)\n\n# normalization \nimages = images/255.0\n","metadata":{"id":"FU28c1LrCTkM"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.array(labels)\nimages = np.array(images)","metadata":{"id":"K01cgmODvKDa"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(images, labels, test_size = 0.2, random_state = 42)","metadata":{"id":"ycKm1J_6xkQw"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#DenseNet121 \n\nd = tf.keras.applications.DenseNet121(include_top=False,\n                   input_shape=(227,227,3),\n                   pooling=None,\n                   weights='imagenet')\nfor i in d.layers:\n  i.trainable = True\noutput_avg = tf.keras.layers.GlobalAveragePooling2D()(d.output)\noutput_max = tf.keras.layers.GlobalMaxPooling2D()(d.output)\nmodel = tf.keras.layers.minimum([output_avg, output_max])\nmodel = tf.keras.layers.Dropout(0.5)(model)\nmodel = tf.keras.layers.Dense(128, activation = \"sigmoid\")(model)\nmodel = tf.keras.layers.Dropout(0.3)(model)\nmodel = tf.keras.layers.Dense(256, activation = \"relu\")(model)\nmodel = tf.keras.layers.Dropout(0.3)(model)\nmodel = tf.keras.layers.Dense(512, activation = \"relu\")(model)\nmodel = tf.keras.layers.Dropout(0.3)(model)\nmodel = tf.keras.layers.Dense(4, activation= \"softmax\")(model)\nmodel = tf.keras.models.Model(inputs = d.input, outputs = model)\nmodel.compile(optimizer = tf.keras.optimizers.Adam(0.001), \n          loss = \"binary_crossentropy\", \n          metrics =[\"accuracy\", \n                     Precision(name='precision'), \n                     Recall(name='recall')])\nmodel.summary()","metadata":{"id":"HEPeSBEVCDeF","outputId":"d831b748-22a8-4d59-a35c-735e8468a9a6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = DenseNet121(include_top = False, input_shape=(227, 227, 3))\n\nmodel = Sequential()\nmodel.add(base_model)\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier(n_neighbors=800)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\ndt = DecisionTreeClassifier()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#AlexNet\n\nmodel = Sequential()\n\nmodel.add(Conv2D(96, (11, 11), strides=(4, 4), input_shape=(227, 227, 3)))\nmodel.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))\nmodel.add(Conv2D(256, (5, 5), activation='relu', padding='same'))\nmodel.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))\nmodel.add(Conv2D(384, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(384, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(4096, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(4096, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))\n\n# compile the model with binary cross-entropy loss and Adam optimizer\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# train the model with the given dataset\nmodel.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_val, y_val))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs = 5, batch_size = 150, validation_data=(x_test, y_test),\n                callbacks = [\n                    tf.keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.1, mode = 'min',\n                                                  patience= 1),\n                    tf.keras.callbacks.EarlyStopping(monitor = 'loss', patience = 3, mode = 'min', restore_best_weights = True)\n                ])","metadata":{"id":"o_T2vCkqt3aK","outputId":"f97e4df7-dace-4277-b1cb-45edd73a34c9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test, y_test, batch_size= 32)","metadata":{"id":"QNthh5uHxG9h","outputId":"959181a5-c5a6-4334-c14f-821e45087e61"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluating the model\n_, acc = model.evaluate(x_test, y_test)\nprint('%.3f' % (acc * 100.0))","metadata":{"id":"QMnEk-4DJZ2_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_accuracy(history):\n  plt.figure(figsize = (10,6))\n  plt.plot(history.history['accuracy'], color = 'blue', label = 'train')\n  plt.plot(history.history['val_accuracy'], color = 'red', label = 'val')\n  plt.legend()\n  plt.title('Accuracy')\n  plt.show()\n  \nplot_accuracy(history)","metadata":{"id":"EUzTsME6RjGe","outputId":"62a74df4-535e-439c-8cc6-e301ab098ee8"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred = []\nfor i in np.arange(len(x_test)):\n  img = x_test[i]\n  img = img.reshape(1, 227, 227, 3)\n  y_p = m.predict(img)\n  y_test_pred.append(y_p)\n\ny_test_pred = np.asarray(y_test_pred)\nprint(y_test_pred)","metadata":{"id":"p7LNftI0Rj9-","outputId":"7a3c4c62-b33d-493e-b3b8-b65ebd0a7615"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = m.predict(x_test)\ny_pred = np.argmax(y_pred, axis = 1)\ny_pred","metadata":{"id":"LOy0nvyLzEXS","outputId":"0c6f08fa-160e-477d-8429-98b8b76db1e9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns \n\ny_test_labels = [np.argmax(vect) for vect in y_test]\ny_test_pred_labels = [np.argmax(vect) for vect in y_test_pred]\n\nconf_mat = confusion_matrix(y_test_labels, y_test_pred_labels)\n\nplt.figure(figsize = (10,8))\nsns.heatmap(conf_mat, linewidths = 0.1, cmap = 'Greens', linecolor = 'gray', \n            fmt = '.1f', annot = True)\nplt.xlabel('Predicted classes', fontsize = 20)\nplt.ylabel('True classes', fontsize = 20)","metadata":{"id":"6QduyD8hKcXn","outputId":"c294dc0e-a35e-4415-82fe-28e14f65a5b1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs = 1, batch_size = 32, validation_data=(x_test,y_test),\n                callbacks = [\n                    tf.keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.1, mode = 'min',\n                                                  patience= 1),\n                    tf.keras.callbacks.EarlyStopping(monitor = 'loss', patience = 3, mode = 'min', restore_best_weights = True)\n                ])","metadata":{"id":"mGVR4SnAxrrv","outputId":"2cd07bb4-9f73-491f-809e-fa5b0de725c2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize = (10, 5))\nplt.plot(history.history['accuracy'], label = \"accuracy\")\nplt.plot(history.history['loss'], label = \"loss\")\nplt.legend()","metadata":{"id":"0vyZNfdaysR2","outputId":"9248d93b-4ced-4ac7-bb57-ffa0d3b719e5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test, y_test, batch_size= 32)","metadata":{"id":"kTV91nz97PIJ","outputId":"89594346-b78b-4697-8c91-2c3f29e157e6"},"execution_count":null,"outputs":[]}]}