Introduction to Numpy (and Python) [JPN]MasaruOinaga
Introduction to Numpy and Python in Japanese.
Intended to programmers interested in Python.
The original motivation was to share information inside the lab where I belong.
Introduction to Numpy (and Python) [JPN]MasaruOinaga
Introduction to Numpy and Python in Japanese.
Intended to programmers interested in Python.
The original motivation was to share information inside the lab where I belong.
【DLゼミ】XFeat: Accelerated Features for Lightweight Image Matchingharmonylab
公開URL:https://arxiv.org/pdf/2404.19174
出典:Guilherme Potje, Felipe Cadar, Andre Araujo, Renato Martins, Erickson R. ascimento: XFeat: Accelerated Features for Lightweight Image Matching, Proceedings of the 2024 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR) (2023)
概要:リソース効率に優れた特徴点マッチングのための軽量なアーキテクチャ「XFeat(Accelerated Features)」を提案します。手法は、局所的な特徴点の検出、抽出、マッチングのための畳み込みニューラルネットワークの基本的な設計を再検討します。特に、リソースが限られたデバイス向けに迅速かつ堅牢なアルゴリズムが必要とされるため、解像度を可能な限り高く保ちながら、ネットワークのチャネル数を制限します。さらに、スパース下でのマッチングを選択できる設計となっており、ナビゲーションやARなどのアプリケーションに適しています。XFeatは、高速かつ同等以上の精度を実現し、一般的なラップトップのCPU上でリアルタイムで動作します。
セル生産方式におけるロボットの活用には様々な問題があるが,その一つとして 3 体以上の物体の組み立てが挙げられる.一般に,複数物体を同時に組み立てる際は,対象の部品をそれぞれロボットアームまたは治具でそれぞれ独立に保持することで組み立てを遂行すると考えられる.ただし,この方法ではロボットアームや治具を部品数と同じ数だけ必要とし,部品数が多いほどコスト面や設置スペースの関係で無駄が多くなる.この課題に対して音𣷓らは組み立て対象物に働く接触力等の解析により,治具等で固定されていない対象物が組み立て作業中に運動しにくい状態となる条件を求めた.すなわち,環境中の非把持対象物のロバスト性を考慮して,組み立て作業条件を検討している.本研究ではこの方策に基づいて,複数物体の組み立て作業を単腕マニピュレータで実行することを目的とする.このとき,対象物のロバスト性を考慮することで,仮組状態の複数物体を同時に扱う手法を提案する.作業対象としてパイプジョイントの組み立てを挙げ,簡易な道具を用いることで単腕マニピュレータで複数物体を同時に把持できることを示す.さらに,作業成功率の向上のために RGB-D カメラを用いた物体の位置検出に基づくロボット制御及び動作計画を実装する.
This paper discusses assembly operations using a single manipulator and a parallel gripper to simultaneously
grasp multiple objects and hold the group of temporarily assembled objects. Multiple robots and jigs generally operate
assembly tasks by constraining the target objects mechanically or geometrically to prevent them from moving. It is
necessary to analyze the physical interaction between the objects for such constraints to achieve the tasks with a single
gripper. In this paper, we focus on assembling pipe joints as an example and discuss constraining the motion of the
objects. Our demonstration shows that a simple tool can facilitate holding multiple objects with a single gripper.
8. Copyright(C) Nowcast, Inc. All rights reserved.
組み込み関数のinput()はかなり遅い!
標準入力
inputとsys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]
8
1000000
1
2
3
4
...
9. Copyright(C) Nowcast, Inc. All rights reserved.
組み込み関数のinput()はかなり遅い!
標準入力
inputとsys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]
9
2077ms
310ms
基本的には
sys.stdin.readlineを
利用した方が良い!
※末尾の改行文字などは
取り除かれないことに
注意
11. Copyright(C) Nowcast, Inc. All rights reserved.
lを長さnのListとする
■ ランダムアクセスはO(1)
● O(1)といいつつ遅いのでなるべく避けた方が良い(後述)
■ 末尾へのAppend / 末尾のPopは早いが、その他の場所のAppendやPopは遅い
■ inやmin、maxといった操作は直感的に書くことができ便利だが、O(n)
基本的なデータ構造の計算量
Listの各種操作の計算量
11
Operation Code example Average Case
Append l.append(x) O(1)
Pop last l.pop() O(1)
in x in l O(n)
min, max min(l), max(l) O(n)
12. Copyright(C) Nowcast, Inc. All rights reserved.
lを長さnのListとする
■ ランダムアクセスはO(1)
● O(1)といいつつ遅いのでなるべく避けるとよい(後述)
■ 末尾へのAppend / 末尾のPopは早いが、その他の場所のAppendやPopは遅い
■ inやmin、maxといった操作は直感的に書くことができ便利だが、O(n)
基本的なデータ構造の計算量
Listの各種操作の計算量
12
Operation Code example Average Case
Append l.append(x) O(1)
Pop last l.pop() O(1)
in x in l O(n)
min, max min(l), max(l) O(n)
SetやDictを
検討しよう!
13. Copyright(C) Nowcast, Inc. All rights reserved.
sをSet、dをDictとする
■ SetとDictはいわゆるハッシュテーブルというデータ構造
● C++の平衡二分探索木の Setとはまったく別物であることに注意!
● inが早い
• Listはinが遅いので、inを多用したい場合は Set / Dictでの実装が要検討
基本的なデータ構造の計算量
Set / Dictの各種操作の計算量
13
Operation Code example Average Case
in x in s O(1)
Add s.add() O(1)?
Operation Code example Average Case
in x in d O(1)
Get item d[x] O(1)
Set item d[x] = y O(1)
Set Dict
15. Copyright(C) Nowcast, Inc. All rights reserved.
Listのindexを使ったアクセス(ランダムアクセス)は非常に遅い。
indexによるアクセスは遅い
15
for i in range(n):
a[i]
for ai in a:
ai
for i, ai in enumerate(a):
ai
n = 10**7; a = list(range(n));
?
それぞれ実行時間は
何秒になるでしょう?
16. Copyright(C) Nowcast, Inc. All rights reserved.
Listのindexを使ったアクセス(ランダムアクセス)は非常に遅い。
indexによるアクセスは遅い
16
for i in range(len(a)):
a[i]
for ai in a:
ai
for i, ai in enumerate(a):
ai
n = 10**7; a = list(range(n));
ランダムアクセスはこのように
非常に遅い!
基本的には中段の書き方が
おすすめ!
indexが必要な場合は下段の
enumerateを使った書き方も。
18. Copyright(C) Nowcast, Inc. All rights reserved.
ListのListなどのソートは遅いが、keyの指定の仕方で工夫できる
ListのListのソートにはoperator.itemgetter
18
a.sort(key=lambda x: x[1])
from operator import itemgetter
a.sort(key=itemgetter(1))
?
それぞれ実行時間は
何秒になるでしょう?
n = 10**6
a = [[random(), random(), random()] for i in range(n)]
19. Copyright(C) Nowcast, Inc. All rights reserved.
ListのListなどのソートは遅いが、keyの指定の仕方で工夫できる
ListのListのソートkeyにはoperator.itemgetter
19
a.sort(key=lambda x: x[1])
from operator import itemgetter
a.sort(key=itemgetter(1))
n = 10**6
a = [[random(), random(), random()] for i in range(n)]
ListのListの要素が文字列
だったりするとさらに遅く
なるので、keyにitemgetter
などを利用して高速化を
図るのが良い
21. Copyright(C) Nowcast, Inc. All rights reserved.
■ 安定したJIT(=Just In Time)コンパイラで、ほとんどすべての組み込みモジュールが提供されてい
る
■ Numpyなどは使用できないが、競技プログラミングでは基本的な制御構文とデータ構造を組み合
わせたコードを書くことが多いので、Pythonで書いたコードをそのままPyPyとして提出するだけ
で速度が上がることが多々ある
■ AtCoderで現在提供されているPyPy3 (7.3.0)はPython3.6.9と互換性のあるもの
PythonとPyPy
PyPyとは?
21
3.6.9 (7.3.0+dfsg-1~ppa1~ubuntu18.04, Dec 24 2019, 08:12:19)
[PyPy 7.3.0 with GCC 7.4.0]
https://www.pypy.org/index.html よりhttps://www.python.jp/pages/about.html より
22. Copyright(C) Nowcast, Inc. All rights reserved.
pyenvを使うと自分のパソコンで複数のPythonのバージョンを管理することができる。
以下のコマンドで切り替えができる。
詳細についてはpyenvのGithubをチェックしてみてください!!
PythonとPyPy
local環境へのinstall
22
$ pyenv local 3.8.2 # set to Python3.8
$ pyenv local pypy3.6-7.3.0 # set to PyPy3
$ pyenv install 3.8.2
$ pyenv install pypy3.6-7.3.0
23. Copyright(C) Nowcast, Inc. All rights reserved.
forやwhileが二重以上になるような場合、PyPyを使った方が明らかに早い。
PythonとPyPy
PyPyを使うと早くなる例
23
n, k = map(int, input().split())
h = tuple(map(int, input().split()))
dp = [10**10] * n
dp[0] = 0
for i in range(1, n):
dp[i] = min(dp[j] + abs(h[j] - h[i])
for j in range(max(0, i - k), i))
print(dp[n - 1])
EDPC B - Frog 2 (n <= 10**5, k <=100)
EDPC B - Frog 2
DPの問題で、制約は以下
■ n <= 10**5
■ k <= 100
左のようなO(nk)の二重ループの
コードをPython3とPyPy3両方で提
出するとどうなるか?
24. Copyright(C) Nowcast, Inc. All rights reserved.
forやwhileが二重以上になるような場合、PyPyを使った方が明らかに早い。
PythonとPyPy
PyPyを使うと早くなる例
24
n, k = map(int, input().split())
h = tuple(map(int, input().split()))
dp = [10**10] * n
dp[0] = 0
for i in range(1, n):
dp[i] = min(dp[j] + abs(h[j] - h[i])
for j in range(max(0, i - k), i))
print(dp[n - 1])
EDPC B - Frog 2 (n <= 10**5, k <=100)
Python3 : 1713 ms
vs.
PyPy3 : 343 ms
このようにまったく同じコード
でも、PyPyにするだけでかなり高速
化できる!
25. Copyright(C) Nowcast, Inc. All rights reserved.
基本的にはPyPyを使うだけでかなりの高速化が見込めるが、一部遅い処理もある
■ 再帰関数を用いた処理
■ 文字列に関連した処理
心配な場合は、local環境やAtCoderのコードテストで両言語で試してみてから早い法を提出するなど
すると良いと思います。
PythonとPyPy
Pythonの方が早い場合
25
26. Copyright(C) Nowcast, Inc. All rights reserved.
■ ハイパフォーマンスPython
■ Effective Python
■ https://wiki.python.org/moin/TimeComplexity
■ https://github.com/pyenv/pyenv
今回のコードはGithubで公開しています
■ https://github.com/Kevinrobot34/cp_python_tips/tree/master/pycon2020
References
26
27. Copyright(C) Nowcast, Inc. All rights reserved.
皆さんも是非Pythonで競プロを始めましょう!
本日の内容は後日加筆して
弊社テックブログ( https://medium.com/finatext )に
投稿予定です。そちらもよろしくお願いします!
Nowcast for Actionable Insights