2017-04-20 12 views
0

私がしたいのは、(h、w、num_images)の形式の画像のスタックを作成し、簡単に描画することができる単一のイメージですが、グリッド内にラップアラウンドしたいと思います(テンソルフローでこれを実行したい、つまりグラフ描画の準備ができたグリッドイメージを出力します)。いずれの入力によってテンソルフローのグリッド(すなわち、ラップアラウンド)でタイル画像

)列の数(単一列の画像の、すなわち最大数)

OR

B)の最大幅(例えば、画面幅)。上記を自動的に計算します

私はそれを行うnumpyコードを持っていますが、それはかなり遅いです、私が考えるグラフの一部としてGPU上で行うのがより理にかなっています。

私tensorflowグラフコードが(最後の軸が画像のスタックを含んでいるので、tは、畳み込み層の出力である)本である:

act = tf.squeeze(t) # batch size is 1, so remove it 
act = tf.unstack(act, num=num_filters, axis=-1) # split last axis (filters) into list of (h, w) 
act = tf.stack(act) # re-stack on first axis 

これは私(num_filters、H、W)を与えるI私が書いたより一般的なnumpyコードにフィードします。これはグリッドに入れます(私のnumpyコードはかなり一般的ですし、可変サイズの画像で動作するので、下には含めません)。

テンソルフローでこれを直接行うことはできますか?

(私が代わりにtf.stackのtf.concatを行うにした場合は、私は側のタイル、それらをサイドすることができますが、彼らはラップアラウンドを持っていない)

答えて

0

マイnumpyのコードが未満でそれをしません20ラインとかなり高速です。 10000x10000x3の画像を並べ替えるとかなり高速です。不十分な画像がある場合は、最後のいくつかのタイルをゼロで埋めます。

def reshape_row(arr): 
    return reduce(lambda x, y: np.concatenate((x,y), axis=1), arr) 

def reshape_col(arr): 
    return reduce(lambda x, y: np.concatenate((x,y), axis=0), arr) 

def arbitrary_rows_cols(arr, num_rows, num_cols, gray=False): 
    num_images, height, width, depth, = arr.shape 
    rows = [] 
    for i in range(num_rows): 
     row_image = arr[i*num_cols:i*num_cols+num_cols] 
     r_n, r_h, r_w, r_d = row_image.shape 
     if row_image.shape[0] != num_cols: 
      for _ in range(num_cols - row_image.shape[0]): 
       row_image = np.concatenate((row_image, np.expand_dims(np.zeros((height, width, depth)), axis=0)), axis=0) 
     row_image = reshape_row(row_image) 
     rows.append(row_image) 
    mosaic = reshape_col(rows) 
    return mosaic 

このコードをTensorFlowコードに変換すると、より高速になる可能性があります。パフォーマンスの比較が面白いでしょうか。

0

実際には、私はちょうど行の数(これは理想的ではありませんが、今は十分です)を入力することによってそれを行うのは非常に簡単な方法を発見しました。

def make_grid(t, num_images, num_rows=2): 
    '''takes stack of images as (1, w, h, num_images) and tiles them into a grid''' 
    t = tf.squeeze(t) # remove single batch, TODO make more flexible to work with higher batch size 
    t = tf.unstack(t, num=num_images, axis=-1) # split last axis (num_images) into list of (h, w) 
    t = tf.concat(t, axis=1) # tile all images horizontally into single row 
    t = tf.split(t, num_rows, axis=1) # split into desired number of rows 
    t = tf.concat(t, axis=0) # tile rows vertically 
    return t 
関連する問題