-
Notifications
You must be signed in to change notification settings - Fork 609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update ExternalSource framework examples #2482
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"execution_count": 5, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -24,8 +24,9 @@ | |
"import numpy as np\n", | ||
"from random import shuffle\n", | ||
"from nvidia.dali.pipeline import Pipeline\n", | ||
"import nvidia.dali.ops as ops \n", | ||
"import nvidia.dali.types as types\n", | ||
"import nvidia.dali as dali\n", | ||
"import nvidia.dali.fn as fn\n", | ||
"import mxnet\n", | ||
"\n", | ||
"batch_size = 3\n", | ||
"epochs = 3" | ||
|
@@ -40,7 +41,7 @@ | |
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -72,17 +73,15 @@ | |
"\n", | ||
" for _ in range(self.batch_size):\n", | ||
" jpeg_filename, label = self.files[self.i].split(' ')\n", | ||
" f = open(self.images_dir + jpeg_filename, 'rb')\n", | ||
" batch.append(np.frombuffer(f.read(), dtype = np.uint8))\n", | ||
" labels.append(np.array([label], dtype = np.uint8))\n", | ||
" batch.append(np.fromfile(self.images_dir + jpeg_filename, dtype = np.uint8)) # we can use numpy\n", | ||
" labels.append(mxnet.ndarray.array([int(label)], dtype = 'uint8')) # or MXNet native arrays\n", | ||
" self.i = (self.i + 1) % self.n\n", | ||
" return (batch, labels)\n", | ||
"\n", | ||
" @property\n", | ||
" def size(self,):\n", | ||
" def __len__(self):\n", | ||
" return self.data_set_len\n", | ||
"\n", | ||
" next = __next__\n" | ||
" next = __next__" | ||
] | ||
}, | ||
{ | ||
|
@@ -91,46 +90,24 @@ | |
"source": [ | ||
"### Defining the pipeline\n", | ||
"\n", | ||
"Now the pipeline itself will be defined. First of all, a framework iterator will be used so we need to make sure that images and the output of the pipeline are uniforms in size, so resize operator is used. Also, `iter_setup` will raise the StopIteration exception when the AdvancedExternalInputIterator run of data. Worth notice is that iterator needs to be recreated so next time `iter_setup` is called it has ready data to consume." | ||
"Now let's define our pipeline. We need an instance of ``Pipeline`` class and some operators which will define the processing graph. Our external source provides 2 outpus which we can conveniently unpack by specifying ``num_outputs=2`` in the external source operator." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class ExternalSourcePipeline(Pipeline):\n", | ||
" def __init__(self, batch_size, num_threads, device_id, external_data):\n", | ||
" super(ExternalSourcePipeline, self).__init__(batch_size,\n", | ||
" num_threads,\n", | ||
" device_id,\n", | ||
" seed=12)\n", | ||
" self.input = ops.ExternalSource()\n", | ||
" self.input_label = ops.ExternalSource()\n", | ||
" self.decode = ops.ImageDecoder(device = \"mixed\", output_type = types.RGB)\n", | ||
" self.res = ops.Resize(device=\"gpu\", resize_x=240, resize_y=240)\n", | ||
" self.cast = ops.Cast(device = \"gpu\",\n", | ||
" dtype = types.UINT8)\n", | ||
" self.external_data = external_data\n", | ||
" self.iterator = iter(self.external_data)\n", | ||
"\n", | ||
" def define_graph(self):\n", | ||
" self.jpegs = self.input()\n", | ||
" self.labels = self.input_label()\n", | ||
" images = self.decode(self.jpegs)\n", | ||
" images = self.res(images)\n", | ||
" output = self.cast(images)\n", | ||
" return (output, self.labels)\n", | ||
"\n", | ||
" def iter_setup(self):\n", | ||
" try:\n", | ||
" (images, labels) = self.iterator.next()\n", | ||
" self.feed_input(self.jpegs, images)\n", | ||
" self.feed_input(self.labels, labels)\n", | ||
" except StopIteration:\n", | ||
" self.iterator = iter(self.external_data)\n", | ||
" raise StopIteration" | ||
"def ExternalSourcePipeline(batch_size, num_threads, device_id, external_data):\n", | ||
" pipe = Pipeline(batch_size, num_threads, device_id)\n", | ||
" with pipe:\n", | ||
" jpegs, labels = fn.external_source(source=external_data, num_outputs=2)\n", | ||
" images = fn.image_decoder(jpegs, device=\"mixed\")\n", | ||
" images = fn.resize(images, resize_x=240, resize_y=240)\n", | ||
" output = fn.cast(images, dtype=dali.types.UINT8)\n", | ||
" pipe.set_outputs(output, labels)\n", | ||
" return pipe" | ||
] | ||
}, | ||
{ | ||
|
@@ -148,17 +125,9 @@ | |
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"/usr/local/lib/python3.6/dist-packages/nvidia/dali/plugin/base_iterator.py:124: Warning: Please set `reader_name` and don't set last_batch_padded and size manually whenever possible. This may lead, in some situations, to miss some samples or return duplicated ones. Check the Sharding section of the documentation for more details.\n", | ||
" _iterator_deprecation_warning()\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
|
@@ -194,7 +163,7 @@ | |
"eii = ExternalInputIterator(batch_size, 0, 1)\n", | ||
"pipe = ExternalSourcePipeline(batch_size=batch_size, num_threads=2, device_id = 0,\n", | ||
" external_data = eii)\n", | ||
"pii = MXNetIterator(pipe, size=eii.size, last_batch_padded=True, last_batch_policy=LastBatchPolicy.PARTIAL)\n", | ||
"pii = MXNetIterator(pipe, size=len(eii), last_batch_padded=True, last_batch_policy=LastBatchPolicy.PARTIAL)\n", | ||
"\n", | ||
"for e in range(epochs):\n", | ||
" for i, data in enumerate(pii):\n", | ||
|
@@ -219,7 +188,7 @@ | |
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.9" | ||
"version": "3.6.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without it debug build with Clang 6 didn't link properly.