Skip to content

Commit 1eec7da

Browse files
committed
Add json-fortran to dependencies; begin parsing a Keras model
1 parent da1f273 commit 1eec7da

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

fpm.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ author = "Milan Curcic"
55
maintainer = "milancurcic@hey.com"
66
copyright = "Copyright 2018-2022, neural-fortran contributors"
77

8+
[build]
9+
external-modules = "hdf5"
10+
link = ["hdf5", "hdf5_fortran"]
11+
812
[dependencies]
9-
h5fortran = { git="https://github.com/geospace-code/h5fortran" }
13+
json-fortran = { git = "https://github.com/jacobwilliams/json-fortran" }

test/test_io_hdf5.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
program test_io_hdf5
22

33
use nf_io_hdf5, only: get_h5_attribute_string
4+
45
implicit none
56

6-
print *, get_h5_attribute_string('test/data/mnist_dense.h5', '.', 'model_config')
7-
print *, get_h5_attribute_string('test/data/mnist_dense.h5', 'model_weights', 'layer_names')
7+
character(:), allocatable :: attr
8+
character(*), parameter :: test_data_path = 'test/data/mnist_dense.h5'
9+
10+
attr = get_h5_attribute_string(test_data_path, '.', 'backend')
811

912
end program test_io_hdf5

test/test_parse_keras_model.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
program test_parse_keras_model
2+
3+
use nf_io_hdf5, only: get_h5_attribute_string
4+
use json_module
5+
6+
implicit none
7+
8+
character(:), allocatable :: model_config_string
9+
character(*), parameter :: test_data_path = 'test/data/mnist_dense.h5'
10+
type(json_core) :: json
11+
type(json_value), pointer :: model_config, layers, next_layer, layer
12+
character(:), allocatable :: class_name, layer_type
13+
logical :: found
14+
integer :: n, num_layers
15+
16+
model_config_string = &
17+
get_h5_attribute_string(test_data_path, '.', 'model_config')
18+
19+
call json % parse(model_config, model_config_string)
20+
call json % get(model_config, 'config.layers', layers)
21+
22+
num_layers = json % count(layers)
23+
print *, 'This model has', num_layers, 'layers.'
24+
25+
do n = 1, num_layers
26+
call json % get_child(layers, n, layer)
27+
print *, 'Layer', n
28+
!call json % print(layer)
29+
call json % get(layer, 'class_name', layer_type)
30+
print *, layer_type
31+
end do
32+
33+
end program test_parse_keras_model

0 commit comments

Comments
 (0)