の形状を設定するために、高さ、TFRecordsファイルに保存された幅情報を使用して、私は、特徴マップはimage_raw
、label
、height
、width
とdepth
が含まれ、TFRecordsファイルに画像とそのラベルのディレクトリを変換しました。関数は次のとおりです。テンソル
def convert_to_tfrecords(data_samples, filename):
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]))
writer = tf.python_io.TFRecordWriter(filename)
for fname, lb in data_samples:
im = cv2.imread(fname, cv2.IMREAD_UNCHANGED)
image_raw = im.tostring()
feats = tf.train.Features(
feature =
{
'image_raw': _bytes_feature(image_raw),
'label': _int64_feature(int(lb)),
'height': _int64_feature(im.shape[0]),
'width': _int64_feature(im.shape[1]),
'depth': _int64_feature(im.shape[2])
}
)
example = tf.train.Example(features=feats)
writer.write(example.SerializeToString())
writer.close()
ここで、このTFRecordsファイルを読み込み、入力パイプラインに入力します。ただし、image_raw
は平坦化されているため、元の[height, width, depth]
サイズに変更する必要があります。では、TFRecordsファイルからheight
,width
、depth
の値を取得するにはどうすればよいですか? height
は値なしのTensorなので、次のコードは機能しないようです。私はTensorflowの公式文書を読んだとき
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
feats = {
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64)
}
features = tf.parse_single_example(serialized_example, features=feats)
image = tf.decode_raw(features['image_raw'], tf.uint8)
label = tf.cast(features['label'], tf.int32)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
image = tf.reshape(image, [height, width, depth]) # <== not work
image = tf.cast(image, tf.float32) * (1./255) - 0.5
return image, label
は、私が[224,224,3]
を言って、彼らは通常、既知のサイズに渡しました。しかし、私はこの情報がTFRecordsファイルに保存されていて、手動で固定サイズに渡しても、そのサイズがファイルに格納されているデータと一致しているかどうかを確認できないため、嫌いです。
だからアイデアはありますか?
「高さがテンソル値でない」と言うとき、どういう意味ですか?一見して、読書コードは大丈夫だと思うが、実際に書いているコードが正しい値を格納しているかどうかは疑問だ。特に、グラフを実行せずにテンソルの形状を計算するのが最善です。したがって、書き出した時点でim.shapeが完全に定義されていますか?筆記記号の多くを見ることなく伝えるのは難しいです。 –
書き込みコードはOKです。 tfrecordsファイルを書くときは、im.shapeが定義されています。私のプログラムでは[128,128,3]です。しかし、それを読むとき、グラフが実行されていないので、 'height'は値が評価されないテンソルだけです。あなたは "グラフを実行せずにベストエフォート"と言いましたが、私は同意します。しかし、今は、それが本当にテンソルの高さの価値を知らない理由です。したがって、 'height'の値を知る他のオプションはありますか? –