|
1 | 1 | # pybioclip
|
2 |
| -Python package to simplify use of BioCLIP |
| 2 | + |
| 3 | + |
| 4 | +[](https://pypi.org/project/bioclip) |
| 5 | +[](https://pypi.org/project/bioclip) |
| 6 | + |
| 7 | +----- |
| 8 | + |
| 9 | +Command line tool and python package to simplify using [BioCLIP](https://imageomics.github.io/bioclip/). |
| 10 | + |
| 11 | + |
| 12 | +**Table of Contents** |
| 13 | + |
| 14 | +- [Installation](#installation) |
| 15 | +- [Command Line Usage](#command-line-usage) |
| 16 | +- [Python Package Usage](#python-package-usage) |
| 17 | +- [License](#license) |
| 18 | +- [Acknowledgments](#acknowledgments) |
| 19 | +- [License](#license) |
| 20 | + |
| 21 | +## Requirements |
| 22 | +- Python compatible with [PyTorch](https://pytorch.org/get-started/locally/#linux-python) |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```console |
| 27 | +pip install git+https://github.com/Imageomics/pybioclip |
| 28 | +``` |
| 29 | + |
| 30 | +If you have any issues with installation, please first upgrade pip by running `pip install --upgrade pip`. |
| 31 | + |
| 32 | +## Command Line Usage |
| 33 | + |
| 34 | +### Predict classification |
| 35 | + |
| 36 | +#### Example: Predict species for an image |
| 37 | +The example image used below is [`Ursus-arctos.jpeg`](https://huggingface.co/spaces/imageomics/bioclip-demo/blob/ef075807a55687b320427196ac1662b9383f988f/examples/Ursus-arctos.jpeg) from the [bioclip-demo](https://huggingface.co/spaces/imageomics/bioclip-demo). |
| 38 | + |
| 39 | +Predict species for an `Ursus-arctos.jpeg` file: |
| 40 | +```console |
| 41 | +bioclip predict Ursus-arctos.jpeg |
| 42 | +``` |
| 43 | +Output: |
| 44 | +``` |
| 45 | ++----------------------------------------------------------------------------------------+-----------------------+ |
| 46 | +| Taxon | Probability | |
| 47 | ++----------------------------------------------------------------------------------------+-----------------------+ |
| 48 | +| Animalia Chordata Mammalia Carnivora Ursidae Ursus arctos (Kodiak bear) | 0.9356034994125366 | |
| 49 | +| Animalia Chordata Mammalia Carnivora Ursidae Ursus arctos syriacus (syrian brown bear) | 0.05616999790072441 | |
| 50 | +| Animalia Chordata Mammalia Carnivora Ursidae Ursus arctos bruinosus | 0.004126196261495352 | |
| 51 | +| Animalia Chordata Mammalia Carnivora Ursidae Ursus arctus | 0.0024959812872111797 | |
| 52 | +| Animalia Chordata Mammalia Carnivora Ursidae Ursus americanus (Louisiana black bear) | 0.0005009894957765937 | |
| 53 | ++----------------------------------------------------------------------------------------+-----------------------+ |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +To save as a CSV or JSON file you can use the `--format <file type>` and `--output <filename>` arguments with `csv` or `json`, respectively. |
| 59 | + |
| 60 | +To save the JSON output to `ursus.json` run: |
| 61 | +```console |
| 62 | +bioclip predict --format json --output ursus.json Ursus-arctos.jpeg |
| 63 | +``` |
| 64 | + |
| 65 | +To save the CSV output to `ursus.csv` run: |
| 66 | +```console |
| 67 | +bioclip predict --format csv --output ursus.csv Ursus-arctos.jpeg |
| 68 | +``` |
| 69 | + |
| 70 | +#### Predict genus for an image |
| 71 | + |
| 72 | +Predict genus for image `Ursus-arctos.jpeg`, restricted to the top 3 predictions: |
| 73 | +```console |
| 74 | +bioclip predict --rank genus --k 3 Ursus-arctos.jpeg |
| 75 | +``` |
| 76 | +Output: |
| 77 | +``` |
| 78 | ++---------------------------------------------------------+------------------------+ |
| 79 | +| Taxon | Probability | |
| 80 | ++---------------------------------------------------------+------------------------+ |
| 81 | +| Animalia Chordata Mammalia Carnivora Ursidae Ursus | 0.9994320273399353 | |
| 82 | +| Animalia Chordata Mammalia Artiodactyla Cervidae Cervus | 0.00032594642834737897 | |
| 83 | +| Animalia Chordata Mammalia Artiodactyla Cervidae Alces | 7.803700282238424e-05 | |
| 84 | ++---------------------------------------------------------+------------------------+ |
| 85 | +``` |
| 86 | + |
| 87 | +#### Optional arguments for predicting classifications: |
| 88 | +- `--rank RANK` - rank of the classification (kingdom, phylum, class, order, family, genus, species) [default: species] |
| 89 | +- `--k K` - number of top predictions to show [default: 5] |
| 90 | +- `--format FORMAT` - format of the output (table, json, or csv) [default: table] |
| 91 | +- `--output OUTPUT` - save output to a filename instead of printing it [default: stdout] |
| 92 | + |
| 93 | + |
| 94 | +### Predict from a list of classes |
| 95 | + |
| 96 | +Create predictions for 3 classes (cat, bird, and bear) for image `Ursus-arctos.jpeg`: |
| 97 | +```console |
| 98 | +bioclip predict --cls cat,bird,bear Ursus-arctos.jpeg |
| 99 | +``` |
| 100 | +Output: |
| 101 | +``` |
| 102 | ++-------+-----------------------+ |
| 103 | +| Taxon | Probability | |
| 104 | ++-------+-----------------------+ |
| 105 | +| cat | 4.581644930112816e-08 | |
| 106 | +| bird | 3.051998476166773e-08 | |
| 107 | +| bear | 0.9999998807907104 | |
| 108 | ++-------+-----------------------+% |
| 109 | +``` |
| 110 | + |
| 111 | +#### Optional arguments for predicting from a list of classes: |
| 112 | +- `--format FORMAT` - format of the output (table, json, or csv) [default: table] |
| 113 | +- `--output OUTPUT` - save output to a filename instead of printing it [default: stdout] |
| 114 | +- `--cls CLS` - comma separated list of classes to predict, when specified the `--rank` and `--k` arguments are ignored [default: all] |
| 115 | + |
| 116 | + |
| 117 | +### View command line help |
| 118 | +```console |
| 119 | +bioclip --help |
| 120 | +``` |
| 121 | + |
| 122 | +## Python Package Usage |
| 123 | +### Predict species classification |
| 124 | + |
| 125 | +```python |
| 126 | +from bioclip import predict_classification, Rank |
| 127 | + |
| 128 | +predictions = predict_classification("Ursus-arctos.jpeg", Rank.SPECIES) |
| 129 | + |
| 130 | +for species_name, probability in predictions.items(): |
| 131 | + print(species_name, probability) |
| 132 | +``` |
| 133 | + |
| 134 | +Output: |
| 135 | +```console |
| 136 | +Animalia Chordata Mammalia Carnivora Ursidae Ursus arctos (Kodiak bear) 0.9356034994125366 |
| 137 | +Animalia Chordata Mammalia Carnivora Ursidae Ursus arctos syriacus (syrian brown bear) 0.05616999790072441 |
| 138 | +Animalia Chordata Mammalia Carnivora Ursidae Ursus arctos bruinosus 0.004126196261495352 |
| 139 | +Animalia Chordata Mammalia Carnivora Ursidae Ursus arctus 0.0024959812872111797 |
| 140 | +Animalia Chordata Mammalia Carnivora Ursidae Ursus americanus (Louisiana black bear) 0.0005009894957765937 |
| 141 | +``` |
| 142 | + |
| 143 | +### Predict from a list of classes |
| 144 | +```python |
| 145 | +from bioclip import predict_classifications_from_list, Rank |
| 146 | + |
| 147 | +predictions = predict_classifications_from_list("Ursus-arctos.jpeg", |
| 148 | + ["duck","fish","bear"]) |
| 149 | + |
| 150 | +for cls, probability in predictions.items(): |
| 151 | + print(cls, probability) |
| 152 | +``` |
| 153 | +Output: |
| 154 | +```console |
| 155 | +duck 1.0306726583309e-09 |
| 156 | +fish 2.932403668845507e-12 |
| 157 | +bear 1.0 |
| 158 | +``` |
| 159 | + |
| 160 | +## License |
| 161 | + |
| 162 | +`pybioclip` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. |
| 163 | + |
| 164 | +## Acknowledgments |
| 165 | +The [prediction code in this repo](src/bioclip/predict.py) is based on work by [@samuelstevens](https://github.com/samuelstevens) in [bioclip-demo](https://huggingface.co/spaces/imageomics/bioclip-demo/tree/ef075807a55687b320427196ac1662b9383f988f). |
0 commit comments