Exemple #1
0
def check_crop_with_out_of_bounds_policy_support(device,
                                                 batch_size,
                                                 input_shape=(100, 200, 3),
                                                 out_of_bounds_policy=None,
                                                 fill_values=(0x76, 0xb9,
                                                              0x00)):
    # This test case is written with HWC layout in mind and "HW" axes in slice arguments
    layout = "HWC"
    assert (len(input_shape) == 3)
    if fill_values is not None and len(fill_values) > 1:
        assert (input_shape[2] == len(fill_values))

    eii = RandomDataIterator(batch_size, shape=input_shape)
    crop_shape = tuple(extent * 2 for extent in input_shape[:2])
    crop_y = 0.5
    crop_x = 0.5
    pipe = CropSynthPipe(device,
                         batch_size,
                         iter(eii),
                         layout=layout,
                         crop_shape=crop_shape,
                         crop_x=crop_y,
                         crop_y=crop_x,
                         out_of_bounds_policy=out_of_bounds_policy,
                         fill_values=fill_values,
                         extra_outputs=True)
    if fill_values is None:
        fill_values = 0
    pipe.build()
    for k in range(3):
        outs = pipe.run()
        out = outs[0]
        in_data = outs[1]
        if isinstance(out, dali.backend_impl.TensorListGPU):
            out = out.as_cpu()
        if isinstance(in_data, dali.backend_impl.TensorListGPU):
            in_data = in_data.as_cpu()

        assert (batch_size == len(out))
        for idx in range(batch_size):
            sample_in = in_data.at(idx)
            sample_out = out.at(idx)
            in_shape = list(sample_in.shape)
            out_shape = list(sample_out.shape)
            crop_anchor_norm = [crop_y, crop_x]
            crop_anchor_abs = [
                crop_anchor_norm[k] * (input_shape[k] - crop_shape[k])
                for k in range(2)
            ]
            abs_start, abs_end, abs_slice_shape = abs_slice_start_and_end(
                in_shape[:2], crop_anchor_abs, crop_shape, False, False)
            check_slice_output(sample_in, sample_out, crop_anchor_abs,
                               abs_slice_shape, abs_start, abs_end,
                               out_of_bounds_policy, fill_values)
def check_cmn_with_out_of_bounds_policy_support(device, batch_size, dtype, input_layout, input_shape, output_layout, 
                                                mirror_probability, mean, std, should_pad,
                                                out_of_bounds_policy=None, fill_values=(0x76, 0xb9, 0x00)):
    # This test case is written with HWC layout in mind and "HW" axes in slice arguments
    assert(input_layout == "HWC")
    assert(len(input_shape) == 3)
    if fill_values is not None and len(fill_values) > 1:
        assert(input_shape[2] == len(fill_values))
    eii = RandomDataIterator(batch_size, shape=input_shape)
    crop_y, crop_x = 0.5, 0.5
    crop_h, crop_w = input_shape[0] * 2, input_shape[1] * 2
    pipe = CMNRandomDataPipeline(device, batch_size, input_layout, iter(eii),
                                 dtype = dtype, output_layout = output_layout,
                                 mirror_probability = mirror_probability, mean = mean, std = std, pad_output = should_pad,
                                 crop_w = crop_w, crop_h = crop_h,
                                 crop_pos_x = crop_x, crop_pos_y = crop_y,
                                 out_of_bounds_policy = out_of_bounds_policy, fill_values = fill_values, extra_outputs = True)
    permute = None
    if output_layout != input_layout:
        permute = []
        for d in range(len(input_layout)):
            perm_d = input_layout.find(output_layout[d])
            permute.append(perm_d)

    if fill_values is None:
        fill_values = 0
    pipe.build()
    for k in range(3):
        outs = pipe.run()
        out = outs[0]
        in_data = outs[1]
        mirror_data = outs[2]
        if isinstance(out, dali.backend_impl.TensorListGPU):
            out = out.as_cpu()
        if isinstance(in_data, dali.backend_impl.TensorListGPU):
            in_data = in_data.as_cpu()

        assert(batch_size == len(out))
        for idx in range(batch_size):
            sample_in = in_data.at(idx)
            sample_out = out.at(idx)
            mirror = mirror_data.at(idx)
            flip = [0, mirror[0]]
            in_shape = list(sample_in.shape)
            out_shape = list(sample_out.shape)
            crop_anchor_norm = [crop_y, crop_x]
            crop_shape = [crop_h, crop_w]
            crop_anchor_abs = [crop_anchor_norm[k] * (input_shape[k] - crop_shape[k]) for k in range(2)]
            abs_start, abs_end, abs_slice_shape = abs_slice_start_and_end(in_shape[:2], crop_anchor_abs, crop_shape, False, False)
            check_slice_output(sample_in, sample_out, crop_anchor_abs, abs_slice_shape, abs_start, abs_end, 
                               out_of_bounds_policy, fill_values, mean=mean, std=std, flip=flip, permute=permute)