|
11 | 11 | "\n",
|
12 | 12 | "### What are transforms?\n",
|
13 | 13 | "\n",
|
14 |
| - "- We use transforms to modify data. In MONAI, we use them to (for exampl) load images from file, add a channel component, normalise the intensities and reshape the image.\n", |
| 14 | + "- We use transforms to modify data. In MONAI, we use them to (for example) load images from file, add a channel component, normalise the intensities and reshape the image.\n", |
15 | 15 | "- We can also use transforms as a method of data augmentation – we have a finite amount of data so to avoid overfitting, we can apply random transforms to modify our data each epoch.\n",
|
16 | 16 | "- Examples of random transformations might be randomly flipping, rotating, cropping, padding, zooming, as well as applying non-rigid deformations.\n",
|
17 | 17 | "\n",
|
|
229 | 229 | " def __call__(self, data):\n",
|
230 | 230 | " d = dict(data)\n",
|
231 | 231 | " im = d[self.label_key]\n",
|
232 |
| - " q = np.sum((im > 0).reshape(-1, im.shape[-1]), axis=0)\n", |
233 |
| - " _slice = np.where(q == np.max(q))[0][0]\n", |
| 232 | + " q = (im > 0).reshape(-1, im.shape[-1]).sum(dim=0)\n", |
| 233 | + " _slice = q.argmax(dim=0)\n", |
234 | 234 | " for key in self.keys:\n",
|
235 | 235 | " d[key] = d[key][..., _slice]\n",
|
236 | 236 | " return d\n",
|
|
244 | 244 | " def __call__(self, data):\n",
|
245 | 245 | " d = {}\n",
|
246 | 246 | " for key in self.keys:\n",
|
247 |
| - " fname = os.path.basename(\n", |
248 |
| - " data[key + \"_meta_dict\"][\"filename_or_obj\"])\n", |
| 247 | + " fname = os.path.basename(data[key].meta[\"filename_or_obj\"])\n", |
249 | 248 | " path = os.path.join(self.path, key, fname)\n",
|
250 |
| - " nib.save(nib.Nifti1Image(data[key], np.eye(4)), path)\n", |
| 249 | + " nib.save(nib.Nifti1Image(data[key].numpy(), np.eye(4)), path)\n", |
251 | 250 | " d[key] = path\n",
|
252 | 251 | " return d\n",
|
253 | 252 | "\n",
|
|
258 | 257 | " os.makedirs(os.path.join(data_dir, key), exist_ok=True)\n",
|
259 | 258 | "transform_2d_slice = Compose([\n",
|
260 | 259 | " LoadImaged(keys),\n",
|
261 |
| - " FromMetaTensord(keys),\n", |
262 | 260 | " AsChannelFirstd(\"image\"),\n",
|
263 | 261 | " AddChanneld(\"label\"),\n",
|
264 | 262 | " SliceWithMaxNumLabelsd(keys, \"label\"),\n",
|
265 | 263 | " SaveSliced(keys, data_dir),\n",
|
266 | 264 | "])\n",
|
| 265 | + "\n", |
267 | 266 | "# Running the whole way through the dataset will create the 2D slices and save to file\n",
|
268 | 267 | "ds_2d = Dataset(data_dicts, transform_2d_slice)\n",
|
269 | 268 | "dl_2d = DataLoader(ds_2d, batch_size=1, num_workers=10)\n",
|
|
306 | 305 | " [\n",
|
307 | 306 | " LoadImaged(keys),\n",
|
308 | 307 | " FromMetaTensord(keys),\n",
|
309 |
| - " Lambdad(\"label\", lambda x: (x > 0).astype(\n", |
310 |
| - " np.float64)), # make label binary\n", |
| 308 | + " Lambdad(\"label\", lambda x: (x > 0).to(\n", |
| 309 | + " torch.float32)), # make label binary\n", |
311 | 310 | " RandAffined(\n",
|
312 | 311 | " keys,\n",
|
313 | 312 | " prob=1.0,\n",
|
|
1627 | 1626 | "minimal_transforms = Compose([\n",
|
1628 | 1627 | " LoadImaged(keys),\n",
|
1629 | 1628 | " FromMetaTensord(keys),\n",
|
1630 |
| - " Lambdad(\"label\", lambda x: (x > 0).astype(\n", |
1631 |
| - " np.float64)), # make label binary\n", |
| 1629 | + " Lambdad(\"label\", lambda x: (x > 0).to(\n", |
| 1630 | + " torch.float32)), # make label binary\n", |
1632 | 1631 | " ScaleIntensityd(\"image\"),\n",
|
1633 | 1632 | " EnsureTyped(keys),\n",
|
1634 | 1633 | "])\n",
|
|
0 commit comments