-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from milancurcic/keras-reader-cnn
Read Conv2D, MaxPooling2D, and Flatten layers from Keras
- Loading branch information
Showing
18 changed files
with
446 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FetchContent_Declare(functional | ||
GIT_REPOSITORY https://github.com/wavebitscientific/functional-fortran | ||
GIT_TAG 0.6.1 | ||
GIT_SHALLOW true | ||
) | ||
|
||
FetchContent_Populate(functional) | ||
|
||
add_library(functional ${functional_SOURCE_DIR}/src/functional.f90) | ||
target_include_directories(functional PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include> | ||
$<INSTALL_INTERFACE:include> | ||
) | ||
|
||
add_library(functional::functional INTERFACE IMPORTED GLOBAL) | ||
target_link_libraries(functional::functional INTERFACE functional) | ||
|
||
install(TARGETS functional) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
foreach(execid cnn mnist mnist_from_keras simple sine) | ||
foreach(execid | ||
cnn | ||
cnn_from_keras | ||
dense_from_keras | ||
mnist | ||
simple | ||
sine | ||
) | ||
add_executable(${execid} ${execid}.f90) | ||
target_link_libraries(${execid} PRIVATE neural h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS}) | ||
target_link_libraries(${execid} PRIVATE | ||
neural | ||
h5fortran::h5fortran | ||
jsonfortran::jsonfortran | ||
${LIBS} | ||
) | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
program cnn_from_keras | ||
|
||
! This example demonstrates loading a convolutional model | ||
! pre-trained on the MNIST dataset from a Keras HDF5 | ||
! file and running an inferrence on the testing dataset. | ||
|
||
use nf, only: network, label_digits, load_mnist | ||
use nf_datasets, only: download_and_unpack, keras_cnn_mnist_url | ||
|
||
implicit none | ||
|
||
type(network) :: net | ||
real, allocatable :: training_images(:,:), training_labels(:) | ||
real, allocatable :: validation_images(:,:), validation_labels(:) | ||
real, allocatable :: testing_images(:,:), testing_labels(:) | ||
character(*), parameter :: keras_cnn_path = 'keras_cnn_mnist.h5' | ||
logical :: file_exists | ||
real :: acc | ||
|
||
inquire(file=keras_cnn_path, exist=file_exists) | ||
if (.not. file_exists) call download_and_unpack(keras_cnn_mnist_url) | ||
|
||
call load_mnist(training_images, training_labels, & | ||
validation_images, validation_labels, & | ||
testing_images, testing_labels) | ||
|
||
print '("Loading a pre-trained CNN model from Keras")' | ||
print '(60("="))' | ||
|
||
net = network(keras_cnn_path) | ||
|
||
call net % print_info() | ||
|
||
if (this_image() == 1) then | ||
acc = accuracy( & | ||
net, & | ||
reshape(testing_images(:,:), shape=[1,28,28,size(testing_images,2)]), & | ||
label_digits(testing_labels) & | ||
) | ||
print '(a,f5.2,a)', 'Accuracy: ', acc * 100, ' %' | ||
end if | ||
|
||
contains | ||
|
||
real function accuracy(net, x, y) | ||
type(network), intent(in out) :: net | ||
real, intent(in) :: x(:,:,:,:), y(:,:) | ||
integer :: i, good | ||
good = 0 | ||
do i = 1, size(x, dim=4) | ||
if (all(maxloc(net % output(x(:,:,:,i))) == maxloc(y(:,i)))) then | ||
good = good + 1 | ||
end if | ||
end do | ||
accuracy = real(good) / size(x, dim=4) | ||
end function accuracy | ||
|
||
end program cnn_from_keras |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name = "neural-fortran" | ||
version = "0.5.0" | ||
version = "0.6.0" | ||
license = "MIT" | ||
author = "Milan Curcic" | ||
maintainer = "[email protected]" | ||
|
@@ -10,5 +10,6 @@ external-modules = "hdf5" | |
link = ["hdf5", "hdf5_fortran"] | ||
|
||
[dependencies] | ||
functional = { git = "https://github.com/wavebitscientific/functional-fortran" } | ||
h5fortran = { git = "https://github.com/geospace-code/h5fortran" } | ||
json-fortran = { git = "https://github.com/jacobwilliams/json-fortran" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.