Skip to content

Commit

Permalink
fix inference.py to be compatible with new Dataset class
Browse files Browse the repository at this point in the history
  • Loading branch information
cheungatm committed Apr 21, 2022
1 parent 32c3ef8 commit 73d7395
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Binary file modified .DS_Store
Binary file not shown.
30 changes: 18 additions & 12 deletions src/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
from dataset_class import IntracranialDataset
from torch.utils.data import DataLoader
from tqdm import tqdm
import os

# Point to the relevent test label data and DICOM files
testing_data = pd.read_csv('../input/label_dataset/prototype_test_labels.csv') # CSV with the ground-truth labels
data_path = '../input/data' # folder containing dicom images
data_path = '../holdout_test_data' # folder containing dicom images
if os.path.exists('../inf_output'):
print('inference output path exists')
else:
print("\noutput directory does not exist")
os.mkdir('../inf_output/')
print("Directory ../inf_output/ created\n")

#Inference
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# initialize model
model_inf = models.model_fxn(pretrained = False, requires_grad = False).to(device)
model_inf = torch.nn.DataParallel(model_inf).to(device)

# load model checkpoint
checkpoint = torch.load('../output/model.pt')
# load model weights state_dict
Expand All @@ -26,15 +34,13 @@


#
label_options = testing_data.columns.values[1:-1]
label_options = ['any','epidural','intraparenchymal','intraventricular','subarachnoid','subdural']

test_data = IntracranialDataset(
testing_data, path = data_path, train = False, test = True
)
test_data = IntracranialDataset(path=data_path, train=False, test=True)

test_loader = DataLoader(
test_data,
batch_size = 1,
batch_size = 512,
shuffle=False
)

Expand Down Expand Up @@ -67,10 +73,10 @@
), id_vars = ['Image'], var_name = 'subtype', value_name = 'ground_truth')

# Save a .csv file with the inference results (label predictions) and ground-truth labels
pd.merge(predictions_df, ground_truth_df).to_csv('../output/inference_results.csv', index = False)
pd.merge(predictions_df, ground_truth_df).to_csv('../inf_output/inference_results.csv', index = False)

# Format and save .csv with ID and predicted labels for Kaggle submission
kaggle_submission = pd.merge(predictions_df, ground_truth_df)
kaggle_submission['ID'] = kaggle_submission['Image'] + '_' + kaggle_submission['subtype']
kaggle_submission = kaggle_submission[['ID', 'pred_label']].rename(columns = {'pred_label': 'Label'})
kaggle_submission.to_csv('../output/kaggle_submission.csv', index = False)
#kaggle_submission = pd.merge(predictions_df, ground_truth_df)
#kaggle_submission['ID'] = kaggle_submission['Image'] + '_' + kaggle_submission['subtype']
#kaggle_submission = kaggle_submission[['ID', 'pred_label']].rename(columns = {'pred_label': 'Label'})
#kaggle_submission.to_csv('../output/kaggle_submission.csv', index = False)

0 comments on commit 73d7395

Please sign in to comment.