Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip H5 files with no model_config #96

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions modelscan/scanners/h5/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ def scan(
)
return None

return self.label_results(self._scan_keras_h5_file(source))
results = self._scan_keras_h5_file(source)
if results:
return self.label_results(results)
else:
return None

def _scan_keras_h5_file(self, source: Union[str, Path]) -> ScanResults:
def _scan_keras_h5_file(self, source: Union[str, Path]) -> Optional[ScanResults]:
machine_learning_library_name = "Keras"
operators_in_model = self._get_keras_h5_operator_names(source)
if not operators_in_model:
return None
return H5LambdaDetectScan._check_for_unsafe_tf_keras_operator(
module_name=machine_learning_library_name,
raw_operator=operators_in_model,
Expand All @@ -55,11 +61,15 @@ def _scan_keras_h5_file(self, source: Union[str, Path]) -> ScanResults:
]["unsafe_keras_operators"],
)

def _get_keras_h5_operator_names(self, source: Union[str, Path]) -> List[str]:
def _get_keras_h5_operator_names(
self, source: Union[str, Path]
) -> Optional[List[str]]:
# Todo: source isn't guaranteed to be a file

with h5py.File(source, "r") as model_hdf5:
try:
if not "model_config" in model_hdf5.attrs.keys():
return None
model_config = json.loads(model_hdf5.attrs.get("model_config", {}))
layers = model_config.get("config", {}).get("layers", {})
lambda_layers = []
Expand Down
Loading