Skip to main content

Octave convolution

Project description

Keras Octave Conv

Unofficial implementation of Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution.

Install

pip install keras-octave-conv

Usage

The OctaveConv2D layer could be used just like the Conv2D layer, except the padding argument is forced to be 'same'.

First Octave

Use a single input for the first octave layer:

from keras.layers import Input
from keras_octave_conv import OctaveConv2D

inputs = Input(shape=(32, 32, 3))
high, low = OctaveConv2D(filters=16, kernel_size=3, octave=2, ratio_out=0.125)(inputs)

The two outputs represent the results in higher and lower spatial resolutions.

Special arguments:

  • octave: default is 2. The division of the spatial dimensions.
  • ratio_out: default is 0.5. The ratio of filters for lower spatial resolution.

Intermediate Octave

The intermediate octave layers takes two inputs and produce two outputs:

from keras.layers import Input, MaxPool2D
from keras_octave_conv import OctaveConv2D

inputs = Input(shape=(32, 32, 3))
high, low = OctaveConv2D(filters=16, kernel_size=3)(inputs)

high, low = MaxPool2D()(high), MaxPool2D()(low)
high, low = OctaveConv2D(filters=8, kernel_size=3)([high, low])

Note that the same octave value should be used throughout the whole model.

Last Octave

Set ratio_out to 0.0 to get a single output for further processing:

from keras.layers import Input, MaxPool2D, Flatten, Dense
from keras.models import Model
from keras_octave_conv import OctaveConv2D

inputs = Input(shape=(32, 32, 3))
high, low = OctaveConv2D(filters=16, kernel_size=3)(inputs)

high, low = MaxPool2D()(high), MaxPool2D()(low)
high, low = OctaveConv2D(filters=8, kernel_size=3)([high, low])

high, low = MaxPool2D()(high), MaxPool2D()(low)
conv = OctaveConv2D(filters=4, kernel_size=3, ratio_out=0.0)([high, low])

flatten = Flatten()(conv)
outputs = Dense(units=10, activation='softmax')(flatten)

model = Model(inputs=inputs, outputs=outputs)
model.summary()

Utility

octave_dual helps to create dual layers for processing the outputs of octave convolutions:

from keras.layers import Input, MaxPool2D, Flatten, Dense
from keras.models import Model
from keras_octave_conv import OctaveConv2D, octave_dual

inputs = Input(shape=(32, 32, 3))
conv = OctaveConv2D(filters=16, kernel_size=3)(inputs)

pool = octave_dual(conv, MaxPool2D())
conv = OctaveConv2D(filters=8, kernel_size=3)(pool)

pool = octave_dual(conv, MaxPool2D())
conv = OctaveConv2D(filters=4, kernel_size=3, ratio_out=0.0)(pool)

flatten = Flatten()(conv)
outputs = Dense(units=10, activation='softmax')(flatten)

model = Model(inputs=inputs, outputs=outputs)
model.summary()

octave_conv_2d creates the octave structure with built-in Keras layers:

from keras.layers import Input, MaxPool2D, Flatten, Dense
from keras.models import Model
from keras.utils import plot_model
from keras_octave_conv import octave_conv_2d, octave_dual

inputs = Input(shape=(32, 32, 3), name='Input')
conv = octave_conv_2d(inputs, filters=16, kernel_size=3, name='Octave-First')

pool = octave_dual(conv, MaxPool2D(name='Pool-1'))
conv = octave_conv_2d(pool, filters=8, kernel_size=3, name='Octave-Mid')

pool = octave_dual(conv, MaxPool2D(name='Pool-2'))
conv = octave_conv_2d(pool, filters=4, kernel_size=3, ratio_out=0.0, name='Octave-Last')

flatten = Flatten(name='Flatten')(conv)
outputs = Dense(units=10, activation='softmax', name='Output')(flatten)

model = Model(inputs=inputs, outputs=outputs)
model.summary()
plot_model(model, to_file='octave_model.png')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keras-octave-conv-0.11.0.tar.gz (109.8 kB view details)

Uploaded Source

File details

Details for the file keras-octave-conv-0.11.0.tar.gz.

File metadata

  • Download URL: keras-octave-conv-0.11.0.tar.gz
  • Upload date:
  • Size: 109.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for keras-octave-conv-0.11.0.tar.gz
Algorithm Hash digest
SHA256 ace18a69892087d2016f22e38a05800f51e16057509ea00d137eb2fcbfbd8b77
MD5 84b918eea3766171f525b255c11c4ef4
BLAKE2b-256 d78f4d0b9a80de4499a0c814d8d8e860c2b0dcc1de9b58722aed73d1283f6597

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page