Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Rishit-dagli/GLOM-TensorFlow

An attempt at the implementation of GLOM, Geoffrey Hinton's paper for emergent part-whole hierarchies from data
https://github.com/Rishit-dagli/GLOM-TensorFlow

capsule-neural-networks convolutional-neural-networks deep-learning embedding geoffrey-hinton glom keras keras-tensorflow machine-learning neural-network representation-learning tensorflow tensorflow2

Last synced: about 2 months ago
JSON representation

An attempt at the implementation of GLOM, Geoffrey Hinton's paper for emergent part-whole hierarchies from data

Awesome Lists containing this project

README

        

# GLOM TensorFlow [![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2FRishit-dagli%2FGLOM-TensorFlow)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2FRishit-dagli%2FGLOM-TensorFlow)

![PyPI](https://img.shields.io/pypi/v/glom-tf)
[![Flake8 Lint](https://github.com/Rishit-dagli/GLOM-TensorFlow/actions/workflows/flake8-lint.yml/badge.svg)](https://github.com/Rishit-dagli/GLOM-TensorFlow/actions/workflows/flake8-lint.yml)
[![Upload Python Package](https://github.com/Rishit-dagli/GLOM-TensorFlow/actions/workflows/python-publish.yml/badge.svg)](https://github.com/Rishit-dagli/GLOM-TensorFlow/actions/workflows/python-publish.yml)
![Python Version](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9-blue)

[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/Rishit-dagli/GLOM-TensorFlow/HEAD)
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Rishit-dagli/GLOM-TensorFlow/blob/main/examples/glom_example.ipynb)

[![GitHub license](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![PEP8](https://img.shields.io/badge/code%20style-pep8-orange.svg)](https://www.python.org/dev/peps/pep-0008/)
[![GitHub stars](https://img.shields.io/github/stars/Rishit-dagli/GLOM-TensorFlow?style=social)](https://github.com/Rishit-dagli/GLOM-TensorFlow/stargazers)
[![GitHub followers](https://img.shields.io/github/followers/Rishit-dagli?label=Follow&style=social)](https://github.com/Rishit-dagli)
[![Twitter Follow](https://img.shields.io/twitter/follow/rishit_dagli?style=social)](https://twitter.com/intent/follow?screen_name=rishit_dagli)

This Python package attempts to implement GLOM in TensorFlow, which allows advances made by several different groups
transformers, neural fields, contrastive representation learning, distillation and capsules to be combined. This was
suggested by Geoffrey Hinton in his paper
["How to represent part-whole hierarchies in a neural network"](https://arxiv.org/abs/2102.12627).

Further, [Yannic Kilcher's video](https://youtu.be/cllFzkvrYmE) and [Phil Wang's repo](https://github.com/lucidrains/glom-pytorch)
was very helpful for me to implement this project.

## Installation

Run the following to install:

```sh
pip install glom-tf
```

## Developing `glom-tf`

To install `glom-tf`, along with tools you need to develop and test, run the following in your virtualenv:

```sh
git clone https://github.com/Rishit-dagli/GLOM-TensorFlow.git
# or clone your own fork

cd GLOM-TensorFlow
pip install -e .[dev]
```

## A bit about GLOM

The GLOM architecture is composed of a large number of columns which
all use exactly the same weights. Each column is a stack of spatially local
autoencoders that learn multiple levels of representation for what is happening
in a small image patch. Each autoencoder transforms the embedding at one level
into the embedding at an adjacent level using a multilayer bottom-up encoder
and a multilayer top-down decoder. These levels correspond to the levels in a
part-whole hierarchy.

![](images/interactions.png)


Interactions among the 3 levels in one column

An example shared by the author was as an example when show a face image, a single column might converge on embedding
vectors representing a nostril, a nose, a face, and a person.

At each discrete time and in each column separately, the embedding at a
level is updated to be the weighted average of:
- bottom-up neural net acting on the embedding at the level below at the previous time
- top-down neural net acting on the embedding at the level above at the previous time
- embedding vector at the previous time step
- attention-weighted average of the embeddings at the same level in nearby columns at the previous time

For a static image, the embeddings at a level should settle down over time to produce similar vectors.

![](images/embeddings.png)


A picture of the embeddings at a particular time

## Usage

```python
from glomtf import Glom

model = Glom(dim = 512,
levels = 5,
image_size = 224,
patch_size = 14)

img = tf.random.normal([1, 3, 224, 224])
levels = model(img, iters = 12) # (1, 256, 5, 12)
# 1 - batch
# 256 - patches
# 5 - levels
# 12 - dimensions
```

Use the `return_all = True` argument to get all the column and level states per iteration. This also gives you access
to all the level data across iterations for clustering, from which you can inspect the islands too.

```python
from glomtf import Glom

model = Glom(dim = 512,
levels = 5,
image_size = 224,
patch_size = 14)

img = tf.random.normal([1, 3, 224, 224])
all_levels = model(img, iters = 12, return_all = True) # (13, 1, 256, 5, 12)
# 13 - time

# top level outputs after iteration 6
top_level_output = all_levels[7, :, :, -1] # (1, 256, 512)
# 1 - batch
# 256 - patches
# 512 - dimensions
```

## Want to Contribute 🙋‍♂️?

Awesome! If you want to contribute to this project, you're always welcome! See [Contributing Guidelines](CONTRIBUTING.md). You can also take a look at [open issues](https://github.com/Rishit-dagli/GLOM-TensorFlow/issues) for getting more information about current or upcoming tasks.

## Want to discuss? 💬

Have any questions, doubts or want to present your opinions, views? You're always welcome. You can [start discussions](https://github.com/Rishit-dagli/GLOM-TensorFlow/discussions).

## Citations

```bibtex
@misc{hinton2021represent,
title = {How to represent part-whole hierarchies in a neural network},
author = {Geoffrey Hinton},
year = {2021},
eprint = {2102.12627},
archivePrefix = {arXiv},
primaryClass = {cs.CV}
}
```

## License

```
Copyright 2020 Rishit Dagli

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```