Example #1
0
def init_gen_model(state):
    gen_model = nn.Sequential()
    gen_model.add(
        nn.Linear(state['noise_size'],
                  state['g_num_filters'] * 4 * 7 * 7,
                  weight=state['g_init'],
                  use_bias=False))
    gen_model.add(nn.BatchNorm(state['g_num_filters'] * 4 * 7 * 7))
    gen_model.add(nn.Expression(T.nnet.relu))

    gen_model.add(
        nn.Expression(lambda x: T.reshape(x, (x.shape[0], state['g_num_filters'
                                                                ] * 4, 7, 7))))

    gen_model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['g_num_filters'] * 4,
                           num_channels=state['g_num_filters'] * 2,
                           step=(2, 2),
                           border_mode=(1, 1),
                           use_bias=False,
                           weight=state['g_conv_init']))
    gen_model.add(nn.BatchNorm(state['g_num_filters'] * 2))
    gen_model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, num_filters, 14, 14)

    gen_model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['g_num_filters'] * 2,
                           num_channels=state['g_num_filters'],
                           step=(2, 2),
                           border_mode=(1, 1),
                           use_bias=False,
                           weight=state['g_conv_init']))
    gen_model.add(nn.BatchNorm(state['g_num_filters']))
    gen_model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, input_channels, 28, 28)

    gen_model.add(
        nn.Deconvolutional(filter_size=(3, 3),
                           num_filters=state['g_num_filters'],
                           num_channels=state['input_channels'],
                           step=(1, 1),
                           border_mode=(1, 1),
                           use_bias=True,
                           weight=state['g_conv_init']))
    # gen_model.add(nn.Expression(T.nnet.sigmoid))

    # out_shape == (b, input_channels, 28, 28)

    return gen_model
def init_dcgan_disc(state):
    disc_model = disc_shared_structure(state)

    disc_model.add(nn.Linear(state['d_num_filters']*4*7*7, 1, weight=state['d_init'], use_bias=True))
    disc_model.add(nn.Expression(lambda x: T.flatten(x)))

    return disc_model
def disc_shared_structure(state):
    model = nn.Sequential()
    model.add(nn.Convolutional(filter_size=(3, 3),
                               num_filters=state['d_num_filters'],
                               num_channels=state['input_channels'],
                               step=(1, 1), border_mode=(1, 1),
                               weight=state['d_conv_init'], use_bias=False))
    model.add(nn.BatchNorm(state['d_num_filters']))
    # model.add(nn.Expression(T.nnet.relu))
    model.add(nn.LeakyRectify())
    # out_shape == (b, num_filters, 28, 28)

    model.add(nn.Convolutional(filter_size=(4, 4),
                               num_filters=state['d_num_filters']*2,
                               num_channels=state['d_num_filters'],
                               step=(2, 2), border_mode=(1, 1),
                               weight=state['d_conv_init'], use_bias=False))
    model.add(nn.BatchNorm(state['d_num_filters']*2))
    # model.add(nn.Expression(T.nnet.relu))
    model.add(nn.LeakyRectify())
    # out_shape == (b, num_filters, 14, 14)

    model.add(nn.Convolutional(filter_size=(4, 4),
                               num_filters=state['d_num_filters']*4,
                               num_channels=state['d_num_filters']*2,
                               step=(2, 2), border_mode=(1, 1),
                               weight=state['d_conv_init'], use_bias=False))
    model.add(nn.BatchNorm(state['d_num_filters']*4))
    # model.add(nn.Expression(T.nnet.relu))
    model.add(nn.LeakyRectify())
    # out_shape == (b, num_filters, 7, 7)

    model.add(nn.Expression(lambda x: T.flatten(x, 2)))

    return model
Example #4
0
def disc_shared_structure(state):
    model = nn.Sequential()
    if state.has_key('init_d') and state['init_d'] is not None:
        model.add(
            nn.Linear(state['input_size'],
                      state['hidden_size'],
                      weight=state['init_d']))
    else:
        model.add(nn.Linear(state['input_size'], state['hidden_size']))
    model.add(nn.Expression(T.nnet.relu))
    if state.has_key('init_d') and state['init_d'] is not None:
        model.add(
            nn.Linear(state['hidden_size'],
                      state['hidden_size'],
                      weight=state['init_d']))
    else:
        model.add(nn.Linear(state['hidden_size'], state['hidden_size']))
    model.add(nn.Expression(T.nnet.relu))

    return model
Example #5
0
def init_gen_model(state):
    gen_model = nn.Sequential()
    gen_model.add(
        nn.Linear(state['noise_size'],
                  state['hidden_size'],
                  weight=state['init_g']))
    gen_model.add(nn.BatchNorm(state['hidden_size']))
    gen_model.add(nn.Expression(T.nnet.relu))
    gen_model.add(
        nn.Linear(state['hidden_size'],
                  state['hidden_size'],
                  weight=state['init_g']))
    gen_model.add(nn.BatchNorm(state['hidden_size']))
    gen_model.add(nn.Expression(T.nnet.relu))
    gen_model.add(
        nn.Linear(state['hidden_size'],
                  state['input_size'],
                  weight=state['init_g']))

    return gen_model
Example #6
0
def init_dcgan_disc(state):
    disc_model = disc_shared_structure(state)

    disc_model.add(
        nn.MinibatchDisc(state['d_num_filters'] * 4 * 4 * 4,
                         num_kernels=100,
                         dim_per_kernel=5,
                         concat=True))
    disc_model.add(
        nn.Linear(state['d_num_filters'] * 4 * 4 * 4 + 100,
                  1,
                  weight=state['d_init'],
                  use_bias=True))
    disc_model.add(nn.Expression(lambda x: T.flatten(x)))

    return disc_model
Example #7
0
def init_decoder(state):
    # inp_shape == (b, d_num_filters*4, 8, 8)
    model = nn.Sequential()
    model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['d_num_filters'] * 4,
                           num_channels=state['d_num_filters'] * 2,
                           step=(2, 2),
                           border_mode=(1, 1),
                           weight=state['d_conv_init'],
                           use_bias=False,
                           name='d_dec_conv1'))
    model.add(nn.BatchNorm(state['d_num_filters'] * 2))
    model.add(nn.LeakyRectify())
    # model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, d_num_filters*2, 16, 16)

    model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['d_num_filters'] * 2,
                           num_channels=state['d_num_filters'],
                           step=(2, 2),
                           border_mode=(1, 1),
                           weight=state['d_conv_init'],
                           use_bias=False,
                           name='d_dec_conv2'))
    model.add(nn.BatchNorm(state['d_num_filters']))
    model.add(nn.LeakyRectify())
    # model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, d_num_filters, 32, 32)

    model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['d_num_filters'],
                           num_channels=state['input_channels'],
                           step=(2, 2),
                           border_mode=(1, 1),
                           weight=state['d_conv_init'],
                           use_bias=False,
                           name='d_dec_conv3'))
    model.add(nn.BatchNorm(state['input_channels']))
    model.add(nn.Expression(T.tanh))
    # out_shape == (b, input_channels, 64, 64)

    return model
Example #8
0
def init_gen_model(state):
    gen_model = nn.Sequential()
    gen_model.add(
        nn.Linear(state['noise_size'],
                  state['g_num_filters'] * 4 * 4 * 4,
                  weight=state['g_init'],
                  use_bias=False))
    gen_model.add(nn.BatchNorm(state['g_num_filters'] * 4 * 4 * 4))
    gen_model.add(nn.Expression(T.nnet.relu))

    gen_model.add(
        nn.Expression(lambda x: T.reshape(x, (x.shape[0], state['g_num_filters'
                                                                ] * 4, 4, 4))))

    gen_model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['g_num_filters'] * 4,
                           num_channels=state['g_num_filters'] * 4,
                           step=(2, 2),
                           border_mode=(1, 1),
                           use_bias=False,
                           weight=state['g_conv_init']))
    gen_model.add(nn.BatchNorm(state['g_num_filters'] * 4))
    gen_model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, g_num_filters*6, 8, 8)

    gen_model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['g_num_filters'] * 4,
                           num_channels=state['g_num_filters'] * 2,
                           step=(2, 2),
                           border_mode=(1, 1),
                           use_bias=False,
                           weight=state['g_conv_init']))
    gen_model.add(nn.BatchNorm(state['g_num_filters'] * 2))
    gen_model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, g_num_filters*2, 16, 16)

    gen_model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['g_num_filters'] * 2,
                           num_channels=state['g_num_filters'],
                           step=(2, 2),
                           border_mode=(1, 1),
                           use_bias=False,
                           weight=state['g_conv_init']))
    gen_model.add(nn.BatchNorm(state['g_num_filters']))
    gen_model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, g_num_filters, 32, 32)

    gen_model.add(
        nn.Deconvolutional(filter_size=(4, 4),
                           num_filters=state['g_num_filters'],
                           num_channels=state['input_channels'],
                           step=(2, 2),
                           border_mode=(1, 1),
                           use_bias=True,
                           weight=state['g_conv_init']))
    gen_model.add(nn.Expression(T.tanh))
    # out_shape == (b, input_channels, 64, 64)

    return gen_model
Example #9
0
def disc_shared_structure(state):
    # inp_shape == (b, input_channels, 64, 64)
    model = nn.Sequential()
    if state['dropout'] > 0:
        model.add(nn.Dropout(state['dropout']))
    model.add(
        nn.Convolutional(filter_size=(4, 4),
                         num_filters=state['d_num_filters'],
                         num_channels=state['input_channels'],
                         step=(2, 2),
                         border_mode=(1, 1),
                         weight=state['d_conv_init'],
                         use_bias=False,
                         name='d_conv1'))
    model.add(nn.BatchNorm(state['d_num_filters']))
    # model.add(nn.LeakyRectify())
    model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, num_filters, 32, 32)

    model.add(
        nn.Convolutional(filter_size=(4, 4),
                         num_filters=state['d_num_filters'] * 2,
                         num_channels=state['d_num_filters'],
                         step=(2, 2),
                         border_mode=(1, 1),
                         weight=state['d_conv_init'],
                         use_bias=False,
                         name='d_conv2'))
    model.add(nn.BatchNorm(state['d_num_filters'] * 2))
    # model.add(nn.LeakyRectify())
    model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, num_filters, 16, 16)

    model.add(
        nn.Convolutional(filter_size=(4, 4),
                         num_filters=state['d_num_filters'] * 4,
                         num_channels=state['d_num_filters'] * 2,
                         step=(2, 2),
                         border_mode=(1, 1),
                         weight=state['d_conv_init'],
                         use_bias=False))
    model.add(nn.BatchNorm(state['d_num_filters'] * 4))
    # model.add(nn.LeakyRectify())
    model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, num_filters, 8, 8)

    model.add(
        nn.Convolutional(filter_size=(4, 4),
                         num_filters=state['d_num_filters'] * 4,
                         num_channels=state['d_num_filters'] * 4,
                         step=(2, 2),
                         border_mode=(1, 1),
                         weight=state['d_conv_init'],
                         use_bias=False))
    model.add(nn.BatchNorm(state['d_num_filters'] * 4))
    # model.add(nn.LeakyRectify())
    model.add(nn.Expression(T.nnet.relu))
    # out_shape == (b, num_filters, 4, 4)

    model.add(nn.Expression(lambda x: T.flatten(x, 2)))

    return model
np.random.seed(12345)

############################
# Init model & parameters
############################

# 1) Eneregy discriminator
disc_model = nn.Sequential()
disc_model.add(nn.Convolutional(filter_size=(3, 3),
                                num_filters=state['d_num_filters'],
                                num_channels=state['input_channels'],
                                step=(1, 1), border_mode=(1, 1),
                                weight=state['d_init'], use_bias=False,
                                name='d_conv1'))
disc_model.add(nn.BatchNorm(state['d_num_filters'], name='d_bn1'))
disc_model.add(nn.Expression(T.nnet.relu))
# out_shape == (b, num_filters, 32, 32)

disc_model.add(nn.Convolutional(filter_size=(4, 4),
                                num_filters=state['d_num_filters']*2,
                                num_channels=state['d_num_filters'],
                                step=(2, 2), border_mode=(1, 1),
                                weight=state['d_init'], use_bias=False,
                                name='d_conv2'))
disc_model.add(nn.BatchNorm(state['d_num_filters']*2, name='d_bn2'))
disc_model.add(nn.Expression(T.nnet.relu))
# out_shape == (b, num_filters, 16, 16)

disc_model.add(nn.Convolutional(filter_size=(4, 4),
                                num_filters=state['d_num_filters']*4,
                                num_channels=state['d_num_filters']*2,