2017-10-11 8 views
1

私はCPU上でPython 2.7.13とTensorflow 1.3.0を使用します。tfsrecとtfslimのデコード

回帰問題にDensNet(https://github.com/pudae/tensorflow-densenet)を使用したいと思います。私のデータには、各画像の37枚のフロートラベルを含む60000枚のJPEG画像が含まれています。 私はでtfrecordsファイルに自分のデータを保存:

def Read_Labels(label_path): 
labels_csv = pd.read_csv(label_path) 
labels = np.array(labels_csv) 
return labels[:,1:] 

`

def load_image(addr): 
# read an image and resize to (224, 224) 
img = cv2.imread(addr) 
img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_CUBIC) 
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
img = img.astype(np.float32) 
return img 

def Shuffle_images_with_labels(shuffle_data, photo_filenames, labels): 
if shuffle_data: 
    c = list(zip(photo_filenames, labels)) 
    shuffle(c) 
    addrs, labels = zip(*c) 
    return addrs, labels 

def image_to_tfexample_mine(image_data, image_format, height, width, label): 
return tf.train.Example(features=tf.train.Features(feature={ 
    'image/encoded': bytes_feature(image_data),  
    'image/format': bytes_feature(image_format), 
    'image/class/label': _float_feature(label), 
    'image/height': int64_feature(height), 
    'image/width': int64_feature(width), 
})) 

def _convert_dataset(split_name, filenames, labels, dataset_dir): 
assert split_name in ['train', 'validation'] 

num_per_shard = int(math.ceil(len(filenames)/float(_NUM_SHARDS))) 

with tf.Graph().as_default(): 

    for shard_id in range(_NUM_SHARDS): 
     output_filename = _get_dataset_filename(dataset_path, split_name, shard_id) 

     with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer: 
      start_ndx = shard_id * num_per_shard 
      end_ndx = min((shard_id+1) * num_per_shard, len(filenames)) 
      for i in range(start_ndx, end_ndx): 
       sys.stdout.write('\r>> Converting image %d/%d shard %d' % (
         i+1, len(filenames), shard_id)) 
       sys.stdout.flush() 

       img = load_image(filenames[i]) 
       image_data = tf.compat.as_bytes(img.tostring()) 

       label = labels[i] 

       example = image_to_tfexample_mine(image_data, image_format, height, width, label) 

       # Serialize to string and write on the file 
       tfrecord_writer.write(example.SerializeToString()) 

sys.stdout.write('\n') 
sys.stdout.flush() 

def run(dataset_dir): 

labels = Read_Labels(dataset_dir + '/training_labels.csv') 

photo_filenames = _get_filenames_and_classes(dataset_dir + '/images_training') 

shuffle_data = True 

photo_filenames, labels = Shuffle_images_with_labels(
     shuffle_data,photo_filenames, labels) 

training_filenames = photo_filenames[_NUM_VALIDATION:] 
training_labels = labels[_NUM_VALIDATION:] 

validation_filenames = photo_filenames[:_NUM_VALIDATION] 
validation_labels = labels[:_NUM_VALIDATION] 

_convert_dataset('train', 
       training_filenames, training_labels, dataset_path) 
_convert_dataset('validation', 
       validation_filenames, validation_labels, dataset_path) 

print('\nFinished converting the Flowers dataset!') 

そして、私はでそれをデコード:それはすべてここまでの罰金だ

with tf.Session() as sess: 

feature = { 
    'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 
    'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), 
    'image/class/label': tf.FixedLenFeature(
     [37,], tf.float32, default_value=tf.zeros([37,], dtype=tf.float32)), 
    } 

filename_queue = tf.train.string_input_producer([data_path], num_epochs=1) 

reader = tf.TFRecordReader() 
_, serialized_example = reader.read(filename_queue) 

features = tf.parse_single_example(serialized_example, features=feature) 

image = tf.decode_raw(features['image/encoded'], tf.float32) 
print(image.get_shape()) 

label = tf.cast(features['image/class/label'], tf.float32) 

image = tf.reshape(image, [224, 224, 3]) 

images, labels = tf.train.shuffle_batch([image, label], batch_size=10, capacity=30, num_threads=1, min_after_dequeue=10) 

init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) 
sess.run(init_op) 

coord = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(coord=coord) 

for batch_index in range(6): 
    img, lbl = sess.run([images, labels]) 
    img = img.astype(np.uint8) 
    print(img.shape) 
    for j in range(6): 
     plt.subplot(2, 3, j+1) 
     plt.imshow(img[j, ...]) 
    plt.show() 

coord.request_stop() 

coord.join(threads) 

ポイント。私はTFRecordファイルを復号するためのベローズのコマンドを使用する場合でも:

reader = tf.TFRecordReader 

keys_to_features = { 
     'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 
     'image/format': tf.FixedLenFeature((), tf.string, default_value='raw'), 
     'image/class/label': tf.FixedLenFeature(
     [37,], tf.float32, default_value=tf.zeros([37,], dtype=tf.float32)), 
     } 

items_to_handlers = { 
     'image': slim.tfexample_decoder.Image('image/encoded'), 
     'label': slim.tfexample_decoder.Tensor('image/class/label'), 
    } 

decoder = slim.tfexample_decoder.TFExampleDecoder(
     keys_to_features, items_to_handlers) 

を私は次のエラーを取得します。

INFO:tensorflow:エラーは、コーディネーターに報告:、アサーションは失敗しました:[JPEG、PNG、GIF、またはBMPなどのバイトをデコードできません] [[ノード:ケース/ If_0/decode_image/cond_jpeg/cond_png/cond_gif/case/If_0/decode_image/cond_jpeg/cond_png/cond_gif/Assert_1/Assert = Assert [T = [DT_STRING]、要約= 3、_device = "/ジョブ:ローカルホスト/レプリカ:0 /タスク:0/cpu:0"/is_bmp、case/If_0/decode_image/cond_jpeg/cond_png/cond_gif/Assert_1/Assert/data_0)]] 情報:tensorflow:OutOfRangeErrorを受け取りました。トレーニングを中止する。 INFO:sensorflow:トレーニングを終えました!モデルをディスクに保存する。


私の問題のためにDensenetを使用するには、私が最初にこのエラーを修正する必要があります。 誰でもこの問題を解決してください。このコードは、https://github.com/pudae/tensorflow-densenet/tree/master/datasetsで利用可能な花、MNIST、CIFAR10などのデータセットに対しては完全に機能しますが、自分のデータでは機能しません。

答えて

0

エラーによると、問題は、TFRecordsを作成するときにデコードされたデータを保存したので、配列データ(デコードされたデータ)のイメージデコーダを使用することだと思います。あなたが気づいたのは、slimを使用していないときは、tf.decode_rawを使用してデータをデコードします。しかし、slimを使用すると、'image/format': tf.FixedLenFeature((), tf.string, default_value='raw')は使用されず、デフォルトではslimがイメージデコーダを使用します。

slim/data、 のコードを使用すると思います。format_key = 'image/format'が必要です。したがって、次のようになります。

keys_to_features = { 
    'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 
    'image/format': tf.FixedLenFeature((), tf.string, default_value='raw'), 
    'image/class/label': tf.FixedLenFeature(
     [1], tf.int64, default_value=tf.zeros([1], dtype=tf.int64)), 
} 

items_to_handlers = { 
    'image': tfexample_decoder.Image(
     image_key = 'image/encoded', 
     format_key = 'image/format', 
    'label': tfexample_decoder.Tensor('image/class/label'), 
} 

decoder = tfexample_decoder.TFExampleDecoder(
    keys_to_features, items_to_handlers) 

私のマシンで作業を再現できないため、これがあなたの問題を完全に解決できるかどうかはわかりません。

1

pudaeのおかげで、問題が解決しました。私は以下を使用する必要がありました:

image_data = tf.gfile.FastGFile(filenames[i], 'rb').read() 

これはデータを読み込むためのものです。それは今、完璧に動作します。

img = load_image(filenames[i]) 
image_data = tf.compat.as_bytes(img.tostring()) 
関連する問題