Beispiel #1
0
def spliceAI_model(input_shape, num_classes=3):
    """ResNet Version 2 Model builder [b]

    Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as
    bottleneck layer
    First shortcut connection per layer is 1 x 1 Conv2D.
    Second and onwards shortcut connection is identity.
    At the beginning of each stage, the feature map size is halved (downsampled)
    by a convolutional layer with strides=2, while the number of filter maps is
    doubled. Within each stage, the layers have the same number filters and the
    same filter map sizes.
    Features maps sizes:
    conv1  : 32x32,  16
    stage 0: 32x32,  64
    stage 1: 16x16, 128
    stage 2:  8x8,  256

    # Arguments
        input_shape (tensor): shape of input image tensor
        depth (int): number of core convolutional layers
        num_classes (int): number of classes (CIFAR10 has 10)

    # Returns
        model (Model): Keras model instance
    """
    # Start model definition.
    num_filters_in = 16
    num_res_blocks = 4

    inputs = Input(shape=input_shape)
    # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths

    x = Conv1D(32, kernel_size=1, strides=1, padding='same', dilation_rate=1)(inputs)
    y = Conv1D(32, kernel_size=1, strides=1, padding='same', dilation_rate=1)(inputs)

    # RB 1: 32 11 1
    for stack in range(4):
        x = RB_block(x, num_filters=32, kernel_size=11, strides=1, activation='relu', dilation_rate=1)

    y = keras.layers.add([x, y])

    # RB 2: 32 11 4
    for stack in range(4):
        x = RB_block(x, num_filters=32, kernel_size=11, strides=1, activation='relu', dilation_rate=4)

    y = keras.layers.add([x, y])  

    # RB 3: 32 21 10
    for stack in range(4):
        x = RB_block(x, num_filters=32, kernel_size=21, strides=1, activation='relu', dilation_rate=10)

    x = Conv1D(32, kernel_size=1, strides=1, padding='same', dilation_rate=1)(x)

    # now adding up what was shortcut from the prev layers
    x = keras.layers.add([x, y]) 

    x = Conv1D(3, kernel_size=1, strides=1, padding='same', dilation_rate=1)(x)

    x = Dense(num_classes, activation='softmax')(x)

    outputs = Cropping1D(cropping=(1000, 1000))(x)

    model = Model(inputs=inputs, outputs=outputs)
    return model
Beispiel #2
0
def getModelGivenModelOptionsAndWeightInits(args):
    #default params (can be overwritten by providing model_params file as input to the training function)
    filters = 1
    conv1_kernel_size = 6
    control_smoothing = [1, 50]
    counts_loss_weight = 1
    profile_loss_weight = 1

    model_params = get_model_param_dict(args.model_params)
    if 'filters' in model_params:
        filters = int(model_params['filters'])
    if 'conv1_kernel_size' in model_params:
        conv1_kernel_size = int(model_params['conv1_kernel_size'])
    if 'counts_loss_weight' in model_params:
        counts_loss_weight = float(model_params['counts_loss_weight'])
    if 'profile_loss_weight' in model_params:
        profile_loss_weight = float(model_params['profile_loss_weight'])

    print("params:")
    print("filters:" + str(filters))
    print("conv1_kernel_size:" + str(conv1_kernel_size))
    print("counts_loss_weight:" + str(counts_loss_weight))
    print("profile_loss_weight:" + str(profile_loss_weight))

    #load the fixed weights
    tobias_data_dnase_k562 = pickle.load(
        open(
            "/srv/scratch/annashch/bias_correction/enzymatic_bias/tobias/dnase/K562.filtered_AtacBias.pickle",
            'rb'))
    tobias_dnase_pssm_forward = np.transpose(
        tobias_data_dnase_k562.bias['forward'].pssm[0:4])[:, [0, 2, 3, 1]]
    conv1_pwm = np.expand_dims(tobias_dnase_pssm_forward, axis=-1)
    conv1_bias = np.zeros((1, ))
    conv1_frozen_weights = [conv1_pwm, conv1_bias]

    #read in arguments
    seed = args.seed
    init_weights = args.init_weights
    sequence_flank = args.tdb_input_flank[0]
    num_tasks = args.num_tasks

    seq_len = 2 * sequence_flank
    out_flank = args.tdb_output_flank[0]
    out_pred_len = 2 * out_flank
    print(seq_len)
    print(out_pred_len)
    #define inputs
    inp = Input(shape=(seq_len, 4), name='sequence')

    # first convolution without dilation
    first_conv = Conv1D(filters,
                        weights=conv1_frozen_weights,
                        kernel_size=conv1_kernel_size,
                        padding='valid',
                        activation='relu',
                        name='1st_conv')(inp)

    profile_out_prebias_shape = int_shape(first_conv)
    cropsize = int(profile_out_prebias_shape[1] / 2) - int(out_pred_len / 2)
    if profile_out_prebias_shape[1] % 2 == 0:
        crop_left = cropsize
        crop_right = cropsize
    else:
        crop_left = cropsize
        crop_right = cropsize + 1
    print(crop_left)
    print(crop_right)
    profile_out_prebias = Cropping1D(
        (crop_left, crop_right), name='prof_out_crop2match_output')(first_conv)
    profile_out = Conv1D(filters=num_tasks,
                         kernel_size=1,
                         name="profile_predictions")(profile_out_prebias)
    gap_combined_conv = GlobalAveragePooling1D(name='gap')(first_conv)
    count_out = Dense(num_tasks,
                      name="logcount_predictions")(gap_combined_conv)
    model = Model(inputs=[inp], outputs=[profile_out, count_out])
    print("got model")
    model.compile(optimizer=Adam(),
                  loss=[MultichannelMultinomialNLL(1), 'mse'],
                  loss_weights=[profile_loss_weight, counts_loss_weight])
    print("compiled model")
    return model
def get_model_linear_systems(input_profile_names, target_profile_names, scalar_input_names,
                             actuator_names, lookbacks, lookahead, profile_length, std_activation, **kwargs):

    profile_inshape = (profile_lookback, profile_length)
    actuator_inshape = (actuator_lookback + lookahead,)
    num_profiles = len(input_profile_names)
    num_targets = len(target_profile_names)
    num_actuators = len(actuator_names)

    profile_inputs = []
    for i in range(num_profiles):
        profile_inputs.append(
            Input(profile_inshape, name='input_' + input_profile_names[i]))
    if num_profiles > 1:
        profiles = Concatenate(axis=-1)(profile_inputs)
    else:
        profiles = profile_inputs[0]
    profile_response = Dense(
        int(profile_length/2*num_profiles), activation=std_activation)(profiles)
    profile_response = Dense(
        int(profile_length/2*num_profiles), activation=std_activation)(profile_response)
    if profile_lookback > 1:
        profile_response = LSTM(int(profile_length/2*num_profiles), activation=std_activation,
                                recurrent_activation='hard_sigmoid',
                                return_sequences=True)(profile_response)
    else:
        profile_response = Dense(int(profile_length/2*num_profiles),
                                 activation=std_activation)(profile_response)
    profile_response = Dense(int(profile_length/2*num_profiles),
                             activation=std_activation)(profile_response)

    actuator_inputs = []
    actuators = []
    for i in range(num_actuators):
        actuator_inputs.append(
            Input(actuator_inshape, name='input_' + actuator_names[i]))
        actuators.append(
            Reshape((actuator_lookback+lookahead, 1))(actuator_inputs[i]))
    if num_actuators > 1:
        actuators = Concatenate(axis=-1)(actuators)
    else:
        actuators = actuators[0]

    actuator_response = Dense(
        profile_lookback, activation=std_activation)(actuators)
    actuator_response = Dense(
        profile_lookback, activation=std_activation)(actuator_response)
    actuator_response = LSTM(profile_lookback, activation=std_activation,
                             recurrent_activation='hard_sigmoid',
                             return_sequences=True)(actuator_response)
    total_response = Dot(axes=(2, 1))([actuator_response, profile_response])
    total_response = LSTM(int(profile_length/2*num_targets), activation=std_activation,
                          recurrent_activation='hard_sigmoid')(total_response)
    total_response = Dense(int(profile_length*.75*num_targets),
                           activation=std_activation)(total_response)
    total_response = Dense(profile_length*num_targets)(total_response)
    total_response = Reshape((num_targets*profile_length, 1))(total_response)

    targets = []
    for i in range(num_targets):
        targets.append(Cropping1D(cropping=(i*profile_length,
                                            (num_targets-i-1)*profile_length))(total_response))
        targets[i] = Reshape((profile_length,),
                             name='target_' + target_profile_names[i])(targets[i])
    model = Model(inputs=profile_inputs+actuator_inputs, outputs=targets)
    return model
Beispiel #4
0
    def generator(self):

        generator = Sequential()

        # Input: 100 * 1
        # Output: 33 * 32d
        generator.add(
            Dense(input_dim=self.z_dim,
                  units=self.gf_dim * 32 * 33,
                  kernel_initializer=RandomNormal(stddev=0.02)))
        generator.add(LeakyReLU(0.2))
        generator.add(
            Reshape(target_shape=(33, self.gf_dim * 32),
                    input_shape=(self.gf_dim * 32 * 33, )))

        # Input: 33 * 32d
        # Output: 132 * 16d
        generator.add(UpSampling1D(size=4))
        generator.add(
            Conv1D(filters=self.gf_dim * 16, kernel_size=25, padding='same'))
        generator.add((LeakyReLU(0.2)))

        # Input: 132 * 16d
        # Output: 528 * 8d
        generator.add(UpSampling1D(size=4))
        generator.add(
            Conv1D(filters=self.gf_dim * 8, kernel_size=25, padding='same'))
        generator.add((LeakyReLU(0.2)))

        # Input: 528 * 8d
        # Output: 2112 * 4d
        generator.add(UpSampling1D(size=4))
        generator.add(
            Conv1D(filters=self.gf_dim * 4, kernel_size=25, padding='same'))
        generator.add((LeakyReLU(0.2)))

        # Input: 2112 * 4d
        # Output: 8448 * 2d
        generator.add(UpSampling1D(size=4))
        generator.add(
            Conv1D(filters=self.gf_dim * 2, kernel_size=25, padding='same'))
        generator.add((LeakyReLU(0.2)))

        # Input: 8448 * 2d
        # Output: 33792 * d
        generator.add(UpSampling1D(size=4))
        generator.add(
            Conv1D(filters=self.gf_dim, kernel_size=25, padding='same'))
        generator.add((LeakyReLU(0.2)))

        # Input: 33792 * d
        # Output: 135168 * 2
        generator.add(UpSampling1D(size=4))
        generator.add(
            Conv1D(filters=self.output_shape[1],
                   kernel_size=25,
                   padding='same'))
        generator.add((LeakyReLU(0.2)))

        # Input: 135168 * 2
        # Output: 132299 * 2
        generator.add(Cropping1D((1434, 1435)))

        generator.add(Activation('tanh'))

        return generator