The load_from_folder module¶
The load_from_folder module contains the classes that are necessary to
load data from disk and these are inspired by the
ImageFolder
class in the torchvision library. This module is designed with one specific case in mind.
Such case is the following: given a multi-modal dataset with tabular data,
images and text, the images do not fit in memory, and therefore, they have to
be loaded from disk. However, as any other functionality in this library,
there is some flexibility and some additional cases can also be addressed
using this module.
For this module to be used, the datasets must be prepared in a certain way:
-
the tabular data must contain a column with the images names as stored in disk, including the extension (
.jpg,.png, etc...). -
Regarding to the text dataset, the tabular data can contain a column with the texts themselves or the names of the files containing the texts as stored in disk.
The tabular data might or might not fit in disk itself. If it does not, please
see the ChunkPreprocessor utilities at the preprocessing module and
the examples folder in the repo, which illustrate such case. Finally note
that only csv format is currently supported in that case (more formats
might come soon).
TabFromFolder ¶
This class is used to load tabular data from disk. The current constrains are:
- The only file format supported right now is csv
- The csv file must contain headers
For examples, please, see the examples folder in the repo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
str
|
the name of the csv file |
required |
directory
|
Optional[str]
|
the path to the directory where the csv file is located. If None,
a |
None
|
target_col
|
Optional[str]
|
the name of the target column. If None, a |
None
|
preprocessor
|
Optional[TabularPreprocessor]
|
a fitted |
None
|
text_col
|
Optional[Union[str, List[str]]]
|
the name of the column with the texts themselves or the names of the
files that contain the text dataset. If None, either there is no text
column or a |
None
|
img_col
|
Optional[Union[str, List[str]]]
|
the name of the column with the the names of the images. If None,
either there is no image column or a |
None
|
ignore_target
|
bool
|
whether to ignore the target column. This is normally set to True when this class is used for a test dataset. |
False
|
reference
|
Optional[Any]
|
a reference |
None
|
verbose
|
Optional[int]
|
verbosity. If 0, no output will be printed during the process. |
1
|
Source code in pytorch_widedeep/load_from_folder/tabular/tabular_from_folder.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
get_item ¶
get_item(idx)
This method is used to retrieve a sample from the csv file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
the index of the sample to retrieve |
required |
Returns:
| Type | Description |
|---|---|
Tuple
|
a tuple with the processed tabular data, the text data and/or the image data, and the target variable |
Source code in pytorch_widedeep/load_from_folder/tabular/tabular_from_folder.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
WideFromFolder ¶
Bases: TabFromFolder
This class is mostly identical to TabFromFolder but exists because we
want to separate the treatment of the wide and the deep tabular
components
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
str
|
the name of the csv file |
required |
directory
|
Optional[str]
|
the path to the directory where the csv file is located. If None,
a |
None
|
target_col
|
Optional[str]
|
the name of the target column. If None, a |
None
|
preprocessor
|
Optional[TabularPreprocessor]
|
a fitted |
None
|
text_col
|
Optional[str]
|
the name of the column with the texts themselves or the names of the
files that contain the text dataset. If None, either there is no text
column or a |
None
|
img_col
|
Optional[str]
|
the name of the column with the the names of the images. If None,
either there is no image column or a |
None
|
ignore_target
|
bool
|
whether to ignore the target column. This is normally used when this class is used for a test dataset. |
False
|
reference
|
Optional[Any]
|
a reference |
None
|
verbose
|
int
|
verbosity. If 0, no output will be printed during the process. |
1
|
Source code in pytorch_widedeep/load_from_folder/tabular/tabular_from_folder.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
TextFromFolder ¶
This class is used to load the text dataset (i.e. the text files) from a folder, or to retrieve the text given a texts column specified within the preprocessor object.
For examples, please, see the examples folder in the repo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preprocessor
|
Union[TextPreprocessor, ChunkTextPreprocessor, HFPreprocessor, ChunkHFPreprocessor, List[TextPreprocessor], List[ChunkTextPreprocessor], List[HFPreprocessor], List[ChunkHFPreprocessor]]
|
The preprocessor used to process the text. It must be fitted before using this class |
required |
Source code in pytorch_widedeep/load_from_folder/text/text_from_folder.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
get_item ¶
get_item(text)
Given a text or a list of texts corresponding to different text columns, this method will return the processed text or a list of processed texts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
Union[str, List[str]]
|
The text or list of texts to be processed |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, List[ndarray]]
|
The processed text or a list of processed texts |
Source code in pytorch_widedeep/load_from_folder/text/text_from_folder.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
ImageFromFolder ¶
This class is used to load the image dataset from disk. It is inspired by
the ImageFolder class at the torchvision library. Here, we have
simply adapted to work within the context of a Wide and Deep multi-modal
model.
For examples, please, see the examples folder in the repo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Optional[Union[str, List[str]]]
|
the path to the directory where the images are located. If None, a preprocessor must be provided. |
None
|
preprocessor
|
Optional[Union[ImagePreprocessor, List[ImagePreprocessor]]]
|
a fitted |
None
|
loader
|
Callable[[str], Any]
|
a function to load a sample given its path. |
default_loader
|
extensions
|
Optional[Tuple[str, ...]]
|
a tuple with the allowed extensions. If None, IMG_EXTENSIONS will be used where IMG_EXTENSIONS =".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp" |
None
|
transforms
|
Optional[Any]
|
a |
None
|
Source code in pytorch_widedeep/load_from_folder/image/image_from_folder.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
get_item ¶
get_item(fname)
This method is used to load the image dataset(s) from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
Union[str, List[str]]
|
the name of the image file(s) to load. If a list is provided, the method will return a list of numpy arrays. Each element in the list corresponds to the image in the different image columns. |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, List[ndarray]]
|
a numpy array or a list of numpy arrays representing the image(s) |
Source code in pytorch_widedeep/load_from_folder/image/image_from_folder.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
WideDeepDatasetFromFolder ¶
Bases: Dataset
This class is the Dataset counterpart of the WideDeepDataset class.
Given a reference tabular dataset, with columns that indicate the path to
the images and to the text files or the texts themselves, it will use the
[...]FromFolder classes to load the data consistently from disk per batch.
For examples, please, see the examples folder in the repo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of samples in the dataset |
required |
tab_from_folder
|
Optional[TabFromFolder]
|
Instance of the |
None
|
wide_from_folder
|
Optional[WideFromFolder]
|
Instance of the |
None
|
text_from_folder
|
Optional[TextFromFolder]
|
Instance of the |
None
|
img_from_folder
|
Optional[ImageFromFolder]
|
Instance of the |
None
|
reference
|
Optional[Any]
|
If |
None
|
Source code in pytorch_widedeep/load_from_folder/wd_dataset_from_folder.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |