Read 2D convolutional layer from file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(conv2d_layer_type), | intent(inout) | :: | this |
Instance of the 2D convolutional layer |
||
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
subroutine read_conv2d(this, unit, verbose) !! Read 2D convolutional layer from file use athena__tools_infile, only: assign_val, assign_vec, move use coreutils, only: to_lower, to_upper, icount use athena__activation, only: read_activation use athena__initialiser, only: initialiser_setup implicit none ! Arguments class(conv2d_layer_type), intent(inout) :: this !! Instance of the 2D convolutional layer integer, intent(in) :: unit !! Unit number integer, optional, intent(in) :: verbose !! Verbosity level ! Local variables integer :: stat !! Status of read integer :: verbose_ = 0 !! Verbosity level integer :: j, k, l, c, itmp1, iline, num_params !! Loop variables and temporary integer integer :: num_filters !! Number of filters logical :: use_bias = .true. !! Whether to use bias character(14) :: kernel_initialiser_name='', bias_initialiser_name='' !! Kernel and bias initialisers character(20) :: padding='', activation_name='' !! Padding and activation function class(base_actv_type), allocatable :: activation !! Activation function class(base_init_type), allocatable :: kernel_initialiser, bias_initialiser !! Initialisers character(256) :: buffer, tag, err_msg !! Buffer, tag, and error message integer, dimension(2) :: kernel_size, stride, dilation !! Kernel size and stride integer, dimension(3) :: input_shape !! Input shape real(real32), allocatable, dimension(:) :: data_list !! Data list integer :: param_line, final_line !! Parameter line number ! Initialise optional arguments !--------------------------------------------------------------------------- if(present(verbose)) verbose_ = verbose ! Loop over tags in layer card !--------------------------------------------------------------------------- iline = 0 param_line = 0 final_line = 0 tag_loop: do ! Check for end of file !------------------------------------------------------------------------ read(unit,'(A)',iostat=stat) buffer if(stat.ne.0)then write(err_msg,'("file encountered error (EoF?) before END ",A)') & to_upper(this%name) call stop_program(err_msg) return end if if(trim(adjustl(buffer)).eq."") cycle tag_loop ! Check for end of layer card !------------------------------------------------------------------------ if(trim(adjustl(buffer)).eq."END "//to_upper(trim(this%name)))then final_line = iline backspace(unit) exit tag_loop end if iline = iline + 1 tag=trim(adjustl(buffer)) if(scan(buffer,"=").ne.0) tag=trim(tag(:scan(tag,"=")-1)) ! Read parameters from save file !------------------------------------------------------------------------ select case(trim(tag)) case("INPUT_SHAPE") call assign_vec(buffer, input_shape, itmp1) case("NUM_FILTERS") call assign_val(buffer, num_filters, itmp1) case("KERNEL_SIZE") call assign_vec(buffer, kernel_size, itmp1) case("STRIDE") call assign_vec(buffer, stride, itmp1) case("DILATION") call assign_vec(buffer, dilation, itmp1) case("USE_BIAS") call assign_val(buffer, use_bias, itmp1) case("PADDING") call assign_val(buffer, padding, itmp1) padding = to_lower(padding) case("ACTIVATION") iline = iline - 1 backspace(unit) activation = read_activation(unit, iline) case("KERNEL_INITIALISER", "KERNEL_INIT", "KERNEL_INITIALIZER") call assign_val(buffer, kernel_initialiser_name, itmp1) case("BIAS_INITIALISER", "BIAS_INIT", "BIAS_INITIALIZER") call assign_val(buffer, bias_initialiser_name, itmp1) case("WEIGHTS") kernel_initialiser_name = 'zeros' bias_initialiser_name = 'zeros' param_line = iline case default ! Don't look for "e" due to scientific notation of numbers ! ... i.e. exponent (E+00) if(scan(to_lower(trim(adjustl(buffer))),& 'abcdfghijklmnopqrstuvwxyz').eq.0)then cycle tag_loop elseif(tag(:3).eq.'END')then cycle tag_loop end if write(err_msg,'("Unrecognised line in input file: ",A)') & trim(adjustl(buffer)) call stop_program(err_msg) return end select end do tag_loop kernel_initialiser = initialiser_setup(kernel_initialiser_name) bias_initialiser = initialiser_setup(bias_initialiser_name) ! Set hyperparameters and initialise layer !--------------------------------------------------------------------------- call this%set_hyperparams( & num_filters = num_filters, & kernel_size = kernel_size, stride = stride, dilation = dilation, & padding = padding, & use_bias = use_bias, & activation = activation, & kernel_initialiser = kernel_initialiser, & bias_initialiser = bias_initialiser, & verbose = verbose_ & ) call this%init(input_shape = input_shape) ! Check if WEIGHTS card was found !--------------------------------------------------------------------------- if(param_line.eq.0)then write(0,*) "WARNING: WEIGHTS card in "//to_upper(trim(this%name))//" not found" else call move(unit, param_line - iline, iostat=stat) num_params = product(this%knl) * input_shape(3) * num_filters allocate(data_list(num_params), source=0._real32) c = 1 k = 1 data_concat_loop: do while(c.le.num_params) read(unit,'(A)',iostat=stat) buffer if(stat.ne.0) exit data_concat_loop k = icount(buffer) read(buffer,*,iostat=stat) (data_list(j),j=c,c+k-1) c = c + k end do data_concat_loop this%params(1)%val(:,1) = data_list deallocate(data_list) allocate(data_list(num_filters), source=0._real32) c = 1 k = 1 data_concat_loop2: do while(c.le.num_filters) read(unit,'(A)',iostat=stat) buffer if(stat.ne.0) exit data_concat_loop2 k = icount(buffer) read(buffer,*,iostat=stat) (data_list(j),j=c,c+k-1) c = c + k end do data_concat_loop2 this%params(2)%val(:,1) = data_list deallocate(data_list) ! Check for end of weights card !------------------------------------------------------------------------ read(unit,'(A)') buffer if(trim(adjustl(buffer)).ne."END WEIGHTS")then write(0,*) trim(adjustl(buffer)) call stop_program("END WEIGHTS not where expected") return end if end if ! Check for end of layer card !--------------------------------------------------------------------------- call move(unit, final_line - iline, iostat=stat) read(unit,'(A)') buffer if(trim(adjustl(buffer)).ne."END "//to_upper(trim(this%name)))then write(0,*) trim(adjustl(buffer)) write(err_msg,'("END ",A," not where expected")') to_upper(this%name) call stop_program(err_msg) return end if end subroutine read_conv2d