This repository was archived by the owner on Mar 24, 2021. It is now read-only.
forked from qqwweee/keras-yolo3
-
Notifications
You must be signed in to change notification settings - Fork 10
Add LocalResponseNorm and BatchNorm in training #15
Open
jatinmandav
wants to merge
4
commits into
Borda:master
Choose a base branch
from
jatinmandav:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,118 @@ | ||
| from keras.layers.core import Layer | ||
| from keras import backend as K | ||
|
|
||
|
|
||
| class LocalResponseNorm(Layer): | ||
| """ | ||
| LocalResponseNorm Layer Definition in Keras | ||
|
|
||
| :param float alpha: | ||
| :param int k: | ||
| :param float beta: | ||
| :param int n: | ||
| :param **kwargs of Layer Class from Keras: | ||
|
|
||
| This code is adapted from pylearn2. | ||
| License at: https://github.com/lisa-lab/pylearn2/blob/master/LICENSE.txt | ||
| """ | ||
| def __init__(self, alpha=0.0001, k=1, beta=0.75, n=5, **kwargs): | ||
| self.alpha = alpha | ||
| self.k = k | ||
| self.beta = beta | ||
| self.n = n | ||
| super(LocalResponseNorm, self).__init__(**kwargs) | ||
|
|
||
| def call(self, x, mask=None): | ||
| b, ch, r, c = x.shape | ||
| half_n = self.n // 2 | ||
| input_sqr = K.square(x) | ||
|
|
||
| extra_channels = K.zeros((b, int(ch) + 2 * half_n, r, c)) | ||
| input_sqr = K.concatenate( | ||
| [ | ||
| extra_channels[:, :half_n, :, :], | ||
| input_sqr, | ||
| extra_channels[:, half_n + int(ch):, :, :] | ||
| ], | ||
| axis=1 | ||
| ) | ||
|
|
||
| scale = self.k # offset for the scale | ||
| norm_alpha = self.alpha / self.n # normalized alpha | ||
| for i in range(self.n): | ||
| scale += norm_alpha * input_sqr[:, i:i + int(ch), :, :] | ||
| scale = scale ** self.beta | ||
| x = x / scale | ||
| return x | ||
|
|
||
| def get_config(self): | ||
| config = {"alpha": self.alpha, | ||
| "k": self.k, | ||
| "beta": self.beta, | ||
| "n": self.n} | ||
| base_config = super(LocalResponseNorm, self).get_config() | ||
| return dict(list(base_config.items()) + list(config.items())) | ||
|
|
||
|
|
||
| class LocalResponseNorm2D(LocalResponseNorm): | ||
| """ | ||
| LocalResponseNorm2D Layer Definition in Keras | ||
|
|
||
| :param floatalpha: | ||
| :param int k: | ||
| :param float beta: | ||
| :param int n: | ||
| :param **kwargs of Layer Class from Keras: | ||
|
|
||
| This code is adapted from pylearn2. | ||
| License at: https://github.com/lisa-lab/pylearn2/blob/master/LICENSE.txt | ||
| """ | ||
| def __init__(self, alpha=1e-4, k=2, beta=0.75, n=5, **kwargs): | ||
| if n % 2 == 0: | ||
| raise NotImplementedError( | ||
| """LocalResponseNorm2D only works | ||
| with odd n. n provided: """ + str(n)) | ||
| super(LocalResponseNorm2D, self).__init__(**kwargs) | ||
| self.alpha = alpha | ||
| self.k = k | ||
| self.beta = beta | ||
| self.n = n | ||
|
|
||
| def get_output(self, train): | ||
| X = self.get_input(train) | ||
jatinmandav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| b, ch, r, c = K.shape(X) | ||
| half_n = self.n // 2 | ||
| input_sqr = K.square(X) | ||
| extra_channels = K.zeros((b, ch + 2 * half_n, r, c)) | ||
| input_sqr = K.concatenate([extra_channels[:, :half_n, :, :], | ||
| input_sqr, | ||
| extra_channels[:, half_n + ch:, :, :]], | ||
| axis=1) | ||
| scale = self.k | ||
| for i in range(self.n): | ||
| scale += self.alpha * input_sqr[:, i:i + ch, :, :] | ||
| scale = scale ** self.beta | ||
| return X / scale | ||
|
|
||
| def get_config(self): | ||
| config = {"name": self.__class__.__name__, | ||
| "alpha": self.alpha, | ||
| "k": self.k, | ||
| "beta": self.beta, | ||
| "n": self.n} | ||
| base_config = super(LocalResponseNorm2D, self).get_config() | ||
| return dict(list(base_config.items()) + list(config.items())) | ||
|
|
||
|
|
||
| class PoolHelper(Layer): | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super(PoolHelper, self).__init__(**kwargs) | ||
|
|
||
| def call(self, x, mask=None): | ||
| return x[:, :, 1:, 1:] | ||
|
|
||
| def get_config(self): | ||
| config = {} | ||
| base_config = super(PoolHelper, self).get_config() | ||
| return dict(list(base_config.items()) + list(config.items())) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.