@@ -30,12 +30,12 @@ def __len__(self) -> int:
3030 def get_image (self , idx : int ) -> np .ndarray :
3131 ...
3232
33- @property
34- def marks_none (self ) -> Optional [np .ndarray ]:
33+ @classmethod
34+ def marks_none (cls ) -> Optional [np .ndarray ]:
3535 return None
3636
37- @property
38- def meta_none (self ) -> Optional [Dict ]:
37+ @classmethod
38+ def meta_none (cls ) -> Optional [Dict ]:
3939 return None
4040
4141 def get_marks (self , idx : int ) -> Optional [np .ndarray ]:
@@ -49,9 +49,9 @@ def __getitem__(
4949 ) -> Tuple [np .ndarray , Optional [np .ndarray ], Optional [Dict [Any , Any ]]]: # (image, marks, meta)
5050 idx = self .idx_sampler [idx ]
5151 marks = self .get_marks (idx )
52- marks = marks if marks is not None else self .marks_none
52+ marks = marks if marks is not None else self .marks_none ()
5353 meta = self .get_meta (idx )
54- meta = meta if meta is not None else self .meta_none
54+ meta = meta if meta is not None else self .meta_none ()
5555 return self .get_image (idx ), marks , meta
5656
5757 @property
@@ -114,16 +114,21 @@ def __init__(
114114 collate_fn : Optional [Callable ] = None ,
115115 ) -> None :
116116 super ().__init__ (name , batch_size = batch_size )
117+ # Get the images paths
117118 images_dir_path = self ._get_absolute_local_path (images_dir_path )
118- landmarks_dir_path = self ._get_absolute_local_path (landmarks_dir_path )
119-
120119 self .image_paths = self ._get_all_paths_based_on_suffix (images_dir_path , self .image_suffix )
121- self .marks_paths = self ._get_all_paths_based_on_suffix (landmarks_dir_path , self .marks_suffix )
122- if len (self .marks_paths ) != len (self .image_paths ):
123- raise ValueError (
124- f"{ self .__class__ .__name__ } : Only { len (self .marks_paths )} found "
125- f"for { len (self .marks_paths )} of the images."
126- )
120+
121+ self .marks_paths = None
122+ # If landmarks folder is not none, we should load them
123+ # Else, the get marks method should be overridden
124+ if landmarks_dir_path is not None :
125+ landmarks_dir_path = self ._get_absolute_local_path (landmarks_dir_path )
126+ self .marks_paths = self ._get_all_paths_based_on_suffix (landmarks_dir_path , self .marks_suffix )
127+ if len (self .marks_paths ) != len (self .image_paths ):
128+ raise ValueError (
129+ f"{ self .__class__ .__name__ } : Only { len (self .marks_paths )} found "
130+ f"for { len (self .marks_paths )} of the images."
131+ )
127132
128133 self .shuffle = shuffle
129134
@@ -151,9 +156,7 @@ def _get_absolute_local_path(self, local_path: Union[str, Path]) -> Path:
151156
152157 @classmethod
153158 def _get_all_paths_based_on_suffix (cls , dir_path : Path , suffix : str ) -> List [Path ]:
154- all_paths_with_suffix = list (
155- sorted ([p for p in dir_path .iterdir () if p .suffix == suffix ], key = lambda p : str (p ))
156- )
159+ all_paths_with_suffix = list (sorted ([p for p in dir_path .iterdir () if p .suffix == suffix ], key = str ))
157160 if len (all_paths_with_suffix ) == 0 :
158161 raise ValueError (
159162 f"{ cls .__class__ .__name__ } : Landmarks with suffix { suffix } "
@@ -162,11 +165,11 @@ def _get_all_paths_based_on_suffix(cls, dir_path: Path, suffix: str) -> List[Pat
162165 return all_paths_with_suffix
163166
164167 def __len__ (self ) -> int :
165- return math .floor (len (self .image_paths ) / self .batch_size )
168+ return math .ceil (len (self .image_paths ) / self .batch_size )
166169
167- @property
168- def marks_none (self ) :
169- return np .full ((self .n_landmarks , self .n_landmarks ), np .nan )
170+ @classmethod
171+ def marks_none (cls ) -> np . ndarray :
172+ return np .full ((cls .n_landmarks , cls .n_landmarks ), np .nan )
170173
171174 def get_image (self , idx : int ) -> np .ndarray :
172175 return self ._load_and_validate_image (self .image_paths [idx ])
0 commit comments