예제 #1
0
파일: marble.py 프로젝트: malyvsen/marble
class Discriminator:
    with tf.variable_scope('discriminator'):
        inputs = Generator.outputs  # will be fed directly if it's a real image

        micro = [inputs]  # for errors in small scale
        wide_micro = tf.layers.conv2d(micro[-1],
                                      filters=8,
                                      kernel_size=(7, 3),
                                      padding='same',
                                      activation=tf.nn.relu)
        high_micro = tf.layers.conv2d(micro[-1],
                                      filters=8,
                                      kernel_size=(3, 7),
                                      padding='same',
                                      activation=tf.nn.relu)
        micro.append(tf.concat((wide_micro, high_micro), axis=-1))
        micro.append(utils.pool(micro[-1], 'MAX', pool_size=4))
        micro.append(
            tf.layers.conv2d(micro[-1],
                             filters=4,
                             kernel_size=5,
                             padding='same',
                             activation=tf.nn.relu))

        macro = [utils.pool(inputs, 'AVG',
                            pool_size=4)]  # for errors in large scale
        wide_macro = tf.layers.conv2d(macro[-1],
                                      filters=8,
                                      kernel_size=(7, 3),
                                      padding='same',
                                      activation=tf.nn.relu)
        high_macro = tf.layers.conv2d(macro[-1],
                                      filters=8,
                                      kernel_size=(3, 7),
                                      padding='same',
                                      activation=tf.nn.relu)
        macro.append(tf.concat((wide_macro, high_macro), axis=-1))
        macro.append(utils.pool(macro[-1], 'MAX', pool_size=4))
        macro.append(
            tf.layers.conv2d(macro[-1],
                             filters=4,
                             kernel_size=5,
                             padding='same',
                             activation=tf.nn.relu))

        logits = tf.reduce_mean(micro[-1], axis=(1, 2, 3)) + tf.reduce_mean(
            macro[-1], axis=(1, 2, 3))
        outputs = tf.nn.sigmoid(logits)  # 0 for fake, 1 for real

    vars = tf.trainable_variables(scope='discriminator')

    targets = tf.placeholder(dtype=tf.float32, shape=(None))
    loss = tf.losses.sigmoid_cross_entropy(targets, logits)
    optimizer = tf.train.AdamOptimizer(5e-4).minimize(loss, var_list=vars)
예제 #2
0
    def bilrp(self, x1, x2, poolsize, gamma_func=None):
        # Computing LRP passes for each branch
        r1 = self.compute_branch(x1, gamma_func)
        r2 = self.compute_branch(x2, gamma_func)

        # Computing BiLRP relevances
        R = [np.array(r).sum(1) for r in [r1, r2]]
        R = np.tensordot(pool(R[0], poolsize),
                         pool(R[1], poolsize),
                         axes=(0, 0))

        return R
def vgg(inputs, struct, dbb_biases, dbb=False):
    def _block(inputs, filters, dbb, dbb_bias):
        inputs, mem1 = conv(inputs,
                            filters,
                            3,
                            strides=1,
                            padding='SAME',
                            dbb=dbb,
                            dbb_bias=dbb_bias)
        inputs, mem2 = batch_norm(inputs)
        #print(inputs)
        return inputs, mem1 + mem2

    total_mem = 0
    inputs, mem1 = _block(inputs, struct[0], dbb, dbb_biases[0])
    inputs, mem2 = _block(inputs, struct[1], dbb, dbb_biases[1])
    inputs = pool(inputs, 2, strides=2, padding='VALID')

    inputs, mem3 = _block(inputs, struct[2], dbb, dbb_biases[2])
    inputs, mem4 = _block(inputs, struct[3], dbb, dbb_biases[3])
    inputs = pool(inputs, 2, strides=2, padding='VALID')

    inputs, mem5 = _block(inputs, struct[4], dbb, dbb_biases[4])
    inputs, mem6 = _block(inputs, struct[5], dbb, dbb_biases[5])
    inputs, mem7 = _block(inputs, struct[6], dbb, dbb_biases[6])
    inputs = pool(inputs, 2, strides=2, padding='VALID')

    inputs, mem8 = _block(inputs, struct[7], dbb, dbb_biases[7])
    inputs, mem9 = _block(inputs, struct[8], dbb, dbb_biases[8])
    inputs, mem10 = _block(inputs, struct[9], dbb, dbb_biases[9])
    inputs = pool(inputs, 2, strides=2, padding='VALID')

    inputs, mem11 = _block(inputs, struct[10], dbb, dbb_biases[10])
    inputs, mem12 = _block(inputs, struct[11], dbb, dbb_biases[11])
    inputs, mem13 = _block(inputs, struct[12], dbb, dbb_biases[12])
    inputs = pool(inputs, 2, strides=2, padding='VALID')

    inputs, mem_flat = flatten(inputs, struct[13], dbb, dbb_biases[13])
    inputs, mem14 = dense(inputs, struct[14], dbb=dbb, dbb_bias=dbb_biases[14])
    inputs, mem15 = batch_norm(inputs)
    inputs, mem16 = dense(inputs, struct[15], dbb=False)

    total_mem = mem1 + mem2 + mem3 + mem4 + \
                mem5 + mem6 + mem7 + mem8 + \
                mem9 + mem10 + mem11 + mem12 + \
                mem13 + mem14 + mem15 + mem16 + mem_flat

    return inputs, total_mem
예제 #4
0
def cnn_model0(IMG_SIZE):
    data_input = Input(shape=(IMG_SIZE, IMG_SIZE,3))
    layer1 = conv(data_input, 32, (5, 5), padding='same', strides=(1, 1), name='layer1')
    layer2 = pool(layer1, pool_size=(3, 3), strides=(2, 2), padding='same', name='layer2', pool_type='max')
    layer3 = conv(layer2, 64, (3, 3), padding='same', strides=(1, 1), name='layer3')
    layer4 = conv(layer3, 96, (3, 3), padding='same', strides=(1, 1), name='layer4')
    layer5 = pool(layer4,pool_size=(3, 3), strides=(2, 2), padding='same',name='layer5',pool_type='max')
    layer6 = conv(layer5, 128, (3, 3), padding='same', strides=(1, 1), name='layer6')
    layer7 = conv(layer6, 256, (3, 3), padding='same', strides=(1, 1), name='layer7')
    layer8 = pool(layer7, pool_size=(3, 3), strides=(2, 2), padding='same', name='layer8', pool_type='max')
    layer9 = conv(layer8, 256, (5, 5), padding='same', strides=(1, 1), name='layer9')
    x = Flatten()(layer9)
    x = Dense(64, activation='relu')(x)
    x = Dense(NUM_CLASSES, activation='softmax')(x)
    x = Model(data_input, x, name='mnist')
    return x
예제 #5
0
def VGG19(x, n_classes, is_pretrain=True):
    with tf.name_scope('VGG16'):
        x = utils.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool1'):
            x = utils.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool2'):
            x = utils.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv3_4', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool3'):
            x = utils.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv4_4', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool4'):
            x = utils.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv5_4', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool5'):
            x = utils.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.FC_layer('fc6', x, out_nodes=4096)
        with tf.name_scope('batch_norm1'):
            x = utils.batch_norm(x)
        x = utils.FC_layer('fc7', x, out_nodes=4096)
        with tf.name_scope('batch_norm2'):
            x = utils.batch_norm(x)
        x = utils.FC_layer('fc8', x, out_nodes=n_classes)

        return x
예제 #6
0
 def encoder(self, x_ph, isTr_ph):
     with tf.variable_scope(self.name):
         l = conv('c1', [5,5,1,64], x_ph)
         l = conv('c2', [5,5,64,64], l)
         l = layer_bn('b1', l, isTr_ph)
         l = pool(l)
         l = conv('c3', [5,5,64,128], l)
         l = conv('c4', [5,5,128,128], l)
         l = layer_bn('b2', l, isTr_ph)
         l = pool(l)
         l = conv('c5', [5,5,128,256], l)
         l = conv('c6', [5,5,256,256], l)
         l = conv('c7', [5,5,256,256], l)
         l = layer_bn('b3', l, isTr_ph)
         l = pool(l)
         l = conv('c8', [5,5,256,512], l)
         l = conv('c9', [5,5,512,512], l)
         l = conv('c10', [5,5,512,512], l)
         l = layer_bn('b4', l, isTr_ph)
         l = pool(l)
         return l
예제 #7
0
def lenet_conv(inputs, struct, dbb=False):
    total_mem = 0

    if dbb:
        inputs, mem1 = conv(inputs, struct[0], 5, strides=1, padding='VALID', \
                                dbb=dbb, dbb_bias=13)
        inputs = pool(inputs, 2, strides=2, padding='VALID')

        inputs, mem2 = conv(inputs, struct[1], 5, strides=1, padding='VALID', \
                                dbb=dbb, dbb_bias=25)
        inputs = pool(inputs, 2, strides=2, padding='VALID')

        inputs, mem3 = flatten(inputs, struct[2], dbb=dbb, dbb_bias=156)
        inputs, mem4 = dense(inputs, struct[3], dbb=dbb, dbb_bias=54)
        inputs, mem5 = dense(inputs, 10, dbb=False)

    else:
        inputs, mem1 = conv(inputs,
                            struct[0],
                            5,
                            strides=1,
                            padding='VALID',
                            dbb=dbb)
        inputs = pool(inputs, 2, strides=2, padding='VALID')

        inputs, mem2 = conv(inputs,
                            struct[1],
                            5,
                            strides=1,
                            padding='VALID',
                            dbb=dbb)
        inputs = pool(inputs, 2, strides=2, padding='VALID')

        inputs, mem3 = flatten(inputs, struct[2], dbb=dbb)
        inputs, mem4 = dense(inputs, struct[3], dbb=dbb)
        inputs, mem5 = dense(inputs, 10, dbb=False)

    total_mem = mem1 + mem2 + mem3 + mem4 + mem5

    return inputs, total_mem
예제 #8
0
        def execute(self, features):
            """
            Executes the layer in convolution->pooling order

            :param features : input features
            :type features : numpy nd array
            """

            # convolution
            convolved_features = self.conv_obj.convolution(
                features, self.kernels, self.biases)
            # pooling
            self.out = pool(convolved_features, self.pool_dim)
예제 #9
0
 def _network(self, in_x, isTr, reuse=False, trainable=True):
     with tf.variable_scope(self.name):
         l = tf.image.resize_images(in_x, [self.wh, self.wh])
         l = conv('c1', [3, 3, 3, 64], l, reuse=reuse, trainable=trainable)
         l = layer_bn('b1', l, isTr, reuse, trainable)
         l = tf.nn.relu(l)
         l = pool(l)
         l = conv('c2', [3, 3, 64, 64], l, reuse=reuse, trainable=trainable)
         l = layer_bn('b2', l, isTr, reuse, trainable)
         l = tf.nn.relu(l)
         l = pool(l)
         l = conv('c3', [3, 3, 64, 64], l, reuse=reuse, trainable=trainable)
         l = layer_bn('b3', l, isTr, reuse, trainable)
         l = tf.nn.relu(l)
         l = pool(l)
         l = conv('c4', [3, 3, 64, 64], l, reuse=reuse, trainable=trainable)
         l = layer_bn('b4', l, isTr, reuse, trainable)
         l = tf.nn.relu(l)
         l = pool(l)
         l = tf.contrib.layers.flatten(l)
         #l = fc('f1', 1024, l, r=reuse, t=trainable)
     return l
예제 #10
0
        def execute(self, features):
            """
            Executes the layer in convolution->pooling order

            :param features : input features
            :type features : numpy nd array
            """
            
            # convolution
            convolved_features = self.conv_obj.convolution(features,
                                                           self.kernels, 
                                                           self.biases) 
            # pooling
            self.out = pool(convolved_features, self.pool_dim)
예제 #11
0
def cnn_model2(IMG_SIZE):
    data_input = Input(shape=(IMG_SIZE, IMG_SIZE,3))
    layer1 = conv(data_input, 32, (5, 5), padding='same', strides=(1, 1), name='layer1')
    x = fire_module(layer1, 1, squeeze=16, expand=64, strides=2)
    x = BatchNormalization(axis=3, epsilon=1e-06)(x)
    x = fire_module(x,2, squeeze=32, expand=128)
    x = BatchNormalization(axis=3, epsilon=1e-06)(x)
    x = pool(x, pool_size=(3, 3), strides=(2, 2), padding='same', name='layer2', pool_type='max')
    x = fire_module(x, 3, squeeze=16, expand=64,strides=1)
    x = BatchNormalization(axis=3, epsilon=1e-06)(x)
    x = Flatten()(x)
    x = Dense(16, activation='relu')(x)
    x = Dense(NUM_CLASSES, activation='softmax')(x)
    x = Model(data_input, x, name='mnist')
    return x
예제 #12
0
파일: marble.py 프로젝트: malyvsen/marble
class Generator:
    with tf.variable_scope('encoder'):
        inputs = tf.placeholder(dtype=tf.float32, shape=(None, None, None, 3))

        representations = [inputs]
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=32,
                             kernel_size=5,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.pool(representations[-1], 'MAX', pool_size=2))
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=32,
                             kernel_size=3,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.pool(representations[-1], 'MAX', pool_size=2))
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=16,
                             kernel_size=3,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.pool(representations[-1], 'MAX', pool_size=2))
        skip = utils.pool(inputs, 'AVG',
                          pool_size=8)  # skip connection directly to pixels
        skip_appended = tf.concat((representations[-1], skip), axis=-1)
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=16,
                             kernel_size=3,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.pool(representations[-1], 'MAX', pool_size=2))

        encoding = representations[-1]

    with tf.variable_scope('decoder'):
        representations.append(
            utils.zoom_nearest_neighbor(representations[-1], zoom=2))
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=16,
                             kernel_size=3,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.zoom_nearest_neighbor(representations[-1], zoom=2))
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=16,
                             kernel_size=3,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.zoom_nearest_neighbor(representations[-1], zoom=2))
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=32,
                             kernel_size=3,
                             padding='same',
                             activation=tf.nn.relu))
        representations.append(
            utils.zoom_nearest_neighbor(representations[-1], zoom=2))
        representations.append(
            tf.layers.conv2d(representations[-1],
                             filters=32,
                             kernel_size=5,
                             padding='same',
                             activation=tf.nn.relu))

        logits = tf.layers.conv2d(representations[-1],
                                  filters=3,
                                  kernel_size=5,
                                  padding='same')
        representations.append(logits)
        outputs = tf.nn.sigmoid(logits) * 255
        representations.append(outputs)

    vars = tf.trainable_variables(scope='encoder') + tf.trainable_variables(
        scope='decoder')

    targets = tf.placeholder(dtype=tf.float32, shape=(None, None, None, 3))
    mse_loss = tf.reduce_mean(tf.square(outputs - targets)) / (
        255**2)  # normalized into interval of size 1
    ssim_loss = -tf.reduce_mean(tf.image.ssim(
        outputs, targets,
        max_val=255)) / 2  # also normalized into interval of size 1