日記とか、工作記録とか

自分に書けることを何でも書いてゆきます。作った物、買ったもの、コンピュータ系の話題が多くなるかもしれません。

Apache alikeを試してみる(おまけ:run_desc_extractor.pyの改良)

その5までで終了するつもりだったのですが、Apache alikeの処理を順に追跡する作業のあいだに改良点を見つけましたので、追加します。

  1. Apache alikeを試してみる(その1:準備と前置き) - 日記と、買い物記録
  2. Apache alikeを試してみる(その2:前提環境構築) - 日記と、買い物記録
  3. Apache alikeを試してみる(その3:alikeのインストール&コンパイル) - 日記と、買い物記録
  4. Apache alikeを試してみる(その4:データの作成) - 日記と、買い物記録
  5. Apache alikeを試してみる(その5:デモWebサーバの起動) - 日記と、買い物記録
  6. Apache alikeを試してみる(おまけ:run_desc_extractor.pyの改良) - 日記と、買い物記録
  7. 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

入門 機械学習

入門 機械学習

集合知プログラミング

集合知プログラミング