2016-12-20 3 views
4

내 모델을 공급하기 위해 tensorflow 레코드를 만들고 싶습니다. 지금까지 다음 코드를 사용하여 uint8 numpy 배열을 TFRecord 형식으로 저장했습니다. 나는이 예제 코드float numpy 배열이있는 tensorflow 레코드

features = tf.parse_single_example(example, features={ 
    'image_raw': tf.FixedLenFeature([], tf.string), 
    'map_raw': tf.FixedLenFeature([], tf.string), 
    'label_raw': tf.FixedLenFeature([], tf.string), 
}) 

image = tf.decode_raw(features['image_raw'], tf.uint8) 
image.set_shape(params.IMAGE_HEIGHT*params.IMAGE_WIDTH*3) 
image = tf.reshape(image_, (params.IMAGE_HEIGHT,params.IMAGE_WIDTH,3)) 

map = tf.decode_raw(features['map_raw'], tf.uint8) 
map.set_shape(params.MAP_HEIGHT*params.MAP_WIDTH*params.MAP_DEPTH) 
map = tf.reshape(map, (params.MAP_HEIGHT,params.MAP_WIDTH,params.MAP_DEPTH)) 

label = tf.decode_raw(features['label_raw'], tf.uint8) 
label.set_shape(params.NUM_CLASSES) 

하고 잘 작동하고 읽을

def _int64_feature(value): 
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 


def _bytes_feature(value): 
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 


def _floats_feature(value): 
    return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) 


def convert_to_record(name, image, label, map): 
    filename = os.path.join(params.TRAINING_RECORDS_DATA_DIR, name + '.' + params.DATA_EXT) 

    writer = tf.python_io.TFRecordWriter(filename) 

    image_raw = image.tostring() 
    map_raw = map.tostring() 
    label_raw = label.tostring() 

    example = tf.train.Example(features=tf.train.Features(feature={ 
     'image_raw': _bytes_feature(image_raw), 
     'map_raw': _bytes_feature(map_raw), 
     'label_raw': _bytes_feature(label_raw) 
    }))   
    writer.write(example.SerializeToString()) 
    writer.close() 

. 이제 uint8 대신 float numpy 배열 인 배열 "map"을 사용하여 동일한 작업을 수행하려고합니다.이 작업을 수행하는 방법에 대한 예제를 찾을 수 없었습니다. 나는 _floats_feature 함수를 시도했다. _floats_feature는 배열에 스칼라를 전달하면 작동하지만 배열에는 그렇지 않다. with uint8 직렬화는 tostring() 메서드로 수행 할 수 있습니다.

어떻게 float numpy 배열을 serialize 할 수 있습니까? 어떻게 다시 읽을 수 있습니까?

답변

6

FloatListBytesList 반복 가능. 그래서 당신은 수레 목록을 전달해야합니다. 당신의 _float_feature에 여분의 괄호를 제거

def _floats_feature(value): 
    return tf.train.Feature(float_list=tf.train.FloatList(value=value)) 

numpy_arr = np.ones((3,)).astype(np.float) 
example = tf.train.Example(features=tf.train.Features(feature={"bytes": _floats_feature(numpy_arr)})) 
print(example) 

features { 
    feature { 
    key: "bytes" 
    value { 
     float_list { 
     value: 1.0 
     value: 1.0 
     value: 1.0 
     } 
    } 
    } 
} 
2

내가 야로슬라프의 대답에 확장됩니다 즉.

Int64List, BytesList 및 FloatList는 iterator of the underlying elements (반복 필드)을 예상합니다. 귀하의 경우 목록을 반복자로 사용할 수 있습니다.

언급 한 내용 : 스칼라를 전달하면 작동하지만 배열은이 아닌 경우 작동합니다. 스칼라를 전달할 때 _floats_feature은 정확히 하나의 float 요소 배열을 만듭니다. 그러나 배열을 전달할 때 배열 목록을 만들고이를 수레 목록을 요구하는 함수에 전달합니다.

그래서 당신의 함수에서 배열의 구조를 제거합니다.

numpy_arr = np.ones ((3,3)) astype (: 습니 배열이 입력 된 경우 야로슬라프의 예에 실패

1

float_list=tf.train.FloatList(value=value) np.float)

입력으로 numpy_arr.ravel()을 사용했을 때 효과가있는 것으로 나타났습니다. 하지만 더 좋은 방법이 있을까요?

+0

야로 슬라브는 수레 목록이 필요하다고 언급했는데, num_arr은 목록이 아니므로 어떻게 든 플랫 화해야하고 모델에 전달하기 전에 모양을 수정해야합니다. – cberkay

관련 문제