From 2d7bf9398557b4844f5ef6367e89b62df45bf027 Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Wed, 9 Dec 2020 12:24:37 +0530 Subject: [PATCH] HELL YEAH: project complete. Successful! Congrats nigs --- 18BCE243_244_245_259.csv | 51 ++++++++++++++++++++++++++++++++++ model.py | 59 ++++++++++++++++++++++------------------ test.py | 28 +++++++++++++++++-- 3 files changed, 108 insertions(+), 30 deletions(-) create mode 100644 18BCE243_244_245_259.csv diff --git a/18BCE243_244_245_259.csv b/18BCE243_244_245_259.csv new file mode 100644 index 0000000..9c4443c --- /dev/null +++ b/18BCE243_244_245_259.csv @@ -0,0 +1,51 @@ +Image, Prediction +45.jpeg, Normal +42.jpeg, Normal +44.jpeg, Normal +12.jpeg, Normal +5.jpeg, Normal +36.jpeg, Normal +17.jpeg, Normal +38.jpeg, Normal +13.jpeg, Normal +49.jpeg, Normal +20.jpeg, Normal +40.jpeg, Normal +1.jpeg, Normal +6.jpeg, Normal +37.jpeg, Normal +7.jpeg, Normal +34.jpeg, Normal +39.jpeg, Normal +19.jpeg, Pneumonia +41.jpeg, Pneumonia +18.jpeg, Pneumonia +3.jpeg, Pneumonia +30.jpeg, Normal +10.jpeg, Pneumonia +50.jpeg, Pneumonia +21.jpeg, Pneumonia +8.jpeg, Pneumonia +22.jpeg, Pneumonia +27.jpeg, Pneumonia +15.jpeg, Pneumonia +24.jpeg, Pneumonia +11.jpeg, Pneumonia +33.jpeg, Pneumonia +46.jpeg, Normal +31.jpeg, Pneumonia +35.jpeg, Pneumonia +9.jpeg, Pneumonia +25.jpeg, Pneumonia +28.jpeg, Pneumonia +4.jpeg, Pneumonia +43.jpeg, Pneumonia +23.jpeg, Pneumonia +26.jpeg, Pneumonia +14.jpeg, Pneumonia +2.jpeg, Normal +48.jpeg, Pneumonia +47.jpeg, Normal +29.jpeg, Normal +16.jpeg, Normal +32.jpeg, Normal diff --git a/model.py b/model.py index d056609..4ecdf2c 100644 --- a/model.py +++ b/model.py @@ -339,7 +339,7 @@ def train( test_accuracies, ) - def test(self, loss_fun, test_data, device="cuda"): + def test(self, loss_fun, test_data, device="cuda", has_labels=False): print("Starting Evaluating....") start = time.time() self.model.eval() @@ -357,36 +357,41 @@ def test(self, loss_fun, test_data, device="cuda"): for batch in test_data: test_images, test_labels = batch test_images = test_images.to(device) - test_labels = test_labels.to(device) + if has_labels: + test_labels = test_labels.to(device) output = self.model(test_images) - loss = loss_fun(output, test_labels) - test_loss += loss.item() + if has_labels: + loss = loss_fun(output, test_labels) + test_loss += loss.item() _, predicted = torch.max(output.data, 1) - predictions.extend(list(np.asarray(predicted.to('cpu')))) - total += test_labels.size(0) - correct += (predicted == test_labels).sum().item() - tp += ((test_labels == 1) & ((predicted) == 1)).sum().item() - fn += ((test_labels != 0) & ((predicted) == 0)).sum().item() - fp += ((test_labels != 1) & ((predicted) == 1)).sum().item() - testing_accuracy = correct / total * 100 - testing_precision = tp / (tp + fp) * 100 - testing_recall = tp / (tp + fn) * 100 - testing_f1 = ( - 2.0 - * testing_recall - * testing_precision - / (testing_recall + testing_precision) - ) + predictions.extend(list(np.asarray(predicted.to("cpu")))) + if has_labels: + total += test_labels.size(0) + correct += (predicted == test_labels).sum().item() + tp += ((test_labels == 1) & ((predicted) == 1)).sum().item() + fn += ((test_labels != 0) & ((predicted) == 0)).sum().item() + fp += ((test_labels != 1) & ((predicted) == 1)).sum().item() + + if has_labels: + testing_accuracy = correct / total * 100 + testing_precision = tp / (tp + fp) * 100 + testing_recall = tp / (tp + fn) * 100 + testing_f1 = ( + 2.0 + * testing_recall + * testing_precision + / (testing_recall + testing_precision) + ) - print( - f"Test Loss => {test_loss:08.4f} " - f"Test accuracy => {testing_accuracy:06.2f} " - f"Test Precision => {testing_precision:06.2f} " - f"Test Recall => {testing_recall:06.2f} " - f"Test F1 Score => {testing_f1:06.2f} " - f"Time Taken => {time.time() - start:08.4f}" - ) + print( + f"Test Loss => {test_loss:08.4f} " + f"Test accuracy => {testing_accuracy:06.2f} " + f"Test Precision => {testing_precision:06.2f} " + f"Test Recall => {testing_recall:06.2f} " + f"Test F1 Score => {testing_f1:06.2f} " + f"Time Taken => {time.time() - start:08.4f}" + ) return predictions diff --git a/test.py b/test.py index 3409feb..ae13d99 100644 --- a/test.py +++ b/test.py @@ -7,6 +7,15 @@ from model import CoronaDetection +if len(sys.argv) != 4: + print("usage: python test.py ") + print(": Path to directory containing the test data") + print( + ": Name of the trained model. See 'pretrained_models' for details" + ) + print(": Whether the directory contains test labels or not.") + exit(1) + if torch.cuda.is_available(): print("Using GPU") device = torch.device("cuda") @@ -27,15 +36,28 @@ ) # get all file names -dirname = os.listdir(test_data_path) +dirname = os.listdir(test_data_path) filename = [] for d in dirname: filename.extend(os.listdir(os.path.join(test_data_path, d))) # testing the data -predictions = model.test(torch.nn.CrossEntropyLoss(), test_data_loader, device=device) +bool_dict = { + "True": True, + "False": False, + "true": True, + "false": False, + "t": True, + "f": False, +} +predictions = model.test( + torch.nn.CrossEntropyLoss(), + test_data_loader, + device=device, + has_labels=bool_dict[sys.argv[3]], +) labels = ("Pneumonia", "Normal") -predictions = [ labels[p] for p in predictions ] +predictions = [labels[p] for p in predictions] # make the prediction file with open(f"test_predictions_{sys.argv[2]}.csv", "w") as f: