Search code examples
pythonnumpyopenvino

Combine array with for-loop cannot get the expected numpy shape using OpenVINO


I am trying to use openvino_2022.1.0.643 version to read a DICOM file as many slices of JPG images.

I use a for-loop to ensure I can read every slice sequentially.

I save a JPG file first and read it as gray scale image every loop, because I have to crop each image to different sizes in the beginning.

Then, I use a list to save every output tensor data, and convert it to the np.array format.

The following is the snippet of my code.

for i in range(ds_arr.shape[0]):

    ...

    LV_corp = cv2.imread("0.jpg", cv2.IMREAD_GRAYSCALE)
    print("corp:", LV_corp.shape)
    core = ov.Core()
    model = core.read_model(model="model/saved_model_A4C_LV/saved_model.xml")
    model.reshape([1, 128, 128, 1])
    compiled_model = core.compile_model(model, "CPU")
    infer_request = compiled_model.create_infer_request()
    input_tensor = ov.Tensor(array=LV_corp, shared_memory=True)
    #infer_request.set_input_tensor(input_tensor)
    infer_request.start_async()
    infer_request.wait()
    output = infer_request.get_output_tensor()
    output_buffer_LV = output.data
    print("output:", output_buffer_LV.shape)
    output_buffer_LV_arr[i] = output_buffer_LV

    ...

output_buffer_LV_arr = np.array(output_buffer_LV_arr, dtype=object)
print("output_LV:", output_buffer_LV_arr.shape)

The print messages below.

corp: (434, 636)
output: (1, 128, 128, 1)
output_LV: (83, 1, 128, 128, 1)

But I except to print out output_LV: (83, 128, 128, 1) to fit my subsequent dynamic model's shape (?, 128, 128, 1).

Why the outcome is saving every (1, 128, 128, 1) output, and the total outputs are 83 records to be (83, 1, 128, 128, 1).

Rather than saving as (1, 128, 128, 1), (2, 128, 128, 1), ... , (82, 128, 128, 1), (83, 128, 128, 1).


Solution

  • Thanks for @hpaulj's help.

    I change

    output_buffer_LV_arr = np.array(output_buffer_LV_arr, dtype=object)
    

    to be

    output_buffer_LV_arr = np.concatenate(output_buffer_LV_arr)
    

    And I save the problem successfully.