-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from bmcfee/tf2
updated for keras 2.2+, tf2
- Loading branch information
Showing
19 changed files
with
497 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: test | ||
channels: | ||
- conda-forge | ||
- defaults | ||
dependencies: | ||
# required | ||
- keras>=2.2 | ||
- tensorflow>=2.0 | ||
- numpy >= 1.17 | ||
- pytest-cov | ||
- pytest-faulthandler | ||
- pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: True | ||
|
||
jobs: | ||
test: | ||
name: "Python ${{ matrix.python-version }} on ${{ matrix.os }}" | ||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- os: ubuntu-latest | ||
python-version: "3.7" | ||
channel-priority: "strict" | ||
envfile: ".github/environment-ci.yml" | ||
|
||
- os: ubuntu-latest | ||
python-version: "3.8" | ||
channel-priority: "strict" | ||
envfile: ".github/environment-ci.yml" | ||
|
||
- os: ubuntu-latest | ||
python-version: "3.9" | ||
channel-priority: "strict" | ||
envfile: ".github/environment-ci.yml" | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Cache conda | ||
uses: actions/cache@v2 | ||
env: | ||
# Increase this value to reset cache if etc/example-environment.yml has not changed | ||
CACHE_NUMBER: 1 | ||
with: | ||
path: ~/conda_pkgs_dir | ||
key: ${{ runner.os }}-${{ matrix.python-version }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles( matrix.envfile ) }} | ||
|
||
- name: Install Conda environment | ||
uses: conda-incubator/setup-miniconda@v2 | ||
with: | ||
auto-update-conda: true | ||
python-version: ${{ matrix.python-version }} | ||
add-pip-as-python-dependency: true | ||
auto-activate-base: false | ||
activate-environment: test | ||
channel-priority: ${{ matrix.channel-priority }} | ||
environment-file: ${{ matrix.envfile }} | ||
use-only-tar-bz2: true # IMPORTANT: This needs to be set for caching to work properly! | ||
|
||
- name: Conda info | ||
shell: bash -l {0} | ||
run: | | ||
conda info -a | ||
conda list | ||
- name: Install autopool | ||
shell: bash -l {0} | ||
run: python -m pip install --upgrade-strategy only-if-needed -e .[tests] | ||
|
||
- name: Run pytest | ||
shell: bash -l {0} | ||
run: pytest -v | ||
|
||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
files: ./coverage.xml | ||
directory: ./coverage/reports/ | ||
flags: unittests | ||
env_vars: OS,PYTHON | ||
name: codecov-umbrella | ||
fail_ci_if_error: true | ||
verbose: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
build-n-publish: | ||
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@master | ||
- name: Set up Python 3.7 | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: 3.7 | ||
|
||
- name: Install pypa/build | ||
run: >- | ||
python -m | ||
pip install | ||
build | ||
--user | ||
- name: Build a binary wheel and a source tarball | ||
run: >- | ||
python -m | ||
build | ||
--sdist | ||
--wheel | ||
--outdir dist/ | ||
. | ||
- name: Publish distribution 📦 to PyPI | ||
if: startsWith(github.ref, 'refs/tags') | ||
uses: pypa/gh-action-pypi-publish@master | ||
with: | ||
password: ${{ secrets.PYPI_API_TOKEN }} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python | ||
'''Autopool: Adaptive pooling operators for multiple instance learning''' | ||
|
||
from tensorflow.keras import backend as K | ||
from tensorflow.keras.layers import Layer, InputSpec | ||
from tensorflow.keras import initializers | ||
from tensorflow.keras import constraints | ||
from tensorflow.keras import regularizers | ||
|
||
|
||
class AutoPool1D(Layer): | ||
'''Automatically tuned soft-max pooling. (tensorflow.keras implementation) | ||
This layer automatically adapts the pooling behavior to interpolate | ||
between mean- and max-pooling for each dimension. | ||
''' | ||
def __init__(self, axis=0, | ||
kernel_initializer='zeros', | ||
kernel_constraint=None, | ||
kernel_regularizer=None, | ||
**kwargs): | ||
''' | ||
Parameters | ||
---------- | ||
axis : int | ||
Axis along which to perform the pooling. By default 0 (should be time). | ||
kernel_initializer: Initializer for the weights matrix | ||
kernel_regularizer: Regularizer function applied to the weights matrix | ||
kernel_constraint: Constraint function applied to the weights matrix | ||
kwargs | ||
''' | ||
|
||
if 'input_shape' not in kwargs and 'input_dim' in kwargs: | ||
kwargs['input_shape'] = (kwargs.pop('input_dim'), ) | ||
|
||
super(AutoPool1D, self).__init__(**kwargs) | ||
|
||
self.axis = axis | ||
self.kernel_initializer = initializers.get(kernel_initializer) | ||
self.kernel_constraint = constraints.get(kernel_constraint) | ||
self.kernel_regularizer = regularizers.get(kernel_regularizer) | ||
self.input_spec = InputSpec(min_ndim=3) | ||
self.supports_masking = True | ||
|
||
def build(self, input_shape): | ||
assert len(input_shape) >= 3 | ||
input_dim = input_shape[-1] | ||
|
||
self.kernel = self.add_weight(shape=(1, input_dim), | ||
initializer=self.kernel_initializer, | ||
name='kernel', | ||
regularizer=self.kernel_regularizer, | ||
constraint=self.kernel_constraint) | ||
self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim}) | ||
self.built = True | ||
|
||
def compute_output_shape(self, input_shape): | ||
return self.get_output_shape_for(input_shape) | ||
|
||
def get_output_shape_for(self, input_shape): | ||
shape = list(input_shape) | ||
del shape[self.axis] | ||
return tuple(shape) | ||
|
||
def get_config(self): | ||
config = {'kernel_initializer': initializers.serialize(self.kernel_initializer), | ||
'kernel_constraint': constraints.serialize(self.kernel_constraint), | ||
'kernel_regularizer': regularizers.serialize(self.kernel_regularizer), | ||
'axis': self.axis} | ||
|
||
base_config = super(AutoPool1D, self).get_config() | ||
return dict(list(base_config.items()) + list(config.items())) | ||
|
||
def call(self, x, mask=None): | ||
weights = K.softmax(self.kernel * x, axis=self.axis) | ||
return K.sum(x * weights, axis=self.axis, keepdims=False) | ||
|
||
|
||
class SoftMaxPool1D(Layer): | ||
''' | ||
Tensorflow-keras softmax pooling layer. | ||
''' | ||
|
||
def __init__(self, axis=0, **kwargs): | ||
''' | ||
Parameters | ||
---------- | ||
axis : int | ||
Axis along which to perform the pooling. By default 0 | ||
(should be time). | ||
kwargs | ||
''' | ||
super(SoftMaxPool1D, self).__init__(**kwargs) | ||
|
||
self.axis = axis | ||
|
||
def get_output_shape_for(self, input_shape): | ||
shape = list(input_shape) | ||
del shape[self.axis] | ||
return tuple(shape) | ||
|
||
def compute_output_shape(self, input_shape): | ||
return self.get_output_shape_for(input_shape) | ||
|
||
def call(self, x, mask=None): | ||
weights = K.softmax(x, axis=self.axis) | ||
return K.sum(x * weights, axis=self.axis, keepdims=False) | ||
|
||
def get_config(self): | ||
config = {'axis': self.axis} | ||
base_config = super(SoftMaxPool1D, self).get_config() | ||
return dict(list(base_config.items()) + list(config.items())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.