Skip to content

Commit

Permalink
Skip H5 files with no model_config (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
swashko authored Feb 5, 2024
1 parent 01d8cbd commit e3a3773
Showing 1 changed file with 13 additions and 3 deletions.
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

0 comments on commit e3a3773

Please sign in to comment.