Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
implementing PR feedback, thanks Anton and Daniel
Browse files Browse the repository at this point in the history
  • Loading branch information
vale-salvatelli committed Jan 11, 2022
1 parent 137dc99 commit c932ef7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
3 changes: 1 addition & 2 deletions InnerEye/Common/fixed_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def repository_parent_directory(path: Optional[PathOrString] = None) -> Path:
:param path: if provided, a relative path to append to the absolute path to the repository root.
:return: The full path to the repository's root directory, with symlinks resolved if any.
"""
current = Path(__file__)
root = current.parent.parent.parent.parent
root = repository_root_directory().parent
if path:
return root / path
else:
Expand Down
27 changes: 14 additions & 13 deletions InnerEye/ML/Histopathology/models/deepmil.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def get_classifier(self) -> Callable:

def get_loss(self) -> Callable:
if self.n_classes > 1:
if self.class_weights is not None:
class_weights = self.class_weights.float()
if self.class_weights is None:
return nn.CrossEntropyLoss
else:
class_weights = self.class_weights
return nn.CrossEntropyLoss(weight=class_weights)
class_weights = self.class_weights.float()
return nn.CrossEntropyLoss(weight=class_weights)
else:
pos_weight = None
if self.class_weights is not None:
Expand Down Expand Up @@ -267,9 +267,11 @@ def test_epoch_end(self, outputs: List[Dict[str, Any]]) -> None: # type: ignore
list_slide_dicts.append(slide_dict)
list_encoded_features.append(results[ResultsKey.IMAGE][slide_idx])

print(f"Metrics results will be output to {fixed_paths.repository_parent_directory()}/outputs")
csv_filename = fixed_paths.repository_parent_directory() / Path('outputs/test_output.csv')
encoded_features_filename = fixed_paths.repository_parent_directory() / Path('outputs/test_encoded_features.pickle')
outputs_path = fixed_paths.repository_parent_directory() / 'outputs'
print(f"Metrics results will be output to {outputs_path}")
outputs_fig_path = outputs_path / 'fig'
csv_filename = outputs_path / 'test_output.csv'
encoded_features_filename = outputs_path / 'test_encoded_features.pickle'

# Collect the list of dictionaries in a list of pandas dataframe and save
df_list = []
Expand All @@ -292,24 +294,23 @@ def test_epoch_end(self, outputs: List[Dict[str, Any]]) -> None: # type: ignore

for key in report_cases.keys():
print(f"Plotting {key} ...")
output_path = Path(fixed_paths.repository_parent_directory(), f'outputs/fig/{key}/')
Path(output_path).mkdir(parents=True, exist_ok=True)
key_folder_path = outputs_fig_path / f'{key}'
Path(key_folder_path).mkdir(parents=True, exist_ok=True)
nslides = len(report_cases[key][0])
for i in range(nslides):
slide, score, paths, top_attn = report_cases[key][0][i]
fig = plot_slide_noxy(slide, score, paths, top_attn, key + '_top', ncols=4)
figpath = Path(output_path, f'{slide}_top.png')
figpath = Path(key_folder_path, f'{slide}_top.png')
fig.savefig(figpath, bbox_inches='tight')

slide, score, paths, bottom_attn = report_cases[key][1][i]
fig = plot_slide_noxy(slide, score, paths, bottom_attn, key + '_bottom', ncols=4)
figpath = Path(output_path, f'{slide}_bottom.png')
figpath = Path(key_folder_path, f'{slide}_bottom.png')
fig.savefig(figpath, bbox_inches='tight')

print("Plotting histogram ...")
fig = plot_scores_hist(results)
output_path = Path(fixed_paths.repository_parent_directory(), 'outputs/fig/hist_scores.png')
fig.savefig(output_path, bbox_inches='tight')
fig.savefig(outputs_fig_path / 'hist_scores.png', bbox_inches='tight')

@staticmethod
def normalize_dict_for_df(dict_old: Dict[str, Any], use_gpu: bool) -> Dict:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def setup(self) -> None:
download_dir="outputs/",
remote_checkpoint_dir=Path("outputs/checkpoints")
)
os.chdir(fixed_paths.repository_root_directory().parent)
os.chdir(fixed_paths.repository_parent_directory())
self.downloader.download_checkpoint_if_necessary()

self.encoder = self.get_encoder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def setup(self) -> None:
download_dir="outputs/",
remote_checkpoint_dir=Path("outputs/checkpoints")
)
os.chdir(fixed_paths.repository_root_directory().parent)
os.chdir(fixed_paths.repository_parent_directory())
self.downloader.download_checkpoint_if_necessary()
self.encoder = self.get_encoder()
self.encoder.cuda()
Expand Down Expand Up @@ -127,7 +127,7 @@ def get_path_to_best_checkpoint(self) -> Path:
if absolute_checkpoint_path.is_file():
return absolute_checkpoint_path

absolute_checkpoint_path_parent = Path(fixed_paths.repository_root_directory().parent,
absolute_checkpoint_path_parent = Path(fixed_paths.repository_parent_directory(),
self.checkpoint_folder_path,
self.best_checkpoint_filename_with_suffix)
if absolute_checkpoint_path_parent.is_file():
Expand Down

0 comments on commit c932ef7

Please sign in to comment.