def scorer_head_model(self, features, training, reuse=False):
     with tf.variable_scope("scorer_head", reuse=reuse): # define variable scope
         dense1 = layers.dense_layer(features, units=1024, use_bias=True)
         relu1 = layers.relu_layer(dense1)
         dense2 = layers.dense_layer(relu1, units=512, use_bias=True)
         relu2 = layers.relu_layer(dense2)
         dense3 = layers.dense_layer(relu2, units=256, use_bias=True)
         relu3 = layers.relu_layer(dense3)
         dense4 = layers.dense_layer(relu3, units=1, use_bias=True)
         
         
     return dense4
Ejemplo n.º 2
0
    def generator_model(self, noise, feats, training, reuse=False):  # construct the graph of the generator
        a = time.time()

        with tf.variable_scope("generator",
                               reuse=reuse):  # define variable scope to easily retrieve vars of the generator

            gen_inp = tf.concat([noise, feats], -1)
            with tf.name_scope("preprocess_inp"):
                dense1 = layers.dense_layer(gen_inp, units=4 * 4 * 1024, use_bias=False)
                bn1 = layers.batch_norm_layer_mcgan(dense1, training, 0.8)
                relu1 = layers.relu_layer(bn1)
                reshaped = tf.reshape(relu1, shape=[-1, 1024, 4, 4])  # shape=(batch_size, 1024, 4, 4)

            deconv1 = layers.deconv_block_mcgan(reshaped, training, momentum=0.8, out_channels=512, filter_size=(4, 4),
                                                strides=(2, 2), padding="same",
                                                use_bias=True)  # shape=(batch_size, 512, 8, 8)
            deconv2 = layers.deconv_block_mcgan(deconv1, training, momentum=0.8, out_channels=256, filter_size=(4, 4), strides=(2, 2),
                                                padding="same", use_bias=True)  # shape=(batch_size, 256, 16, 16)
            deconv3 = layers.deconv_block_mcgan(deconv2, training, momentum=0.8, out_channels=128, filter_size=(4, 4), strides=(2, 2),
                                                padding="same", use_bias=True)  # shape=(batch_size, 128, 32, 32)
            deconv4 = layers.deconv_layer(deconv3, out_channels=1, filter_size=(4, 4), strides=(2, 2), padding="same",
                                          use_bias=True)  # shape=(batch_size, 1, 64, 64)

            gen_out = layers.tanh_layer(deconv4)
        print("Built Generator model in {} s".format(time.time() - a))
        list_ops = {"dense1": dense1, "bn1":bn1, "relu1": relu1, "reshaped": reshaped, "deconv1": deconv1, "deconv2": deconv2,
                    "deconv3": deconv3, "deconv4": deconv4,
                    "gen_out": gen_out}  # list of operations, can be used to run the graph up to a certain op
        # i,e get the subgraph
        return gen_out, list_ops
Ejemplo n.º 3
0
    def __call__(self, inp, training):
        a = time.time()

        with tf.variable_scope("StackedSRM"):  # define variable scope
            inter = inp  # intermediate input
            outputs = []
            for i in range(self.nb_stacks):
                with tf.name_scope("stack"):
                    conv = layers.conv_layer(inter,
                                             out_channels=64,
                                             filter_size=(4, 4),
                                             strides=(2, 2),
                                             padding=(1, 1),
                                             pad_values=0,
                                             use_bias=False)
                    relu = layers.relu_layer(conv)
                    res_module = layers.residual_module_srm(relu,
                                                            training,
                                                            out_channels=64,
                                                            nb_blocks=6,
                                                            pad_values=0,
                                                            use_bias=False)

                    h, w = tf.shape(res_module)[2], tf.shape(res_module)[3]
                    up_sample1 = layers.resize_layer(
                        res_module,
                        new_size=[2 * h, 2 * w],
                        resize_method=tf.image.ResizeMethod.NEAREST_NEIGHBOR
                    )  # nearest neighbor up sampling
                    conv1 = layers.conv_layer(up_sample1,
                                              out_channels=128,
                                              filter_size=(3, 3),
                                              strides=(1, 1),
                                              padding=(1, 1),
                                              pad_values=0,
                                              use_bias=False)

                    h, w = tf.shape(conv1)[2], tf.shape(conv1)[3]
                    up_sample2 = layers.resize_layer(
                        conv1,
                        new_size=[2 * h, 2 * w],
                        resize_method=tf.image.ResizeMethod.NEAREST_NEIGHBOR
                    )  # nearest neighbor up sampling
                    conv2 = layers.conv_layer(up_sample2,
                                              out_channels=1,
                                              filter_size=(3, 3),
                                              strides=(1, 1),
                                              padding=(1, 1),
                                              pad_values=0,
                                              use_bias=False)

                    inter = (
                        layers.tanh_layer(conv2) + 1
                    ) / 2.0  # apply tanh and renormalize so that the output is in the range [0, 1] to prepare it to be inputted to the next stack

                outputs.append(inter)

        print("SRM Model built in {} s".format(time.time() - a))
        return outputs
Ejemplo n.º 4
0
def discriminator(input_vectors, sequence_length, is_real=True, **opts):
    """
    Args:
        input_vectors: output of the RNN either from real or generated data
        sequence_length: TensorFlow queue or placeholder tensor for number of word ids for each sentence
        is_real: if True, RNN outputs when feeding in actual data, if False feeds in generated data
    """
    tf.logging.info(" Setting up discriminator")

    rnn_final_state = (
        input_vectors >>
        lay.dense_layer(hidden_dims=opts["embedding_dim"]) >>
        lay.recurrent_layer(sequence_length=sequence_length, hidden_dims=opts["rnn_hidden_dim"],
                            return_final_state=True)
    )

    prediction_logits = (
        rnn_final_state >>
        lay.dense_layer(hidden_dims=opts["output_hidden_dim"]) >>
        lay.relu_layer() >>
        lay.dropout_layer(opts["output_dropout_keep_prob"]) >>
        lay.dense_layer(hidden_dims=opts["output_hidden_dim"]) >>
        lay.relu_layer() >>
        lay.dropout_layer(opts["output_dropout_keep_prob"]) >>
        lay.dense_layer(hidden_dims=1)
    )

    if is_real:
        target = tf.ones_like(prediction_logits)
    else:
        target = tf.zeros_like(prediction_logits)

    # TODO: add accuracy
    loss = (
        prediction_logits >>
        lay.sigmoid_cross_entropy_layer(target=target)
    )

    # TODO: return logits in case for WGAN and l2 GANs
    return DiscriminatorTuple(rnn_final_state=rnn_final_state, prediction_logits=prediction_logits, loss=loss)
Ejemplo n.º 5
0
def discriminator(input_vectors, sequence_length, is_real=True, **opts):
    """
    Args:
        input_vectors: output of the RNN either from real or generated data
        sequence_length: TensorFlow queue or placeholder tensor for number of word ids for each sentence
        is_real: if True, RNN outputs when feeding in actual data, if False feeds in generated data
    """
    tf.logging.info(" Setting up discriminator")

    rnn_final_state = (
        input_vectors >>
        lay.dense_layer(hidden_dims=opts["embedding_dim"]) >>
        lay.recurrent_layer(sequence_length=sequence_length, hidden_dims=opts["rnn_hidden_dim"],
                            return_final_state=True)
    )

    prediction_logits = (
        rnn_final_state >>
        lay.dense_layer(hidden_dims=opts["output_hidden_dim"]) >>
        lay.relu_layer() >>
        lay.dropout_layer(opts["output_dropout_keep_prob"]) >>
        lay.dense_layer(hidden_dims=opts["output_hidden_dim"]) >>
        lay.relu_layer() >>
        lay.dropout_layer(opts["output_dropout_keep_prob"]) >>
        lay.dense_layer(hidden_dims=1)
    )

    if is_real:
        target = tf.ones_like(prediction_logits)
    else:
        target = tf.zeros_like(prediction_logits)

    # TODO: add accuracy
    loss = (
        prediction_logits >>
        lay.sigmoid_cross_entropy_layer(target=target)
    )

    # TODO: return logits in case for WGAN and l2 GANs
    return DiscriminatorTuple(rnn_final_state=rnn_final_state, prediction_logits=prediction_logits, loss=loss)
Ejemplo n.º 6
0
    def discriminator_model(self, inp, feats, training, reuse=False, resize=False, minibatch=False):  # construct the graph of the discriminator
        a = time.time()
        with tf.variable_scope("discriminator",
                               reuse=reuse):  # define variable scope to easily retrieve vars of the discriminator

            if resize:
                inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2), padding=(12, 12))
                inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2))
                inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2))
                inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2))

            conv1 = layers.conv_block_mcgan(inp, training, momentum=0.8, out_channels=128, filter_size=(4, 4), strides=(2, 2),
                                            padding="same", use_bias=True, batch_norm=False,
                                            alpha=0.3)  # shape=(batch_size, 128, 32, 32)
            conv2 = layers.conv_block_mcgan(conv1, training, momentum=0.8, out_channels=256, filter_size=(4, 4), strides=(2, 2),
                                            padding="same", use_bias=True,
                                            alpha=0.3)  # shape=(batch_size, 256, 16, 16)
            conv3 = layers.conv_block_mcgan(conv2, training, momentum=0.8, out_channels=512, filter_size=(4, 4), strides=(2, 2),
                                            padding="same", use_bias=True, alpha=0.3)  # shape=(batch_size, 512, 8, 8)
            conv4 = layers.conv_block_mcgan(conv3, training, momentum=0.8, out_channels=1024, filter_size=(4, 4), strides=(2, 2),
                                            padding="same", use_bias=True, alpha=0.3)  # shape=(batch_size, 1024, 4, 4)
            flat = tf.reshape(conv4, [-1, 1024 * 4 * 4])

            if(minibatch):
                minibatched = layers.minibatch(flat, num_kernels=5, kernel_dim=3)
                dense1 = layers.dense_layer(minibatched, 128, use_bias=True)
            else:
                dense1 = layers.dense_layer(flat, 128, use_bias=True)
            
            drop1 = layers.dropout_layer(dense1, training, dropout_rate=0.3)
            LRU1 = layers.leaky_relu_layer(drop1, alpha=0.3)

            dense2 = layers.dense_layer(feats, units=3, use_bias=True)
            relu2 = layers.relu_layer(dense2)
            bn2 = layers.batch_norm_layer_mcgan(relu2, training, 0.8)
            dense3 = layers.dense_layer(bn2, units=1)

            merged = tf.concat([LRU1, dense3], axis=-1)

            logits = layers.dense_layer(merged, units=2, use_bias=True)
            out = logits
        print("Built Discriminator model in {} s".format(time.time() - a))

        list_ops = {"inp": inp, "conv1": conv1, "conv2": conv2, "conv3": conv3, "conv4": conv4, "flat": flat,
                    "dense1":dense1, "drop1":drop1, "LRU1":LRU1, "dense2":dense2, "relu2":relu2, "bn2":bn2,
                    "dense3":dense3, "logits": logits, "out": out}

        return out, list_ops
Ejemplo n.º 7
0
	def get_model(self):
		params = {
			'batch_size': 20,
			'learning_rate': 0.01,
			'epochs':  20,
			'num_classes':  10,
			'dropout_rate': 0.1
		}
		
		Model = model.model()
		Model.add(layers.convolution_layer(field=8, padding=0, stride=1, depth=1, filters=5))
		Model.add(layers.relu_layer())
		Model.add(layers.flatten_layer())
		Model.add(layers.dropout_layer(r = params['dropout_rate']))
		Model.add(layers.linear_layer(13*13*5, 10))
		Model.set_loss(layers.softmax_cross_entropy())
		Model.set_hyper_params(params)

		return Model
Ejemplo n.º 8
0
    def __init__(self,
                 input_size,
                 hidden_size,
                 output_size,
                 weight_init_std=0.01):
        self.params = {}
        self.params['W1'] = weight_init_std * np.random.randn(
            input_size, hidden_size)
        self.params['b1'] = np.zeros(hidden_size)
        self.params['W2'] = weight_init_std * np.random.randn(
            hidden_size, output_size)
        self.params['b2'] = np.zeros(output_size)

        #レイヤーの作成
        self.layers = OrderedDict()
        self.layers["affine1"] = lay.affin_layer(
            self.params['W1'], self.params['b1'])  #インスタンス生成時の引数はコンストラクタへの引数
        self.layers["relu1"] = lay.relu_layer()
        self.layers["affine2"] = lay.affin_layer(self.params['W2'],
                                                 self.params['b2'])

        self.lastlayer = lay.softmax_loss_layer()
Ejemplo n.º 9
0
def run():
    model_name = 'alexnet'
    directory_caffe = './caffemodel'
    directory_theano = './theanomodel'
    url_prototxt = 'https://raw.githubusercontent.com/BVLC/caffe/master/models/bvlc_alexnet/deploy.prototxt'
    url_caffemodel = 'http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel'
    filename_prototxt = '%s/%s.prototxt' % (directory_caffe, model_name)
    filename_caffemodel = '%s/%s.caffemodel' % (directory_caffe, model_name)
    filename_theanomodel = '%s/%s.model' % (directory_theano, model_name)

    # download caffemodel
    print 'downloading caffemodel'
    if not os.path.exists(directory_caffe):
        os.mkdir(directory_caffe)
    if not os.path.exists(filename_prototxt):
        p = subprocess.Popen(('wget', url_prototxt, '-O', filename_prototxt))
        p.wait()
    if not os.path.exists(filename_caffemodel):
        p = subprocess.Popen((
            'wget',
            url_caffemodel,
            '-O',
            filename_caffemodel,
        ))
        p.wait()

    # load caffe model
    print 'loading caffe model'
    model_caffe = caffe.Net(filename_prototxt, filename_caffemodel, True)
    conv1_W = theano.shared(model_caffe.params['conv1'][0].data[:, :, ::-1, ::-1])
    conv2_W = theano.shared(model_caffe.params['conv2'][0].data[:, :, ::-1, ::-1])
    conv3_W = theano.shared(model_caffe.params['conv3'][0].data[:, :, ::-1, ::-1])
    conv4_W = theano.shared(model_caffe.params['conv4'][0].data[:, :, ::-1, ::-1])
    conv5_W = theano.shared(model_caffe.params['conv5'][0].data[:, :, ::-1, ::-1])
    conv1_b = theano.shared(model_caffe.params['conv1'][1].data.squeeze())
    conv2_b = theano.shared(model_caffe.params['conv2'][1].data.squeeze())
    conv3_b = theano.shared(model_caffe.params['conv3'][1].data.squeeze())
    conv4_b = theano.shared(model_caffe.params['conv4'][1].data.squeeze())
    conv5_b = theano.shared(model_caffe.params['conv5'][1].data.squeeze())
    fc6_W = theano.shared(model_caffe.params['fc6'][0].data.squeeze())
    fc7_W = theano.shared(model_caffe.params['fc7'][0].data.squeeze())
    fc8_W = theano.shared(model_caffe.params['fc8'][0].data.squeeze())
    fc6_b = theano.shared(model_caffe.params['fc6'][1].data.squeeze())
    fc7_b = theano.shared(model_caffe.params['fc7'][1].data.squeeze())
    fc8_b = theano.shared(model_caffe.params['fc8'][1].data.squeeze())

    # make theano model
    print 'building theano model'
    model_theano = collections.OrderedDict()
    model_theano['data'] = T.tensor4()
    model_theano['conv1'] = layers.convolution_layer(model_theano['data'], conv1_W, conv1_b, subsample=(4, 4))
    model_theano['relu1'] = layers.relu_layer(model_theano['conv1'])
    model_theano['norm1'] = layers.lrn_layer(model_theano['relu1'])
    model_theano['pool1'] = layers.pooling_layer(model_theano['norm1'])
    model_theano['conv2'] = layers.convolution_layer(model_theano['pool1'], conv2_W, conv2_b, border='same', group=2)
    model_theano['relu2'] = layers.relu_layer(model_theano['conv2'])
    model_theano['norm2'] = layers.lrn_layer(model_theano['relu2'])
    model_theano['pool2'] = layers.pooling_layer(model_theano['norm2'])
    model_theano['conv3'] = layers.convolution_layer(model_theano['pool2'], conv3_W, conv3_b, border='same')
    model_theano['relu3'] = layers.relu_layer(model_theano['conv3'])
    model_theano['conv4'] = layers.convolution_layer(model_theano['relu3'], conv4_W, conv4_b, border='same', group=2)
    model_theano['relu4'] = layers.relu_layer(model_theano['conv4'])
    model_theano['conv5'] = layers.convolution_layer(model_theano['relu4'], conv5_W, conv5_b, border='same', group=2)
    model_theano['relu5'] = layers.relu_layer(model_theano['conv5'])
    model_theano['pool5'] = layers.pooling_layer(model_theano['relu5'])
    model_theano['fc6'] = layers.inner_product_layer(model_theano['pool5'], fc6_W, fc6_b)
    model_theano['relu6'] = layers.relu_layer(model_theano['fc6'])
    model_theano['fc7'] = layers.inner_product_layer(model_theano['relu6'], fc7_W, fc7_b)
    model_theano['relu7'] = layers.relu_layer(model_theano['fc7'])
    model_theano['fc8'] = layers.inner_product_layer(model_theano['relu7'], fc8_W, fc8_b)
    model_theano['prob'] = layers.softmax_layer(model_theano['fc8'])

    # check
    print 'checking model'
    data = np.random.randn(*model_caffe.blobs['data'].data.shape)
    data = data.astype(np.float32) * 10
    model_caffe.blobs['data'].data[:] = data
    model_caffe.forward()
    theano_output = theano.function(
        [model_theano['data']],
        model_theano['prob'],
    )(data)
    error = (
        theano_output.squeeze() -
        model_caffe.blobs['prob'].data.squeeze()
    ).max()
    assert error < 1e-6

    # save
    print 'saving'
    if not os.path.exists(directory_theano):
        os.mkdir(directory_theano)
    sys.setrecursionlimit(100000)
    pickle.dump(
        model_theano, 
        open(filename_theanomodel, 'wb'),        
        protocol=pickle.HIGHEST_PROTOCOL,
    )
    print 'done'
Ejemplo n.º 10
0
def run():
    model_name = 'alexnet'
    directory_caffe = './caffemodel'
    directory_theano = './theanomodel'
    url_prototxt = 'https://raw.githubusercontent.com/BVLC/caffe/master/models/bvlc_alexnet/deploy.prototxt'
    url_caffemodel = 'http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel'
    filename_prototxt = '%s/%s.prototxt' % (directory_caffe, model_name)
    filename_caffemodel = '%s/%s.caffemodel' % (directory_caffe, model_name)
    filename_theanomodel = '%s/%s.model' % (directory_theano, model_name)

    # download caffemodel
    print 'downloading caffemodel'
    if not os.path.exists(directory_caffe):
        os.mkdir(directory_caffe)
    if not os.path.exists(filename_prototxt):
        p = subprocess.Popen(('wget', url_prototxt, '-O', filename_prototxt))
        p.wait()
    if not os.path.exists(filename_caffemodel):
        p = subprocess.Popen((
            'wget',
            url_caffemodel,
            '-O',
            filename_caffemodel,
        ))
        p.wait()

    # load caffe model
    print 'loading caffe model'
    model_caffe = caffe.Net(filename_prototxt, filename_caffemodel, True)
    conv1_W = theano.shared(
        model_caffe.params['conv1'][0].data[:, :, ::-1, ::-1])
    conv2_W = theano.shared(
        model_caffe.params['conv2'][0].data[:, :, ::-1, ::-1])
    conv3_W = theano.shared(
        model_caffe.params['conv3'][0].data[:, :, ::-1, ::-1])
    conv4_W = theano.shared(
        model_caffe.params['conv4'][0].data[:, :, ::-1, ::-1])
    conv5_W = theano.shared(
        model_caffe.params['conv5'][0].data[:, :, ::-1, ::-1])
    conv1_b = theano.shared(model_caffe.params['conv1'][1].data.squeeze())
    conv2_b = theano.shared(model_caffe.params['conv2'][1].data.squeeze())
    conv3_b = theano.shared(model_caffe.params['conv3'][1].data.squeeze())
    conv4_b = theano.shared(model_caffe.params['conv4'][1].data.squeeze())
    conv5_b = theano.shared(model_caffe.params['conv5'][1].data.squeeze())
    fc6_W = theano.shared(model_caffe.params['fc6'][0].data.squeeze())
    fc7_W = theano.shared(model_caffe.params['fc7'][0].data.squeeze())
    fc8_W = theano.shared(model_caffe.params['fc8'][0].data.squeeze())
    fc6_b = theano.shared(model_caffe.params['fc6'][1].data.squeeze())
    fc7_b = theano.shared(model_caffe.params['fc7'][1].data.squeeze())
    fc8_b = theano.shared(model_caffe.params['fc8'][1].data.squeeze())

    # make theano model
    print 'building theano model'
    model_theano = collections.OrderedDict()
    model_theano['data'] = T.tensor4()
    model_theano['conv1'] = layers.convolution_layer(model_theano['data'],
                                                     conv1_W,
                                                     conv1_b,
                                                     subsample=(4, 4))
    model_theano['relu1'] = layers.relu_layer(model_theano['conv1'])
    model_theano['norm1'] = layers.lrn_layer(model_theano['relu1'])
    model_theano['pool1'] = layers.pooling_layer(model_theano['norm1'])
    model_theano['conv2'] = layers.convolution_layer(model_theano['pool1'],
                                                     conv2_W,
                                                     conv2_b,
                                                     border='same',
                                                     group=2)
    model_theano['relu2'] = layers.relu_layer(model_theano['conv2'])
    model_theano['norm2'] = layers.lrn_layer(model_theano['relu2'])
    model_theano['pool2'] = layers.pooling_layer(model_theano['norm2'])
    model_theano['conv3'] = layers.convolution_layer(model_theano['pool2'],
                                                     conv3_W,
                                                     conv3_b,
                                                     border='same')
    model_theano['relu3'] = layers.relu_layer(model_theano['conv3'])
    model_theano['conv4'] = layers.convolution_layer(model_theano['relu3'],
                                                     conv4_W,
                                                     conv4_b,
                                                     border='same',
                                                     group=2)
    model_theano['relu4'] = layers.relu_layer(model_theano['conv4'])
    model_theano['conv5'] = layers.convolution_layer(model_theano['relu4'],
                                                     conv5_W,
                                                     conv5_b,
                                                     border='same',
                                                     group=2)
    model_theano['relu5'] = layers.relu_layer(model_theano['conv5'])
    model_theano['pool5'] = layers.pooling_layer(model_theano['relu5'])
    model_theano['fc6'] = layers.inner_product_layer(model_theano['pool5'],
                                                     fc6_W, fc6_b)
    model_theano['relu6'] = layers.relu_layer(model_theano['fc6'])
    model_theano['fc7'] = layers.inner_product_layer(model_theano['relu6'],
                                                     fc7_W, fc7_b)
    model_theano['relu7'] = layers.relu_layer(model_theano['fc7'])
    model_theano['fc8'] = layers.inner_product_layer(model_theano['relu7'],
                                                     fc8_W, fc8_b)
    model_theano['prob'] = layers.softmax_layer(model_theano['fc8'])

    # check
    print 'checking model'
    data = np.random.randn(*model_caffe.blobs['data'].data.shape)
    data = data.astype(np.float32) * 10
    model_caffe.blobs['data'].data[:] = data
    model_caffe.forward()
    theano_output = theano.function(
        [model_theano['data']],
        model_theano['prob'],
    )(data)
    error = (theano_output.squeeze() -
             model_caffe.blobs['prob'].data.squeeze()).max()
    assert error < 1e-6

    # save
    print 'saving'
    if not os.path.exists(directory_theano):
        os.mkdir(directory_theano)
    sys.setrecursionlimit(100000)
    pickle.dump(
        model_theano,
        open(filename_theanomodel, 'wb'),
        protocol=pickle.HIGHEST_PROTOCOL,
    )
    print 'done'
Ejemplo n.º 11
0
def run():
    model_name = "vggnet"
    directory_caffe = "./caffemodel"
    directory_theano = "./theanomodel"
    url_prototxt = "https://gist.githubusercontent.com/ksimonyan/3785162f95cd2d5fee77/raw/f02f8769e64494bcd3d7e97d5d747ac275825721/VGG_ILSVRC_19_layers_deploy.prototxt"
    url_caffemodel = "http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_19_layers.caffemodel"
    filename_prototxt = "%s/%s.prototxt" % (directory_caffe, model_name)
    filename_caffemodel = "%s/%s.caffemodel" % (directory_caffe, model_name)
    filename_theanomodel = "%s/%s.model" % (directory_theano, model_name)

    # download caffemodel
    print "downloading caffemodel"
    if not os.path.exists(directory_caffe):
        os.mkdir(directory_caffe)
    if not os.path.exists(filename_prototxt):
        p = subprocess.Popen(("wget", url_prototxt, "-O", filename_prototxt))
        p.wait()
    if not os.path.exists(filename_caffemodel):
        p = subprocess.Popen(("wget", url_caffemodel, "-O", filename_caffemodel))
        p.wait()

    # load caffe model
    model_caffe = caffe.Net(filename_prototxt, filename_caffemodel, True)
    conv1_1_W = theano.shared(model_caffe.params["conv1_1"][0].data[:, :, ::-1, ::-1])
    conv1_2_W = theano.shared(model_caffe.params["conv1_2"][0].data[:, :, ::-1, ::-1])
    conv2_1_W = theano.shared(model_caffe.params["conv2_1"][0].data[:, :, ::-1, ::-1])
    conv2_2_W = theano.shared(model_caffe.params["conv2_2"][0].data[:, :, ::-1, ::-1])
    conv3_1_W = theano.shared(model_caffe.params["conv3_1"][0].data[:, :, ::-1, ::-1])
    conv3_2_W = theano.shared(model_caffe.params["conv3_2"][0].data[:, :, ::-1, ::-1])
    conv3_3_W = theano.shared(model_caffe.params["conv3_3"][0].data[:, :, ::-1, ::-1])
    conv3_4_W = theano.shared(model_caffe.params["conv3_4"][0].data[:, :, ::-1, ::-1])
    conv4_1_W = theano.shared(model_caffe.params["conv4_1"][0].data[:, :, ::-1, ::-1])
    conv4_2_W = theano.shared(model_caffe.params["conv4_2"][0].data[:, :, ::-1, ::-1])
    conv4_3_W = theano.shared(model_caffe.params["conv4_3"][0].data[:, :, ::-1, ::-1])
    conv4_4_W = theano.shared(model_caffe.params["conv4_4"][0].data[:, :, ::-1, ::-1])
    conv5_1_W = theano.shared(model_caffe.params["conv5_1"][0].data[:, :, ::-1, ::-1])
    conv5_2_W = theano.shared(model_caffe.params["conv5_2"][0].data[:, :, ::-1, ::-1])
    conv5_3_W = theano.shared(model_caffe.params["conv5_3"][0].data[:, :, ::-1, ::-1])
    conv5_4_W = theano.shared(model_caffe.params["conv5_4"][0].data[:, :, ::-1, ::-1])
    conv1_1_b = theano.shared(model_caffe.params["conv1_1"][1].data.squeeze())
    conv1_2_b = theano.shared(model_caffe.params["conv1_2"][1].data.squeeze())
    conv2_1_b = theano.shared(model_caffe.params["conv2_1"][1].data.squeeze())
    conv2_2_b = theano.shared(model_caffe.params["conv2_2"][1].data.squeeze())
    conv3_1_b = theano.shared(model_caffe.params["conv3_1"][1].data.squeeze())
    conv3_2_b = theano.shared(model_caffe.params["conv3_2"][1].data.squeeze())
    conv3_3_b = theano.shared(model_caffe.params["conv3_3"][1].data.squeeze())
    conv3_4_b = theano.shared(model_caffe.params["conv3_4"][1].data.squeeze())
    conv4_1_b = theano.shared(model_caffe.params["conv4_1"][1].data.squeeze())
    conv4_2_b = theano.shared(model_caffe.params["conv4_2"][1].data.squeeze())
    conv4_3_b = theano.shared(model_caffe.params["conv4_3"][1].data.squeeze())
    conv4_4_b = theano.shared(model_caffe.params["conv4_4"][1].data.squeeze())
    conv5_1_b = theano.shared(model_caffe.params["conv5_1"][1].data.squeeze())
    conv5_2_b = theano.shared(model_caffe.params["conv5_2"][1].data.squeeze())
    conv5_3_b = theano.shared(model_caffe.params["conv5_3"][1].data.squeeze())
    conv5_4_b = theano.shared(model_caffe.params["conv5_4"][1].data.squeeze())
    fc6_W = theano.shared(model_caffe.params["fc6"][0].data.squeeze())
    fc7_W = theano.shared(model_caffe.params["fc7"][0].data.squeeze())
    fc8_W = theano.shared(model_caffe.params["fc8"][0].data.squeeze())
    fc6_b = theano.shared(model_caffe.params["fc6"][1].data.squeeze())
    fc7_b = theano.shared(model_caffe.params["fc7"][1].data.squeeze())
    fc8_b = theano.shared(model_caffe.params["fc8"][1].data.squeeze())

    # make theano model
    model_theano = collections.OrderedDict()
    model_theano["data"] = T.tensor4()

    model_theano["conv1_1"] = layers.convolution_layer(model_theano["data"], conv1_1_W, conv1_1_b, border="same")
    model_theano["relu1_1"] = layers.relu_layer(model_theano["conv1_1"])
    model_theano["conv1_2"] = layers.convolution_layer(model_theano["relu1_1"], conv1_2_W, conv1_2_b, border="same")
    model_theano["relu1_2"] = layers.relu_layer(model_theano["conv1_2"])
    model_theano["pool1"] = layers.pooling_layer(model_theano["relu1_2"], size=(2, 2), stride=(2, 2))

    model_theano["conv2_1"] = layers.convolution_layer(model_theano["pool1"], conv2_1_W, conv2_1_b, border="same")
    model_theano["relu2_1"] = layers.relu_layer(model_theano["conv2_1"])
    model_theano["conv2_2"] = layers.convolution_layer(model_theano["relu2_1"], conv2_2_W, conv2_2_b, border="same")
    model_theano["relu2_2"] = layers.relu_layer(model_theano["conv2_2"])
    model_theano["pool2"] = layers.pooling_layer(model_theano["relu2_2"], size=(2, 2), stride=(2, 2))

    model_theano["conv3_1"] = layers.convolution_layer(model_theano["pool2"], conv3_1_W, conv3_1_b, border="same")
    model_theano["relu3_1"] = layers.relu_layer(model_theano["conv3_1"])
    model_theano["conv3_2"] = layers.convolution_layer(model_theano["relu3_1"], conv3_2_W, conv3_2_b, border="same")
    model_theano["relu3_2"] = layers.relu_layer(model_theano["conv3_2"])
    model_theano["conv3_3"] = layers.convolution_layer(model_theano["relu3_2"], conv3_3_W, conv3_3_b, border="same")
    model_theano["relu3_3"] = layers.relu_layer(model_theano["conv3_3"])
    model_theano["conv3_4"] = layers.convolution_layer(model_theano["relu3_3"], conv3_4_W, conv3_4_b, border="same")
    model_theano["relu3_4"] = layers.relu_layer(model_theano["conv3_4"])
    model_theano["pool3"] = layers.pooling_layer(model_theano["relu3_4"], size=(2, 2), stride=(2, 2))

    model_theano["conv4_1"] = layers.convolution_layer(model_theano["pool3"], conv4_1_W, conv4_1_b, border="same")
    model_theano["relu4_1"] = layers.relu_layer(model_theano["conv4_1"])
    model_theano["conv4_2"] = layers.convolution_layer(model_theano["relu4_1"], conv4_2_W, conv4_2_b, border="same")
    model_theano["relu4_2"] = layers.relu_layer(model_theano["conv4_2"])
    model_theano["conv4_3"] = layers.convolution_layer(model_theano["relu4_2"], conv4_3_W, conv4_3_b, border="same")
    model_theano["relu4_3"] = layers.relu_layer(model_theano["conv4_3"])
    model_theano["conv4_4"] = layers.convolution_layer(model_theano["relu4_3"], conv4_4_W, conv4_4_b, border="same")
    model_theano["relu4_4"] = layers.relu_layer(model_theano["conv4_4"])
    model_theano["pool4"] = layers.pooling_layer(model_theano["relu4_4"], size=(2, 2), stride=(2, 2))

    model_theano["conv5_1"] = layers.convolution_layer(model_theano["pool4"], conv5_1_W, conv5_1_b, border="same")
    model_theano["relu5_1"] = layers.relu_layer(model_theano["conv5_1"])
    model_theano["conv5_2"] = layers.convolution_layer(model_theano["relu5_1"], conv5_2_W, conv5_2_b, border="same")
    model_theano["relu5_2"] = layers.relu_layer(model_theano["conv5_2"])
    model_theano["conv5_3"] = layers.convolution_layer(model_theano["relu5_2"], conv5_3_W, conv5_3_b, border="same")
    model_theano["relu5_3"] = layers.relu_layer(model_theano["conv5_3"])
    model_theano["conv5_4"] = layers.convolution_layer(model_theano["relu5_3"], conv5_4_W, conv5_4_b, border="same")
    model_theano["relu5_4"] = layers.relu_layer(model_theano["conv5_4"])
    model_theano["pool5"] = layers.pooling_layer(model_theano["relu5_4"], size=(2, 2), stride=(2, 2))

    model_theano["fc6"] = layers.inner_product_layer(model_theano["pool5"], fc6_W, fc6_b)
    model_theano["relu6"] = layers.relu_layer(model_theano["fc6"])
    model_theano["fc7"] = layers.inner_product_layer(model_theano["relu6"], fc7_W, fc7_b)
    model_theano["relu7"] = layers.relu_layer(model_theano["fc7"])
    model_theano["fc8"] = layers.inner_product_layer(model_theano["relu7"], fc8_W, fc8_b)
    model_theano["prob"] = layers.softmax_layer(model_theano["fc8"])

    # check
    data = np.random.randn(*model_caffe.blobs["data"].data.shape).astype(np.float32) * 10
    model_caffe.blobs["data"].data[:] = data
    model_caffe.forward()
    theano_output = theano.function([model_theano["data"]], model_theano["prob"])(data)
    error = (theano_output.squeeze() - model_caffe.blobs["prob"].data.squeeze()).max()
    assert error < 1e-6

    # save
    print "saving"
    if not os.path.exists(directory_theano):
        os.mkdir(directory_theano)
    sys.setrecursionlimit(100000)
    pickle.dump(model_theano, open(filename_theanomodel, "wb"), protocol=pickle.HIGHEST_PROTOCOL)
    print "done"
Ejemplo n.º 12
0
def build_encoder(x):
    # The encoder uses the deep residual network.
    outputs = []
    pooling = [1, 2, 2, 1]

    shape = x.get_shape().as_list()
    bs = shape[0]
    seq = shape[1]
    temp_shape = [bs * seq] + shape[2:]
    x = tf.reshape(x, temp_shape)
    # print x.get_shape().as_list()

    # layer 0
    with tf.variable_scope("encoder_layer0", reuse=tf.AUTO_REUSE):
        conv0_0 = layers.conv_layer(name="conv0_0",
                                    x=x,
                                    filter_shape=layers.create_variable(
                                        "filter0_0", shape=[7, 7, 3, 96]))
        conv0_0 = layers.batch_normalization(conv0_0, "conv0_0_bn")
        conv0_0 = layers.relu_layer(conv0_0)
        conv0_1 = layers.conv_layer(name="conv0_1",
                                    x=conv0_0,
                                    filter_shape=layers.create_variable(
                                        "filter0_1", shape=[3, 3, 96, 96]))
        conv0_1 = layers.batch_normalization(conv0_1, "conv0_1_bn")
        conv0_1 = layers.relu_layer(conv0_1)
        shortcut0 = layers.conv_layer(name="shortcut",
                                      x=x,
                                      filter_shape=layers.create_variable(
                                          "filter0_2", shape=[1, 1, 3, 96]))
        shortcut0 = layers.batch_normalization(shortcut0, "shortcut0_bn")
        shortcut0 = layers.relu_layer(shortcut0)
        layer0 = layers.pooling_layer("pooling", conv0_1 + shortcut0, pooling)
        outputs.append(layer0)  # [bs * size, 64, 64, 96]

    # layer 1
    with tf.variable_scope("encoder_layer1", reuse=tf.AUTO_REUSE):
        conv1_0 = layers.conv_layer(name="conv1_0",
                                    x=layer0,
                                    filter_shape=layers.create_variable(
                                        "filter1_0", shape=[3, 3, 96, 128]))
        conv1_0 = layers.batch_normalization(conv1_0, "conv1_0_bn")
        conv1_0 = layers.relu_layer(conv1_0)
        conv1_1 = layers.conv_layer(name="conv1_1",
                                    x=conv1_0,
                                    filter_shape=layers.create_variable(
                                        "filter1_1", shape=[3, 3, 128, 128]))
        conv1_1 = layers.batch_normalization(conv1_1, "conv1_1_bn")
        conv1_1 = layers.relu_layer(conv1_1)
        shortcut1 = layers.conv_layer(name="shortcut",
                                      x=layer0,
                                      filter_shape=layers.create_variable(
                                          "filter1_2", shape=[1, 1, 96, 128]))
        shortcut1 = layers.batch_normalization(shortcut1, "shortcut1_bn")
        shortcut1 = layers.relu_layer(shortcut1)
        layer1 = layers.pooling_layer("pooling", conv1_1 + shortcut1, pooling)
        outputs.append(layer1)  # [bs * size, 32, 32, 128]

    # layer 2
    with tf.variable_scope("encoder_layer2", reuse=tf.AUTO_REUSE):
        conv2_0 = layers.conv_layer(name="conv2_0",
                                    x=layer1,
                                    filter_shape=layers.create_variable(
                                        "filter2_0", shape=[3, 3, 128, 256]))
        conv2_0 = layers.batch_normalization(conv2_0, "conv2_0_bn")
        conv2_0 = layers.relu_layer(conv2_0)
        conv2_1 = layers.conv_layer(name="conv2_1",
                                    x=conv2_0,
                                    filter_shape=layers.create_variable(
                                        "filter2_1", shape=[3, 3, 256, 256]))
        conv2_1 = layers.batch_normalization(conv2_1, "conv2_1_bn")
        conv2_1 = layers.relu_layer(conv2_1)
        shortcut2 = layers.conv_layer(name="shortcut",
                                      x=layer1,
                                      filter_shape=layers.create_variable(
                                          "filter2_2", shape=[1, 1, 128, 256]))
        shortcut2 = layers.batch_normalization(shortcut2, "shortcut2_bn")
        shortcut2 = layers.relu_layer(shortcut2)
        layer2 = layers.pooling_layer("pooling", conv2_1 + shortcut2, pooling)
        outputs.append(layer2)  # [bs * size, 16, 16, 256]

    # layer 3
    with tf.variable_scope("encoder_layer3", reuse=tf.AUTO_REUSE):
        conv3_0 = layers.conv_layer(name="conv3_0",
                                    x=layer2,
                                    filter_shape=layers.create_variable(
                                        "filter3_0", shape=[3, 3, 256, 256]))
        conv3_0 = layers.batch_normalization(conv3_0, "conv3_0_bn")
        conv3_0 = layers.relu_layer(conv3_0)
        conv3_1 = layers.conv_layer(name="conv3_1",
                                    x=conv3_0,
                                    filter_shape=layers.create_variable(
                                        "filter3_1", shape=[3, 3, 256, 256]))
        conv3_1 = layers.batch_normalization(conv3_1, "conv3_1_bn")
        conv3_1 = layers.relu_layer(conv3_1)
        layer3 = layers.pooling_layer("pooling", conv3_1, pooling)
        outputs.append(layer3)  # [bs * size, 8, 8, 256]

    # layer 4
    with tf.variable_scope("encoder_layer4", reuse=tf.AUTO_REUSE):
        conv4_0 = layers.conv_layer(name="conv4_0",
                                    x=layer3,
                                    filter_shape=layers.create_variable(
                                        "filter4_0", shape=[3, 3, 256, 256]))
        conv4_0 = layers.batch_normalization(conv4_0, "conv4_0_bn")
        conv4_0 = layers.relu_layer(conv4_0)
        conv4_1 = layers.conv_layer(name="conv4_1",
                                    x=conv4_0,
                                    filter_shape=layers.create_variable(
                                        "filter4_1", shape=[3, 3, 256, 256]))
        conv4_1 = layers.batch_normalization(conv4_1, "conv4_1_bn")
        conv4_1 = layers.relu_layer(conv4_1)
        shortcut4 = layers.conv_layer(name="shortcut",
                                      x=layer3,
                                      filter_shape=layers.create_variable(
                                          "filter4_2", shape=[1, 1, 256, 256]))
        shortcut4 = layers.batch_normalization(shortcut4, "shortcut4_bn")
        shortcut4 = layers.relu_layer(shortcut4)
        layer4 = layers.pooling_layer("pooling", conv4_1 + shortcut4, pooling)
        outputs.append(layer4)  # [bs * size, 4, 4, 256]

    # layer 5
    with tf.variable_scope("encoder_layer5", reuse=tf.AUTO_REUSE):
        conv5_0 = layers.conv_layer(name="conv5_0",
                                    x=layer4,
                                    filter_shape=layers.create_variable(
                                        "filter5_0", shape=[3, 3, 256, 256]))
        conv5_0 = layers.batch_normalization(conv5_0, "conv5_0_bn")
        conv5_0 = layers.relu_layer(conv5_0)
        conv5_1 = layers.conv_layer(name="conv5_1",
                                    x=conv5_0,
                                    filter_shape=layers.create_variable(
                                        "filter5_1", shape=[3, 3, 256, 256]))
        conv5_1 = layers.batch_normalization(conv5_1, "conv5_1_bn")
        conv5_1 = layers.relu_layer(conv5_1)
        shortcut5 = layers.conv_layer(name="shortcut",
                                      x=layer4,
                                      filter_shape=layers.create_variable(
                                          "filter5_2", shape=[1, 1, 256, 256]))
        shortcut5 = layers.batch_normalization(shortcut5, "shortcut5_bn")
        shortcut5 = layers.relu_layer(shortcut5)
        layer5 = layers.pooling_layer("pooling", conv5_1 + shortcut5, pooling)
        outputs.append(layer5)  # [bs * size, 2, 2, 256]

    final_shape = [bs, seq, fc_layer_size[0]]
    # Flatten layer and fully connected layer
    flatten = layers.flatten_layer(layer5)
    outputs.append(flatten)

    with tf.variable_scope("fc_layer", reuse=tf.AUTO_REUSE):
        layer_fc = layers.fully_connected_layer(flatten, fc_layer_size[0],
                                                "fclayer_w", "fclayer_b")
        # layer_fc = layers.batch_normalization(layer_fc, "fc_bn")
        layer_fc = layers.relu_layer(layer_fc)
        outputs.append(layer_fc)  # [bs * size, 1024]

    # [bs, size, 1024]
    return tf.reshape(outputs[-1], final_shape)