Repository: NegatioN/OnlineMiningTripletLoss Branch: master Commit: 4c22eac01ee8 Files: 67 Total size: 135.4 KB Directory structure: gitextract_53kablsc/ ├── .github/ │ └── workflows/ │ └── main.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── docs/ │ ├── .gitignore │ ├── Gemfile │ ├── _config.yml │ ├── _data/ │ │ ├── alerts.yml │ │ ├── definitions.yml │ │ ├── glossary.yml │ │ ├── sidebars/ │ │ │ └── home_sidebar.yml │ │ ├── tags.yml │ │ ├── terms.yml │ │ └── topnav.yml │ ├── _includes/ │ │ ├── archive.html │ │ ├── callout.html │ │ ├── footer.html │ │ ├── google_analytics.html │ │ ├── head.html │ │ ├── head_print.html │ │ ├── image.html │ │ ├── important.html │ │ ├── initialize_shuffle.html │ │ ├── inline_image.html │ │ ├── links.html │ │ ├── note.html │ │ ├── search_google_custom.html │ │ ├── search_simple_jekyll.html │ │ ├── sidebar.html │ │ ├── tip.html │ │ ├── toc.html │ │ ├── topnav.html │ │ └── warning.html │ ├── _layouts/ │ │ ├── default.html │ │ ├── default_print.html │ │ ├── none.html │ │ ├── page.html │ │ └── page_print.html │ ├── css/ │ │ ├── boxshadowproperties.css │ │ ├── customstyles.css │ │ ├── fonts/ │ │ │ └── FontAwesome.otf │ │ ├── modern-business.css │ │ ├── printstyles.css │ │ ├── syntax.css │ │ ├── theme-blue.css │ │ └── theme-green.css │ ├── feed.xml │ ├── fonts/ │ │ └── FontAwesome.otf │ ├── index.html │ ├── js/ │ │ ├── customscripts.js │ │ ├── jekyll-search.js │ │ └── toc.js │ ├── licenses/ │ │ ├── LICENSE │ │ └── LICENSE-BSD-NAVGOCO.txt │ ├── sidebar.json │ ├── sitemap.xml │ ├── tooltips.json │ └── triplet_loss.html ├── index.ipynb ├── online_triplet_loss/ │ ├── __init__.py │ ├── _nbdev.py │ └── losses.py ├── settings.ini ├── setup.py └── triplet_loss.ipynb ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/workflows/main.yml ================================================ name: CI on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions/setup-python@v1 with: python-version: '3.6' architecture: 'x64' - name: Install the library run: | pip install nbdev jupyter pip install -e . - name: Read all notebooks run: | nbdev_read_nbs - name: Check if all notebooks are cleaned run: | echo "Check we are starting with clean git checkout" if [ -n "$(git status -uno -s)" ]; then echo "git status is not clean"; false; fi echo "Trying to strip out notebooks" nbdev_clean_nbs echo "Check that strip out was unnecessary" git status -s # display the status to see which nbs need cleaning up if [ -n "$(git status -uno -s)" ]; then echo -e "!!! Detected unstripped out notebooks\n!!!Remember to run nbdev_install_git_hooks"; false; fi - name: Check if there is no diff library/notebooks run: | if [ -n "$(nbdev_diff_nbs)" ]; then echo -e "!!! Detected difference between the notebooks and the library"; false; fi - name: Run tests run: | nbdev_test_nbs ================================================ FILE: .gitignore ================================================ .last_checked .idea/ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so .gitconfig .gitattributes # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ ================================================ FILE: CONTRIBUTING.md ================================================ # How to contribute ## How to get started Before anything else, please install the git hooks that run automatic scripts during each commit and merge to strip the notebooks of superfluous metadata (and avoid merge conflicts). After cloning the repository, run the following command inside it: ``` nbdev_install_git_hooks ``` ## Did you find a bug? * Ensure the bug was not already reported by searching on GitHub under Issues. * If you're unable to find an open issue addressing the problem, open a new one. Be sure to include a title and clear description, as much relevant information as possible, and a code sample or an executable test case demonstrating the expected behavior that is not occurring. * Be sure to add the complete error messages. #### Did you write a patch that fixes a bug? * Open a new GitHub pull request with the patch. * Ensure that your PR includes a test that fails without your patch, and pass with it. * Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable. ## PR submission guidelines * Keep each PR focused. While it's more convenient, do not combine several unrelated fixes together. Create as many branches as needing to keep each PR focused. * Do not mix style changes/fixes with "functional" changes. It's very difficult to review such PRs and it most likely get rejected. * Do not add/remove vertical whitespace. Preserve the original style of the file you edit as much as you can. * Do not turn an already submitted PR into your development playground. If after you submitted PR, you discovered that more work is needed - close the PR, do the required work and then submit a new PR. Otherwise each of your commits requires attention from maintainers of the project. * If, however, you submitted a PR and received a request for changes, you should proceed with commits inside that PR, so that the maintainer can see the incremental fixes and won't need to review the whole PR again. In the exception case where you realize it'll take many many commits to complete the requests, then it's probably best to close the PR, do the work and then submit it again. Use common sense where you'd choose one way over another. ## Do you want to contribute to the documentation? * Docs are automatically created from the notebooks in the nbs folder. ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) [2019] [Joakim Rishaug] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Makefile ================================================ SRC = $(wildcard lib/*.ipynb) all: "online_triplet_loss" docs "online_triplet_loss": $(SRC) nbdev_build_lib touch "online_triplet_loss" docs_serve: docs cd docs && bundle exec jekyll serve docs: $(SRC) nbdev_build_docs touch docs test: nbdev_test_nbs release: pypi nbdev_bump_version pypi: dist twine upload --repository pypi dist/* dist: clean python setup.py sdist bdist_wheel clean: rm -rf dist ================================================ FILE: README.md ================================================ # online_triplet_loss > PyTorch conversion of the excellent post on the same topic in Tensorflow. Simply an implementation of a triple loss with online mining of candidate triplets used in semi-supervised learning. ## Install `pip install online_triplet_loss` Then import with: `from online_triplet_loss.losses import *` PS: Requires Pytorch version 1.1.0 or above to use. ## How to use In these examples I use a really large margin, since the embedding space is so small. A more realistic margins seems to be between `0.1 and 2.0` ``` from torch import nn import torch model = nn.Embedding(10, 10) ``` ``` #from online_triplet_loss.losses import * labels = torch.randint(high=10, size=(5,)) # our five labels embeddings = model(labels) print('Labels:', labels) print('Embeddings:', embeddings) loss = batch_hard_triplet_loss(labels, embeddings, margin=100) print('Loss:', loss) loss.backward() ``` Labels: tensor([6, 1, 3, 6, 6]) Embeddings: tensor([[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457, 0.0180, -0.4500], [ 1.0757, -0.8420, -0.7630, -0.0746, 1.1545, 0.4017, 0.5587, 1.7947, 0.1992, -2.2288], [ 0.2646, 1.2383, 0.1949, 0.5743, -0.8460, -0.9929, -2.0350, 0.2095, 0.2129, -0.4855], [-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457, 0.0180, -0.4500], [-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457, 0.0180, -0.4500]], grad_fn=) Loss: tensor(95.1271, grad_fn=) ``` #from online_triplet_loss.losses import * embeddings = model(labels) print('Labels:', labels) print('Embeddings:', embeddings) loss, fraction_pos = batch_all_triplet_loss(labels, embeddings, squared=False, margin=100) print('Loss:', loss) loss.backward() ``` Labels: tensor([6, 1, 3, 6, 6]) Embeddings: tensor([[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457, 0.0180, -0.4500], [ 1.0757, -0.8420, -0.7630, -0.0746, 1.1545, 0.4017, 0.5587, 1.7947, 0.1992, -2.2288], [ 0.2646, 1.2383, 0.1949, 0.5743, -0.8460, -0.9929, -2.0350, 0.2095, 0.2129, -0.4855], [-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457, 0.0180, -0.4500], [-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457, 0.0180, -0.4500]], grad_fn=) tensor(94.9947, grad_fn=) tensor(1.) Loss: tensor(94.9947, grad_fn=) ## References * [Triplet Loss and Online Triplet Mining in Tensorflow](https://github.com/omoindrot/tensorflow-triplet-loss) * [Facenet paper](https://arxiv.org/abs/1503.03832) * [adambielski's nice implementation](https://github.com/adambielski/siamese-triplet) (unfortunately context switches between CPU / GPU) ================================================ FILE: docs/.gitignore ================================================ _site/ ================================================ FILE: docs/Gemfile ================================================ source "https://rubygems.org" gem 'github-pages', group: :jekyll_plugins # Added at 2019-11-25 10:11:40 -0800 by jhoward: gem "jekyll", "~> 3.7" ================================================ FILE: docs/_config.yml ================================================ repository: "NegatioN/OnlineMiningTripletLoss" output: web topnav_title: "online_triplet_loss" site_title: "online_triplet_loss" company_name: "Joakim Rishaug" description: "Online mining triplet losses for Pytorch" # Set to false to disable KaTeX math use_math: true # Add Google analytics id if you have one and want to use it here google_analytics: # See http://nbdev.fast.ai/search for help with adding Search google_search: host: 127.0.0.1 # the preview server used. Leave as is. port: 4000 # the port where the preview is rendered. exclude: - .idea/ - .gitignore - vendor exclude: [vendor] highlighter: rouge markdown: kramdown kramdown: input: GFM auto_ids: true hard_wrap: false syntax_highlighter: rouge collections: tooltips: output: false defaults: - scope: path: "" type: "pages" values: layout: "page" comments: true search: true sidebar: home_sidebar topnav: topnav - scope: path: "" type: "tooltips" values: layout: "page" comments: true search: true tooltip: true sidebars: - home_sidebar permalink: pretty theme: jekyll-theme-cayman baseurl: /OnlineMiningTripletLoss/ ================================================ FILE: docs/_data/alerts.yml ================================================ tip: '