<font style="font-size:14px;font-family:sans-serif">
<div><font style="font-family:sans-serif;font-size:14px"><font style="font-family:sans-serif">Hey,</font></font></div><div><font style="font-family:sans-serif">without knowing further details nor having verified the code, I guess your image is just too large to fit into the memory of your GPU.</font></div><br><div>Naïvely, I would calculate the size in GBytes of your image and compare with the memory of your GPU. Increasing the spacing means using fewer pixels = less memory. On top of that, I guess there are the projections which require space on the GPU.</div><div></div><div>If you need a small spacing, maybe you can reconstruct in chunks along the rotation axis and merge the chunks later?</div><div>Or the RTK experts know other tricks ...</div><div></div><div>just my non-expert two-cents ...</div><div>Nils</div><div></div><div class="gmail_quote_attribution">On Apr 25 2024, at 3:21 am, Rahman, Obaid <rahmano@ornl.gov> wrote:</div><blockquote class="gmail_quote"><div><div>Hello all,</div><div><br></div><div>I am getting “out of memory” error when I run CudaForwardProjectionImageFilter.</div><div>Please refer to the attached screenshot.</div><div><br></div><div>These are the array sizes with which I get the error:</div><div><strong>Projection size: (slice/row, view, column)=(1456,145,1840)</strong></div><div><strong>Recon size: (z, y, x)=(1264, 1356, 1356)</strong></div><div><br></div><div>When I increase the voxel size to twice i.e. <strong>Recon size: (z, y, x)=(632, 678, 678)</strong>, it works fine.</div><div>Maybe I’m not being very efficient with the memory?</div><div><br></div><div>If anyone has suggestion to make it more memory efficient, please let me know.</div><div>Thanks.</div><div><br></div><div>Best,</div><div>Obaid</div><div><br></div><div><strong><u>Here’s what I’m doing:</u></strong></div><div><div><div><br></div><div><span style="color:rgb(180, 237, 255)">def rtk_projection(recon_arrar, proj_params, miscalib, vol_params, return_itk=False, return_RVC=True):</span></div><div><span style="color:rgb(180, 237, 255)"> '''</span></div><div><span style="color:rgb(180, 237, 255)"> inputs:</span></div><div><span style="color:rgb(180, 237, 255)"> recon_arrar: image numpy array (ZXY format is expected)</span></div><div><span style="color:rgb(180, 237, 255)"> proj_params: projection parameter dictionary</span></div><div><span style="color:rgb(180, 237, 255)"> miscalib: miscalibation parameter dictionary</span></div><div><span style="color:rgb(180, 237, 255)"> vol_params: image volume parameter dictionary</span></div><div><span style="color:rgb(180, 237, 255)"> return_itk: True if itk image is expected, False if a numpy array is expected</span></div><div><span style="color:rgb(180, 237, 255)"> output:</span></div><div><span style="color:rgb(180, 237, 255)"> itk image (c,r,v format internally) or image numpy array (r,v,c format) of projection data</span></div><div><span style="color:rgb(180, 237, 255)"> '''</span></div><div><span style="color:rgb(180, 237, 255)"> ImageType = itk.Image[itk.F,3]; CudaImageType = itk.CudaImage[itk.F,3]</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # Convert the recon to itk format</span></div><div><span style="color:rgb(180, 237, 255)"> ###########################################################################</span></div><div><span style="color:rgb(180, 237, 255)"> # proj_data is in (z,x,y); rtk requires numpy array in (x,z,y)</span></div><div><span style="color:rgb(180, 237, 255)"> recon_arrar = np.transpose(recon_arrar, [1,0,2]).astype('float32') # now in (x,z,y)</span></div><div><span style="color:rgb(180, 237, 255)"> recon_shape = recon_arrar.shape</span></div><div><span style="color:rgb(180, 237, 255)"> recon_arrar = itk.GetImageFromArray(recon_arrar) # internally img has format (y,z,x)</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # Center the image around 0 which is the default center of rotation</span></div><div><span style="color:rgb(180, 237, 255)"> recon_arrar.SetOrigin([-0.5*(recon_arrar.GetLargestPossibleRegion().GetSize()[0]-1)*recon_arrar.GetSpacing()[0],</span></div><div><span style="color:rgb(180, 237, 255)"> -0.5*(recon_arrar.GetLargestPossibleRegion().GetSize()[1]-1)*recon_arrar.GetSpacing()[1],</span></div><div><span style="color:rgb(180, 237, 255)"> -0.5*(recon_arrar.GetLargestPossibleRegion().GetSize()[2]-1)*recon_arrar.GetSpacing()[2]])</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # Graft the projections to an itk::CudaImage</span></div><div><span style="color:rgb(180, 237, 255)"> ###########################################################################</span></div><div><span style="color:rgb(180, 237, 255)"> vol_xy = float(vol_params['vox_xy']); vol_z = float(vol_params['vox_z'])</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon = CudaImageType.New()# img will have format crv</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon.SetPixelContainer(recon_arrar.GetPixelContainer()) # img has format crv</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon.SetLargestPossibleRegion(recon_arrar.GetLargestPossibleRegion())</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon.SetBufferedRegion(recon_arrar.GetBufferedRegion())</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon.SetRequestedRegion(recon_arrar.GetRequestedRegion())</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon.SetSpacing([vol_xy, vol_z, vol_xy]) # spacing for xzy</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_recon.SetOrigin([-0.5*(recon_shape[2]-1)*vol_xy, # origin for x direction</span></div><div><span style="color:rgb(180, 237, 255)"> -0.5*(recon_shape[1]-1)*vol_z, # origin for z direction</span></div><div><span style="color:rgb(180, 237, 255)"> -0.5*(recon_shape[0]-1)*vol_xy]) # origin for y direction</span></div><div><span style="color:rgb(180, 237, 255)"> # print('GPU recon:', rtk_recon)</span></div><div><span style="color:rgb(180, 237, 255)"> del recon_arrar</span></div><div><span style="color:rgb(180, 237, 255)"> ###########################################################################</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # Defines the RTK geometry object</span></div><div><span style="color:rgb(180, 237, 255)"> geometry = rtk.ThreeDCircularProjectionGeometry.New()</span></div><div><span style="color:rgb(180, 237, 255)"> for i in range(len(proj_params['angles'])):</span></div><div><span style="color:rgb(180, 237, 255)"> angle_deg = proj_params['angles'][i]*180/np.pi # angle in degree</span></div><div><span style="color:rgb(180, 237, 255)"> geometry.AddProjection(proj_params['cone_params']['src_orig'],</span></div><div><span style="color:rgb(180, 237, 255)"> proj_params['cone_params']['src_orig']+proj_params['cone_params']['orig_det'],</span></div><div><span style="color:rgb(180, 237, 255)"> angle_deg, miscalib['delta_u'], miscalib['delta_v'])</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # define some parameters (to make code more readable)</span></div><div><span style="color:rgb(180, 237, 255)"> det_pix_x = proj_params['cone_params']['pix_x'] # row direction pixel size</span></div><div><span style="color:rgb(180, 237, 255)"> det_pix_y = proj_params['cone_params']['pix_y'] # channel direction pixel size</span></div><div><span style="color:rgb(180, 237, 255)"> proj_shape = [int(proj_params['dims'][0]), int(proj_params['dims'][1]), int(proj_params['dims'][2])] # r,v,c</span></div><div><span style="color:rgb(180, 237, 255)"> ###########################################################################</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # define a zero projection (GPU)</span></div><div><span style="color:rgb(180, 237, 255)"> constantImageSource = rtk.ConstantImageSource[CudaImageType].New()</span></div><div><span style="color:rgb(180, 237, 255)"> constantImageSource.SetSpacing( [det_pix_y, det_pix_x, 1.] ) # c,r,v</span></div><div><span style="color:rgb(180, 237, 255)"> constantImageSource.SetSize( [proj_shape[2], proj_shape[0], proj_shape[1]] ) # c,r,v</span></div><div><span style="color:rgb(180, 237, 255)"> constantImageSource.SetOrigin([ -0.5*(proj_shape[2]-1)*det_pix_y, # origin for channel direction</span></div><div><span style="color:rgb(180, 237, 255)"> -0.5*(proj_shape[0]-1)*det_pix_x, # origin for row direction</span></div><div><span style="color:rgb(180, 237, 255)"> -0.5*(proj_shape[1]-1)*1]) # origin for view direction (will be ignored anyway)</span></div><div><span style="color:rgb(180, 237, 255)"> constantImageSource.SetConstant(0.)</span></div><div><span style="color:rgb(180, 237, 255)"> ###########################################################################</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # forward project the recon to fill out the zero projection image</span></div><div><span style="color:rgb(180, 237, 255)"> ForwardProj = rtk.CudaForwardProjectionImageFilter[CudaImageType].New()</span></div><div><span style="color:rgb(180, 237, 255)"> ForwardProj.SetGeometry( geometry )</span></div><div><span style="color:rgb(180, 237, 255)"> ForwardProj.SetInput(0, constantImageSource.GetOutput()) # projection image</span></div><div><span style="color:rgb(180, 237, 255)"> ForwardProj.SetInput(1, cpu2gpu(rtk_recon)) # recon volume</span></div><div><span style="color:rgb(180, 237, 255)"> ForwardProj.SetStepSize(float(vol_params['vox_xy'])/4) # step size along the ray (default is 1mm)</span></div><div><span style="color:rgb(180, 237, 255)"> ForwardProj.Update()</span></div><div><span style="color:rgb(180, 237, 255)"> ###########################################################################</span></div><div><br></div><div><span style="color:rgb(180, 237, 255)"> # graft the projection to CPU / extract the array</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_reprojection = gpu2cpu(ForwardProj.GetOutput()) # array inside the image has c,r,v format</span></div><div><span style="color:rgb(180, 237, 255)"> if return_itk:</span></div><div><span style="color:rgb(180, 237, 255)"> return rtk_reprojection # array inside the image has c,r,v format</span></div><div><span style="color:rgb(180, 237, 255)"> else:</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_reprojection = itk.GetArrayViewFromImage(rtk_reprojection) # now v,r,c format</span></div><div><span style="color:rgb(180, 237, 255)"> if return_RVC:</span></div><div><span style="color:rgb(180, 237, 255)"> rtk_reprojection = np.transpose(rtk_reprojection, [1,0,2]) # r,v,c format to match Astra projection</span></div><div><span style="color:rgb(180, 237, 255)"> return rtk_reprojection # numpy array (r,v,c)</span></div></div></div><div><br></div><div><br></div><div><img alt="" src="cid:417B41B3-5736-41A2-AA06-75C877EED5F7" style="width:602px"></div><div><br></div><div><br></div></div><div><div>_______________________________________________</div><div>Rtk-users mailing list</div><div>rtk-users@openrtk.org</div><div>https://www.creatis.insa-lyon.fr/mailman/listinfo/rtk-users</div></div></blockquote>
</font>