Skip to content

Commit 60b5357

Browse files
authored
Merge pull request #55 from rouson/download-mnist.tar.gz
2 parents 1f4a65b + 08da417 commit 60b5357

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
*.gz
12
*.o
23
*.mod
4+
*.dat
35
build
4-
data/*/*.dat
56
doc

data/mnist/mnist.tar.gz

-16.3 MB
Binary file not shown.

src/mod_io_submodule.f90

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
implicit none
44

5+
integer, parameter :: message_len = 128
6+
57
contains
68

79
module subroutine read_binary_file_1d(filename, dtype, nrec, array)
810
character(len=*), intent(in) :: filename
911
integer(ik), intent(in) :: dtype, nrec
1012
real(rk), allocatable, intent(in out) :: array(:)
1113
integer(ik) :: fileunit
14+
character(len=message_len) :: io_message
15+
integer :: io_status
16+
io_status = 0
17+
open(newunit=fileunit, file=filename, access='direct', action='read', &
18+
recl=dtype * nrec, status='old', iostat=io_status, iomsg=io_message)
19+
if (io_status /= 0) error stop trim(io_message)
1220
allocate(array(nrec))
13-
open(newunit=fileunit, file=filename, access='direct',&
14-
action='read', recl=dtype * nrec, status='old')
1521
read(fileunit, rec=1) array
1622
close(fileunit)
1723
end subroutine read_binary_file_1d
@@ -21,9 +27,13 @@ module subroutine read_binary_file_2d(filename, dtype, dsize, nrec, array)
2127
integer(ik), intent(in) :: dtype, dsize, nrec
2228
real(rk), allocatable, intent(in out) :: array(:,:)
2329
integer(ik) :: fileunit, i
30+
character(len=message_len) :: io_message
31+
integer :: io_status
32+
io_status = 0
33+
open(newunit=fileunit, file=filename, access='direct', action='read', &
34+
recl=dtype * dsize, status='old', iostat=io_status, iomsg=io_message)
35+
if (io_status /= 0) error stop trim(io_message)
2436
allocate(array(dsize, nrec))
25-
open(newunit=fileunit, file=filename, access='direct',&
26-
action='read', recl=dtype * dsize, status='old')
2737
do i = 1, nrec
2838
read(fileunit, rec=i) array(:,i)
2939
end do

src/mod_mnist_submodule.f90

+44-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,41 @@
1010

1111
implicit none
1212

13+
integer, parameter :: message_len = 128
14+
1315
contains
1416

17+
subroutine download_and_uncompress()
18+
character(len=*), parameter :: download_mechanism = 'curl -LO '
19+
character(len=*), parameter :: base_url='https://github.com/modern-fortran/neural-fortran/files/8498876/'
20+
character(len=*), parameter :: download_filename = 'mnist.tar.gz'
21+
character(len=*), parameter :: download_command = download_mechanism // base_url // download_filename
22+
character(len=*), parameter :: uncompress_file = 'tar xvzf ' // download_filename
23+
character(len=message_len) :: command_message
24+
character(len=:), allocatable :: error_message
25+
integer :: exit_status, command_status
26+
27+
exit_status=0
28+
call execute_command_line(command=download_command, wait=.true., &
29+
exitstat=exit_status, cmdstat=command_status, cmdmsg=command_message)
30+
31+
if (any([exit_status, command_status] /= 0)) then
32+
error_message = 'command "' // download_command // '" failed'
33+
if (command_status /= 0) error_message = error_message // " with message " // trim(command_message)
34+
error stop error_message
35+
end if
36+
37+
call execute_command_line(command=uncompress_file, wait=.true., &
38+
exitstat=exit_status, cmdstat=command_status, cmdmsg=command_message)
39+
40+
if (any([exit_status, command_status] /= 0)) then
41+
error_message = 'command "' // uncompress_file // '" failed'
42+
if (command_status /= 0) error_message = error_message // " with message " // trim(command_message)
43+
error stop error_message
44+
end if
45+
46+
end subroutine download_and_uncompress
47+
1548
pure module function label_digits(labels) result(res)
1649
real(rk), intent(in) :: labels(:)
1750
real(rk) :: res(10, size(labels))
@@ -42,21 +75,26 @@ module subroutine load_mnist(tr_images, tr_labels, te_images,&
4275
integer(ik), parameter :: tr_nimages = 50000
4376
integer(ik), parameter :: te_nimages = 10000
4477
integer(ik), parameter :: va_nimages = 10000
78+
logical :: file_exists
79+
80+
! Check if MNIST data is present and download it if not.
81+
inquire(file='mnist_training_images.dat', exist=file_exists)
82+
if (.not. file_exists) call download_and_uncompress()
4583

46-
call read_binary_file('data/mnist/mnist_training_images.dat',&
84+
call read_binary_file('mnist_training_images.dat',&
4785
dtype, image_size, tr_nimages, tr_images)
48-
call read_binary_file('data/mnist/mnist_training_labels.dat',&
86+
call read_binary_file('mnist_training_labels.dat',&
4987
dtype, tr_nimages, tr_labels)
5088

51-
call read_binary_file('data/mnist/mnist_testing_images.dat',&
89+
call read_binary_file('mnist_testing_images.dat',&
5290
dtype, image_size, te_nimages, te_images)
53-
call read_binary_file('data/mnist/mnist_testing_labels.dat',&
91+
call read_binary_file('mnist_testing_labels.dat',&
5492
dtype, te_nimages, te_labels)
5593

5694
if (present(va_images) .and. present(va_labels)) then
57-
call read_binary_file('data/mnist/mnist_validation_images.dat',&
95+
call read_binary_file('mnist_validation_images.dat',&
5896
dtype, image_size, va_nimages, va_images)
59-
call read_binary_file('data/mnist/mnist_validation_labels.dat',&
97+
call read_binary_file('mnist_validation_labels.dat',&
6098
dtype, va_nimages, va_labels)
6199
end if
62100

0 commit comments

Comments
 (0)