Skip to content

Commit

Permalink
Formatting.
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
  • Loading branch information
mzient committed Oct 2, 2024
1 parent e5541b1 commit 614b649
Showing 1 changed file with 75 additions and 46 deletions.
121 changes: 75 additions & 46 deletions docs/examples/image_processing/multiple_crops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"import math\n",
"import os\n",
"\n",
"\n",
"def show(images, rows, columns):\n",
" n = len(images)\n",
"\n",
Expand All @@ -49,30 +50,30 @@
" continue\n",
" img = np.array(images[i])\n",
" plt.imshow(img)\n",
" \n",
"\n",
"\n",
"def show_seqs(sequences):\n",
" sequences = [np.array(seq) for seq in sequences]\n",
" max_len = 0\n",
" for seq in sequences:\n",
" max_len = max(max_len, seq.shape[0])\n",
" \n",
" if max_len == 0: return\n",
"\n",
" if max_len == 0:\n",
" return\n",
"\n",
" images = []\n",
" \n",
"\n",
" for seq in sequences:\n",
" seq_len = seq.shape[0]\n",
" for i in range(seq_len):\n",
" images.append(seq[i])\n",
" for i in range(seq_len, max_len):\n",
" images.append(None)\n",
" \n",
"\n",
" show(images, len(sequences), max_len)\n",
" \n",
"\n",
"dir = os.path.join(os.environ[\"DALI_EXTRA_PATH\"], 'db', 'single', 'jpeg')\n",
"\n"
"\n",
"dir = os.path.join(os.environ[\"DALI_EXTRA_PATH\"], \"db\", \"single\", \"jpeg\")"
]
},
{
Expand All @@ -97,44 +98,67 @@
"source": [
"@pipeline_def(\n",
" batch_size=3,\n",
" num_threads=4, device_id=0,\n",
" num_threads=4,\n",
" device_id=0,\n",
" experimental_exec_dynamic=True, # allows to manipulate shapes of GPU images in a CPU op\n",
" seed=42\n",
" seed=42,\n",
")\n",
"def fixed_size_crops():\n",
" files, labels = fn.readers.file(file_root=dir, random_shuffle=True)\n",
" img = fn.decoders.image(files, device=\"mixed\")\n",
" shape = fn.shapes(img, device=\"cpu\", dtype=dali.types.INT32)\n",
" h, w = shape[0], shape[1]\n",
" # this is fixed for all crops taken from given image\n",
" crop_w = fn.random.uniform(range=fn.stack(w/4, w/2), dtype=dali.types.INT32)\n",
" crop_h = fn.random.uniform(range=fn.stack(h/4, h/2), dtype=dali.types.INT32)\n",
" crop_w = fn.random.uniform(\n",
" range=fn.stack(w / 4, w / 2), dtype=dali.types.INT32\n",
" )\n",
" crop_h = fn.random.uniform(\n",
" range=fn.stack(h / 4, h / 2), dtype=dali.types.INT32\n",
" )\n",
" # the number of crops is random, from 1 to 5\n",
" num_crops = fn.random.uniform(range=(1, 5), dtype=dali.types.INT32)\n",
" # the crop origin is selected so that the crop doesn't go outside the image\n",
" crop_x_range = fn.cast(fn.stack(0, w-crop_w), dtype=dali.types.FLOAT) # stacking different types\n",
" crop_y_range = fn.cast(fn.stack(0, h-crop_h), dtype=dali.types.FLOAT)\n",
" crop_x_range = fn.cast(\n",
" fn.stack(0, w - crop_w), dtype=dali.types.FLOAT\n",
" ) # stacking different types\n",
" crop_y_range = fn.cast(fn.stack(0, h - crop_h), dtype=dali.types.FLOAT)\n",
" crop_param_shape = num_crops[dali.newaxis]\n",
" crop_x = fn.random.uniform(shape=crop_param_shape, range=crop_x_range, dtype=dali.types.INT32)\n",
" crop_y = fn.random.uniform(shape=crop_param_shape, range=crop_y_range, dtype=dali.types.INT32)\n",
" \n",
" crop_x = fn.random.uniform(\n",
" shape=crop_param_shape, range=crop_x_range, dtype=dali.types.INT32\n",
" )\n",
" crop_y = fn.random.uniform(\n",
" shape=crop_param_shape, range=crop_y_range, dtype=dali.types.INT32\n",
" )\n",
"\n",
" # these are in XY order as opposed to tensor shape, which is HW\n",
" crop_start = fn.stack(crop_x, crop_y, axis=1)\n",
" crop_size = fn.stack(crop_w, crop_h, axis=0)[dali.newaxis]\n",
" crop_end = crop_start + crop_size\n",
" \n",
" crop_start = fn.per_frame(crop_start + 0.) # convert to float and mark outermost dimension as\"frame\"\n",
" crop_end = fn.per_frame(crop_end + 0.)\n",
" crop_size = fn.per_frame(crop_size + 0.)\n",
" \n",
" # create a \"crop\" transform matrix - the \"from\" coordinates are the coordinates within the source image\n",
" # the \"to\" coordinates are in the destination image\n",
" mtx = fn.transforms.crop(from_start=crop_start, from_end=crop_end, to_start=0., to_end=crop_size)\n",
" \n",
" crops = fn.warp_affine(img, mtx, size=fn.stack(crop_h, crop_w), interp_type=dali.types.INTERP_NN, inverse_map=False)\n",
" \n",
"\n",
" crop_start = fn.per_frame(\n",
" crop_start + 0.0\n",
" ) # convert to float and mark outermost dimension as\"frame\"\n",
" crop_end = fn.per_frame(crop_end + 0.0)\n",
" crop_size = fn.per_frame(crop_size + 0.0)\n",
"\n",
" # create a \"crop\" transform matrix\n",
" # - the \"from\" coordinates are the coordinates within the source image\n",
" # - the \"to\" coordinates are in the destination image\n",
" mtx = fn.transforms.crop(\n",
" from_start=crop_start, from_end=crop_end, to_start=0.0, to_end=crop_size\n",
" )\n",
"\n",
" crops = fn.warp_affine(\n",
" img,\n",
" mtx,\n",
" size=fn.stack(crop_h, crop_w),\n",
" interp_type=dali.types.INTERP_NN,\n",
" inverse_map=False,\n",
" )\n",
"\n",
" return crops\n",
"\n",
"\n",
"fixed_crop_pipe = fixed_size_crops()\n",
"fixed_crop_pipe.build()"
]
Expand All @@ -159,9 +183,8 @@
}
],
"source": [
"crops, = fixed_crop_pipe.run()\n",
"show_seqs(crops.as_cpu())\n",
"\n"
"(crops,) = fixed_crop_pipe.run()\n",
"show_seqs(crops.as_cpu())"
]
},
{
Expand All @@ -185,9 +208,10 @@
"source": [
"@pipeline_def(\n",
" batch_size=3,\n",
" num_threads=4, device_id=0,\n",
" num_threads=4,\n",
" device_id=0,\n",
" experimental_exec_dynamic=True, # allows to manipulate shapes of GPU images in a CPU op\n",
" seed=123\n",
" seed=123,\n",
")\n",
"def var_sized_resized_crops():\n",
" files, labels = fn.readers.file(file_root=dir, random_shuffle=True)\n",
Expand All @@ -201,22 +225,27 @@
" input_size_wh = fn.stack(w, h)\n",
" crop_size = fn.random.uniform(shape=crop_param_shape, range=(0.25, 0.5))\n",
" crop_start = fn.random.uniform(shape=crop_param_shape, range=(0, 1))\n",
" crop_start *= (1 - crop_size)\n",
" \n",
" output_size = dali.types.Constant([64., 64.])\n",
" \n",
" crop_start *= 1 - crop_size\n",
"\n",
" output_size = dali.types.Constant([64.0, 64.0])\n",
"\n",
" crop_end = crop_start + crop_size\n",
" \n",
" crop_start = fn.per_frame(crop_start * input_size_wh + 0.) # convert to float and mark outermost dimension as\"frame\"\n",
" crop_end = fn.per_frame(crop_end * input_size_wh + 0.)\n",
" crop_size = fn.per_frame(crop_size * input_size_wh + 0.)\n",
" \n",
" mtx = fn.transforms.crop(from_start=crop_start, from_end=crop_end, to_start=0, to_end=output_size)\n",
" \n",
"\n",
" crop_start = fn.per_frame(\n",
" crop_start * input_size_wh + 0.0\n",
" ) # convert to float and mark outermost dimension as\"frame\"\n",
" crop_end = fn.per_frame(crop_end * input_size_wh + 0.0)\n",
" crop_size = fn.per_frame(crop_size * input_size_wh + 0.0)\n",
"\n",
" mtx = fn.transforms.crop(\n",
" from_start=crop_start, from_end=crop_end, to_start=0, to_end=output_size\n",
" )\n",
"\n",
" crops = fn.warp_affine(img, mtx, size=output_size, inverse_map=False)\n",
" \n",
"\n",
" return crops\n",
"\n",
"\n",
"var_crop_pipe = var_sized_resized_crops()\n",
"var_crop_pipe.build()"
]
Expand Down Expand Up @@ -248,7 +277,7 @@
"source": [
"crops, *tail = var_crop_pipe.run()\n",
"print(*tail)\n",
"show_seqs(crops.as_cpu())\n"
"show_seqs(crops.as_cpu())"
]
}
],
Expand Down

0 comments on commit 614b649

Please sign in to comment.