text_plain_output_1.png
from cuml.metrics.confusion_matrix import confusion_matrix

from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

import cupy as np

import cv2

import matplotlib.pyplot as plt

import os

import seaborn as sns

image_size = 200
labels = ['PNEUMONIA', 'NORMAL']
def data_loader(data_dir):
    data = list()
    for label in labels:
        path = os.path.join(data_dir, label)
        class_num = labels.index(label)
        for img in os.listdir(path):
            img_arr = cv2.imread(os.path.join(path, img), 0)
            resized_arr = cv2.resize(img_arr, (image_size, image_size))
            data.append([resized_arr, class_num])
    return data

val = data_loader('../input/chest-xray-pneumonia/chest_xray/val')
test = data_loader('../input/chest-xray-pneumonia/chest_xray/test')
train = data_loader('../input/chest-xray-pneumonia/chest_xray/train')

def normalize_list(train):
    for pair in train:
        pair[0] = np.array(pair[0]) / 255
    return train
train = normalize_list(train)
test = normalize_list(test)
val = normalize_list(val)

def make_x_y(data):
    X = []
    Y = []
    for pair in data:
        X.append(pair[0])
        Y.append(pair[1])
    return (np.array(X), np.array(Y))
x_train, y_train = make_x_y(train)
x_test, y_test = make_x_y(test)
x_val, y_val = make_x_y(val)

x_train = x_train.reshape(len(x_train), 200 * 200)
x_test = x_test.reshape(len(x_test), 200 * 200)
x_val = x_val.reshape(len(x_val), 200 * 200)

cuml_model = cuRFC(max_features=1.0, n_bins=8, n_estimators=40)
cuml_model.fit(x_train.astype('float32'), y_train.astype('float32'))

y_rfc_predict = cuml_model.predict(x_test.astype('float32'))

y_test_temp = y_test.reshape(1, len(y_test))[0]

y_np_test = np.asnumpy(y_test_temp)
y_np_rfc_predict = np.asnumpy(y_rfc_predict)

print('confusion matrix of model: \n')
cmap = confusion_matrix(y_np_test, y_np_rfc_predict)
plt.figure(figsize=(4, 4), dpi=150)
hm = sns.heatmap(data=cmap, annot=True, fmt='g')