Skip to content
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

rocAL - cmn fix #1024

Merged
merged 7 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions rocAL/docs/examples/image_processing/inference_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright (c) 2018 - 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import sys
from tkinter import W
from amd.rocal.pipeline import pipeline_def
import numpy as np
import rocal_pybind as b
from amd.rocal.plugin.pytorch import ROCALClassificationIterator
import amd.rocal.fn as fn
import amd.rocal.types as types
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt


seed = 1549361629
image_dir = "../../../../data/images/AMD-tinyDataSet/"
batch_size = 4
gpu_id = 0

def show_images(image_batch, device):
columns = 4
rows = (batch_size + 1) // (columns)
#fig = plt.figure(figsize = (32,(32 // columns) * rows))
gs = gridspec.GridSpec(rows, columns)
for j in range(rows*columns):
#print('\n Display image: ', j)
plt.subplot(gs[j])
img = image_batch[j]
plt.axis("off")
if device == "cpu":
plt.imshow(img)
else:
plt.imshow(img.cpu())
plt.show()

def show_images(image_batch, image_batch1, device):
columns = 4
rows = (batch_size + 1) // (columns)
#fig = plt.figure(figsize = (32,(32 // columns) * rows))
gs = gridspec.GridSpec(rows, columns*2)
for j in range(rows*columns):
#print('\n Display image: ', j)
k = j*2
plt.subplot(gs[k])
img = image_batch[j]
plt.axis("off")
if device == "cpu":
plt.imshow(img)
else:
plt.imshow(img.cpu())

plt.subplot(gs[k+1])
img = image_batch1[j]
plt.axis("off")
if device == "cpu":
plt.imshow(img)
else:
plt.imshow(img.cpu())
plt.show()



def show_pipeline_output(pipe, device):
pipe.build()
data_loader = ROCALClassificationIterator(pipe, device)
images = next(iter(data_loader))
show_images(images[0], device)

def show_pipeline_outputs(pipe0, pipe1, device):
pipe0.build()
pipe1.build()
data_loader = ROCALClassificationIterator(pipe0, device)
data_loader1 = ROCALClassificationIterator(pipe1, device)
images = next(iter(data_loader))
images1 = next(iter(data_loader1))
show_images(images[0], images1[0], device)

@pipeline_def(seed=seed)
def inference_pipeline(device="cpu", path=image_dir):
jpegs, labels = fn.readers.file(file_root=path, shard_id=0, num_shards=1, random_shuffle=False)
images = fn.decoders.image(jpegs, file_root=path, max_decoded_width=1024, max_decoded_height=1024, device="cpu", output_type=types.RGB, shard_id=0, num_shards=1, random_shuffle=False)
images_res = fn.resize(images, scaling_mode=types.SCALING_MODE_NOT_SMALLER, interpolation_type=types.TRIANGULAR_INTERPOLATION, resize_shorter=256)
return fn.centre_crop(images_res, crop=(224, 224))

@pipeline_def(seed=seed)
def inference_pipeline_cmn(device="cpu", path=image_dir):
jpegs, labels = fn.readers.file(file_root=path, shard_id=0, num_shards=1, random_shuffle=False)
images = fn.decoders.image(jpegs, file_root=path, max_decoded_width=1024, max_decoded_height=1024, device="cpu", output_type=types.RGB, shard_id=0, num_shards=1, random_shuffle=False)
images_res = fn.resize(images, scaling_mode=types.SCALING_MODE_NOT_SMALLER, interpolation_type=types.TRIANGULAR_INTERPOLATION, resize_shorter=256)
return fn.crop_mirror_normalize(images_res , device="cpu",
output_dtype=types.FLOAT,
output_layout=types.NHWC,
crop=(224, 224),
mirror=0,
image_type=types.RGB,
mean=[0,0,0],
std=[255.0,255.0,255.0])

def main():
print ('Optional arguments: <cpu/gpu image_folder>')
bs = batch_size
rocal_device = "cpu"
img_folder = image_dir
if len(sys.argv) > 1:
if(sys.argv[1] == "gpu"):
rocal_device = "gpu"
if len(sys.argv) > 2:
img_folder = sys.argv[2]

pipe = inference_pipeline(batch_size=bs, num_threads=1, device_id=gpu_id, rocal_cpu=True, tensor_layout=types.NHWC,
reverse_channels=True, multiplier = [0.00392,0.00392,0.00392], device=rocal_device, path=img_folder)
pipe1 = inference_pipeline_cmn(batch_size=bs, num_threads=1, device_id=gpu_id, rocal_cpu=True, tensor_layout=types.NHWC,
reverse_channels=True, multiplier = [0.00392,0.00392,0.00392], device=rocal_device, path=img_folder)
show_pipeline_outputs(pipe, pipe1, device=rocal_device)

if __name__ == '__main__':
main()
7 changes: 4 additions & 3 deletions rocAL/rocAL/include/parameters/parameter_crop.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CropParam
// V Y directoin
public:
CropParam() = delete;
CropParam(unsigned int batch_size): batch_size(batch_size), _random(false), _is_center_crop(false)
CropParam(unsigned int batch_size): batch_size(batch_size), _random(false), _is_fixed_crop(false)
{
x_drift_factor = default_x_drift_factor();
y_drift_factor = default_y_drift_factor();
Expand All @@ -58,7 +58,7 @@ class CropParam
in_height = in_height_;
}
void set_random() {_random = true;}
void set_center_crop() { _is_center_crop = true; }
void set_fixed_crop(float anchor_x, float anchor_y) { _is_fixed_crop = true; _random = false; _crop_anchor[0] = anchor_x; _crop_anchor[1] = anchor_y;}
void set_x_drift_factor(Parameter<float>* x_drift);
void set_y_drift_factor(Parameter<float>* y_drift);
std::vector<uint32_t> in_width, in_height;
Expand All @@ -83,7 +83,8 @@ class CropParam
Parameter<float>* default_x_drift_factor();
Parameter<float>* default_y_drift_factor();
std::vector<uint32_t> x1_arr_val, y1_arr_val, croph_arr_val, cropw_arr_val, x2_arr_val, y2_arr_val;
bool _random, _is_center_crop;
bool _random, _is_fixed_crop;
float _crop_anchor [2] = {0.5, 0.5};
virtual void fill_crop_dims(){};
void update_crop_array();
};
5 changes: 3 additions & 2 deletions rocAL/rocAL/source/api/rocal_api_augmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ rocalResize(
unsigned resize_shorter,
unsigned resize_longer,
RocalResizeInterpolationType interpolation_type) {

Image* output = nullptr;
if ((p_context == nullptr) || (p_input == nullptr)) {
ERR("Invalid ROCAL context or invalid input image")
Expand Down Expand Up @@ -1747,7 +1748,7 @@ rocalCropFixed(
ImageInfo output_info = input->info();
output_info.width(crop_width);
output_info.height(crop_height);
output = context->master_graph->create_image(input->info(), is_output);
output = context->master_graph->create_image(output_info, is_output);
output->reset_image_roi();
std::shared_ptr<CropNode> crop_node = context->master_graph->add_node<CropNode>({input}, {output});
crop_node->init(crop_height, crop_width, crop_pos_x, crop_pos_y);
Expand Down Expand Up @@ -1786,7 +1787,7 @@ rocalCropCenterFixed(
ImageInfo output_info = input->info();
output_info.width(crop_width);
output_info.height(crop_height);
output = context->master_graph->create_image(input->info(), is_output);
output = context->master_graph->create_image(output_info, is_output);
output->reset_image_roi();
std::shared_ptr<CropNode> crop_node = context->master_graph->add_node<CropNode>({input}, {output});
crop_node->init(crop_height, crop_width);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void CropNode::init(unsigned int crop_h, unsigned int crop_w)
_crop_param->crop_h = crop_h;
_crop_param->x1 = 0;
_crop_param->y1 = 0;
_crop_param->set_center_crop();
_crop_param->set_fixed_crop(0.5, 0.5); // for center_crop
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ void CropMirrorNormalizeNode::update_node()
_mirror.update_array();
}

void CropMirrorNormalizeNode::init(int crop_h, int crop_w, float start_x, float start_y, float mean, float std_dev, IntParam *mirror)
void CropMirrorNormalizeNode::init(int crop_h, int crop_w, float anchor_x, float anchor_y, float mean, float std_dev, IntParam *mirror)
{
_crop_param->x1 = start_x;
_crop_param->y1 = start_y;
// current implementation does a fixed crop with specified dims and anchor
_crop_param->x1 = 0;
_crop_param->y1 = 0;
_crop_param->crop_h = crop_h;
_crop_param->crop_w = crop_w;
_crop_param->set_fixed_crop(anchor_x, anchor_y);
_mean = mean;
_std_dev = std_dev;
_mirror.set_param(core(mirror));
Expand Down
6 changes: 3 additions & 3 deletions rocAL/rocAL/source/parameters/parameter_rocal_crop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void RocalCropParam::fill_crop_dims() {
// Evaluating user given crop
cropw_arr_val[img_idx] = (crop_w <= in_width[img_idx] && crop_w > 0) ? crop_w : in_width[img_idx];
croph_arr_val[img_idx] = (crop_h <= in_height[img_idx] && crop_h > 0) ? crop_h : in_height[img_idx];
if (_is_center_crop) {
x1_arr_val[img_idx] = static_cast<size_t>(0.5 * (in_width[img_idx] - cropw_arr_val[img_idx]));
y1_arr_val[img_idx] = static_cast<size_t>(0.5 * (in_height[img_idx] - croph_arr_val[img_idx]));
if (_is_fixed_crop) {
x1_arr_val[img_idx] = static_cast<size_t>(_crop_anchor[0] * (in_width[img_idx] - cropw_arr_val[img_idx]));
y1_arr_val[img_idx] = static_cast<size_t>(_crop_anchor[1] * (in_height[img_idx] - croph_arr_val[img_idx]));
} else {
x1_arr_val[img_idx] = (x1 >= in_width[img_idx]) ? 0 : x1;
y1_arr_val[img_idx] = (y1 >= in_height[img_idx]) ? 0 : y1;
Expand Down
2 changes: 1 addition & 1 deletion rocAL/rocAL_pybind/amd/rocal/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def crop_mirror_normalize(*inputs, bytes_per_sample_hint=0, crop=[0, 0], crop_d=
mirror = b.CreateIntParameter(1)

# pybind call arguments
kwargs_pybind = {"input_image0": inputs[0], "crop_depth":crop_depth, "crop_height":crop_height, "crop_width":crop_width, "start_x":0, "start_y":0, "start_z":0, "mean":mean, "std_dev":std,
kwargs_pybind = {"input_image0": inputs[0], "crop_depth":crop_depth, "crop_height":crop_height, "crop_width":crop_width, "start_x":crop_pos_x, "start_y":crop_pos_y, "start_z":crop_pos_z, "mean":mean, "std_dev":std,
"is_output": False, "mirror": mirror}
cmn = b.CropMirrorNormalize(Pipeline._current_pipeline._handle ,*(kwargs_pybind.values()))
Pipeline._current_pipeline._tensor_layout = output_layout
Expand Down