Skip to content

Commit

Permalink
HELL YEAH: project complete. Successful! Congrats nigs
Browse files Browse the repository at this point in the history
  • Loading branch information
tirthasheshpatel committed Dec 9, 2020
1 parent 54c931e commit 2d7bf93
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 30 deletions.
51 changes: 51 additions & 0 deletions 18BCE243_244_245_259.csv
Original file line number Diff line number Diff line change
@@ -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
59 changes: 32 additions & 27 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down
28 changes: 25 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

from model import CoronaDetection

if len(sys.argv) != 4:
print("usage: python test.py <data_directory_path> <model_name> <has_labels_flag>")
print("<data_directory_path>: Path to directory containing the test data")
print(
"<model_name>: Name of the trained model. See 'pretrained_models' for details"
)
print("<has_labels_flag>: Whether the directory contains test labels or not.")
exit(1)

if torch.cuda.is_available():
print("Using GPU")
device = torch.device("cuda")
Expand All @@ -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:
Expand Down

0 comments on commit 2d7bf93

Please sign in to comment.