├── .gitignore ├── LICENSE ├── README.md └── rank_pooling.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 MRzzm 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rank-pooling-python 2 | This is an implementation of rank pooling for action recognition with python. This code is mainly based on the [original matlab code](https://bitbucket.org/bfernando/videodarwin) 3 | 4 | > "Fernando, Basura, Efstratios Gavves, José Oramas, Amir Ghodrati, and Tinne Tuytelaars. "Rank pooling for action recognition." IEEE transactions on pattern analysis and machine intelligence 39, no. 4 (2017): 773-787.". 5 | 6 | 7 | # Requirements 8 | * numpy 9 | * scikit-learn 10 | * scipy 11 | 12 | # Contact 13 | If you have any question, pls contact zhangzhimeng1@gmail.com 14 | -------------------------------------------------------------------------------- /rank_pooling.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' 4 | @project: Rank pooling 5 | @author: MRzzm 6 | @E-mail: zhangzhimeng1@gmail.com 7 | @github: https://github.com/MRzzm/rank-pooling-python.git 8 | ''' 9 | import numpy as np 10 | import scipy.sparse 11 | from sklearn import svm 12 | 13 | def smoothSeq(seq): 14 | 15 | res = np.cumsum(seq, axis=1) 16 | seq_len = np.size(res, 1) 17 | res = res / np.expand_dims(np.linspace(1, seq_len, seq_len), 0) 18 | return res 19 | 20 | def rootExpandKernelMap(data): 21 | 22 | element_sign=np.sign(data) 23 | nonlinear_value=np.sqrt(np.fabs(data)) 24 | return np.vstack((nonlinear_value*(element_sign>0),nonlinear_value*(element_sign<0))) 25 | 26 | def getNonLinearity(data,nonLin='ref'): 27 | 28 | # we don't provide the Chi2 kernel in our code 29 | if nonLin=='none': 30 | return data 31 | if nonLin=='ref': 32 | return rootExpandKernelMap(data) 33 | elif nonLin=='tanh': 34 | return np.tanh(data) 35 | elif nonLin=='ssr': 36 | return np.sign(data)*np.sqrt(np.fabs(data)) 37 | else: 38 | raise("We don't provide {} non-linear transformation".format(nonLin)) 39 | 40 | def normalize(seq,norm='l2'): 41 | 42 | if norm=='l2': 43 | seq_norm = np.linalg.norm(seq, ord=2, axis=0) 44 | seq_norm[seq_norm == 0] = 1 45 | seq_norm = seq / np.expand_dims(seq_norm, 0) 46 | return seq_norm 47 | elif norm=='l1': 48 | seq_norm=np.linalg.norm(seq,ord=1,axis=0) 49 | seq_norm[seq_norm==0]=1 50 | seq_norm=seq/np.expand_dims(seq_norm,0) 51 | return seq_norm 52 | else: 53 | raise("We only provide l1 and l2 normalization methods") 54 | 55 | 56 | 57 | def rank_pooling(time_seq,C = 1,NLStyle = 'ssr'): 58 | ''' 59 | This function only calculate the positive direction of rank pooling. 60 | :param time_seq: D x T 61 | :param C: hyperparameter 62 | :param NLStyle: Nonlinear transformation.Including: 'ref', 'tanh', 'ssr'. 63 | :return: Result of rank pooling 64 | ''' 65 | 66 | seq_smooth=smoothSeq(time_seq) 67 | seq_nonlinear=getNonLinearity(seq_smooth,NLStyle) 68 | seq_norm=normalize(seq_nonlinear) 69 | seq_len=np.size(seq_norm, 1) 70 | Labels=np.array(range(1,seq_len+1)) 71 | seq_svr=scipy.sparse.csr_matrix(np.transpose(seq_norm)) 72 | svr_model = svm.LinearSVR(epsilon=0.1, tol=0.001, C=C, loss='squared_epsilon_insensitive', fit_intercept=False, dual=False) 73 | svr_model.fit(seq_svr,Labels) 74 | return svr_model.coef_ --------------------------------------------------------------------------------