scripts/convert_ndds_data.py
def convert_to_nerfdata(basedir, newdir, n_down, near_far = [2.,6.]):
  """Convert Blender data to multiscale."""
  if not os.path.exists(newdir):
    os.makedirs(newdir)
  splits = ['train', 'val', 'test']
  bigmeta = {}
  # Foreach split in the dataset
  for split in splits:
    print('Split', split)
    # Load everything
    data = load_renderings(basedir, split)

    # Save out all the images
    imgdir = 'images_{}'.format(split)
    os.makedirs(os.path.join(newdir, imgdir), exist_ok=True)
    fnames = []
    widths = []
    heights = []
    focals = []
    cam2worlds = []
    lossmults = []
    labels = []
    nears, fars = [], []
    f = data['focal']
    print('Saving images')
    for i, img in enumerate(data['images']):
      for j in range(n_down):
        fname = '{}/{:03d}_d{}.png'.format(imgdir, i, j)
        fnames.append(fname)
        fname = os.path.join(newdir, fname)
        with open(fname, 'wb') as imgout:
          img8 = Image.fromarray(np.uint8(img * 255))
          img8.save(imgout)
        widths.append(img.shape[1])
        heights.append(img.shape[0])
        focals.append(f / 2**j)
        cam2worlds.append(data['camtoworlds'][i].tolist())
        lossmults.append(4.**j)
        labels.append(j)
        nears.append(near_far[0])
        fars.append(near_far[1])
        img = down2(img)

    # Create metadata
    meta = {}
    meta['file_path'] = fnames
    meta['cam2world'] = cam2worlds
    meta['width'] = widths
    meta['height'] = heights
    meta['focal'] = focals
    meta['label'] = labels
    meta['near'] = nears
    meta['far'] = fars
    meta['lossmult'] = lossmults

    fx = np.array(focals)
    fy = np.array(focals)
    cx = np.array(meta['width']) * .5
    cy = np.array(meta['height']) * .5
    arr0 = np.zeros_like(cx)
    arr1 = np.ones_like(cx)
    k_inv = np.array([
        [arr1 / fx, arr0, -cx / fx],
        [arr0, -arr1 / fy, cy / fy],
        [arr0, arr0, -arr1],
    ])
    k_inv = np.moveaxis(k_inv, -1, 0)
    meta['pix2cam'] = k_inv.tolist()

    bigmeta[split] = meta

  for k in bigmeta:
    for j in bigmeta[k]:
      print(k, j, type(bigmeta[k][j]), np.array(bigmeta[k][j]).shape)

  jsonfile = os.path.join(newdir, 'metadata.json')
  with open(jsonfile, 'w') as f:
    json.dump(bigmeta, f, ensure_ascii=False, indent=4)
Convert Blender data to multiscale.
Convert Blender data to multiscale.
Convert Blender data to multiscale .
def convert_to_nerfdata(basedir, newdir, n_down, near_far = [2.,6.]):
  if not os.path.exists(newdir):
    os.makedirs(newdir)
  splits = ['train', 'val', 'test']
  bigmeta = {}
  for split in splits:
    print('Split', split)
    data = load_renderings(basedir, split)
    imgdir = 'images_{}'.format(split)
    os.makedirs(os.path.join(newdir, imgdir), exist_ok=True)
    fnames = []
    widths = []
    heights = []
    focals = []
    cam2worlds = []
    lossmults = []
    labels = []
    nears, fars = [], []
    f = data['focal']
    print('Saving images')
    for i, img in enumerate(data['images']):
      for j in range(n_down):
        fname = '{}/{:03d}_d{}.png'.format(imgdir, i, j)
        fnames.append(fname)
        fname = os.path.join(newdir, fname)
        with open(fname, 'wb') as imgout:
          img8 = Image.fromarray(np.uint8(img * 255))
          img8.save(imgout)
        widths.append(img.shape[1])
        heights.append(img.shape[0])
        focals.append(f / 2**j)
        cam2worlds.append(data['camtoworlds'][i].tolist())
        lossmults.append(4.**j)
        labels.append(j)
        nears.append(near_far[0])
        fars.append(near_far[1])
        img = down2(img)
    meta = {}
    meta['file_path'] = fnames
    meta['cam2world'] = cam2worlds
    meta['width'] = widths
    meta['height'] = heights
    meta['focal'] = focals
    meta['label'] = labels
    meta['near'] = nears
    meta['far'] = fars
    meta['lossmult'] = lossmults
    fx = np.array(focals)
    fy = np.array(focals)
    cx = np.array(meta['width']) * .5
    cy = np.array(meta['height']) * .5
    arr0 = np.zeros_like(cx)
    arr1 = np.ones_like(cx)
    k_inv = np.array([
        [arr1 / fx, arr0, -cx / fx],
        [arr0, -arr1 / fy, cy / fy],
        [arr0, arr0, -arr1],
    ])
    k_inv = np.moveaxis(k_inv, -1, 0)
    meta['pix2cam'] = k_inv.tolist()
    bigmeta[split] = meta
  for k in bigmeta:
    for j in bigmeta[k]:
      print(k, j, type(bigmeta[k][j]), np.array(bigmeta[k][j]).shape)
  jsonfile = os.path.join(newdir, 'metadata.json')
  with open(jsonfile, 'w') as f:
    json.dump(bigmeta, f, ensure_ascii=False, indent=4)
def convert_to_nerfdata ( basedir , newdir , n_down , near_far = [ 2. , 6. ] ) : if not os . path . exists ( newdir ) : os . makedirs ( newdir ) splits = [ 'train' , 'val' , 'test' ] bigmeta = { } for split in splits : print ( 'Split' , split ) data = load_renderings ( basedir , split ) imgdir = 'images_{}' . format ( split ) os . makedirs ( os . path . join ( newdir , imgdir ) , exist_ok = True ) fnames = [ ] widths = [ ] heights = [ ] focals = [ ] cam2worlds = [ ] lossmults = [ ] labels = [ ] nears , fars = [ ] , [ ] f = data [ 'focal' ] print ( 'Saving images' ) for i , img in enumerate ( data [ 'images' ] ) : for j in range ( n_down ) : fname = '{}/{:03d}_d{}.png' . format ( imgdir , i , j ) fnames . append ( fname ) fname = os . path . join ( newdir , fname ) with open ( fname , 'wb' ) as imgout : img8 = Image . fromarray ( np . uint8 ( img * 255 ) ) img8 . save ( imgout ) widths . append ( img . shape [ 1 ] ) heights . append ( img . shape [ 0 ] ) focals . append ( f / 2 ** j ) cam2worlds . append ( data [ 'camtoworlds' ] [ i ] . tolist ( ) ) lossmults . append ( 4. ** j ) labels . append ( j ) nears . append ( near_far [ 0 ] ) fars . append ( near_far [ 1 ] ) img = down2 ( img ) meta = { } meta [ 'file_path' ] = fnames meta [ 'cam2world' ] = cam2worlds meta [ 'width' ] = widths meta [ 'height' ] = heights meta [ 'focal' ] = focals meta [ 'label' ] = labels meta [ 'near' ] = nears meta [ 'far' ] = fars meta [ 'lossmult' ] = lossmults fx = np . array ( focals ) fy = np . array ( focals ) cx = np . array ( meta [ 'width' ] ) * .5 cy = np . array ( meta [ 'height' ] ) * .5 arr0 = np . zeros_like ( cx ) arr1 = np . ones_like ( cx ) k_inv = np . array ( [ [ arr1 / fx , arr0 , - cx / fx ] , [ arr0 , - arr1 / fy , cy / fy ] , [ arr0 , arr0 , - arr1 ] , ] ) k_inv = np . moveaxis ( k_inv , - 1 , 0 ) meta [ 'pix2cam' ] = k_inv . tolist ( ) bigmeta [ split ] = meta for k in bigmeta : for j in bigmeta [ k ] : print ( k , j , type ( bigmeta [ k ] [ j ] ) , np . array ( bigmeta [ k ] [ j ] ) . shape ) jsonfile = os . path . join ( newdir , 'metadata.json' ) with open ( jsonfile , 'w' ) as f : json . dump ( bigmeta , f , ensure_ascii = False , indent = 4 )
Convert Blender data to multiscale.
Convert Blender data to multiscale .
"""Convert Blender data to multiscale.""" # Foreach split in the dataset # Load everything # Save out all the images # Create metadata
basedir
newdir
n_down
near_far