Beispiel #1
0
    def set_RFs(self,
                eRF,
                layer,
                eSRF=None,
                iSRF=None,
                SSN=None,
                SSF=None,
                V1_CRF=0.26,
                V1_neCRF=0.54,
                V1_feCRF=1.41,
                default_stride=1,
                rf_calculation='pixel_wise',
                padding=1):
        """Set RF sizes for the normalizations.

        Based on calculation of an effective RF (i.e. not
        simply kernel size of the current layer, but instead
        w.r.t. input).

        Angelucci & Shushruth 2013 V1 RFs:
        https://www.shadlenlab.columbia.edu/people/Shushruth/Lab_Page/Home_files/GalleyProof.pdf
        CRF = 0.26 degrees (1x)
        eCRF near = 0.54 degrees (2x)
        eCRF far = 1.41 degrees (5.5x)

        Implementation is to calculate the RF of a computational unit in
        an activity tensor. Then, near and far eCRFs are derived relative
        to the CRF size. This means that the *filter size* for the CRF is 1
        tensor pixel. And the eRFs are deduced as the appropriate filter size
        for their calculated RFs.

        For instance, units in pool_2 of VGG16 have RFs of 16x16 pixels of
        the input image. Setting the CRF filter size to 1, this means that the
        near eCRF filter size must capture an RF of ~ 32x32 pixels, and the
        far eCRF filter size must capture an RF of ~ 80x80 pixels. The eRF
        calculator can deduce these filter sizes.
        """
        if 'hardcoded_erfs' in layer.keys():
            # Use specified effective receptive fields
            self.SRF = layer['hardcoded_erfs']['SRF']
            if 'CRF_excitation' in layer['hardcoded_erfs'].keys():
                self.CRF_excitation = layer['hardcoded_erfs']['CRF_excitation']
            else:
                self.CRF_excitation = self.SRF
            if 'CRF_inhibition' in layer['hardcoded_erfs'].keys():
                self.CRF_inhibition = layer['hardcoded_erfs']['CRF_inhibition']
            else:
                self.CRF_inhibition = self.SRF
            self.SSN = layer['hardcoded_erfs']['SSN']
            self.SSF = layer['hardcoded_erfs']['SSF']
        else:
            # Calculate effective receptive field for this layer.
            # Adjust eCRF filters to yield appropriate RF sizes.
            eRFc = eRF_calculator()
            conv = {
                'padding': padding,
                'kernel': layer['filter_size'][0],
                'stride': default_stride
            }
            if len(layer['filter_size']) > 1:
                raise RuntimeError(
                    'API not implemented for layers with > 1 module.')

            # Each pixel at this layer corresponds to an input image RF
            # of eRF['r_i'].
            if rf_calculation == 'pixel_wise':
                # self.SRF = 1
                # self.CRF_excitation = 1
                # self.CRF_inhibition = 1
                self.SRF = 7
                self.CRF_excitation = 7
                self.CRF_inhibition = 7
            elif rf_calculation == 'fit_rf':
                # Adjust SRF filter, then adjust eCRFs w.r.t to that.
                self.SRF = eRFc.outFromIn(conv=conv,
                                          layer=eRF,
                                          fix_r_out=eRF['r_i'])
                self.CRF_excitation = eRF['r_i']
                self.CRF_inhibition = eRF['r_i']
            if eSRF is not None:
                self.CRF_excitation
            if iSRF is not None:
                self.CRF_inhibition
            if SSN is None:
                SSN_eRF = py_utils.iceil(eRF['r_i'] * (V1_neCRF / V1_CRF))
                self.SSN = eRFc.outFromIn(conv=conv,
                                          layer=eRF,
                                          fix_r_out=SSN_eRF)
            else:
                self.SSN = SSN
            if SSF is None:
                SSF_eRF = py_utils.iceil(eRF['r_i'] * (V1_feCRF / V1_CRF))
                self.SSF = eRFc.outFromIn(conv=conv,
                                          layer=eRF,
                                          fix_r_out=SSF_eRF)
            else:
                self.SSF = SSF
"""Build DNN models from python dictionaries."""
import json
import numpy as np
import tensorflow as tf
# from models.layers import ff
# from models.layers import pool
from models.layers.ff import ff
from models.layers.activations import activations
from models.layers.normalizations import normalizations
from models.layers.regularizations import regularizations
from ops.eRF_calculator import eRF_calculator

eRF = eRF_calculator()


class model_class(object):
    """Default model class that is generated with a layer_structure dict."""
    def __getitem__(self, name):
        """Get class attributes."""
        return getattr(self, name)

    def __contains__(self, name):
        """Check class attributes."""
        return hasattr(self, name)

    def __init__(self, mean, training, output_size, **kwargs):
        """Set model to trainable/not and pass the mean values."""
        self.var_dict = {}
        self.data_dict = None
        self.regularizations = {}
        self.training = training