Apache alikeを試してみる(おまけ:run_desc_extractor.pyの改良)
その5までで終了するつもりだったのですが、Apache alikeの処理を順に追跡する作業のあいだに改良点を見つけましたので、追加します。
- Apache alikeを試してみる(その1:準備と前置き) - 日記と、買い物記録
- Apache alikeを試してみる(その2:前提環境構築) - 日記と、買い物記録
- Apache alikeを試してみる(その3:alikeのインストール&コンパイル) - 日記と、買い物記録
- Apache alikeを試してみる(その4:データの作成) - 日記と、買い物記録
- Apache alikeを試してみる(その5:デモWebサーバの起動) - 日記と、買い物記録
- Apache alikeを試してみる(おまけ:run_desc_extractor.pyの改良) - 日記と、買い物記録
- Apache alikeを試してみる(おまけ2:動作例とサンプル画像) - 日記と、買い物記録
run_desc_extractor.pyの改良版
多数の画像からkeypointを探し出す処理をするalike/trunc/demo/run_desc_extractor.pyですが、デモプログラムということもあってか、処理効率よりもわかりやすさを優先しているような雰囲気です。
私の環境のCPUはCore i5なので4つのコアがあるのですが、一度にひとつの画像ファイルしか取り扱ってくれません。テスト環境には10000を超えるファイルがあるので、結構時間がかかります(デフォルトでは400個のファイルしか処理しないので、引数に"all"を渡す必要があります)。
そこでPoolを使用して並列処理を行うように改良しました。並列数はMAX_CPUSで指定します。従来はfor文を使用して順に処理していた部分を関数に変更して、Pool.map()から呼び出しています。
""" Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import os import sys import desc_extractor as de import conf from multiprocessing import Pool def treat_a_file(fl): if not fl.endswith('.jpg'): return 0 fi = IMAGES_DIR + '/' + fl fo = 'desc/' + fl[:fl.find('.')] + '.txt' de.extract_desc(cfg.detector(), fi, fo) return 0 IMAGES_DIR = 'ukbench/full' MAX_IMGS = 100 * 4 MAX_CPUS = 4 cfg = conf.Conf('demo-conf.xml') imglist = os.listdir(IMAGES_DIR) imglist.sort() if len(sys.argv) > 1 and sys.argv[1] == 'all': img_proc_list = imglist else: img_proc_list = imglist[:MAX_IMGS] p = Pool(MAX_CPUS) p.map(treat_a_file, img_proc_list)
使い方は変わっていません。普通に呼び出すにはREADMEのとおりですが、この場合400ファイルしか処理対象になりません。
$ python run_desc_extractor.py
もっと多数のファイルがある場合にはallを指定します。
$ python run_desc_extractor.py all
- 作者: Drew Conway,John Myles White,萩原正人,奥野陽,水野貴明,木下哲也
- 出版社/メーカー: オライリージャパン
- 発売日: 2012/12/22
- メディア: 大型本
- 購入: 2人 クリック: 41回
- この商品を含むブログ (12件) を見る
- 作者: Toby Segaran,當山仁健,鴨澤眞夫
- 出版社/メーカー: オライリージャパン
- 発売日: 2008/07/25
- メディア: 大型本
- 購入: 91人 クリック: 2,220回
- この商品を含むブログ (277件) を見る