2017-05-30 5 views
1

Ich versuche, einige numpy Arrays in Python zu schreiben, um LMDB:schreiben numpy Arrays LMDB

import numpy as np 
import lmdb 

def write_lmdb(filename): 
    lmdb_env = lmdb.open(filename, map_size=int(1e9)) 
    lmdb_txn = lmdb_env.begin(write=True) 

    X= np.array([[1.0, 0.0], [0.1, 2.0]]) 
    y= np.array([1.4, 2.1]) 

    #Put first pair of arrays 
    lmdb_txn.put('X', X) 
    lmdb_txn.put('y', y) 

    #Put second pair of arrays 
    lmdb_txn.put('X', X+1.6) 
    lmdb_txn.put('y', y+1.2) 

def read_lmdb(filename): 
    lmdb_env = lmdb.open(filename) 
    lmdb_txn = lmdb_env.begin() 
    lmdb_cursor = lmdb_txn.cursor() 
    for key, value in lmdb_cursor: 
     print type(key) 
     print type(value) 

     print key 
     print value 

write_lmdb('temp.db') 
read_lmdb('temp.db') 

aber read_lmdb druckt nichts, was ist der richtige Weg numpy Arrays LMDB zu schreiben?

Update: Basierend auf @frankyjuang Antwort schaffe ich es zu tun, howewer nicht in sehr elegante Weise: mehrdimensionales Array verlieren Form ist, sollte jedes Array haben einen eigenen Namen.

import numpy as np 
import lmdb 

def write_lmdb(filename): 
    print 'Write lmdb' 

    lmdb_env = lmdb.open(filename, map_size=int(1e9)) 

    n_samples= 2 
    X= (255*np.random.rand(n_samples,3,4,3)).astype(np.uint8) 
    y= np.random.rand(n_samples).astype(np.float32) 

    for i in range(n_samples): 
     with lmdb_env.begin(write=True) as lmdb_txn: 
      lmdb_txn.put('X_'+str(i), X) 
      lmdb_txn.put('y_'+str(i), y) 

      print 'X:',X 
      print 'y:',y 

def read_lmdb(filename): 
    print 'Read lmdb' 

    lmdb_env = lmdb.open(filename) 
    lmdb_txn = lmdb_env.begin() 
    lmdb_cursor = lmdb_txn.cursor() 

    n_samples=0 
    with lmdb_env.begin() as lmdb_txn: 
     with lmdb_txn.cursor() as lmdb_cursor: 
      for key, value in lmdb_cursor: 
       print key 
       if('X' in key): 
        print np.fromstring(value, dtype=np.uint8) 
       if('y' in key): 
        print np.fromstring(value, dtype=np.float32) 

       n_samples=n_samples+1 

    print 'n_samples',n_samples 

write_lmdb('temp.db') 
read_lmdb('temp.db') 

Test-Skript-Ausgabe sollte so etwas wie sein:

Write lmdb 
X: [[[[ 48 224 119] 
    [ 76 87 174] 
    [ 14 88 183] 
    [ 76 234 56]] 

    [[234 223 65] 
    [ 63 85 175] 
    [184 252 125] 
    [100 7 225]] 

    [[134 159 41] 
    [ 2 146 221] 
    [ 99 74 225] 
    [169 57 59]]] 


[[[100 202 3] 
    [ 88 204 131] 
    [ 96 238 243] 
    [103 58 30]] 

    [[157 125 107] 
    [238 207 99] 
    [102 220 64] 
    [ 27 240 33]] 

    [[ 74 93 131] 
    [107 88 206] 
    [ 55 86 35] 
    [212 235 187]]]] 
y: [ 0.80826157 0.01407595] 
X: [[[[ 48 224 119] 
    [ 76 87 174] 
    [ 14 88 183] 
    [ 76 234 56]] 

    [[234 223 65] 
    [ 63 85 175] 
    [184 252 125] 
    [100 7 225]] 

    [[134 159 41] 
    [ 2 146 221] 
    [ 99 74 225] 
    [169 57 59]]] 


[[[100 202 3] 
    [ 88 204 131] 
    [ 96 238 243] 
    [103 58 30]] 

    [[157 125 107] 
    [238 207 99] 
    [102 220 64] 
    [ 27 240 33]] 

    [[ 74 93 131] 
    [107 88 206] 
    [ 55 86 35] 
    [212 235 187]]]] 
y: [ 0.80826157 0.01407595] 
Read lmdb 
X_0 
[ 48 224 119 76 87 174 14 88 183 76 234 56 234 223 65 63 85 175 
184 252 125 100 7 225 134 159 41 2 146 221 99 74 225 169 57 59 
100 202 3 88 204 131 96 238 243 103 58 30 157 125 107 238 207 99 
102 220 64 27 240 33 74 93 131 107 88 206 55 86 35 212 235 187] 
X_1 
[ 48 224 119 76 87 174 14 88 183 76 234 56 234 223 65 63 85 175 
184 252 125 100 7 225 134 159 41 2 146 221 99 74 225 169 57 59 
100 202 3 88 204 131 96 238 243 103 58 30 157 125 107 238 207 99 
102 220 64 27 240 33 74 93 131 107 88 206 55 86 35 212 235 187] 
y_0 
[ 0.80826157 0.01407595] 
y_1 
[ 0.80826157 0.01407595] 
n_samples 4 

Antwort

1

unter with Ihre Transaktionen wickeln. Und denken Sie daran, den Wert von Bytes (String) zurück zu numpy Array mit np.fromstring zu konvertieren.

Um ehrlich zu sein, ist es keine gute Idee, numpy Array in lmdb zu speichern, da die Konvertierung von Array zu Bytes zurück zu Array einige Informationen verlieren wird (zB shape). Sie können versuchen, ein Diktat von numply Arrays mit pickle zu speichern.

def write_lmdb(filename): 
    ... 
    with lmdb_env.begin(write=True) as lmdb_txn: 
     ... 

def read_lmdb(filename): 
    ... 
    with lmdb_env.begin() as lmdb_txn: 
     with lmdb_txn.cursor() as lmdb_cursor: 
      ... 
      print np.fromstring(value, dtype=np.float64) 
+0

ist hier als Lösung, wie Bild zu speichern, ohne Dimensionen des Bildes zu verlieren LMDB https://stackoverflow.com/a/44370939/1179925 – mrgloom

+0

So ist es ein Bild ist, die Sie speichern möchten. Großartig, um die Lösung zu sehen! – frankyjuang