title: Extract patch and reconstruct image
body: <p>i am trying my hands on a segmentation task, the images are 3d volumes since i cannot process them at once because of gpu memory constraints, i am extracting patches of the image and performing operations on them.</p>
  
  <p>for extracting the patches i am </p>
  
  <pre><code>    def cutup(data, blck, strd):
          sh = np.array(data.shape)
          blck = np.asanyarray(blck)
          strd = np.asanyarray(strd)
          nbl = (sh - blck) // strd + 1
          strides = np.r_[data.strides * strd, data.strides]
          dims = np.r_[nbl, blck]
          data6 = stride_tricks.as_strided(data, strides=strides, shape=dims)
          return data6.reshape(-1, *blck)
  
      def make_patches(image_folder, mask_folder):
          '''
          Given niigz image and mask files will create numpy files 
          '''
          for image, mask in tqdm.tqdm(zip(os.listdir(image_folder), os.listdir(mask_folder))):
              mask_ = mask
              mask = mask.split('_')
              image = mask[0]
              image_name = mask[0]
              mask_name = mask[0]
              image, mask = read_image_and_seg(os.path.join(image_folder, image), os.path.join(mask_folder,mask_))
              if image.shape[1] &gt; 600:
                  image = image[:,:600,:]
              desired_size_w = 896
              desired_size_h = 600
              desired_size_z = 600
              delta_w = desired_size_w - image.shape[0]
              delta_h = desired_size_h - image.shape[1]
              delta_z = desired_size_z - image.shape[2]
  
              padded_image =np.pad(image, ((0,delta_w), (0,delta_h), (0, delta_z)), 'constant')
              padded_mask  =np.pad(mask, ((0,delta_w), (0,delta_h), (0, delta_z)), 'constant')
              y  = cutup(padded_image, (128,128,128),(128,128,128))#Actually extract more patches by changing stride size
              y_ = cutup(padded_mask,  (128,128,128),(128,128,128))
              print(image_name)
              for index, (im , label) in enumerate(zip(y , y_)):
                  if len(np.unique(im)) ==1:
                      continue
                  else:
                      if not os.path.exists(os.path.join('../data/patches/images/',image_name.split('.')[0]+str(index))):
                          np.save(os.path.join('../data/patches/images/',image_name.split('.')[0]+str(index)), im)
                          np.save(os.path.join('../data/patches/masks/', image_name.split('.')[0]+str(index)), label)
  </code></pre>
  
  <p>now this will extract non - overlapping patches and give me the patches in numpy array, just as an aside i am converting the image to shape(padding with 0)  896,640,640 so i can extarct all patches  </p>
  
  <p>The problem is i dont know if the above code works!to test it wanted to extract the patches and then take those patches and reconstruct the image,now i am not exactly sure how to go about this,</p>
  
  <p>for now this is what i have </p>
  
  <pre><code>    def reconstruct_image(folder_path_of_npy_files):
          slice_shape = len(os.listdir(folder_path_of_npy_files))
          recon_image = np.array([])
          for index, file in enumerate(os.listdir(folder_path_of_npy_files)):
              read_image = np.load(os.path.join(folder_path_of_npy_files, file))
              recon_image = np.append(recon_image, read_image)
          return recon_image
  </code></pre>
  
  <p>but this does not work as it makes an array of (x, 128,128,128) and keeps filling up the 0th dimension.</p>
  
  <p>So my question is , how do i reconstruct the image? or is there just a plain better way to extract and reconstruct patches.</p>
  
  <p>Thanks in advance.</p>