numpyとopenCVを使った画像のフーリエ変換と逆変換

openCVを使い画像読み込み、fftで周波数データに変換。その後逆変換で元の画像に戻すテスト。

入力に使用する画像は↓。サイズは360×240。
input data to fft

まず画像入力、グレースケールで取り込む。

実行すると元の画像がグレースケールで表示される。
imreadは引数2つ、1つ目は読み込みファイル、2つ目は読み込みオプションで以下3つ(1,0,-1でも指定可)。

  cv2.IMREAD_COLOR (or 1)
  cv2.IMREAD_GRAYSCALE (or 0)
  cv2.IMREAD_UNCHANGED (or -1)

続けてFFT実施

np.fft.fft2()は2次元FFT。
実行結果が下記。入力が360×240画像だったので、360×240のnumpy arrayで返ってくる。

変換結果をパワースペクトルで確認してみる。確認に使用したコードが下記。

実行結果下記
input data to fft

最後に逆変換で元の画像に戻すコード。注意として変換後の結果には複素数成分が含まれているので、実部を取り出す処理が必要。またグレースケールで表示するために0-255階調への値調整が必要になる。

実行結果が下記グレースケールでもとの画像に戻ってきた。
results fft and inv fft

Read images using openCV, convert to frequency data with fft. And then back to the original image with reverse transformation.
Code 1 is reading image by gray scale.
Code 2 is 2D fft by numpy. Frequency distribution is returned.
Code 3 is checking Power spectrum.
Code 4 is invers Fourie by numpy.

pythonのthreadingを使いクーロン力の並列計算をするテスト

以前行った、multiprocessingでのクーロン力並列計算に引き続き、threadingを使ったクーロン力の並列計算をテストする。計算条件はmultiprocessingの時と同じくcore数を4、計算粒子数を15^3個とした。

結果、計算時間は14.36[sec]。multiprocessingでの計算は2.09[sec]であったため、大分時間がかかるというかシングルスレッドで計算していた時より時間がかかっている。threadingでは各threadがメモリを共有するために必要な情報を取得するためメモリアクセスする際に、排他ロック(GIL)が起きているのかな、と考えられる。試しにthread数を1にして実行してみると処理時間は6.92[sec]。thread化しない方が高速であった。

■結果 [Results summary]

code type 時間[sec]
threading (4threads) 14.36 <-(new!)
threading (1thread) 6.92 <-(new!)
multiprocessing 2.09
itertools使用 (no1) 8.18
range記述 (no2) 7.93
xrange記述 (no3) 7.89
ループ内周でnumpy使用 (no4) 78.46

■使用したコードは下記(use 4 threads)

Following previous parallel computation of Coulomb force in multiprocessing, we test parallel computation of Coulomb force using threading. The same condition as before the multiprocessing calculation condition, the core number was set to 4 and the number of calculated particles was set to 15 ^ 3.

As a result, the calculation time is 14.36 [sec]. The computation with multiprocessing was 2.09 [sec], so it took a long time or it took more time than when computing with single thread. In threading, it is considered that an exclusive lock (GIL) is occurring when memory access is performed in order to acquire necessary information for each thread to share memory. If we try to run with thread number 1, the processing time is 6.92 [sec]. It was faster to not thread.

マルチスレッドのテスト

スレッドを発行して並列化するテスト。スレッドの開始はstart(),終了待ちはjoin()で行う。すべてのスレッドの終了をチェックするためには、現在発行中の全スレッドをリストアップするenumerate()を使う。ただしこのリストの中には、メインスレッドも含まれているので、メインスレッドは抜く必要がある。メインスレッドはcurrentThread()であらわせるので、これをremoveで取り除く。取り除いた結果に対して、join()で終了待ちし、全スレッドが終了したら終了メッセージを出す。各スレッドからの返り値はQueueを使って貯めることができる。貯めたいqueueを引数に渡してやることで、スレッド終了後に全スレッドの結果を読み出すことができる。

■実行結果
thread = 0 rand = 1.97370804812
thread = 1 rand = 3.45525477664
thread = 2 rand = 0.651252139145
thread = 3 rand = 3.96551177159
thread = 4 rand = 3.57845628232
thread = 5 rand = 1.81758678574
thread = 6 rand = 2.31490107118
thread = 7 rand = 1.21471678276
end thread = 2
end thread = 7
end thread = 5
end thread = 0
end thread = 6
end thread = 1
end thread = 4
end thread = 3
[0.6512521391445797, 1.2147167827645151, 1.817586785739616, 1.9737080481200742, 2.3149010711817626, 3.4552547766386787, 3.5784562823201704, 3.9655117715879653]
end

A test that issues threads and parallelizes them. The start of the thread is done with start (), and the wait for completion is done with join (). To check the termination of all threads, use enumerate () which lists all the threads that are currently issuing. However, since the main thread is also included in this list, it is necessary to withdraw the main thread. The main thread can be set with currentThread (), so remove it with remove. Wait for join () to finish removing, and issue a termination message when all threads are finished. The return value from each thread can be stored using Queue. By passing the queue you want to save as an argument, you can read the results of all threads after the thread ends.

pythonのmultiprocessingを使いクーロン力の並列計算をするテスト

前回クーロン力計算部分の高速化を検討した。結果としてはシンプルに2重ループを使った計算。今回はこのクーロン力計算部分をmultiprocessingを使ったマルチプロセスによる並列化で高速化を試してみる。

作成したコードが下記。結果は2.09[sec]。使用coreは4スレッド仕様なので、4プロセスでの処理を行った。前回の処理時間が約8秒であったのを考えると、理想どおり4分の1になる結果が得られた。

■結果 [Results summary]

code type 時間[sec]
multiprocessing (4core) 2.09 <-(new)
itertools使用 (no1) 8.18
range記述 (no2) 7.93
xrange記述 (no3) 7.89
ループ内周でnumpy使用 (no4) 78.46

We examined the speedup of the previous Coulomb force calculation part. As a result, more fast results calculation was using a simple double loop. In this time, I will try speeding up by multiprocessing parallel processing of this Coulomb force calculation part.

The code you created is below. The result is 2.09 [sec]. Since core used is 4 thread specification, processing in 4 processes was done. Considering that the last processing time was about 8 seconds, the result was 1/4 as ideal.

クーロン力計算の高速化検討

前回イオントラップシミュレーションにて、分子動力学のopenGLを使った可視化を行った。この処理にかかるほぼすべての時間はクーロン力計算の箇所であり、各粒子同士の計算が必要となる。粒子数をN個とすると、計算量は1ステップあたりNC2回が必要となる。特に今回のイオントラップの様な、閉じた系の中では周期境界条件が存在しないため、Ewald methodの様な高速化の方法も用いることができない。

今回はこのイオントラップにおけるクーロン力計算の高速化を検討してみたいと思う。まず高速化対象のコードだけを抜き出してきたのが下記。

上記コードの処理時間は手元の環境で約8.18[sec](10回実行した平均)
このコードのfind_pair関数内のitertools箇所を下記の様にシンプルにforループを2重で処理する様書き替えた場合が下記。

上記コードの処理時間は約7.93[sec](10回実行した平均)。
itertoolsを使った方が高速になるのかなと思ってたけど、シンプルにforの2重ループの方が高速な結果になった。

次に、forループ範囲をrangeコマンドで作っているのを、xrangeに変更してみる。rangeは内部でリストを生成してから要素参照するのに対して、xrangeはリストを作らず要素参照するらしい。

上記コードで約7.89[sec](10回実行した平均)。
気持ち速くなった程度だった。今回は粒子数15^3個で試しているが、数を増やしたら効果が出てくるかもしれない。

最後、numpyを使ってみる。どうしても上手い使い方が思い浮かばず、forループの中の演算を無理矢理numpyを使った処理に書き替えた。

上記コードで約78.46[sec](10回実行した平均)。
全然駄目だった。多重ループの内周で、何度も実行されるような場所にnumpyの処理を配置するととんでもなく遅くなる。遅くなるとは思っていたが予想以上の重さ。

■結果 [Results summary]

code type 時間[sec]
itertools使用 (no1) 8.18
range記述 (no2) 7.93
xrange記述 (no3) 7.89
ループ内周でnumpy使用 (no4) 78.46

あと考えられる手法はスレッド化。これはまた今度試してみたいと思う。

We performed visualization using openGL of molecular dynamics in the previous ion trap simulation. Almost all the time required for this process is the location of the Coulomb force calculation and it is necessary to calculate each particle. Assuming that the number of particles is N, the amount of calculation needs NC2 Times per step. Especially in the closed system such as the ion trap of this time there is no periodic boundary condition, so speeding method like Ewald method can not be used.

In this time I would like to examine how to speed up the calculation of the Coulomb force in this ion trap.

First, Code No1 that it has been extracted only the code of speeding up the subject. Results time is 8.18sec.

Code No.2 is when the itertools location in the above find_pair function is rewritten with a double for loop.Results time is 7.93sec.

Code No 3 changes range to xrange.Results time is 7.89sec.

Last Code No4 use numpy.But, Since it is used many times on the inner periphery, slow processing can be expected.Results time is 78.46sec.

Another possible method is threading. I want to try this again next time.

phantomjs を使い、wordpress へ記事を投稿するスクリプト

Script sample to posting to wordpress by phantomjs.