Exemplo n.º 1
0
def montecarlo(cause, effect, unknown, n, *ignore):
    cnt_cause = count(zip(cause))
    cnt_unknown = count(zip(unknown))
    cnt_cause_effect = count(zip(cause, effect))
    cnt_effect_unknown = count(zip(effect, unknown))
    sumarr(cnt_cause_effect, 0.1) # make beta dist work with zeros
    sumarr(cnt_cause, 0.1)
    sumarr(cnt_unknown, 0.1)
    sumarr(cnt_effect_unknown, 0.1)
    cnt_cause_unknown = count(zip(cause, unknown))
    rounds = 500
    p_overall = struct(cause_unknown_chain=[[0,0],[0,0]],
                       cause_unknown_collide=[[0,0],[0,0]])
    for i in range(rounds):
        p=struct()
        p.cause = 1-beta(*cnt_cause)
        p.unknown = 1-beta(*cnt_unknown)
        p.effect_given_cause = [1-beta(*cnts) for cnts in cnt_cause_effect]
        p.unknown_given_effect = [1-beta(*cnts) for cnts in cnt_effect_unknown]
        p = get_joints_by_model(p)
        acclarr(p_overall.cause_unknown_chain, p.cause_unknown_chain)
        acclarr(p_overall.cause_unknown_collide, p.cause_unknown_collide)
    mularr(p_overall.cause_unknown_chain, 1.0/rounds)
    mularr(p_overall.cause_unknown_collide, 1.0/rounds)
    try:
        bayes_factor = get_factor(p_overall, cnt_cause_unknown)
    except ValueError:
        print '==ValueError=='
        print p_overall.__dict__
        raise ValueError()
    return struct(bayes_fwd_rev=bayes_factor)
Exemplo n.º 2
0
def mle(cause, effect, unknown, n, p_cause, p_effect_given_cause):
    p=struct(cause=p_cause, effect_given_cause=p_effect_given_cause)
    cnt = count(zip(effect, unknown))
    chi_indep = chi2_contingency(cnt)
    p.unknown_given_effect = [ float(cnt[0][1]) / sum(cnt[0]),
                               float(cnt[1][1]) / sum(cnt[1]) ]
    cnt = count(zip(unknown))
    p.unknown = float(cnt[1]) / sum(cnt)
    p = get_joints_by_model(p)
    cnt = count(zip(cause, unknown))
    bayes_factor = get_factor(p, cnt)
    return struct(reject_indep=chi_indep,
                  bayes_fwd_rev=bayes_factor)
Exemplo n.º 3
0
def chi2_dir(cause, effect, unknown, n, p_cause, p_effect_given_cause):
    cnt = count(zip(effect, unknown))
    #print cnt
    chi_indep = chi2_contingency(cnt)[1]
    p_unknown_given_effect = [ float(cnt[0][1]) / sum(cnt[0]),
                               float(cnt[1][1]) / sum(cnt[1]) ]
    #print 'p(bact|cd)=%s' % p_unknown_given_effect
    exp=[[0,0],[0,0]]
    for c in range(2):
        for e in range(2):
            for u in range(2):
                exp[c][u] += (n * 
                              p_of_val(p_cause, c) *
                              p_of_val(p_effect_given_cause[c], e) *
                              p_of_val(p_unknown_given_effect[e], u))
    cnt = count(zip(cause, unknown))
    #print "obs=%s" % cnt
    #print 'cnt=%s' % cnt
    #print 'expected if cd->bact=%s' % exp
    chi_rev = chisquare(cnt, exp, axis=None, ddof=2)
    chi_fwd = chi2_contingency(cnt)
    #print 'expected if bact->cd=%s' % chi_fwd[3]
    bayes_factor = chi2.pdf(chi_fwd[0],1) / chi2.pdf(chi_rev.statistic,1)
    return struct(reject_indep=chi_indep,
                  bayes_fwd_rev=bayes_factor,
                  reject_fwd=chi_fwd[1],
                  reject_rev=chi_rev.pvalue)
Exemplo n.º 4
0
 def get_caffe_params(l_name):
     layer_params = np.array(net.params[l_name])
     filter = caffe.io.blobproto_to_array(layer_params[0])
     bias = caffe.io.blobproto_to_array(layer_params[1])
     return utils.struct(
         filter=filter,
         bias=bias
     )
Exemplo n.º 5
0
def mnistloader(mnist_path="../MNIST_data"):
    '''
    Args :
        mnist_path - string
            path of mnist folder 
    '''
    mnist = input_data.read_data_sets(mnist_path, one_hot=True)
    train = struct()
    test = struct()
    val = struct()
    train.image = mnist.train.images
    train.label = mnist.train.labels
    test.image = mnist.test.images
    test.label = mnist.test.labels
    val.image = mnist.validation.images
    val.label = mnist.validation.labels
    return train, test, val
Exemplo n.º 6
0
 def get_caffe_params(l_name):
     layer_params = np.array(net.params[l_name])
     filter = caffe.io.blobproto_to_array(layer_params[0])
     bias =  caffe.io.blobproto_to_array(layer_params[1])
     return utils.struct(
         filter=filter,
         bias=bias
     )
def build_model(batch_size=batch_size):
    l_in = nn.layers.InputLayer(shape=(batch_size,)+image.shape)
    l = l_in

    l = conv3(l, num_filters=64)
    l = conv3(l, num_filters=64)

    l = max_pool(l)

    l = conv3(l, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = dnn.Conv2DDNNLayer(l,
                num_filters=4096,
                strides=(1, 1),
                border_mode="valid",
                filter_size=(7,7))
    l = dnn.Conv2DDNNLayer(l,
                num_filters=4096,
                strides=(1, 1),
                border_mode="same",
                filter_size=(1,1))

    l = dnn.Conv2DDNNLayer(l,
                num_filters=n_classes,
                strides=(1,1),
                border_mode="same",
                filter_size=(1,1),
                nonlinearity=None)

    l_to_strengthen = l
    l_out = l

    return utils.struct(
        input=l_in,
        out=l_out,
        to_strengthen=l_to_strengthen)
def build_model(batch_size=batch_size):
    l_in = nn.layers.InputLayer(shape=(batch_size,)+image.shape)
    l = l_in

    l = conv3(l, num_filters=64)
    l = conv3(l, num_filters=64)

    l = max_pool(l)

    l = conv3(l, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = dnn.Conv2DDNNLayer(l,
                num_filters=4096,
                strides=(1, 1),
                border_mode="valid",
                filter_size=(7,7))
    l = dnn.Conv2DDNNLayer(l,
                num_filters=4096,
                strides=(1, 1),
                border_mode="same",
                filter_size=(1,1))

    l = dnn.Conv2DDNNLayer(l,
                num_filters=n_classes,
                strides=(1,1),
                border_mode="same",
                filter_size=(1,1),
                nonlinearity=None)

    l_to_strengthen = l
    l_out = l

    return utils.struct(
        input=l_in,
        out=l_out,
        to_strengthen=l_to_strengthen)
Exemplo n.º 9
0
def vgg16(batch_shape):
    """
    Create a vgg16, with the parameters from http://www.robots.ox.ac.uk/~vgg/research/very_deep/
    See googlenet.py for the method used to convert these caffe parameters to lasagne parameters.
    :param batch_shape: The shape of the input images. This should be of size (N, 3, X>=224, Y>=224). Note flexible
    image size, as the last dense layers have been implemented here with convolutional layers.
    :return: a struct with the input layer, the logit layer (before the final softmax) and the output layer.
    """
    l_in = nn.layers.InputLayer(shape=batch_shape)
    l = l_in

    l = conv3(l, num_filters=64)
    l = conv3(l, num_filters=64)

    l = max_pool(l)

    l = conv3(l, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = dnn.Conv2DDNNLayer(l, num_filters=4096, stride=(1, 1), border_mode="valid", filter_size=(7, 7))
    l = dnn.Conv2DDNNLayer(l, num_filters=4096, stride=(1, 1), border_mode="same", filter_size=(1, 1))

    l_logit = dnn.Conv2DDNNLayer(
        l, num_filters=1000, stride=(1, 1), border_mode="same", filter_size=(1, 1), nonlinearity=None
    )

    l_logit_flat = nn.layers.FlattenLayer(l_logit)
    l_dense = nn.layers.NonlinearityLayer(l_logit_flat, nonlinearity=nn.nonlinearities.softmax)

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vgg16.pkl")

    if os.path.exists(filename):
        with open(filename, "r") as f:
            nn.layers.set_all_param_values(l_dense, pickle.load(f))

    return utils.struct(input=l_in, logit=l_logit, out=l_dense)
Exemplo n.º 10
0
 def read_from_files(self, 
                     sick_fn='../../study2_sick.txt',
                     gene_fn='../../study2_genome.txt',
                     sample_fn='../study2/gsi',
                     sex_fn='../study2/sex',
                     bact_dir='../study2/'):
     for fn in [sick_fn, gene_fn]:
         for line in file(fn):
             line = line.strip()
             if line=='' or line[0]=='#':
                 continue
             words = line.split('\t')
             if words[0]=='dbGaP SubjID':
                 continue
             if fn==sick_fn:
                 self.patients[words[0]] = struct(id=words[0], health=words[3])
             else:
                 self.patients[words[0]].nod2 = int(words[2])
                 self.patients[words[0]].atg16l1 = int(words[3])
     pid_by_sample = {}
     for line in file(sample_fn):
         line=line.strip()
         [sample, pid]=line.split(' ')
         if 'samples' not in self.patients[pid]:
             self.patients[pid].samples=[]
         self.patients[pid].samples.append(sample)
         pid_by_sample[sample]=pid
     for line in file(sex_fn):
         line=line.strip()
         [x, sample, sex] = line.split(' ')
         patient = self.patients[pid_by_sample[sample]]
         if 'sex' in patient and patient.sex!=sex:
             print 'WARNING: inconsistent sex for %s at %s' % (patient.id, sample)
         if sex!='unknown':
             patient.sex = sex
     for pid in self.patients:
         patient = self.patients[pid]
         if 'samples' not in patient:
             continue
         patient.raw_counts = defaultdict(lambda:0)
         patient.total_count = 0
         for sid in patient.samples:
             for line in file(bact_dir+sid+'.results'):
                 try:
                     (n,species)=re.match(" *([0-9]*) (.*)\n",line).groups()
                 except:
                     print 'ERROR: '+line
                 n=int(n)
                 patient.raw_counts[species] += n
                 patient.total_count += n
                 self.bacteria[species] = True
         patient.fractions = defaultdict(lambda:0.0)
         for species in patient.raw_counts:
             if species > 1:
                 patient.fractions[species] = float(patient.raw_counts[species]) / patient.total_count
Exemplo n.º 11
0
def mnistloader():
    '''
    Return :
        struct is defined in utils.py

        train - mnist train set with struct 
        test - mnist test set with struct
        val - mnist validation set with struct
    '''
    mnist = input_data.read_data_sets(MNIST_PATH, one_hot=True)
    train = struct()
    test = struct()
    val = struct()
    train.image = mnist.train.images
    train.label = mnist.train.labels
    test.image = mnist.test.images
    test.label = mnist.test.labels
    val.image = mnist.validation.images
    val.label = mnist.validation.labels
    return train, test, val
Exemplo n.º 12
0
    def loadLabels(self, j):
        shapes = []
        for d in j['shapes']:
            s = Shape(None, None)
            s.__dict__.update(d)
            s.label = utils.struct(**s.label)
            s.points = [QtCore.QPointF(x, y) for x, y in s.points]
            if s.shape_type == Mode_box:
                box = Box3D(s)
                box.bounds = d['box']
                box.label = s.label
                s.box = box
                s.__class__ = ShapeBox
            shapes.append(s)

        return shapes
Exemplo n.º 13
0
def build_model(batch_size=batch_size, seq_length=seq_length):
    l_in = nn.layers.InputLayer(shape=(batch_size, seq_length, n_channels)+im_shape)
    l = l_in

    l = nn.layers.ReshapeLayer(l, (batch_size*seq_length,n_channels)+im_shape)

    l = conv_layer(l, num_filters=16, filter_size=(3,3))
    l = conv_layer(l, num_filters=16, filter_size=(3,3))
    l = max_pooling(l, ds=(2,2))
    l = conv_layer(l, num_filters=32, filter_size=(3,3))
    l = conv_layer(l, num_filters=32, filter_size=(3,3))
    l = max_pooling(l, ds=(2,2))
    l = conv_layer(l, num_filters=64, filter_size=(3,3))
    l = conv_layer(l, num_filters=64, filter_size=(3,3))
    l = max_pooling(l, ds=(2,2))
    l = conv_layer(l, num_filters=128, filter_size=(3,3))
    l = conv_layer(l, num_filters=128, filter_size=(3,3))
    l = max_pooling(l, ds=(2,2))

    l = nn.layers.ReshapeLayer(l, (batch_size, seq_length, -1))

    l = nn.layers.DropoutLayer(l, p=0.5)

    n_states = 2048
    l_fwd = rnn_layer(l, n_states)
    l_bck = rnn_layer(l, n_states, backwards=True)
    l = nn.layers.ElemwiseSumLayer([l_fwd, l_bck])

    l = nn.layers.DropoutLayer(l, p=0.5)
    
    l = nn.layers.ReshapeLayer(l, (batch_size*seq_length, n_states))

    l_out = nn.layers.DenseLayer(l, num_units=n_classes, 
                            nonlinearity=nn.nonlinearities.softmax)
    l_out_reshape = nn.layers.ReshapeLayer(l_out, (batch_size, seq_length, n_classes))

    return utils.struct(
        input=l_in, 
        out=l_out, 
        out_reshape=l_out_reshape)
Exemplo n.º 14
0
def get_googlenet(batchsize=10, n_channels=3, img_height=224, img_width=224):

    l_in = lasagne.layers.InputLayer(
        shape=(batchsize, n_channels, img_height, img_width),
        name='input',
    )

    l_conv1 = dnn.Conv2DDNNLayer(
        l_in,
        num_filters=64,
        filter_size=(7, 7),
        pad=3,
        stride=(2, 2),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv1/7x7_s2',
    )

    l_pool1 = flip(dnn.MaxPool2DDNNLayer(
        flip(l_conv1),
        pool_size=(3, 3),#pool_size
        stride=(2, 2),
        pad=(1,1),
        name='pool1/3x3_s2'
    ))

    lrn = LocalResponseNormalization2DLayer(
        l_pool1,
        alpha= 0.0001 / 5,
        beta= 0.75,
        k=1,
        n=5,
        name='pool1/norm1',
    )

    l_conv2 = dnn.Conv2DDNNLayer(
        lrn,
        num_filters=64,
        filter_size=(1, 1),
        pad=0,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv2/3x3_reduce',
    )

    l_conv2b = dnn.Conv2DDNNLayer(
        l_conv2,
        num_filters=192,
        filter_size=(3, 3),
        pad=1,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv2/3x3',
    )

    lrn2 = LocalResponseNormalization2DLayer(
        l_conv2b,
        alpha= 0.0001 / 5,
        beta= 0.75,
        k=1,
        n=5,
        name='conv2/norm2',
    )

    l_pool2 = flip(dnn.MaxPool2DDNNLayer(
        flip(lrn2),
        pool_size=(3, 3),#pool_size
        stride=(2, 2),
        pad=(1,1),
        name='pool2/3x3_s2'
    ))

    def inception(layer, name, no_1x1=64, no_3x3r=96, no_3x3=128, no_5x5r=16, no_5x5=32, no_pool=32):
        l_conv_inc = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_1x1,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name+'/1x1',
        )
        l_conv_inc2 = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_3x3r,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name+'/3x3_reduce',
        )
        l_conv_inc2b = dnn.Conv2DDNNLayer(
            l_conv_inc2,
            num_filters=no_3x3,
            filter_size=(3, 3),
            pad=1,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name+'/3x3',
        )
        l_conv_inc2c = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_5x5r,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name+'/5x5_reduce',
        )
        l_conv_inc2d = dnn.Conv2DDNNLayer(
            l_conv_inc2c,
            num_filters=no_5x5,
            filter_size=(5, 5),
            pad=2,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name+'/5x5',
        )
        l_pool2 = flip(dnn.MaxPool2DDNNLayer(
            flip(layer),
            pool_size=(3, 3),#pool_size
            stride=(1, 1),
            pad=(1, 1),
            name=name+'/pool'
        ))
        l_conv_inc2e = dnn.Conv2DDNNLayer(
            l_pool2,
            num_filters=no_pool,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name+'/pool_proj',
        )


        l_inc_out = nn.layers.concat([l_conv_inc, l_conv_inc2b, l_conv_inc2d, l_conv_inc2e])
        return l_inc_out

    l_inc_3a = inception(l_pool2, 'inception_3a', no_1x1=64, no_3x3r=96, no_3x3=128, no_5x5r=16, no_5x5=32, no_pool=32)
    l_inc_3b = inception(l_inc_3a, 'inception_3b', no_1x1=128, no_3x3r=128, no_3x3=192, no_5x5r=32, no_5x5=96, no_pool=64)

    l_pool3 = flip(dnn.MaxPool2DDNNLayer(
        flip(l_inc_3b),
        pool_size=(3, 3),#pool_size
        stride=(2, 2),
        pad=1,
        name='pool3/3x3_s2'
    ))

    l_inc_4a = inception(l_pool3,  'inception_4a', no_1x1=192, no_3x3r=96, no_3x3=208, no_5x5r=16, no_5x5=48, no_pool=64)
    l_inc_4b = inception(l_inc_4a, 'inception_4b', no_1x1=160, no_3x3r=112, no_3x3=224, no_5x5r=24, no_5x5=64, no_pool=64)
    l_inc_4c = inception(l_inc_4b, 'inception_4c', no_1x1=128, no_3x3r=128, no_3x3=256, no_5x5r=24, no_5x5=64, no_pool=64)
    l_inc_4d = inception(l_inc_4c, 'inception_4d', no_1x1=112, no_3x3r=144, no_3x3=288, no_5x5r=32, no_5x5=64, no_pool=64)
    l_inc_4e = inception(l_inc_4d, 'inception_4e', no_1x1=256, no_3x3r=160, no_3x3=320, no_5x5r=32, no_5x5=128, no_pool=128)

    l_pool4 = flip(dnn.MaxPool2DDNNLayer(
        flip(l_inc_4e),
        pool_size=(3, 3),#pool_size
        stride=(2, 2),
        pad=1,
        name='pool4/3x3_s2'
    ))

    l_inc_5a = inception(l_pool4,  'inception_5a', no_1x1=256, no_3x3r=160, no_3x3=320, no_5x5r=32, no_5x5=128, no_pool=128)
    l_inc_5b = inception(l_inc_5a, 'inception_5b', no_1x1=384, no_3x3r=192, no_3x3=384, no_5x5r=48, no_5x5=128, no_pool=128)

    l_pool5 = flip(dnn.Pool2DDNNLayer(
        flip(l_inc_5b),
        pool_size=(7, 7),#pool_size
        stride=(1, 1),
        pad=0,
        mode='average',
        name='pool5/7x7_s1'
    ))


    l_logit = dnn.Conv2DDNNLayer(
        l_pool5,
        num_filters=1000,
        filter_size=(1, 1),
        pad=0,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.linear,
        name='prob',
    )


    l_logit_flat = lasagne.layers.FlattenLayer(l_logit)
    l_dense = lasagne.layers.NonlinearityLayer(l_logit_flat, nonlinearity=lasagne.nonlinearities.softmax)
    l_out = l_dense

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'googlenet.pkl')

    if os.path.exists(filename):
        with open(filename, 'r') as f:
            nn.layers.set_all_param_values(l_dense, pickle.load(f))

    return utils.struct(
        input=l_in,
        logit=l_logit,
        out=l_out
    )
Exemplo n.º 15
0
def googlenet(batch_shape):
    """
    Create a googlenet, with the parameters from https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet
    :param batch_shape: The shape of the input images. This should be of size (N, 3, 224, 224)
    :return: a struct with the input layer, the logit layer (before the final softmax) and the output layer.
    """
    l_in = lasagne.layers.InputLayer(
        shape=batch_shape,
        name='input',
    )

    l_conv1 = dnn.Conv2DDNNLayer(
        l_in,
        num_filters=64,
        filter_size=(7, 7),
        pad=3,
        stride=(2, 2),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv1/7x7_s2',
    )

    l_pool1 = flip(dnn.MaxPool2DDNNLayer(
        flip(l_conv1),
        pool_size=(3, 3),  # pool_size
        stride=(2, 2),
        pad=(1, 1),
        name='pool1/3x3_s2'
    ))

    lrn = LocalResponseNormalization2DLayer(
        l_pool1,
        alpha=0.0001 / 5,
        beta=0.75,
        k=1,
        n=5,
        name='pool1/norm1',
    )

    l_conv2 = dnn.Conv2DDNNLayer(
        lrn,
        num_filters=64,
        filter_size=(1, 1),
        pad=0,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv2/3x3_reduce',
    )

    l_conv2b = dnn.Conv2DDNNLayer(
        l_conv2,
        num_filters=192,
        filter_size=(3, 3),
        pad=1,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv2/3x3',
    )

    lrn2 = LocalResponseNormalization2DLayer(
        l_conv2b,
        alpha=0.0001 / 5,
        beta=0.75,
        k=1,
        n=5,
        name='conv2/norm2',
    )

    l_pool2 = flip(dnn.MaxPool2DDNNLayer(
        flip(lrn2),
        pool_size=(3, 3),  # pool_size
        stride=(2, 2),
        pad=(1, 1),
        name='pool2/3x3_s2'
    ))

    def inception(layer, name, no_1x1=64, no_3x3r=96, no_3x3=128, no_5x5r=16, no_5x5=32, no_pool=32):
        l_conv_inc = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_1x1,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/1x1',
        )
        l_conv_inc2 = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_3x3r,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/3x3_reduce',
        )
        l_conv_inc2b = dnn.Conv2DDNNLayer(
            l_conv_inc2,
            num_filters=no_3x3,
            filter_size=(3, 3),
            pad=1,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/3x3',
        )
        l_conv_inc2c = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_5x5r,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/5x5_reduce',
        )
        l_conv_inc2d = dnn.Conv2DDNNLayer(
            l_conv_inc2c,
            num_filters=no_5x5,
            filter_size=(5, 5),
            pad=2,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/5x5',
        )
        l_pool2 = flip(dnn.MaxPool2DDNNLayer(
            flip(layer),
            pool_size=(3, 3),  # pool_size
            stride=(1, 1),
            pad=(1, 1),
            name=name + '/pool'
        ))
        l_conv_inc2e = dnn.Conv2DDNNLayer(
            l_pool2,
            num_filters=no_pool,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/pool_proj',
        )

        l_inc_out = nn.layers.concat([l_conv_inc, l_conv_inc2b, l_conv_inc2d, l_conv_inc2e])
        return l_inc_out

    l_inc_3a = inception(l_pool2, 'inception_3a', no_1x1=64, no_3x3r=96, no_3x3=128, no_5x5r=16, no_5x5=32, no_pool=32)
    l_inc_3b = inception(l_inc_3a, 'inception_3b', no_1x1=128, no_3x3r=128, no_3x3=192, no_5x5r=32, no_5x5=96,
                         no_pool=64)

    l_pool3 = flip(dnn.MaxPool2DDNNLayer(
        flip(l_inc_3b),
        pool_size=(3, 3),  # pool_size
        stride=(2, 2),
        pad=1,
        name='pool3/3x3_s2'
    ))

    l_inc_4a = inception(l_pool3, 'inception_4a', no_1x1=192, no_3x3r=96, no_3x3=208, no_5x5r=16, no_5x5=48, no_pool=64)
    l_inc_4b = inception(l_inc_4a, 'inception_4b', no_1x1=160, no_3x3r=112, no_3x3=224, no_5x5r=24, no_5x5=64,
                         no_pool=64)
    l_inc_4c = inception(l_inc_4b, 'inception_4c', no_1x1=128, no_3x3r=128, no_3x3=256, no_5x5r=24, no_5x5=64,
                         no_pool=64)
    l_inc_4d = inception(l_inc_4c, 'inception_4d', no_1x1=112, no_3x3r=144, no_3x3=288, no_5x5r=32, no_5x5=64,
                         no_pool=64)
    l_inc_4e = inception(l_inc_4d, 'inception_4e', no_1x1=256, no_3x3r=160, no_3x3=320, no_5x5r=32, no_5x5=128,
                         no_pool=128)

    l_pool4 = flip(dnn.MaxPool2DDNNLayer(
        flip(l_inc_4e),
        pool_size=(3, 3),  # pool_size
        stride=(2, 2),
        pad=1,
        name='pool4/3x3_s2'
    ))

    l_inc_5a = inception(l_pool4, 'inception_5a', no_1x1=256, no_3x3r=160, no_3x3=320, no_5x5r=32, no_5x5=128,
                         no_pool=128)
    l_inc_5b = inception(l_inc_5a, 'inception_5b', no_1x1=384, no_3x3r=192, no_3x3=384, no_5x5r=48, no_5x5=128,
                         no_pool=128)

    l_pool5 = flip(dnn.Pool2DDNNLayer(
        flip(l_inc_5b),
        pool_size=(7, 7),  # pool_size
        stride=(1, 1),
        pad=0,
        mode='average',
        name='pool5/7x7_s1'
    ))

    l_logit = dnn.Conv2DDNNLayer(
        l_pool5,
        num_filters=1000,
        filter_size=(1, 1),
        pad=0,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.linear,
        name='prob',
    )

    l_logit_flat = lasagne.layers.FlattenLayer(l_logit)
    l_dense = lasagne.layers.NonlinearityLayer(l_logit_flat, nonlinearity=lasagne.nonlinearities.softmax)
    l_out = l_dense

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'googlenet.pkl')

    if os.path.exists(filename):
        with open(filename, 'r') as f:
            nn.layers.set_all_param_values(l_dense, pickle.load(f))

    return utils.struct(
        input=l_in,
        logit=l_logit,
        out=l_out,
        interesting=l_inc_4c,
    )
Exemplo n.º 16
0
import healpy as hp
import numpy as np
from healpy import Rotator
import pymaster as nmt
import pandas as pd

from env_config import DATA_PATH
from utils import struct, get_masked_map, tansform_map_and_mask_to_nside

cmb_columns_idx = struct(
    I_STOKES=0,
    Q_STOKES=1,
    U_STOKES=2,
    TMASK=3,
    PMASK=4,
    I_STOKES_INP=5,
    Q_STOKES_INP=6,
    U_STOKES_INP=7,
    TMASK_INP=8,
    PMASKINP=9,
)


def get_cmb_temperature_power_spectra(nside):
    assert 3 * nside <= 2508

    path = os.path.join(DATA_PATH,
                        'Planck2018/COM_PowerSpect_CMB-TT-full_R3.01.txt')
    data = np.loadtxt(path, unpack=False)
    data = pd.DataFrame(data, columns=['l', 'Dl', '-dDl', '+dDl'])
Exemplo n.º 17
0
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setAcceptDrops(True)

        self.loader = None
        self.dirty = False
        self._selectSlotBlock = False
        self._config = config.get_default_config()

        self.labelListWidget = LabelListWidget(self)
        self.labelListDock = QtWidgets.QDockWidget('Displayed Label List',
                                                   self)
        self.labelListDock.setWidget(self.labelListWidget)

        self.allLabelList = LabelListWidget(self)
        self.allLabelListDock = QtWidgets.QDockWidget('Label List', self)
        self.allLabelListDock.setWidget(self.allLabelList)

        self.colorTableWidget = TaglistWidget(self)
        self.colorTableWidget.loadFromJson(self._config['tags'])
        self.colorTableDock = QtWidgets.QDockWidget('Color Table', self)
        self.colorTableDock.setWidget(self.colorTableWidget)

        self.addDockWidget(Qt.RightDockWidgetArea, self.allLabelListDock)
        self.addDockWidget(Qt.RightDockWidgetArea, self.colorTableDock)
        self.tabifyDockWidget(self.allLabelListDock, self.labelListDock)

        self.view = BaseView()
        self.setCentralWidget(self.view)
        self.statusBar().show()
        self.resize(1200, 800)

        action = functools.partial(utils.createAction, self)
        open_ = action("&Open", self.open, 'open',
                       self.tr('Open image or label file'))
        exit_ = action("&Exit", tip=self.tr('Quit Application'))
        openDir_ = action("&Open Dir", self.open, 'open')
        createMode_ = action("&Create Polygons",
                             lambda: self.toggleDrawMode(type.Mode_polygon),
                             'objects', self.tr('Start drawing polygons'))

        createRectangleMode_ = action(
            self.tr('Create Rectangle'),
            lambda: self.toggleDrawMode(type.Mode_rectangle),
            'objects',
            self.tr('Start drawing rectangles'),
            enabled=False,
        )
        createCircleMode_ = action(
            self.tr('Create Circle'),
            lambda: self.toggleDrawMode(type.Mode_circle),
            'objects',
            self.tr('Start drawing circles'),
            enabled=False,
        )
        createLineMode_ = action(
            self.tr('Create Line'),
            lambda: self.toggleDrawMode(type.Mode_line),
            'objects',
            self.tr('Start drawing lines'),
            enabled=False,
        )
        createPointMode_ = action(
            self.tr('Create Point'),
            lambda: self.toggleDrawMode(type.Mode_point),
            'objects',
            self.tr('Start drawing points'),
            enabled=False,
        )
        createLineStripMode_ = action(
            self.tr('Create LineStrip'),
            lambda: self.toggleDrawMode(type.Mode_linestrip),
            'objects',
            self.tr('Start drawing linestrip. Ctrl+LeftClick ends creation.'),
            enabled=False,
        )
        createBoxMode_ = action(
            self.tr('Create Box'),
            lambda: self.toggleDrawMode(type.Mode_box),
            'objects',
            self.tr('Start drawing box.'),
            enabled=False,
        )

        delete_ = action(self.tr('Delete Polygons'),
                         self.deleteSelectedShape,
                         'cancel',
                         self.tr('Delete the selected polygons'),
                         enabled=False)

        deleteAll_ = action(self.tr('Delete All'),
                            self.deleteSelectedShape,
                            'delete',
                            self.tr('Delete all polygons'),
                            enabled=False)

        edit_ = action('&Edit Label',
                       lambda: self.toggleDrawMode(None),
                       'edit',
                       'Modify the label of the selected polygon',
                       enabled=False)

        save_ = action(self.tr('&Save'),
                       self.saveFile,
                       'save',
                       self.tr('Save labels to file'),
                       enabled=False)

        saveAs_ = action(self.tr('&Save As'),
                         self.saveFileAs,
                         'save-as',
                         self.tr('Save labels to a different file'),
                         enabled=False)

        nextTag_ = action(
            self.tr('&Next Tag'),
            slot=lambda: self.colorTableWidget.selectNext(),
            tip=self.tr('Go to a next tag'),
        )
        prevTag_ = action(
            self.tr('&Previous Tag'),
            slot=lambda: self.colorTableWidget.selectPrev(),
            tip=self.tr('Go to a previous tag'),
        )
        homeTag_ = action(
            self.tr('&Home Tag'),
            slot=lambda: self.colorTableWidget.selectHome(),
            tip=self.tr('Go to a start tag'),
        )
        endTag_ = action(
            self.tr('&End Tag'),
            slot=lambda: self.colorTableWidget.selectEnd(),
            tip=self.tr('Go to a end tag'),
        )
        deleteTag_ = action(
            self.tr('&Delete Tag'),
            slot=lambda: self.colorTableWidget.deleteSelected())
        addTag_ = action(self.tr('&Add Tag'),
                         slot=lambda: self.colorTableWidget.addTag())
        insertTag_ = action(self.tr('&Insert Tag'),
                            slot=lambda: self.colorTableWidget.insertTag())

        fitWindow_ = action(self.tr('&Fit Window'),
                            slot=self.fitWindow,
                            icon='fit-window',
                            tip=self.tr('Zoom follows window size'))

        undo_ = action(self.tr('&Undo'),
                       slot=self.undo,
                       icon='undo',
                       tip=self.tr('undo'))

        self.zoom_widget = ZoomWidget()
        zoom_ = QtWidgets.QWidgetAction(self)
        zoom_.setDefaultWidget(self.zoom_widget)

        self.actions = utils.struct(
            open=open_,
            exit=exit_,
            openDir=openDir_,
            createMode=createMode_,
            createRectangleMode=createRectangleMode_,
            createCircleMode=createCircleMode_,
            createLineMode=createLineMode_,
            createPointMode=createPointMode_,
            createLineStripMode=createLineStripMode_,
            createBoxMode=createBoxMode_,
            edit=edit_,
            delete=delete_,
            save=save_,
            saveAs=saveAs_,
            nextTag=nextTag_,
            prevTag=prevTag_,
            homeTag=homeTag_,
            endTag=endTag_,
            deleteTag=deleteTag_,
            fitWindow=fitWindow_,
            deleteAll=deleteAll_,
            undo=undo_,
            # load=load_,
            fileMenu=(
                open_,
                openDir_,
                None,
                save_,
                saveAs_,
                None,
                # load_,
                None,
                exit_,
            ),
            editMenu=(
                createMode_,
                createRectangleMode_,
                createCircleMode_,
                createLineMode_,
                createPointMode_,
                createLineStripMode_,
                createBoxMode_,
                None,
                edit_,
                undo_,
                None,
                delete_,
                deleteAll_,
            ),
            selectionMenu=(
                nextTag_,
                prevTag_,
                homeTag_,
                endTag_,
            ),
            viewMenu=(
                self.labelListDock.toggleViewAction(),
                self.allLabelListDock.toggleViewAction(),
                self.colorTableDock.toggleViewAction(),
                None,
                fitWindow_,
            ),
            labelListMenu=(delete_, ),
            tagListMenu=(
                addTag_,
                insertTag_,
                deleteTag_,
            ))

        self.tools_actions = (
            open_,
            openDir_,
            save_,
            saveAs_,
            None,
            delete_,
            deleteAll_,
            undo_,
            None,
            createMode_,
            edit_,
            None,
            zoom_,
            fitWindow_,
        )
        self.toolbar = ToolBar('toolbar')
        self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.addToolBar(Qt.TopToolBarArea, self.toolbar)
        utils.addActions(self.toolbar, self.tools_actions)
        # utils.addActions(self.canvas.menu, self.actions.editMenu)
        self.view.addMenu(self.actions.editMenu)
        self.labelListWidget.addMenu(self.actions.labelListMenu)
        self.allLabelList.addMenu(self.actions.labelListMenu)
        self.colorTableWidget.addMenu(self.actions.tagListMenu)

        self.addMenu("&File", self.actions.fileMenu)
        self.addMenu("&Edit", self.actions.editMenu)
        self.addMenu("&Selection", self.actions.selectionMenu)
        self.addMenu("&View", self.actions.viewMenu)
        # signal

        self.zoom_widget.valueChanged.connect(self.zoomChanged)
        self.labelListWidget.itemChanged.connect(self.labelItemChanged)
        self.labelListWidget.itemDoubleClicked.connect(
            lambda: (self.toggleDrawMode(None), self.labelSelectionChanged()))
        self.labelListWidget.itemSelectionChanged.connect(
            self.labelSelectionChanged)
        self.labelListWidget.itemDeleteSelected.connect(
            self.deleteSelectedShape)
        self.allLabelList.itemSelectionChanged.connect(
            self.allLabelListSelected)
        self.allLabelList.itemDoubleClicked.connect(self.editLabel)

        self.colorTableWidget.itemsDelete.connect(self.tagsDelete)

        self.init()
        self.initViewSlot()
Exemplo n.º 18
0
def vgg19(batch_shape):
    """
    Create a vgg19, with the parameters from http://www.robots.ox.ac.uk/~vgg/research/very_deep/
    See googlenet.py for the method used to convert these caffe parameters to lasagne parameters.
    :param batch_shape: The shape of the input images. This should be of size (N, 3, X>=224, Y>=224). Note flexible
    image size, as the last dense layers have been implemented here with convolutional layers.
    :return: a struct with the input layer, the logit layer (before the final softmax) and the output layer.
    """
    l_in = nn.layers.InputLayer(shape=batch_shape)
    l = l_in

    l = conv3(l, num_filters=64)
    l = conv3(l, num_filters=64)

    l = max_pool(l)

    l = conv3(l, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = dnn.Conv2DDNNLayer(l,
                           num_filters=4096,
                           stride=(1, 1),
                           border_mode="valid",
                           filter_size=(7, 7))
    l = dnn.Conv2DDNNLayer(l,
                           num_filters=4096,
                           stride=(1, 1),
                           border_mode="same",
                           filter_size=(1, 1))

    l_logit = dnn.Conv2DDNNLayer(l,
                                 num_filters=1000,
                                 stride=(1, 1),
                                 border_mode="same",
                                 filter_size=(1, 1),
                                 nonlinearity=None)

    l_logit_flat = nn.layers.FlattenLayer(l_logit)
    l_dense = nn.layers.NonlinearityLayer(
        l_logit_flat, nonlinearity=nn.nonlinearities.softmax)

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            'vgg19.pkl')

    if os.path.exists(filename):
        with open(filename, 'r') as f:
            nn.layers.set_all_param_values(l_dense, pickle.load(f))

    return utils.struct(input=l_in, out=l_dense, logit=l_logit)
Exemplo n.º 19
0
def build_model(batch_size=batch_size, seq_length=seq_length):
    l_in = nn.layers.InputLayer(shape=(batch_size, seq_length, n_channels)+im_shape)
    l = l_in

    _,s,_,h,w = l.get_output_shape()
    l = nn.layers.ReshapeLayer(l, (batch_size*seq_length,n_channels)+im_shape)


    n = 16
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l, s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l, s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = maxpool(l, s, ds=(2,2,2))


    n = 32
    _,s,_,h,w = l.get_output_shape()
    l = reshape_bschw_to_bchw(l)
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l,s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l, s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = maxpool(l, s, ds=(2,2,2))

    n = 64
    _,s,_,h,w = l.get_output_shape()
    l = reshape_bschw_to_bchw(l)
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l,s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l, s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = maxpool(l, s, ds=(2,2,2))

    n = 128
    _,s,_,h,w = l.get_output_shape()
    l = reshape_bschw_to_bchw(l)
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l,s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = reshape_bchw_to_bcs(l, s)
    l = conv1d_layer(l, num_filters=n, filter_length=3)
    l = reshape_bcs_to_bchw(l, h, w)

    l = maxpool(l, s, ds=(2,2,2))


    l = nn.layers.DropoutLayer(l, p=p_shared)

    l = dense_layer(l, num_units=2048)

    l = nn.layers.DropoutLayer(l, p=p_shared)

    l = dense_layer(l, num_units=2048)

    l = nn.layers.DropoutLayer(l, p=p_shared)

    l_out = nn.layers.DenseLayer(l, num_units=n_classes, 
                            nonlinearity=nn.nonlinearities.softmax)

    return utils.struct(
        input=l_in, 
        out=l_out)
Exemplo n.º 20
0
from utils import struct, count, any#, severs

tot=defaultdict(lambda:0)
hit=defaultdict(lambda:0)

cats=20

def record(sev, truth):
    post = 1.0 / (1 + 1/sev) # prior=1/2
    rpost = round(post*cats)
    tot[rpost] += 1
    if truth:
        hit[rpost] += 1

for i in range(1):
    net=struct()
    net.a = random()
    net.b_given_a=[random(), random()]
    c_given_b = [random(), random()]
    net.c_given_ab = [c_given_b] * 2
    net.d_given_a = [random(), random()]
    data = simulate(net, 1000)
    cnt = count(zip(*data))
    if any(cnt, lambda(x):x==0):
        print 'net: %s' % net
        print 'skipping %s' % cnt
        continue
    bf = has_common_cause_bf(data[0:3])
    record(bf, False)
    bf = has_common_cause_bf(data[1:4])
    record(bf, True)
Exemplo n.º 21
0
import traceback
from collections import deque
from functools import reduce

import pp

from lex import lex
from stack import Stack
from utils import struct


# TODO: capture stdin, stdout, stderr
Record = struct('cmd', 'args', 'result')


class Stark:

    def __init__(self, hist_length=1000):
        self.hist = deque(maxlen=hist_length)
        self.stack = Stack({
            name[4:].replace('_', '-'): getattr(self, name)
            for name in dir(self)
            if name.startswith('cmd_')
        })

    def cmd_record(self, *args, **kwargs):
        self.hist.append(Record(*args, **kwargs))

    def cmd_lex(self, code):
        return lex(code)
Exemplo n.º 22
0
def googlenet(batch_shape):
    """
    Create a googlenet, with the parameters from https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet
    :param batch_shape: The shape of the input images. This should be of size (N, 3, 224, 224)
    :return: a struct with the input layer, the logit layer (before the final softmax) and the output layer.
    """
    l_in = lasagne.layers.InputLayer(
        shape=batch_shape,
        name='input',
    )

    l_conv1 = dnn.Conv2DDNNLayer(
        l_in,
        num_filters=64,
        filter_size=(7, 7),
        pad=3,
        stride=(2, 2),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv1/7x7_s2',
    )

    l_pool1 = flip(
        dnn.MaxPool2DDNNLayer(
            flip(l_conv1),
            pool_size=(3, 3),  # pool_size
            stride=(2, 2),
            pad=(1, 1),
            name='pool1/3x3_s2'))

    lrn = LocalResponseNormalization2DLayer(
        l_pool1,
        alpha=0.0001 / 5,
        beta=0.75,
        k=1,
        n=5,
        name='pool1/norm1',
    )

    l_conv2 = dnn.Conv2DDNNLayer(
        lrn,
        num_filters=64,
        filter_size=(1, 1),
        pad=0,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv2/3x3_reduce',
    )

    l_conv2b = dnn.Conv2DDNNLayer(
        l_conv2,
        num_filters=192,
        filter_size=(3, 3),
        pad=1,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.rectify,
        name='conv2/3x3',
    )

    lrn2 = LocalResponseNormalization2DLayer(
        l_conv2b,
        alpha=0.0001 / 5,
        beta=0.75,
        k=1,
        n=5,
        name='conv2/norm2',
    )

    l_pool2 = flip(
        dnn.MaxPool2DDNNLayer(
            flip(lrn2),
            pool_size=(3, 3),  # pool_size
            stride=(2, 2),
            pad=(1, 1),
            name='pool2/3x3_s2'))

    def inception(layer,
                  name,
                  no_1x1=64,
                  no_3x3r=96,
                  no_3x3=128,
                  no_5x5r=16,
                  no_5x5=32,
                  no_pool=32):
        l_conv_inc = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_1x1,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/1x1',
        )
        l_conv_inc2 = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_3x3r,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/3x3_reduce',
        )
        l_conv_inc2b = dnn.Conv2DDNNLayer(
            l_conv_inc2,
            num_filters=no_3x3,
            filter_size=(3, 3),
            pad=1,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/3x3',
        )
        l_conv_inc2c = dnn.Conv2DDNNLayer(
            layer,
            num_filters=no_5x5r,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/5x5_reduce',
        )
        l_conv_inc2d = dnn.Conv2DDNNLayer(
            l_conv_inc2c,
            num_filters=no_5x5,
            filter_size=(5, 5),
            pad=2,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/5x5',
        )
        l_pool2 = flip(
            dnn.MaxPool2DDNNLayer(
                flip(layer),
                pool_size=(3, 3),  # pool_size
                stride=(1, 1),
                pad=(1, 1),
                name=name + '/pool'))
        l_conv_inc2e = dnn.Conv2DDNNLayer(
            l_pool2,
            num_filters=no_pool,
            filter_size=(1, 1),
            pad=0,
            stride=(1, 1),
            nonlinearity=lasagne.nonlinearities.rectify,
            name=name + '/pool_proj',
        )

        l_inc_out = nn.layers.concat(
            [l_conv_inc, l_conv_inc2b, l_conv_inc2d, l_conv_inc2e])
        return l_inc_out

    l_inc_3a = inception(l_pool2,
                         'inception_3a',
                         no_1x1=64,
                         no_3x3r=96,
                         no_3x3=128,
                         no_5x5r=16,
                         no_5x5=32,
                         no_pool=32)
    l_inc_3b = inception(l_inc_3a,
                         'inception_3b',
                         no_1x1=128,
                         no_3x3r=128,
                         no_3x3=192,
                         no_5x5r=32,
                         no_5x5=96,
                         no_pool=64)

    l_pool3 = flip(
        dnn.MaxPool2DDNNLayer(
            flip(l_inc_3b),
            pool_size=(3, 3),  # pool_size
            stride=(2, 2),
            pad=1,
            name='pool3/3x3_s2'))

    l_inc_4a = inception(l_pool3,
                         'inception_4a',
                         no_1x1=192,
                         no_3x3r=96,
                         no_3x3=208,
                         no_5x5r=16,
                         no_5x5=48,
                         no_pool=64)
    l_inc_4b = inception(l_inc_4a,
                         'inception_4b',
                         no_1x1=160,
                         no_3x3r=112,
                         no_3x3=224,
                         no_5x5r=24,
                         no_5x5=64,
                         no_pool=64)
    l_inc_4c = inception(l_inc_4b,
                         'inception_4c',
                         no_1x1=128,
                         no_3x3r=128,
                         no_3x3=256,
                         no_5x5r=24,
                         no_5x5=64,
                         no_pool=64)
    l_inc_4d = inception(l_inc_4c,
                         'inception_4d',
                         no_1x1=112,
                         no_3x3r=144,
                         no_3x3=288,
                         no_5x5r=32,
                         no_5x5=64,
                         no_pool=64)
    l_inc_4e = inception(l_inc_4d,
                         'inception_4e',
                         no_1x1=256,
                         no_3x3r=160,
                         no_3x3=320,
                         no_5x5r=32,
                         no_5x5=128,
                         no_pool=128)

    l_pool4 = flip(
        dnn.MaxPool2DDNNLayer(
            flip(l_inc_4e),
            pool_size=(3, 3),  # pool_size
            stride=(2, 2),
            pad=1,
            name='pool4/3x3_s2'))

    l_inc_5a = inception(l_pool4,
                         'inception_5a',
                         no_1x1=256,
                         no_3x3r=160,
                         no_3x3=320,
                         no_5x5r=32,
                         no_5x5=128,
                         no_pool=128)
    l_inc_5b = inception(l_inc_5a,
                         'inception_5b',
                         no_1x1=384,
                         no_3x3r=192,
                         no_3x3=384,
                         no_5x5r=48,
                         no_5x5=128,
                         no_pool=128)

    l_pool5 = flip(
        dnn.Pool2DDNNLayer(
            flip(l_inc_5b),
            pool_size=(7, 7),  # pool_size
            stride=(1, 1),
            pad=0,
            mode='average',
            name='pool5/7x7_s1'))

    l_logit = dnn.Conv2DDNNLayer(
        l_pool5,
        num_filters=1000,
        filter_size=(1, 1),
        pad=0,
        stride=(1, 1),
        nonlinearity=lasagne.nonlinearities.linear,
        name='prob',
    )

    l_logit_flat = lasagne.layers.FlattenLayer(l_logit)
    l_dense = lasagne.layers.NonlinearityLayer(
        l_logit_flat, nonlinearity=lasagne.nonlinearities.softmax)
    l_out = l_dense

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            'googlenet.pkl')

    if os.path.exists(filename):
        with open(filename, 'r') as f:
            nn.layers.set_all_param_values(l_dense, pickle.load(f))

    return utils.struct(
        input=l_in,
        logit=l_logit,
        out=l_out,
        interesting=l_inc_4c,
    )
Exemplo n.º 23
0
def build_model(batch_size=batch_size, seq_length=seq_length):
    l_in = nn.layers.InputLayer(shape=(batch_size, seq_length, n_channels)+im_shape)
    l = l_in

    l = nn.layers.ReshapeLayer(l, (batch_size*seq_length,n_channels)+im_shape)


    n = 16
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = conv1d_layer(l, num_filters=n, filter_size=3)
    l = conv_layer(l, num_filters=n, filter_size=(3,3))

    l = maxpool(l, pool_size=(2,2))


    n = 32
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = maxpool(l, pool_size=(2,2))

    n = 64
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = maxpool(l, pool_size=(2,2))

    n = 128
    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = conv_layer(l, num_filters=n, filter_size=(3,3))
    l = conv1d_layer(l, num_filters=n, filter_size=3)

    l = maxpool(l, pool_size=(2,2))

    l = nn.layers.ReshapeLayer(l, (batch_size, seq_length, -1))

    l = nn.layers.DropoutLayer(l, p=p_shared)

    n_states = 512
    l_fwd = lstm_layer(l, n_states)
    l_bck = lstm_layer(l, n_states, backwards=True)
    l = nn.layers.ElemwiseSumLayer([l_fwd, l_bck])

    l = nn.layers.DropoutLayer(l, p=p_shared)

    l = nn.layers.ReshapeLayer(l, (batch_size*seq_length, n_states))

    l_out = nn.layers.DenseLayer(l, num_units=n_classes, 
                            nonlinearity=nn.nonlinearities.softmax)
    l_out_reshape = nn.layers.ReshapeLayer(l_out, (batch_size, seq_length, n_classes))

    return utils.struct(
        input=l_in, 
        out=l_out, 
        out_reshape=l_out_reshape)
Exemplo n.º 24
0
 def getTagObj(self, id=0):
     obj = utils.struct(id=id,
                        color=self.color_edit.getColor().getRgb(),
                        desc=self.line_edit.text())
     return obj