2016-12-15 8 views
2

h5pyを使ってデータを(画像、アングル)のタプルのリストとして保存しようとしています。画像は、OpenCVのuint8型のサイズ(240,320,3)のnumpy配列ですが、角度はfloat16型の数値にすぎません。H5PY/Numpy - numpy配列の内部形状を設定する(h5pyの場合)

h5pyを使用する場合は、読み書き可能な速度を維持するために、所定の形状にする必要があります。 H5pyは、データセット全体を任意の値で事前ロードします。この値で後でインデックスを作成し、これらの値を任意の値に設定できます。

h5pyのデータセットの形状を初期化するときに、内側のnumpy配列の形状を設定する方法を知りたいと思います。私は、同じ解決策が同様にnumpyにも適用されると信じています。 numpyの中で問題をRecreateing

import h5py 
import numpy as np 

dset_length = 100 

# fake data of same shape 
images = np.ones((dset_length,240,320,3), dtype='uint8') * 255 

# fake data of same shape 
angles = np.ones(dset_length, dtype='float16') * 90 
f = h5py.File('dataset.h5', 'a') 
dset = f.create_dataset('dset1', shape=(dset_length,2)) 

for i in range(dset_length): 
    # does not work since the shape of dset[0][0] is a number, 
    # and can't store an array datatype 
    dset[i] = np.array((images[i],angles[i])) 

は次のようになります。numpyので

import numpy as np 

a = np.array([ 
      [np.array([0,0]), 0], 
      [np.array([0,0]), 0], 
      [np.array([0,0]), 0] 
     ]) 

a.shape # (3, 2) 

b = np.empty((3,2)) 

b.shape # (3, 2) 

a[0][0] = np.array([1,1]) 

b[0][0] = np.array([1,1]) # ValueError: setting an array element with a sequence. 
+0

'B = np.empty((3,2)、DTYPE =オブジェクト)'それはA' 'のように動作するであろう。しかし、それはあなたがそれをどうにかして動作させる方法ではありません。 – Eric

答えて

2

@Ericはで動作するはず作成dtypenumpyh5pyの両方です。しかし、あなたが本当にそれを望んでいるのか必要があるのか​​疑問に思いますもう1つの方法は、numpy,imagesおよびanglesの2つのアレイを1つは4d uint8、もう1つはフロートとすることです。 h5pyでは、groupを作成し、これら2つの配列をdatasetsとして保存することができます。二つの配列と

g.create_dataset('data', (3,), dtype=dt) 
g['data'][:] = data 

またはデータセット:化合物DTYPEと

import h5py 
dt = np.dtype([('angle', np.float16), ('image', np.uint8, (40,20,3))]) 
data = np.ones((3,), dt) 

f = h5py.File('test.h5','w') 
g = f.create_group('data') 

セット:

あなたは例えば

images[i,...], angles[i]  # or 
data[i]['image'], data[i]['angle'] 
ith'

と画像の値を選択することができます

g.create_dataset('image', (3,40,20,3), dtype=np.uint8) 
g.create_dataset('angle', (3,), dtype=np.float16) 
g['image'][:] = data['image'] 
g['angle'][:] = data['angle'] 

は、データセットのいずれかから角度アレイをフェッチ:

g['data']['angle'][:] 
g['angle'][:] 
2

、あなたは構造化された配列で、そのデータを保存することができます

dtype = np.dtype([('angle', np.float16), ('image', np.uint8, (240,320,3))]) 
data = np empty(10, dtype=dtype) 
data[0]['angle'] = ... # etc 
関連する問題