Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/alos2/dataset.py: 0%

56 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-13 08:53 +0000

1# MIT License 

2 

3# Copyright (c) 2024 Jeremy Fix 

4 

5# Permission is hereby granted, free of charge, to any person obtaining a copy 

6# of this software and associated documentation files (the "Software"), to deal 

7# in the Software without restriction, including without limitation the rights 

8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

9# copies of the Software, and to permit persons to whom the Software is 

10# furnished to do so, subject to the following conditions: 

11 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23# Standard imports 

24# External imports 

25from torch.utils.data import Dataset 

26import numpy as np 

27 

28# Local imports 

29from . import VolFile, LeaderFile, TrailerFile, SARImage 

30 

31 

32class ALOSDataset(Dataset): 

33 r""" 

34 ALOSDataset 

35 

36 The format is described in 

37 `<https://www.eorc.jaxa.jp/ALOS/en/alos-2/pdf/product_format_description/PALSAR-2_xx_Format_CEOS_E_g.pdf>`_ 

38 

39 The dataset is constructed from the volume file. If leader and trailer files 

40 are colocated, they are loaded as well. 

41 

42 Important, this code has been developed for working with L1.1 HBQ-R Quad Pol 

43 datafiles. It is not expected to work out of the box for other levels and 

44 for less than 4 polarizations. 

45 

46 Arguments: 

47 volpath: the path to the VOLUME file 

48 transform : the transform applied the cropped image. It applies 

49 on a dictionnary of patches {'HH': np.array, 'HV': np.array} 

50 crop_coordinates: the subpart of the image to consider as ((row_i, col_i), (row_j, col_j)) 

51 defining the corner coordinates 

52 patch_size: the dimensions of the patches to consider (rows, cols) 

53 patch_stride: the shift between two consecutive patches, default:patch_size 

54 """ 

55 

56 def __init__( 

57 self, 

58 volpath: str = None, 

59 transform=None, 

60 crop_coordinates: tuple = None, 

61 patch_size: tuple = (128, 128), 

62 patch_stride: tuple = None, 

63 ): 

64 super().__init__() 

65 

66 self.transform = transform 

67 

68 self.patch_size = patch_size 

69 self.patch_stride = patch_stride 

70 if patch_stride is None: 

71 self.patch_stride = patch_size 

72 

73 self.volFile = VolFile(volpath) 

74 

75 leader_filepath = volpath.parents[0] / volpath.name.replace("VOL-", "LED-") 

76 self.leaderFile = None 

77 if leader_filepath.exists(): 

78 self.leaderFile = LeaderFile(leader_filepath) 

79 

80 trailer_filepath = volpath.parents[0] / volpath.name.replace("VOL-", "TRL-") 

81 self.trailerFile = None 

82 if trailer_filepath.exists(): 

83 self.trailerFile = TrailerFile(trailer_filepath) 

84 

85 self.crop_coordinates = None 

86 if crop_coordinates is not None: 

87 self.crop_coordinates = crop_coordinates 

88 

89 self.images = {} 

90 for pol in ["HH", "HV", "VH", "VV"]: 

91 filepath = volpath.parents[0] / volpath.name.replace("VOL-", f"IMG-{pol}-") 

92 if not filepath.exists(): 

93 continue 

94 self.images[pol] = SARImage(filepath) 

95 if self.crop_coordinates is None: 

96 self.crop_coordinates = ( 

97 (0, 0), 

98 (self.images[pol].num_rows, self.images[pol].num_cols), 

99 ) 

100 

101 if len(self.images) != self.volFile.num_polarizations: 

102 raise RuntimeError( 

103 f"I was expecting {self.volFile.num_polarizations} data file but I found {len(self.images)} data file" 

104 ) 

105 

106 # Precompute the dimension of the grid of patches 

107 nrows = self.crop_coordinates[1][0] - self.crop_coordinates[0][0] 

108 ncols = self.crop_coordinates[1][1] - self.crop_coordinates[0][1] 

109 

110 nrows_patch, ncols_patch = self.patch_size 

111 row_stride, col_stride = self.patch_stride 

112 

113 self.nsamples_per_rows = (nrows - nrows_patch) // row_stride + 1 

114 self.nsamples_per_cols = (ncols - ncols_patch) // col_stride + 1 

115 

116 @property 

117 def polarizations(self): 

118 return self.images.keys() 

119 

120 def describe(self): 

121 print( 

122 f""" 

123Volume File 

124=========== 

125{self.volFile} 

126 

127Leader File 

128=========== 

129{self.leaderFile} 

130 

131Trailer File 

132=========== 

133{self.trailerFile} 

134""" 

135 ) 

136 

137 def __len__(self) -> int: 

138 """ 

139 Returns the length of the dataset according to the patch size, stride 

140 and image size 

141 

142 Returns: 

143 int: the total number of available patches 

144 """ 

145 

146 return self.nsamples_per_rows * self.nsamples_per_cols 

147 

148 def __getitem__(self, idx: int): 

149 """ 

150 Access and returns the subpatch specified by the index 

151 

152 Arguments: 

153 idx: the index of the patch to access 

154 """ 

155 row_stride, col_stride = self.patch_stride 

156 start_row = ( 

157 self.crop_coordinates[0][0] + (idx // self.nsamples_per_cols) * row_stride 

158 ) 

159 start_col = ( 

160 self.crop_coordinates[0][1] + (idx % self.nsamples_per_cols) * col_stride 

161 ) 

162 num_rows, num_cols = self.patch_size 

163 patches = { 

164 pol: im.read_patch(start_row, num_rows, start_col, num_cols) 

165 * self.leaderFile.calibration_factor 

166 for pol, im in self.images.items() 

167 } 

168 

169 if self.transform is not None: 

170 patches = self.transform(patches) 

171 else: 

172 patches = np.stack([patchi for _, patchi in patches.items()]) 

173 

174 return patches