Skip to content

Commit db40736

Browse files
rijobrowyli
andauthored
densenet training download data (#685)
Co-authored-by: Wenqi Li <[email protected]>
1 parent 36c5dde commit db40736

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

3d_classification/densenet_training_array.ipynb

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,40 +131,17 @@
131131
}
132132
],
133133
"source": [
134-
"# set this in your environment or previous cell to wherever IXI is downloaded and extracted\n",
134+
"# Set data directory\n",
135135
"directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n",
136-
"\n",
137-
"if directory is None:\n",
138-
" resource = \"http://biomedic.doc.ic.ac.uk/brain-development/downloads/IXI/IXI-T1.tar\"\n",
139-
" md5 = \"34901a0593b41dd19c1a1f746eac2d58\"\n",
140-
"\n",
141-
" root_dir = tempfile.mkdtemp()\n",
142-
"\n",
143-
" dataset_dir = os.path.join(root_dir, \"ixi\")\n",
144-
" tarfile_name = f\"{dataset_dir}.tar\"\n",
145-
"\n",
146-
" download_and_extract(resource, tarfile_name, dataset_dir, md5)\n",
147-
"else:\n",
148-
" root_dir = directory\n",
149-
"\n",
136+
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
150137
"print(root_dir)"
151138
]
152139
},
153140
{
154141
"cell_type": "code",
155142
"execution_count": 4,
156143
"metadata": {},
157-
"outputs": [
158-
{
159-
"name": "stdout",
160-
"output_type": "stream",
161-
"text": [
162-
"<class 'torch.Tensor'> torch.Size([3, 1, 96, 96, 96]) tensor([[1., 0.],\n",
163-
" [1., 0.],\n",
164-
" [1., 0.]]) torch.Size([3, 2])\n"
165-
]
166-
}
167-
],
144+
"outputs": [],
168145
"source": [
169146
"# IXI dataset as a demo, downloadable from https://brain-development.org/ixi-dataset/\n",
170147
"images = [\n",
@@ -195,8 +172,58 @@
195172
"\n",
196173
"# Represent labels in one-hot format for binary classifier training,\n",
197174
"# BCEWithLogitsLoss requires target to have same shape as input\n",
198-
"labels = torch.nn.functional.one_hot(torch.as_tensor(labels)).float()\n",
175+
"labels = torch.nn.functional.one_hot(torch.as_tensor(labels)).float()"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 5,
181+
"metadata": {},
182+
"outputs": [
183+
{
184+
"name": "stderr",
185+
"output_type": "stream",
186+
"text": [
187+
"ixi.tar: 100%|██████████| 4.51G/4.51G [08:19<00:00, 9.70MB/s] \n"
188+
]
189+
},
190+
{
191+
"name": "stdout",
192+
"output_type": "stream",
193+
"text": [
194+
"2022-05-04 12:23:06,530 - INFO - Downloaded: /mnt/data/rbrown/Documents/Data/MONAI/ixi.tar\n",
195+
"2022-05-04 12:23:13,734 - INFO - Verified 'ixi.tar', md5: 34901a0593b41dd19c1a1f746eac2d58.\n",
196+
"2022-05-04 12:23:13,735 - INFO - Writing into directory: /mnt/data/rbrown/Documents/Data/MONAI/ixi.\n"
197+
]
198+
}
199+
],
200+
"source": [
201+
"if not os.path.isfile(images[0]):\n",
202+
" resource = \"http://biomedic.doc.ic.ac.uk/brain-development/downloads/IXI/IXI-T1.tar\"\n",
203+
" md5 = \"34901a0593b41dd19c1a1f746eac2d58\"\n",
199204
"\n",
205+
" dataset_dir = os.path.join(root_dir, \"ixi\")\n",
206+
" tarfile_name = f\"{dataset_dir}.tar\"\n",
207+
"\n",
208+
" download_and_extract(resource, tarfile_name, dataset_dir, md5)"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": 6,
214+
"metadata": {},
215+
"outputs": [
216+
{
217+
"name": "stdout",
218+
"output_type": "stream",
219+
"text": [
220+
"<class 'torch.Tensor'> torch.Size([3, 1, 96, 96, 96]) tensor([[1., 0.],\n",
221+
" [1., 0.],\n",
222+
" [1., 0.]]) torch.Size([3, 2])\n"
223+
]
224+
}
225+
],
226+
"source": [
200227
"# Define transforms\n",
201228
"train_transforms = Compose([ScaleIntensity(), AddChannel(), Resize((96, 96, 96)), RandRotate90(), EnsureType()])\n",
202229
"\n",
@@ -220,7 +247,7 @@
220247
},
221248
{
222249
"cell_type": "code",
223-
"execution_count": 5,
250+
"execution_count": 7,
224251
"metadata": {},
225252
"outputs": [
226253
{
@@ -367,7 +394,7 @@
367394
},
368395
{
369396
"cell_type": "code",
370-
"execution_count": 6,
397+
"execution_count": 8,
371398
"metadata": {},
372399
"outputs": [],
373400
"source": [
@@ -395,7 +422,7 @@
395422
},
396423
{
397424
"cell_type": "code",
398-
"execution_count": 7,
425+
"execution_count": 9,
399426
"metadata": {},
400427
"outputs": [],
401428
"source": [
@@ -405,7 +432,7 @@
405432
},
406433
{
407434
"cell_type": "code",
408-
"execution_count": 8,
435+
"execution_count": 10,
409436
"metadata": {
410437
"scrolled": true,
411438
"tags": []
@@ -462,7 +489,7 @@
462489
},
463490
{
464491
"cell_type": "code",
465-
"execution_count": 9,
492+
"execution_count": 11,
466493
"metadata": {},
467494
"outputs": [],
468495
"source": [

0 commit comments

Comments
 (0)