# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="N4cIPNBc1zhH" colab={"base_uri": "https://localhost:8080/"} outputId="26dc2151-b1a8-498d-8fb6-0fd220cc331e"
import numpy as np
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torch.utils.data.sampler import SubsetRandomSampler
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import torchvision.models


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score
from sklearn.metrics import plot_confusion_matrix

# + colab={"base_uri": "https://localhost:8080/"} id="fu6cwQ4KkvEx" outputId="6f9b3ed0-1383-479a-8617-a47eaa8de717"
from google.colab import drive
drive.mount('/content/gdrive')


# + id="PgGbz85umHM7"
def loadData():
  np.random.seed(100) 
  #Ensuring data is a 224x224 image, used the centercrop function to crop at center
  transform = transforms.Compose([transforms.Resize((224,224)), 
                                transforms.ToTensor()])

  
  numWorkers = 1
  batchSize = 1

  classes = ['COVID-19', 'Normal', 'Pneumonial-Bacterial', 'Pneumonial-Viral']
  #datasetPath = '/content/gdrive/MyDrive/APS360/ProgressReport/APS360SampleData'

  datasetPath = '/content/gdrive/MyDrive/Winter2021/APS360/project'


  sampleSet = torchvision.datasets.ImageFolder(datasetPath, transform=transform)
  print(len(sampleSet))
  #All the data loaded is valid so we can use any index
  train = int((len(sampleSet) * 0.8))
  #val = int((len(sampleSet) * 0.1))
  test = int((len(sampleSet) * 0.2))

  # Used the random_split data function to split the dataset into a 70, 20, 10 proportion 
  trainData, testData = torch.utils.data.random_split(sampleSet,
                  [train,test],generator=torch.Generator().manual_seed(100))
  
  print(trainData, testData)  

  #Load all the data
  trainLoader = torch.utils.data.DataLoader(trainData, batch_size=batchSize, 
                                            num_workers= numWorkers,
                                            shuffle=True)
  # valLoader = torch.utils.data.DataLoader(valData, batch_size=batchSize, 
  #                                         num_workers= numWorkers,
  #                                                 shuffle=True)
  testLoader = torch.utils.data.DataLoader(testData, batch_size=batchSize, 
                                           num_workers= numWorkers,
                                                  shuffle=True)
  return trainLoader, testLoader

# + colab={"base_uri": "https://localhost:8080/"} id="_8KJu6JHmrCo" outputId="3fdf390c-f0ac-45f2-edf9-e00b977f509d"
test = loadData()
trainLoader = test[0]
# valLoader = test[1]
testLoader = test[1]
print(len(trainLoader),len(testLoader))

# + id="vKlC52onnH0J"
# classes = ['COVID-19', 'Normal', 'Pneumonial-Bacterial', 'Pneumonial-Viral']
# dataiter = iter(trainLoader)
# images, labels = dataiter.next()
# image = np.transpose(images[0], (1, 2, 0))
# label = classes[labels[0]]
# for images, labels in trainLoader:
#   print(classes[labels[0]])

# + id="EDq66NPmu9QV"
#links
#https://towardsdatascience.com/dealing-with-multiclass-data-78a1a27c5dcc
#https://www.codementor.io/@agarrahul01/multiclass-classification-using-random-forest-on-scikit-learn-library-hkk4lwawu

# + colab={"base_uri": "https://localhost:8080/"} id="KqcEK0KIvhqa" outputId="568c3d76-2abf-430c-815e-3b928b00c6ed"
train_x = []
train_y = []

for x, y in trainLoader:
  train_x.append(x)
  train_y.append(y)

train_x = torch.stack(train_x)
train_y = torch.stack(train_y)
train_x = train_x.reshape(760, 224*224*3)
print(train_x.shape)
print(train_y.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="QJKjltiUztfK" outputId="e5d3119e-58dc-4dd5-849a-a988c461189f"
test_x = []
test_y = []
for x, y in testLoader:
  test_x.append(x)
  test_y.append(y)

test_x = torch.stack(test_x)
test_x = test_x.reshape(190, 224*224*3)
test_y = torch.stack(test_y)
print(test_x.shape)
print(test_y.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="nDdciTglxv5h" outputId="35d647c9-17c4-4137-a0de-ee7af619a1a8"
# Create a Gaussian Classfier
model = RandomForestClassifier(n_estimators = 220, oob_score=True, criterion = 'entropy', random_state = 100)
model.fit(train_x,train_y)
#predict2 = model.predict(train_x)
predict = model.predict(test_x)
value = accuracy_score(test_y,predict)
#value2 = accuracy_score(train_y,predict2)
print(value)
#print(value2)

# + colab={"base_uri": "https://localhost:8080/"} id="rd3YP9o9MYK1" outputId="d7d872a0-4d60-41fc-a31c-e368b5e25672"
print(predict)
print(trainLoader)

# + colab={"base_uri": "https://localhost:8080/", "height": 333} id="iro1imeBNpzJ" outputId="e74b4f95-64e9-4b9e-9a39-5732357acf4b"
labels = [0,1,2,3]
conf_mat = confusion_matrix(test_y, predict, normalize='true')
print(conf_mat)
# Visualize it as a heatmap
import seaborn
seaborn.heatmap(conf_mat)
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="y8GLNgFNOy0S" outputId="737635f9-11ff-466b-aa5d-7e5c62efbeb1"
print(test_y.shape)
predictions = torch.from_numpy(predict)
print(predictions.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 580} id="cytUWUP1QCKS" outputId="d136c9cc-aed9-496a-8673-9180eb5da276"
#plt.figure(figsize=(100, 100))
fig, ax = plt.subplots(figsize=(10, 10))
classes = ['COVID-19', 'Normal', 'Pneumonial-Bacterial', 'Pneumonial-Viral']
disp = plot_confusion_matrix(model, test_x, test_y,
                            display_labels=classes,
                            cmap=plt.cm.Blues, ax=ax)
#disp.ax_.set_adjustable()
#print(disp.confusion_matrix)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered