{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2 0 1]\n", " [0 1 0]\n", " [1 1 2]]\n" ] }, { "data": { "text/plain": [ "(0.3548387096774194, 0.3076923076923077)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.metrics import cohen_kappa_score, confusion_matrix\n", "y_true = [2, 0, 2, 2, 0, 0, 1, 2]\n", "y_pred = [1, 0, 2, 2, 0, 2, 1, 0]\n", "\n", "print( confusion_matrix(y_true, y_pred))\n", "cohen_kappa_score(y_true, y_pred, weights='linear'), cohen_kappa_score(y_true, y_pred, weights='quadratic'), \n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.5/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", " from ._conv import register_converters as _register_converters\n", "Using TensorFlow backend.\n" ] } ], "source": [ "import json\n", "import math\n", "import os\n", "\n", "import cv2\n", "from PIL import Image\n", "import numpy as np\n", "from keras import layers\n", "from keras.applications import DenseNet121\n", "from keras.callbacks import Callback, ModelCheckpoint\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.models import Sequential\n", "from keras.optimizers import Adam\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import cohen_kappa_score, accuracy_score\n", "import scipy\n", "import tensorflow as tf\n", "from tqdm import tqdm\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"2\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Edited Point Loss" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def hp_ordering_loss(y_true, y_pred):\n", "\n", " \"\"\" Evaluate the ordinal loss of the predictions y_pred.\n", " \n", " Parameters\n", " ----------\n", " y_true : array-like\n", " y_pred : array-like\n", " \n", " Returns\n", " -------\n", " loss: float\n", " A non-negative floating point value (best value is 0.0)\n", " \n", " Usage\n", " -------\n", " loss = hp_ordering_loss([4,1,2,0,4,2,1], [6.0,3.1,5.2,1.0,4.0,2.2,3.7])\n", " print('Loss: ', loss.numpy()) # Loss: 2.8\n", " \n", " Usage with the `compile` API:\n", " \n", " ```python\n", " model = tf.keras.Model(inputs, outputs)\n", " model.compile(loss=hp_ordering_loss, optimizer='adam', loss=hp_ordering_loss)\n", " ```\n", " \n", " \"\"\"\n", "\n", " y_pred = tf.convert_to_tensor(y_pred)\n", " y_true = tf.dtypes.cast(y_true, y_pred.dtype)\n", " y_pred = tf.reshape(tf.transpose(y_pred),[-1,1])\n", "\n", " # one hot vector for y_true\n", " ords, idx = tf.unique(tf.reshape(y_true, [-1])) \n", " num = tf.shape(ords)[0]\n", " y_true_1hot = tf.one_hot(idx, num)\n", "\n", " # mean distance for each class\n", " yO = tf.transpose(y_pred) @ y_true_1hot\n", " yc = tf.reduce_sum(y_true_1hot,0)\n", " class_mean = tf.divide(yO,yc) \n", "\n", " # min. distance\n", " ords = tf.dtypes.cast(ords, tf.float32)\n", " ords0 = tf.reshape(ords, [-1,1])\n", " ords1 = tf.reshape(ords, [1,-1])\n", " \n", " min_distance = tf.subtract(ords0, ords1)\n", " # apply ReLU\n", " min_distance = tf.nn.relu (min_distance)\n", " \n", " # keeps min. distance\n", " keep = tf.minimum(min_distance,1)\n", "\n", " # distance to centroid \n", " class_mean0 = tf.reshape(class_mean, [-1,1])\n", " class_mean1 = tf.reshape(class_mean, [1,-1])\n", " class_mean = tf.subtract(class_mean0, class_mean1) \n", " # apply ReLU \n", " class_mean = tf.nn.relu(class_mean)\n", " centroid_distance = tf.multiply(keep, class_mean)\n", " \n", " hp_ordering_loss = tf.subtract(min_distance,centroid_distance)\n", " # apply ReLU\n", " hp_ordering_loss = tf.nn.relu(hp_ordering_loss)\n", " \n", " return tf.reduce_sum(hp_ordering_loss)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def hp_point_loss(y_true, y_pred, L, U, margin=0.1):\n", " \"\"\" Evaluate the point loss of the predictions y_pred.\n", "\n", " Parameters\n", " ----------\n", " y_true : array-like\n", " y_pred : array-like\n", " minlabel : integer\n", " maxlabel : integer\n", " margin : float\n", "\n", " Returns\n", " -------\n", " loss: float\n", " A non-negative floating point value (best value is 0.0)\n", " \n", " Usage\n", " -------\n", " loss = hp_point_loss([4,1,2,0,4,2,1], [6.0,3.1,5.2,1.0,4.0,2.2,3.7],0,4,.3)\n", " print('Loss: ', loss.numpy()) # Loss: 3.1\n", " \n", " \n", " Usage with the `compile` API:\n", " \n", " ```python\n", " \n", " Example Keras wrapper for hp_point_loss:\n", " \n", " def get_ohpl_wrapper (min_label, max_label, margin):\n", " def ohpl(y_true, y_pred):\n", " return hp_point_loss(y_true, y_pred, min_label, max_label, margin)\n", " return ohpl\n", "\n", " point_loss = get_ohpl_wrapper(0,4,0.01)\n", " \n", " model = tf.keras.Model(inputs, outputs)\n", " model.compile(loss=ohpl_point_loss, optimizer='adam', loss=ohpl_point_loss)\n", " ```\n", " \n", " \"\"\"\n", " \n", " y_pred = tf.reshape(tf.convert_to_tensor(y_pred), [-1,1])\n", " y_true = tf.cast(y_true, dtype=tf.float32)\n", " \n", " upper = tf.reshape(tf.constant(U, dtype=tf.float32), [-1,1])\n", " lower = tf.reshape(tf.constant(L, dtype=tf.float32), [-1,1])\n", " uthreshold = tf.matmul(y_true, upper, name='upper_MM')\n", " lthreshold = tf.matmul(y_true, lower, name='lower_MM')\n", "\n", " upper_loss = tf.nn.relu(tf.subtract(y_pred, uthreshold))\n", " lower_loss = tf.nn.relu(tf.subtract(lthreshold, y_pred))\n", " \n", " hp_point_loss = tf.add(upper_loss, lower_loss)\n", " \n", " return tf.reduce_sum(hp_point_loss)\n", " \n", " \"\"\" \n", " References\n", " ----------\n", " .. [1] Vanderheyden, Bob and Ying Xie. Ordinal Hyperplane Loss. (2018). \n", " 2018 IEEE International Conference on Big Data (Big Data), \n", " 2018 IEEE International Conference On, 2337. https://doi-org.proxy.kennesaw.edu/10.1109/BigData.2018.8622079\n", " \"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# new weighted point loss" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def wgt_point_loss(y_true, y_pred, L, U, wgt, margin=0.1):\n", " \"\"\" Evaluate the point loss of the predictions y_pred.\n", "\n", " Parameters\n", " ----------\n", " y_true : array-like\n", " y_pred : array-like\n", " minlabel : integer\n", " maxlabel : integer\n", " margin : float\n", "\n", " Returns\n", " -------\n", " loss: float\n", " A non-negative floating point value (best value is 0.0)\n", " \n", " Usage\n", " -------\n", " loss = hp_point_loss([4,1,2,0,4,2,1], [6.0,3.1,5.2,1.0,4.0,2.2,3.7],0,4,.3)\n", " print('Loss: ', loss.numpy()) # Loss: 3.1\n", " \n", " Usage with the `compile` API:\n", " \n", " ```python\n", " \n", " Example Keras wrapper for hp_point_loss:\n", " \n", " def get_ohpl_wrapper (min_label, max_label, margin):\n", " def ohpl(y_true, y_pred):\n", " return hp_point_loss(y_true, y_pred, min_label, max_label, margin)\n", " return ohpl\n", "\n", " point_loss = get_ohpl_wrapper(0,4,0.01)\n", " \n", " model = tf.keras.Model(inputs, outputs)\n", " model.compile(loss=ohpl_point_loss, optimizer='adam', loss=ohpl_point_loss)\n", " ```\n", " \n", " \"\"\"\n", " \n", " y_pred = tf.reshape(tf.convert_to_tensor(y_pred), [-1,1])\n", " y_true = tf.cast(y_true, dtype=tf.float32)\n", " \n", " upper = tf.reshape(tf.constant(U, dtype=tf.float32), [-1,1])\n", " lower = tf.reshape(tf.constant(L, dtype=tf.float32), [-1,1])\n", " weigh = tf.reshape(tf.constant(wgt, dtype=tf.float32), [-1,1])\n", " uthreshold = tf.matmul(y_true, upper, name='upper_MM')\n", " lthreshold = tf.matmul(y_true, lower, name='lower_MM')\n", " weight = tf.matmul(y_true, weigh, name='Weight_Value')\n", "\n", " uerror = tf.multiply(weight, tf.nn.relu(tf.subtract(y_pred, uthreshold)))\n", " lerror = tf.multiply(weight, tf.nn.relu(tf.subtract(lthreshold, y_pred)))\n", " \n", " hp_point_loss = tf.add(uerror, lerror)\n", " \n", " return tf.reduce_sum(hp_point_loss)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "np.random.seed(2019)\n", "tf.set_random_seed(2019)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(12603, 2)\n", "(3153, 2)\n" ] }, { "data": { "text/html": [ "
\n", " | data | \n", "label | \n", "
---|---|---|
0 | \n", "0_22440 | \n", "0 | \n", "
1 | \n", "0_6509 | \n", "0 | \n", "
2 | \n", "0_15699 | \n", "0 | \n", "
3 | \n", "0_32913 | \n", "0 | \n", "
4 | \n", "0_18551 | \n", "0 | \n", "