Exemple #1
0
    def generate_model(self, **kwargs):
        nfilters_embeded = int(kwargs.get("nfilters_embeded", 1))
        nlayers_embeded = int(kwargs.get("nlayers_embeded", 1))
        nfilters_cloud = int(kwargs.get("nfilters_cloud", 1))
        nlayers_cloud = int(kwargs.get("nlayers_cloud", 1))

        model = Sequential()
        for i in range(nlayers_embeded):
            if i == 0:
                nfilters = self.input_dims
            else:
                nfilters = nfilters_embeded
            model.add(Convolution2D(nfilters, nfilters_embeded, 3, 1, 1))
            model.add(max_pooling_2d(3, 1, 1))
            model.add(BatchNormalization(nfilters_embeded))
            model.add(Activation('bst'))

        branch = Sequential()
        branch.add(Linear(None, self.output_dims))
        branch.add(BatchNormalization(self.output_dims))

        model.add(branch)

        # float branch
        for i in range(nlayers_cloud):
            if i == 0:
                nfilters = nfilters_embeded
            else:
                nfilters = nfilters_cloud
            model.add(Convolution2D(nfilters, nfilters_cloud, 3, 1, 1))
            model.add(max_pooling_2d(3, 1, 1))
            model.add(BatchNormalization(nfilters_cloud))
            model.add(Activation('bst'))
            # Note: should we move pool to before batch norm like in binary?
        model.add(Linear(None, self.output_dims))
        model.add(BatchNormalization(self.output_dims))

        model.build()
        return model
    def generate_model(self, **kwargs):
        nfilters_embeded = int(kwargs.get("nfilters_embeded", 1))
        nlayers_embeded = int(kwargs.get("nlayers_embeded", 1))
        nfilters_cloud = int(kwargs.get("nfilters_cloud", 1))
        nlayers_cloud = int(kwargs.get("nlayers_cloud", 1))

        for key, value in kwargs.iteritems():
            print key, value

        input_model = Sequential()
        for i in range(nlayers_embeded):
            if i == 0:
                nfilters = self.input_dims
                input_model.add(
                    ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1,
                                  1))
            else:
                nfilters = nfilters_embeded
                input_model.add(
                    BinaryConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3,
                                        1, 1))
        branch = Sequential()
        branch.add(BinaryLinearBNSoftmax(None, self.output_dims))
        input_model.add(branch)

        model = MultiInputSequential(self.ninputs)
        for i in range(self.ninputs):
            model.add_input(input_model)

        # float branch
        for i in range(nlayers_cloud):
            if i == 0:
                nfilters = self.ninputs * nfilters_embeded
            else:
                nfilters = nfilters_cloud
            model.add(Convolution2D(nfilters, nfilters_cloud, 3, 1, 1))
            model.add(max_pooling_2d(3, 1, 1))
            model.add(BatchNormalization(nfilters_cloud))
            model.add(Activation('bst'))
            # Note: should we move pool to before batch norm like in binary?
        model.add(Linear(None, self.output_dims))
        model.add(BatchNormalization(self.output_dims))

        model.build()
        return model
Exemple #3
0
from chainer import functions as F
#import chainer.computational_graph as c
from chainer import computational_graph

folder = "_models/test_seq"

nfilters_embeded = int(2)
nlayers_embeded = int(1)
nfilters_cloud = int(2)
nlayers_cloud = int(1)
branchweight = int(3)
lr = numpy.float64(0.001)
nepochs = int(10)
name = str("_lr_0.001_nfilters_embeded_2_nlayers_embeded_1.0")

model = Sequential()

nfilters = 1
model.add(ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1, 1))
#model.add(ConvBNBST(nfilters, nfilters_embeded, 3, 1, 1))
nfilters = nfilters_embeded
model.add(BinaryConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1, 1))
#model.add(BinaryConvBNBST(nfilters, nfilters_embeded, 3, 1, 1))


branch = Sequential()
branch.add(BinaryLinearBNSoftmax(None, 10))
model.add(branch)

############################ Embeded network ##############################
folder = "models/test_cifar10"

nfilters_embeded = int(32)
nlayers_embeded = int(1)
nfilters_cloud = int(32)
nlayers_cloud = int(2)
branchweight = float(0.1)
lr = numpy.float64(0.001)
nepochs = int(100)
ent_T = numpy.float64(100)
name = str("cifar10_32f_nb_good_100")

input_dims = 3
output_dims = 10

model = Sequential()
nfilters = input_dims
model.add(ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1, 1))

branch = Sequential()
branch.add(BinaryLinearBNSoftmax(None, output_dims))
model.add(branch)

for i in range(nlayers_cloud):
    if i == 0:
        nfilters = nfilters_embeded
    else:
        nfilters = nfilters_cloud
    model.add(Convolution2D(nfilters, nfilters_cloud, 3, 1, 1))
    model.add(Activation('relu'))
    model.add(Convolution2D(nfilters, nfilters_cloud, 3, 1, 1))
Exemple #5
0
    def generate_model(self, **kwargs):
        nfilters_embeded = int(kwargs.get("nfilters_embeded", 1))
        nlayers_embeded = int(kwargs.get("nlayers_embeded", 1))
        nfilters_cloud = int(kwargs.get("nfilters_cloud", 1))
        nlayers_cloud = int(kwargs.get("nlayers_cloud", 1))

        model = Sequential()
        for i in range(nlayers_embeded):
            if i == 0:
                nfilters = self.input_dims
                model.add(
                    ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1,
                                  1))
            else:
                nfilters = nfilters_embeded
                model.add(
                    BinaryConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3,
                                        1, 1))

        branch = Sequential()
        branch.add(BinaryLinearBNSoftmax(None, self.output_dims))
        model.add(branch)

        for i in range(nlayers_cloud):
            if i == 0:
                nfilters = nfilters_embeded
            else:
                nfilters = nfilters_cloud
            model.add(Convolution2D(nfilters, nfilters_cloud, 3, 1, 1))
            model.add(BatchNormalization(nfilters_cloud))
            model.add(Activation('relu'))
            model.add(max_pooling_2d(3, 1, 1))
        model.add(Linear(None, self.output_dims))
        model.build()
        return model
Exemple #6
0
    def generate_model(self, **kwargs):
        nfilters_embeded_last = int(kwargs.get("nfilters_embeded_last", 1))
        nfilters_embeded = int(kwargs.get("nfilters_embeded", 1))
        nlayers_embeded = int(kwargs.get("nlayers_embeded", 1))

        input_model = Sequential()
        for i in range(nlayers_embeded):
            if i == 0:
                nfilters = self.input_dims
                input_model.add(
                    ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1,
                                  1))
            elif i == nlayers_embeded - 1:
                nfilters = nfilters_embeded
                input_model.add(
                    BinaryConvPoolBNBST(nfilters, nfilters_embeded_last, 3, 1,
                                        1, 3, 1, 1))
            else:
                nfilters = nfilters_embeded
                input_model.add(
                    BinaryConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3,
                                        1, 1))
        input_model.add(BinaryLinearBNSoftmax(None, self.output_dims))

        input_model.build()
        return input_model
Exemple #7
0
from chainer_sequential.sequential import Sequential
from chainer_sequential.function import *
from chainer_sequential.link import *
from chainer_sequential.binary_link import *
from chainer import functions as F

folder = "_models/test_seq"

nfilters_embeded = int(2)
nlayers_embeded = int(1)
branchweight = int(3)
lr = numpy.float64(0.001)
nepochs = int(2)
name = str("_lr_0.001_nfilters_embeded_2_nlayers_embeded_1.0")

model = Sequential()

nfilters = 1
model.add(ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1, 1))
nfilters = nfilters_embeded
model.add(BinaryConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 1, 1))
model.add(BinaryLinearBNSoftmax(None, 10))
model.build()

chain = Chain(branchweight=branchweight)
chain.add_sequence(model)
chain.setup_optimizers('adam', lr)

trainset, testset = chainer.datasets.get_mnist(ndim=3)

trainer = Trainer('{}/{}'.format(folder, name),
    def generate_model(self, **kwargs):
        nfilters_embeded_last = int(kwargs.get("nfilters_embeded_last", 1))
        nfilters_embeded = int(kwargs.get("nfilters_embeded", 1))
        nlayers_embeded = int(kwargs.get("nlayers_embeded", 1))
        nfilters_cloud = int(kwargs.get("nfilters_cloud", 1))
        nlayers_cloud = int(kwargs.get("nlayers_cloud", 1))
        nfilters_edge = int(kwargs.get("nfilters_edge", 1))
        nlayers_edge = int(kwargs.get("nlayers_edge", 1))

        input_model = Sequential()
        if nlayers_embeded == 1:
            nfilters_embeded_last = nfilters_embeded
        for i in range(nlayers_embeded):
            if i == 0:
                nfilters = self.input_dims
                input_model.add(
                    ConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3, 2,
                                  1))
            elif i == nlayers_embeded - 1:
                nfilters = nfilters_embeded
                input_model.add(
                    BinaryConvPoolBNBST(nfilters, nfilters_embeded_last, 3, 1,
                                        1, 3, 2, 1))
            else:
                nfilters = nfilters_embeded
                input_model.add(
                    BinaryConvPoolBNBST(nfilters, nfilters_embeded, 3, 1, 1, 3,
                                        2, 1))
        branch = Sequential()
        branch.add(BinaryLinearBNSoftmax(None, self.output_dims))
        input_model.add(branch)

        model = MultiInputSequential(self.ninputs,
                                     merge_function=self.merge_function)
        for i in range(self.ninputs):
            model.add_input(input_model)

        # Local branch
        local_branch = Sequential()
        #local_branch.add(Linear(None, self.output_dims))
        #local_branch.add(BatchNormalization(self.output_dims))
        if self.merge_function in [
                'concat', 'concat_avg_pool', 'concat_max_pool'
        ]:
            local_branch.add(BinaryLinearBNSoftmax(None, self.output_dims))
        model.add_local(local_branch)

        # Edge branches
        #for i in range(nlayers_edge):
        #    if i == 0:
        #        if 'concat' in self.merge_function:
        #            nfilters = self.ninputs*nfilters_embeded_last
        #        else:
        #            nfilters = nfilters_embeded_last
        #    else:
        #        nfilters = nfilters_edge
        #    model.add(BinaryConvPoolBNBST(nfilters, nfilters_edge, 3, 1, 1, 3, 1, 1))
        #
        #    #model.add(Convolution2D(nfilters, nfilters_edge, 3, 1, 1))
        #    #model.add(Activation('relu'))
        #    #model.add(max_pooling_2d(3,1,1))
        #    #model.add(BatchNormalization(nfilters_edge))
        #    #model.add(Activation('relu'))
        #    # Note: should we move pool to before batch norm like in binary?
        #
        #edge_branch = Sequential()
        ##edge_branch.add(Linear(None, self.output_dims))
        ##edge_branch.add(BatchNormalization(self.output_dims))
        #edge_branch.add(BinaryLinearBNSoftmax(None, self.output_dims))
        #model.add(edge_branch)

        # Cloud branches
        for i in range(nlayers_cloud):
            if i == 0:
                if self.merge_function in [
                        'concat', 'avg_pool_concat', 'max_pool_concat'
                ]:
                    nfilters = self.ninputs * nfilters_embeded_last
                else:
                    nfilters = nfilters_embeded_last
                if self.drop_comm_train > 0:
                    model.add(dropout_comm_train(self.drop_comm_train))
                if self.drop_comm_test > 0:
                    model.add(dropout_comm_test(self.drop_comm_test))
            else:
                nfilters = nfilters_cloud
            model.add(
                BinaryConvPoolBNBST(nfilters, nfilters_cloud, 3, 1, 1, 3, 1,
                                    1))

            #model.add(Convolution2D(nfilters, nfilters_cloud, 3, 1, 1))
            #model.add(Activation('relu'))
            #model.add(max_pooling_2d(3,1,1))
            #model.add(BatchNormalization(nfilters_cloud))
            #model.add(Activation('relu'))
            # Note: should we move pool to before batch norm like in binary?
        #model.add(Linear(None, self.output_dims))
        #model.add(BatchNormalization(self.output_dims))
        model.add(BinaryLinearBNSoftmax(None, self.output_dims))

        model.build()
        return model