示例#1
0
    def __init__(self):

        print "Loading model..."
        model_file = open('params.pkl', 'r')
        params = cPickle.load(model_file)
        model_file.close()

        layer0_w = params[-2]
        layer0_b = params[-1]
        layer1_w = params[-4]
        layer1_b = params[-3]
        layer2_w = params[-6]
        layer2_b = params[-5]

        # compile theano function for efficient forward propagation
        x = T.tensor4('x')

        layer0 = ConvPoolLayer(input=x,
                               image_shape=(1, 3, 32, 32),
                               filter_shape=(32, 3, 5, 5),
                               W=layer0_w,
                               b=layer0_b,
                               poolsize=(2, 2),
                               activation=relu_nonlinear)

        layer1 = ConvPoolLayer(input=layer0.output,
                               image_shape=(1, 32, 14, 14),
                               filter_shape=(50, 32, 5, 5),
                               W=layer1_w,
                               b=layer1_b,
                               poolsize=(2, 2),
                               activation=relu_nonlinear)

        layer2 = ConvPoolLayer(input=layer1.output,
                               image_shape=(1, 50, 5, 5),
                               filter_shape=(64, 50, 5, 5),
                               W=layer2_w,
                               b=layer2_b,
                               poolsize=(1, 1),
                               activation=relu_nonlinear)
        print "Compiling theano.function..."
        self.forward = theano.function([x], layer2.output)
        self.forward2 = theano.function([x], layer2.output)
        self.forward1 = theano.function([x], layer1.output)
        self.forward0 = theano.function([x], layer0.output)

        # Stages that ZUCHENG DeConvNet
        up_layer0 = CPRStage_Up(image_shape=(1, 3, 32, 32),
                                filter_shape=(32, 3, 5, 5),
                                poolsize=2,
                                W=layer0_w,
                                b=layer0_b,
                                activation=activation)

        up_layer1 = CPRStage_Up(image_shape=(1, 32, 14, 14),
                                filter_shape=(50, 32, 5, 5),
                                poolsize=2,
                                W=layer1_w,
                                b=layer1_b,
                                activation=activation)

        up_layer2 = CPRStage_Up(image_shape=(1, 50, 5, 5),
                                filter_shape=(64, 50, 5, 5),
                                poolsize=1,
                                W=layer2_w,
                                b=layer2_b,
                                activation=activation)
        # backward
        down_layer2 = CPRStage_Down(image_shape=(1, 64, 1, 1),
                                    filter_shape=(50, 64, 5, 5),
                                    poolsize=1,
                                    W=layer2_w,
                                    b=layer2_b,
                                    activation=activation)

        down_layer1 = CPRStage_Down(image_shape=(1, 50, 10, 10),
                                    filter_shape=(32, 50, 5, 5),
                                    poolsize=2,
                                    W=layer1_w,
                                    b=layer1_b,
                                    activation=activation)

        down_layer0 = CPRStage_Down(image_shape=(1, 32, 28, 28),
                                    filter_shape=(3, 32, 5, 5),
                                    poolsize=2,
                                    W=layer0_w,
                                    b=layer0_b,
                                    activation=activation)

        self.Stages = [
            up_layer0, up_layer1, up_layer2, down_layer2, down_layer1,
            down_layer0
        ]
    def __init__(self,
                 model_name="plankton_conv_visualize_model.pkl.params",
                 bScalar=False):

        print "Loading plankton model..."
        model_file = open(trainedModelPath + model_name, 'r')
        params = cPickle.load(model_file)
        model_file.close()

        layer0_w = params[-2]
        layer0_b = params[-1]
        layer1_w = params[-4]
        layer1_b = params[-3]
        layer2_w = params[-6]
        layer2_b = params[-5]
        '''
        Note: after applying convolution, our weight is a weight per pixel
        whereas the weight here is per the whole array. Fix this by averaging for now
        '''
        if bScalar:
            print "Using b as scalar"
            layer0_b = np.mean(layer0_b, axis=(1, 2))
            layer1_b = np.mean(layer1_b, axis=(1, 2))
            layer2_b = np.mean(layer2_b, axis=(1, 2))
        else:
            print "Using b as matrix"

        # compile theano function for efficient forward propagation
        x = T.tensor4('x')

        layer0 = ConvPoolLayer(input=x,
                               image_shape=(1, NUM_C, 40, 40),
                               filter_shape=(128, NUM_C, 5, 5),
                               W=layer0_w,
                               b=layer0_b,
                               poolsize=(2, 2),
                               activation=relu_nonlinear)

        layer1 = ConvPoolLayer(input=layer0.output,
                               image_shape=(1, 128, 18, 18),
                               filter_shape=(256, 128, 5, 5),
                               W=layer1_w,
                               b=layer1_b,
                               poolsize=(2, 2),
                               activation=relu_nonlinear)

        layer2 = ConvPoolLayer(input=layer1.output,
                               image_shape=(1, 256, 7, 7),
                               filter_shape=(512, 256, 2, 2),
                               W=layer2_w,
                               b=layer2_b,
                               poolsize=(2, 2),
                               activation=relu_nonlinear)
        print "Compiling theano.function..."
        self.forward = theano.function([x], layer2.output)
        self.forward2 = theano.function([x], layer2.output)
        self.forward1 = theano.function([x], layer1.output)
        self.forward0 = theano.function([x], layer0.output)

        up_layer0 = CPRStage_Up(image_shape=(1, NUM_C, 40, 40),
                                filter_shape=(128, NUM_C, 5, 5),
                                poolsize=2,
                                W=layer0_w,
                                b=layer0_b,
                                activation=activation)

        up_layer1 = CPRStage_Up(image_shape=(1, 128, 18, 18),
                                filter_shape=(256, 128, 5, 5),
                                poolsize=2,
                                W=layer1_w,
                                b=layer1_b,
                                activation=activation)

        up_layer2 = CPRStage_Up(image_shape=(1, 256, 7, 7),
                                filter_shape=(512, 256, 2, 2),
                                poolsize=2,
                                W=layer2_w,
                                b=layer2_b,
                                activation=activation)
        # backward
        down_layer2 = CPRStage_Down(image_shape=(1, 512, 6, 6),
                                    filter_shape=(256, 512, 2, 2),
                                    poolsize=2,
                                    W=layer2_w,
                                    b=layer2_b,
                                    activation=activation)

        down_layer1 = CPRStage_Down(image_shape=(1, 256, 14, 14),
                                    filter_shape=(128, 256, 5, 5),
                                    poolsize=2,
                                    W=layer1_w,
                                    b=layer1_b,
                                    activation=activation)

        down_layer0 = CPRStage_Down(image_shape=(1, 128, 36, 36),
                                    filter_shape=(NUM_C, 128, 5, 5),
                                    poolsize=2,
                                    W=layer0_w,
                                    b=layer0_b,
                                    activation=activation)

        self.Stages = [
            up_layer0, up_layer1, up_layer2, down_layer2, down_layer1,
            down_layer0
        ]
    def __init__(self,
                 model_name="plankton_conv_visualize_model.pkl.params",
                 bScalar=False):

        print "Loading plankton model..."
        model_file = open(trainedModelPath + model_name, 'r')
        params = cPickle.load(model_file)
        model_file.close()

        layer0_w = params[-2]
        layer0_b = params[-1]
        layer1_w = params[-4]
        layer1_b = params[-3]
        layer2_w = params[-6]
        layer2_b = params[-5]
        layer3_w = params[-8]
        layer3_b = params[-7]
        layer4_w = params[-10]
        layer4_b = params[-9]

        print 'layer0_w shape', np.shape(layer0_w)
        print 'layer0_b shape', np.shape(layer0_b)
        print 'layer1_w shape', np.shape(layer1_w)
        print 'layer1_b shape', np.shape(layer1_b)
        print 'layer2_w shape', np.shape(layer2_w)
        print 'layer2_b shape', np.shape(layer2_b)
        print 'layer3_w shape', np.shape(layer3_w)
        print 'layer3_b shape', np.shape(layer3_b)
        print 'layer4_w shape', np.shape(layer4_w)
        print 'layer4_b shape', np.shape(layer4_b)
        '''
        Note: after applying convolution, our weight is a weight per pixel
        whereas the weight here is per the whole array. Fix this by averaging for now
        '''
        if bScalar:
            print "Using b as scalar"
            layer0_b = np.mean(layer0_b, axis=(1, 2))
            layer1_b = np.mean(layer1_b, axis=(1, 2))
            layer2_b = np.mean(layer2_b, axis=(1, 2))
            layer3_b = np.mean(layer3_b, axis=(1, 2))
            layer4_b = np.mean(layer4_b, axis=(1, 2))
        else:
            print "Using b as matrix"

        # compile theano function for efficient forward propagation
        x = T.tensor4('x')
        numLayers = 5  # counting 0
        conv = [5, 5, 3, 3, 3]
        pool = [1, 2, 1, 2, 2]
        chs = [16, 16, 32, 32, 32]
        print 'conv', conv
        print 'pool', pool
        print 'chs', chs
        img_size = 40
        ap = [0 for i in range(numLayers)]
        ac = [0 for i in range(numLayers)]
        for i in range(numLayers):
            if i == 0:
                ac[i] = img_size - conv[i] + 1
            else:
                print ac[i]
                print ap[i - 1]
                print conv[i]
                ac[i] = ap[i - 1] - conv[i] + 1
            if ac[i] % pool[i] != 0:
                print 'Warning! Pooling not valid!'
            ap[i] = ac[i] / pool[i]
        print 'img_after conv', ac
        print 'img_after pool', ap

        layer0 = ConvPoolLayer(input=x,
                               image_shape=(1, NUM_C, img_size, img_size),
                               filter_shape=(chs[0], NUM_C, conv[0], conv[0]),
                               W=layer0_w,
                               b=layer0_b,
                               poolsize=(pool[0], pool[0]),
                               activation=relu_nonlinear)

        layer1 = ConvPoolLayer(input=layer0.output,
                               image_shape=(1, chs[0], ap[0], ap[0]),
                               filter_shape=(chs[1], chs[0], conv[1], conv[1]),
                               W=layer1_w,
                               b=layer1_b,
                               poolsize=(pool[1], pool[1]),
                               activation=relu_nonlinear)

        layer2 = ConvPoolLayer(input=layer1.output,
                               image_shape=(1, chs[1], ap[1], ap[1]),
                               filter_shape=(chs[2], chs[1], conv[2], conv[2]),
                               W=layer2_w,
                               b=layer2_b,
                               poolsize=(pool[2], pool[2]),
                               activation=relu_nonlinear)
        layer3 = ConvPoolLayer(input=layer2.output,
                               image_shape=(1, chs[2], ap[2], ap[2]),
                               filter_shape=(chs[3], chs[2], conv[3], conv[3]),
                               W=layer3_w,
                               b=layer3_b,
                               poolsize=(pool[3], pool[3]),
                               activation=relu_nonlinear)
        layer4 = ConvPoolLayer(input=layer3.output,
                               image_shape=(1, chs[3], ap[3], ap[3]),
                               filter_shape=(chs[4], chs[3], conv[4], conv[4]),
                               W=layer4_w,
                               b=layer4_b,
                               poolsize=(pool[4], pool[4]),
                               activation=relu_nonlinear)

        print "Compiling theano.function..."
        self.forward = theano.function([x], layer4.output)
        self.forward4 = theano.function([x], layer4.output)
        self.forward3 = theano.function([x], layer3.output)
        self.forward2 = theano.function([x], layer2.output)
        self.forward1 = theano.function([x], layer1.output)
        self.forward0 = theano.function([x], layer0.output)

        up_layer0 = CPRStage_Up(image_shape=(1, NUM_C, img_size, img_size),
                                filter_shape=(chs[0], NUM_C, conv[0], conv[0]),
                                poolsize=pool[0],
                                W=layer0_w,
                                b=layer0_b,
                                activation=activation)

        up_layer1 = CPRStage_Up(image_shape=(1, chs[0], ap[0], ap[0]),
                                filter_shape=(chs[1], chs[0], conv[1],
                                              conv[1]),
                                poolsize=pool[1],
                                W=layer1_w,
                                b=layer1_b,
                                activation=activation)

        up_layer2 = CPRStage_Up(image_shape=(1, chs[1], ap[1], ap[1]),
                                filter_shape=(chs[2], chs[1], conv[2],
                                              conv[2]),
                                poolsize=pool[2],
                                W=layer2_w,
                                b=layer2_b,
                                activation=activation)
        up_layer3 = CPRStage_Up(image_shape=(1, chs[2], ap[2], ap[2]),
                                filter_shape=(chs[3], chs[2], conv[3],
                                              conv[3]),
                                poolsize=pool[3],
                                W=layer3_w,
                                b=layer3_b,
                                activation=activation)
        up_layer4 = CPRStage_Up(image_shape=(1, chs[3], ap[3], ap[3]),
                                filter_shape=(chs[4], chs[3], conv[4],
                                              conv[4]),
                                poolsize=pool[4],
                                W=layer4_w,
                                b=layer4_b,
                                activation=activation)

        # backward
        down_layer4 = CPRStage_Down(image_shape=(1, chs[4], ac[4], ac[4]),
                                    filter_shape=(chs[3], chs[4], conv[4],
                                                  conv[4]),
                                    poolsize=pool[4],
                                    W=layer4_w,
                                    b=layer4_b,
                                    activation=activation)

        down_layer3 = CPRStage_Down(image_shape=(1, chs[3], ac[3], ac[3]),
                                    filter_shape=(chs[2], chs[3], conv[3],
                                                  conv[3]),
                                    poolsize=pool[3],
                                    W=layer3_w,
                                    b=layer3_b,
                                    activation=activation)

        down_layer2 = CPRStage_Down(image_shape=(1, chs[2], ac[2], ac[2]),
                                    filter_shape=(chs[1], chs[2], conv[2],
                                                  conv[2]),
                                    poolsize=pool[2],
                                    W=layer2_w,
                                    b=layer2_b,
                                    activation=activation)

        down_layer1 = CPRStage_Down(image_shape=(1, chs[1], ac[1], ac[1]),
                                    filter_shape=(chs[0], chs[1], conv[1],
                                                  conv[1]),
                                    poolsize=pool[1],
                                    W=layer1_w,
                                    b=layer1_b,
                                    activation=activation)

        down_layer0 = CPRStage_Down(image_shape=(1, chs[0], ac[0], ac[0]),
                                    filter_shape=(NUM_C, chs[0], conv[0],
                                                  conv[0]),
                                    poolsize=pool[0],
                                    W=layer0_w,
                                    b=layer0_b,
                                    activation=activation)

        self.Stages = [
            up_layer0, up_layer1, up_layer2, up_layer3, up_layer4, down_layer4,
            down_layer3, down_layer2, down_layer1, down_layer0
        ]