{"id":25642727,"url":"https://github.com/ClementWalter/Keras-FewShotLearning","last_synced_at":"2025-02-23T06:02:56.984Z","repository":{"id":42424859,"uuid":"197632932","full_name":"few-shot-learning/Keras-FewShotLearning","owner":"few-shot-learning","description":"Some State-of-the-Art few shot learning algorithms in tensorflow 2","archived":false,"fork":false,"pushed_at":"2023-10-17T22:31:31.000Z","size":2198,"stargazers_count":213,"open_issues_count":6,"forks_count":42,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-08-10T11:01:21.417Z","etag":null,"topics":["deep-learning","few-shot-learning","keras-tensorflow","one-shot-learning","tensorflow","tensorflow2","tf2"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/few-shot-learning.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-07-18T17:51:24.000Z","updated_at":"2024-07-26T01:17:40.000Z","dependencies_parsed_at":"2024-01-15T04:12:46.433Z","dependency_job_id":null,"html_url":"https://github.com/few-shot-learning/Keras-FewShotLearning","commit_stats":null,"previous_names":["clementwalter/keras-fewshotlearning"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/few-shot-learning%2FKeras-FewShotLearning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/few-shot-learning%2FKeras-FewShotLearning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/few-shot-learning%2FKeras-FewShotLearning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/few-shot-learning%2FKeras-FewShotLearning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/few-shot-learning","download_url":"https://codeload.github.com/few-shot-learning/Keras-FewShotLearning/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240275917,"owners_count":19775615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","few-shot-learning","keras-tensorflow","one-shot-learning","tensorflow","tensorflow2","tf2"],"created_at":"2025-02-23T06:01:49.966Z","updated_at":"2025-02-23T06:02:56.945Z","avatar_url":"https://github.com/few-shot-learning.png","language":"Python","funding_links":[],"categories":["Sample Codes / Projects \u003ca name=\"sample\" /\u003e ⛏️📐📁"],"sub_categories":["Reinforcement Learning \u003ca name=\"RL\" /\u003e🔮"],"readme":"![Install Package](https://github.com/few-shot-learning/Keras-FewShotLearning/workflows/Install%20Package/badge.svg)\n![Tests](https://github.com/few-shot-learning/Keras-FewShotLearning/workflows/Tests/badge.svg)\n[![codecov](https://codecov.io/gh/few-shot-learning/Keras-FewShotLearning/branch/master/graph/badge.svg)](https://codecov.io/gh/few-shot-learning/Keras-FewShotLearning)\n\nCurrently supporting `python` `3.6`, `3.7` and `tensorflow ^2.1`.\n\n# Welcome to keras-fsl!\n\nAs years go by, Few Shot Learning (FSL) and especially Metric Learning is becoming a hot topic not only in academic\npapers but also in production applications.\n\nWhile a lot of researcher nowadays tend to publish their code on github, there is still no easy framework to get\nstarted with FSL. Especially when it comes to benchmarking existing models on personal datasets it is not always easy\nto find its path into each single repo. Not mentioning the Tensorflow/PyTorch issue.\n\nThis repo aims at filling this gap by providing a single entry-point for Few Shot Learning. It is deeply inspired by\nKeras because it shares the same philosophy:\n\n\u003e It was developed with a focus on enabling fast experimentation.\n\u003e Being able to go from idea to result with the least possible delay is key to doing good research.\n\nThus this repo mainly relies on two of the main high-level python packages for data science: Keras and Pandas. While\nPandas may not seem very useful for researchers working with static dataset, it becomes a strong backbone in production\napplications when you always need to tinker with your data.\n\n## Few-Shot Learning\n\nFew-shot learning is a task consisting in classifying unseen samples into _n_ classes (so called n way task) where each\nclasses is only described with few (from 1 to 5 in usual benchmarks) examples.\n\nMost of the state-of-the-art algorithms\ntry to sort of learn a metric into a well suited (optimized) feature space. Thus deep networks usually first encode the\nbase images into a feature space onto which a _distance_ or _similarity_ is learnt.\n\nThis similarity is meant to be used to later classify samples according to their relative distance, either in a pair-wise\nmanner where the nearest support set samples is used to classify the query sample ([Voronoi diagram](https://en.wikipedia.org/wiki/Voronoi_diagram))\nor in a more advanced classifier. Indeed, this philosophy is most commonly known as [the kernel trick](https://en.wikipedia.org/wiki/Kernel_method)\nwhere the kernel is actually the similarity learnt during training. Hence any kind of usual kernel based Machine Learning\ncould potentially be plugged onto this learnt similarity (see the [min_eigenvalue](keras_fsl/metrics/gram_matrix_metrics.py) metric\nto track eigenvalues of the learnt similarity to see if it as actually a kernel).\n\nThere is no easy answer to the optimal choice of such a classifier in the feature space. This may depend on performance\nas well as on complexity and real application parameters. For instance if the support set is strongly imbalanced, you\nmay not want to fit an advanced classifier onto it but rather use a raw nearest neighbor approach.\n\nAll these considerations lead to the need of a code architecture that will let you play with these parameters with your\nown data in order to take the best from them.\n\nAmongst other, the original Siamese Nets is usually known as the network from [Koch et al.](https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pdf)\nThis algorithm learns a pair-wise similarity between images. More precisely it uses a densely connected layers on top\nof the absolute difference between the two embeddings to predict 0 (different) or 1 (same).\n\nActually, and as it is now expressed in recent papers, the [representation learning framework](https://arxiv.org/pdf/2002.05709.pdf) is as follows:\n - a data augmentation module A\n - an encoder network E\n - a projection network P\n - a loss L\n\nThis repo mimics this framework by proving model builders and notebooks to implement current SOTA algorithms and your\nown tweaks seamlessly:\n - use `tf.data.Dataset.map` to apply data augmentation\n - define a `tf.Keras.Sequential` model for your encoder\n - define a `kernel`, ie a `tf.keras.Layer` with two inputs and a real-valued output (see [head models](keras_fsl/models/head_models))\n - use any [`support_layers`](keras_fsl/layers/support_layer.py) to wrap the kernel and compute similarities in\n a `tf.keras.Sequential` manner (see notebooks for instance).\n - use any loss chosen accordingly to the output of the `tf.keras.Sequential` model ([GramMatrix](keras_fsl/layers/gram_matrix.py) or\n [CentroidsMatrix](keras_fsl/layers/centroids_matrix.py) for instance)\n \nAs an example, the TripletLoss algorithm uses indeed:\n - data augmentation: whatever you want\n - encoder: any backbone like ResNet50 or MobileNet\n - kernel: the l2 norm: `k(x, x') = ||x - x'||^2 = tf.keras.layers.Lambda(lambda inputs: tf.reduce_sum(tf.square(inputs[0] - inputs[1]), axis=1))`\n - support_layer: triplet loss uses all the pair-wises distances, hence it is the GramMatrix\n - loss: `k(a, p) + margin - k(a, n)` with semi-hard mining (see [triplet_loss](keras_fsl/losses/gram_matrix_losses.py))\n\n \n## Overview\n\nThis repos provides several tools for few-shot learning:\n\n - Keras layers and models\n - Keras sequences and Tensorflow datasets for training the models\n - Notebooks with proven learning sequences\n \nAll these tools can be used all together or separately. One may want to stick with the keras model trained on regular\nnumpy arrays, with or without callbacks. When designing more advanced `keras.Sequence` or `tf.data.Dataset` for\ntraining, it is advised (and some examples are provided) to use Pandas though it is not necessary at all.\n\nFeel free to experiment and share your thought on this repo by contributing to it! \n\n## Getting started\n\nThe [notebooks](notebooks) section provides some examples. For instance, just run:\n\n```python\nimport tensorflow as tf\nimport tensorflow_datasets as tfds\nfrom tensorflow.keras.models import Sequential\n\nfrom keras_fsl.models.encoders import BasicCNN\nfrom keras_fsl.layers import GramMatrix\nfrom keras_fsl.losses.gram_matrix_losses import BinaryCrossentropy\nfrom keras_fsl.metrics.gram_matrix_metrics import classification_accuracy, min_eigenvalue\nfrom keras_fsl.utils.tensors import get_dummies\n\n\n#%% Get data\ntrain_dataset, val_dataset, test_dataset = [\n    dataset.shuffle(1024).batch(64).map(lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), get_dummies(y)[0]))\n    for dataset in tfds.load(name=\"omniglot\", split=[\"train[:90%]\", \"train[90%:]\", \"test\"], as_supervised=True)\n]\ninput_shape = next(tfds.as_numpy(train_dataset.take(1)))[0].shape[1:]  # first shape is batch_size\n\n#%% Training\nencoder = BasicCNN(input_shape=input_shape)\nsupport_layer = GramMatrix(kernel=\"DenseSigmoid\")\nmodel = Sequential([encoder, support_layer])\nmodel.compile(optimizer=\"Adam\", loss=BinaryCrossentropy(), metrics=[classification_accuracy(), min_eigenvalue])\nmodel.fit(train_dataset, validation_data=val_dataset, epochs=5)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FClementWalter%2FKeras-FewShotLearning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FClementWalter%2FKeras-FewShotLearning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FClementWalter%2FKeras-FewShotLearning/lists"}