Beispiel #1
0
def iW_D(D, img_shape, cond_dim=2400, lmbd=10.):
    real_img = Input(shape=img_shape, name='REAL_IMG')
    fake_img = Input(shape=img_shape, name='FAKE_IMG')
    right_cond = Input(shape=(cond_dim, ), name='RIGHT_COND')
    wrong_cond = Input(shape=(cond_dim, ),
                       name='WRONG_COND')  # w.r.t. real_img.

    pos = D([real_img, right_cond])
    neg1 = D([fake_img, right_cond])
    neg2 = D([real_img, wrong_cond])
    neg = Average()([neg1, neg2])
    score = Subtract(name='Score_by_D')([pos, neg])

    fuzzy_img = Interpolation(name='FUZZY_IMG')([real_img, fake_img])
    penalty1 = D([fuzzy_img, right_cond])
    gradnorm1 = GradNorm()([penalty1, fuzzy_img, right_cond])

    fuzzy_cond = Interpolation(name='FUZZY_COND')([right_cond, wrong_cond])
    penalty2 = D([real_img, fuzzy_cond])
    gradnorm2 = GradNorm()([penalty2, real_img, fuzzy_cond])

    model = Model(inputs=[real_img, fake_img, right_cond, wrong_cond],
                  outputs=[score, gradnorm1, gradnorm2])
    D.trainable = True
    model.compile(loss=[loss_for_D, 'mse', 'mse'],
                  optimizer=myOpt,
                  loss_weights=[1.0, lmbd / 2., lmbd / 2.])
    return model
Beispiel #2
0
def inception_net(_input):
    #x = Reshape((28, 28, 1))(_input)
    #x = Conv2D(32, (3, 3), strides=((1, 1)))(x)
    #x = Activation('relu')(x)
    x = Conv2D(16, (3, 3),strides=(2, 2))(_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(48, (3, 3),strides=((1, 1)))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    #x = MaxPooling2D(((3, 3)), strides=(2,2))(x)
    x = mniny_inception_module(x, 1)
    x = mniny_inception_module(x, 2)
    #x = MaxPooling2D(((3, 3)), strides=(2,2))(x)
    x = mniny_inception_module(x, 2)
    x, soft1 = mniny_inception_module(x, 3, True)
    x = mniny_inception_module(x, 3)
    x = mniny_inception_module(x, 3)
    x, soft2 = mniny_inception_module(x, 4, True)
    x = MaxPooling2D(((3, 3)), strides=(2,2))(x)
    x = mniny_inception_module(x, 4)
    x = mniny_inception_module(x, 5)
    x = AveragePooling2D(((5, 5)), strides=((1, 1)))(x)
    x = Dropout(0.4)(x)
    x = Flatten()(x)
    x = Dense(17)(x)
    soft3 = Activation('hard_sigmoid')(x)
    out = Average()([soft1, soft2, soft3])
    return out
Beispiel #3
0
def inception_net(_input):
    '''
    My version of the 'inception net',
    tailored specifically for MNIST
    '''
    x = Reshape((28, 28, 1))(_input)
    x = Conv2D(16, (3, 3), strides=(2, 2))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(48, (3, 3), strides=(1, 1))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = mniny_inception_module(x, 1)
    x = mniny_inception_module(x, 2)
    x = mniny_inception_module(x, 2)
    x, soft1 = mniny_inception_module(x, 3, True)

    x = mniny_inception_module(x, 3)
    x = mniny_inception_module(x, 3)
    x, soft2 = mniny_inception_module(x, 4, True)

    x = MaxPooling2D((3, 3), strides=(2, 2))(x)
    x = mniny_inception_module(x, 4)
    x = mniny_inception_module(x, 5)
    x = AveragePooling2D((5, 5), strides=(1, 1))(x)
    x = Dropout(0.4)(x)
    x = Flatten()(x)
    x = Dense(10)(x)
    soft3 = Activation('softmax')(x)
    out = Average()([soft1, soft2, soft3])
    return out
Beispiel #4
0
def M8_M5_average_merge(models_folder):
    truncated_models_outputs = []
    models = []
    for model_file in os.listdir(models_folder):
        # Load single model
        model_path = os.path.join(models_folder, model_file)
        current_model = load_model(model_path)
        models.append(current_model)

        # Get rid of softmax activation layer so we get raw percentages
        current_model.pop()
        truncated_models_outputs.append(current_model.layers[-1].output)

    new_models = []
    input_layer = Input(shape=(SEQUENCE_LENGTH - 1, len(labels)))
    for model in models:
        model = model(input_layer)
        new_models.append(model)

    # Merge all truncated models and add softmax at the end
    output = Average()([model for model in new_models])
    output = Activation("softmax", name="predictions")(output)

    model = Model(input_layer, output)
    optimizer = RMSprop(lr=0.01)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=["accuracy"])
    return model
Beispiel #5
0
def inception_model(X):
    x = Reshape((img_cols, img_rows, 1))(X)
    x = Conv2D(16, (3, 3), strides=(2, 2))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(48, (3, 3), strides=(1, 1))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = inception_module(x, 1)
    x = inception_module(x, 2)
    x = inception_module(x, 2)
    x, soft1 = inception_module(x, 3, True)

    x = inception_module(x, 3)
    x = inception_module(x, 3)
    x, soft2 = inception_module(x, 4, True)

    x = MaxPooling2D((3, 3), strides=(2, 2))(x)
    x = inception_module(x, 4)
    x = inception_module(x, 5)
    x = AveragePooling2D((5, 5), strides=(1, 1))(x)
    x = Dropout(0.4)(x)
    x = Flatten()(x)
    x = Dense(num_classes)(x)

    soft3 = Activation('softmax')(x)
    out = Average()([soft1, soft2, soft3])

    return out
def load_defense_model(models=['xception','resnet50'], \
        dist_pairs = [('xception', 'resnet50')], \
        aug_or_rotate='aug', \
        make_model=False, \
        grad_dict=None):

    if grad_dict is not None:
        grad_dict.clear()

    input_img = Input(shape=(299, 299, 3), name='src_input_img')
    noise_input = Input(batch_shape=(1, ), name='augmentation_scale')

    if aug_or_rotate not in ['aug', 'rot', 'none']:
        raise ValueError('invalid value for aug_or_rotate')

    model_outputs = []
    model_outputs_dict = {}
    for model_name in models:
        print('preparing', model_name)
        model_pred = load_branch(model_name,
                                 input_img,
                                 aug_or_rotate,
                                 noise_input,
                                 False,
                                 load_weights=True)
        model_outputs.append(model_pred)
        model_outputs_dict[model_name] = model_pred

        # print(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))

    print('preparing avg pred')
    model_outputs_avg = Average()(model_outputs)

    print('preparing gradients')
    pred_diff = None
    for m1, m2 in dist_pairs:
        tv = TotalVariationDistance(name='dist_{0}_{1}'.format(m1,m2))(\
                [model_outputs_dict[m1], model_outputs_dict[m2]])
        grad = Gradients(-1, name='grad_{0}_{1}'.format(m1,
                                                        m2))([input_img, tv])
        if grad_dict is not None:
            if make_model:
                grad_dict[(m1, m2)] = grad
            else:
                grad_dict[(m1,m2)] = K.function(\
                        [input_img, noise_input, K.learning_phase()], \
                        [grad])

    if make_model:
        grad_dict['PRED'] = model_outputs_avg
    else:
        grad_dict['PRED'] = K.function(
            [input_img, noise_input,
             K.learning_phase()], [model_outputs_avg])

    if make_model:
        return Model(inputs=[input_img, noise_input], \
                outputs=[grad_dict[(m1,m2)] for m1,m2 in dist_pairs]+[grad_dict['PRED']])
    else:
        return None
def pred_ensemble():

    _, _, val, val_label = get_data()

    ensemble = []
    for weights in glob.glob("models/keras/*.h5"):
        model = build_network()
        model.load_weights(weights)
        ensemble.append(model)
    x = Input((img_rows * img_cols, ))
    outputs = [model(x) for model in ensemble]
    y = Average()(outputs)

    ensemble_model = Model(x, y)

    preds = ensemble_model.predict(val)

    preds = np.argmax(preds, axis=1)
    print(preds)
    print(val_labels)
    metrics = [accuracy_score, recall_score, precision_score, f1_score]

    for metric in metrics:
        if str(metric.__name__) == "accuracy_score":
            print(metric.__name__, ":", metric(np.array(val_label), preds))
        else:
            print(metric.__name__, ":",
                  metric(np.array(val_label), preds, average='macro'))
    def __init__(self, image_shape, structure, data,
                 v_noise=0.1, n_pack=2, pre_epochs=3, activation="relu",
                 model_dir="./defensive_models/"):
        """
        Train different autoencoders.
        Demo code for graybox scenario.

        pre_epochs: How many epochs do we train before fine-tuning.
        n_pack: Number of autoencoders we want to train at once.
        """
        self.v_noise = v_noise
        self.n_pack = n_pack
        self.model_dir = model_dir
        pack = []

        for i in range(n_pack):
            dae = DenoisingAutoEncoder(image_shape, structure, v_noise=v_noise,
                                       activation=activation, model_dir=model_dir)
            dae.train(data, "", if_save=False, num_epochs=pre_epochs)
            pack.append(dae.model)

        shared_input = Input(shape=image_shape, name="shared_input")
        outputs = [dae(shared_input) for dae in pack]
        avg_output = Average()(outputs)
        delta_outputs = [add([avg_output, Lambda(lambda x: -x)(output)])
                         for output in outputs]

        self.model = Model(inputs=shared_input, outputs=outputs+delta_outputs)
Beispiel #9
0
def discriminator_model():
    # PatchGAN
    inputs = Input(shape=patch_shape)
    x = Convolution2D(filters=channel_rate,
                      kernel_size=(3, 3),
                      strides=(2, 2),
                      padding="same")(inputs)
    x = BatchNormalization()(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = Convolution2D(filters=2 * channel_rate,
                      kernel_size=(3, 3),
                      strides=(2, 2),
                      padding="same")(x)
    x = BatchNormalization()(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = Convolution2D(filters=4 * channel_rate,
                      kernel_size=(3, 3),
                      strides=(2, 2),
                      padding="same")(x)
    x = BatchNormalization()(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = Convolution2D(filters=4 * channel_rate,
                      kernel_size=(3, 3),
                      strides=(2, 2),
                      padding="same")(x)
    x = BatchNormalization()(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = non_local_block(x, mode='embedded', compression=2)

    x = Flatten()(x)
    outputs = Dense(units=1, activation='sigmoid')(x)
    model = Model(inputs=inputs, outputs=outputs, name='PatchGAN')
    # model.summary()

    # discriminator
    inputs = Input(shape=image_shape)

    list_row_idx = [(i * channel_rate, (i + 1) * channel_rate)
                    for i in range(int(image_shape[0] / patch_shape[0]))]
    list_col_idx = [(i * channel_rate, (i + 1) * channel_rate)
                    for i in range(int(image_shape[1] / patch_shape[1]))]

    list_patch = []
    for row_idx in list_row_idx:
        for col_idx in list_col_idx:
            x_patch = Lambda(lambda z: z[:, row_idx[0]:row_idx[1], col_idx[0]:
                                         col_idx[1], :])(inputs)
            list_patch.append(x_patch)

    x = [model(patch) for patch in list_patch]
    outputs = Average()(x)
    model = Model(inputs=inputs, outputs=outputs, name='Discriminator')
    #model.summary()
    return model
Beispiel #10
0
def train():

    imgs = np.load(train_set)
    print imgs.shape
    sals = np.load(sal_set)
    print sals.shape
    input_dim = (img_size, img_size, 1)

    saliency_network = base_network(input_dim)

    input_img = Input(shape=input_dim)
    out1 = saliency_network(input_img)

    input_img2 = AveragePooling2D(pool_size=(2, 2))(input_img)
    out2 = saliency_network(input_img2)
    out2 = UpSampling2D(size=(2, 2))(out2)
    out2 = Convolution2D(1, 3, 3, border_mode='same', activation='relu')(out2)

    input_img3 = AveragePooling2D(pool_size=(2, 2))(input_img2)
    out3 = saliency_network(input_img3)
    out3 = UpSampling2D(size=(2, 2))(out3)
    out3 = Convolution2D(1, 3, 3, border_mode='same', activation='relu')(out3)
    out3 = UpSampling2D(size=(2, 2))(out3)
    out3 = Convolution2D(1, 3, 3, border_mode='same', activation='relu')(out3)

    output_sal = Average()([out1, out2, out3])

    model = Model(input=input_img, output=output_sal)
    model.summary()

    # train
    model_checkpoint = ModelCheckpoint(checkpoint_file, monitor='loss')
    rms = RMSprop()
    model.compile(loss='mse', optimizer=rms)

    lr = 1e-3
    for i in range(1):  # num times to drop learning rate
        print('Learning rate: {0}'.format(lr))
        K.set_value(model.optimizer.lr, lr)
        model_data = np.expand_dims(imgs, axis=3)
        saliency_data = np.expand_dims(sals, axis=3)
        history = model.fit([model_data], [saliency_data],
                            validation_split=0.1,
                            batch_size=128,
                            nb_epoch=nb_epoch,
                            callbacks=[model_checkpoint])
        lr = lr * .1
        print(history.history.keys())
        # summarize history for accuracy
        # summarize history for loss
        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.show()
Beispiel #11
0
def two_stream_model(spatial_weights, temporal_weights):

    spatial_stream = finetuned_resnet(include_top=True, weights_dir=spatial_weights)

    temporal_stream = CNN(input_shape, temporal_weights)

    spatial_output = spatial_stream.output
    temporal_output = temporal_stream.output

    fused_output = Average(name='fusion_layer')([spatial_output, temporal_output])

    model = Model(inputs=[spatial_stream.input, temporal_stream.input],
     outputs=fused_output, name=two_stream)

    return model
def ensemble(models, model_input):

    ymodels = [model(model_input) for model in models]
    yAvg = Average()(ymodels)
    modelENS = Model(inputs=model_input, outputs=yAvg, name='ensemble')

    adam = Adam()
    modelENS.compile(loss='sparse_categorical_crossentropy', optimizer=adam)
    print("model summary: ")
    print(modelENS.summary())

    #     outputs = [model.outputs for model in models]
    #     Y = Average()(outputs)
    #     model = Model(model_input, Y, name='ensemble')
    return modelENS
Beispiel #13
0
    def __build_model(self):
        input_layer_positive = Input(shape=(1, ), name='input_node_positive')
        input_layer_negative = Input(shape=(self._model_params.num_negative +
                                            1, ),
                                     name='input_node_negative')
        input_layer_relation = Input(shape=(1, ), name='input_relation')

        # keep track of all input and output layers
        input_layer_list = [
            input_layer_positive, input_layer_negative, input_layer_relation
        ]
        output_layer_list = []

        # build
        input_layers, score_layer, l2_offset_layer = self._doubler.build_model(
            self._model_params, self._gp, input_layer_positive,
            input_layer_negative, input_layer_relation)
        output_layer_list.append(score_layer)
        input_layer_list.extend(input_layers)

        # combine score layers
        if len(output_layer_list) > 1:
            score_layer_total = Average()(output_layer_list)
        else:
            score_layer_total = output_layer_list[0]

        # build model
        score_layer_total = Activation(
            self._model_params.activation_fn,
            name='output_overall_score')(score_layer_total)

        if l2_offset_layer is not None:
            l2_offset_layer = Activation(
                'relu', name='output_overall_offset')(l2_offset_layer)
            self._model = Model(inputs=input_layer_list,
                                outputs=[score_layer_total, l2_offset_layer])
            self._model.compile(loss=self.__custom_loss,
                                optimizer=self._model_params.optimizer)
        else:
            self._model = Model(inputs=input_layer_list,
                                outputs=[score_layer_total])
            self._model.compile(loss=self._model_params.loss,
                                optimizer=self._model_params.optimizer)

        # print model
        print(self._model.summary())
Beispiel #14
0
def difCNN(depth, filters=64):

    # first input model
    inpt3 = Input(shape=(None, None, 1))
    inpt1 = Input(shape=(None, None, 1))
    # 1st layer, Conv+relu
    x1 = Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1),
                padding='same')(inpt1)
    x1 = Activation('relu')(x1)
    # 15 layers, Conv+BN+relu
    for i in range(depth):
        x1 = Conv2D(filters=64,
                    kernel_size=(3, 3),
                    strides=(1, 1),
                    padding='same')(x1)
        x1 = BatchNormalization(axis=-1, epsilon=1e-3)(x1)
        x1 = Activation('relu')(x1)
    # last layer, Conv
    x1 = Conv2D(filters=1, kernel_size=(3, 3), strides=(1, 1),
                padding='same')(x1)

    # first input model
    inpt2 = Input(shape=(None, None, 1))
    # 1st layer, Conv+relu
    x2 = Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1),
                padding='same')(inpt2)
    x2 = Activation('relu')(x2)
    # 15 layers, Conv+BN+relu
    for ii in range(depth):
        x2 = Conv2D(filters=64,
                    kernel_size=(3, 3),
                    strides=(1, 1),
                    padding='same')(x2)
        x2 = BatchNormalization(axis=-1, epsilon=1e-3)(x2)
        x2 = Activation('relu')(x2)
    # last layer, Conv
    x2 = Conv2D(filters=1, kernel_size=(3, 3), strides=(1, 1),
                padding='same')(x2)
    sub = Average()([x1, x2])
    #sub = Subtract()([inpt3, added])   # input - noise

    model = Model(inputs=[inpt1, inpt2, inpt3], outputs=sub)
    return model
Beispiel #15
0
def get_output_2D_hal(input_2D, model_2D, model_hal, extract_2D_hal, n_classes): 
	intermediate_model_2D = Model(inputs = model_2D.layers[0].input, 
									outputs = model_2D.layers[extract_2D_hal].output)
	intermediate_model_2D.name = "2D_EXTRACT_MDL"
	in_2D = intermediate_model_2D(input_2D)
	intermediate_model_hal = Model(inputs = model_hal.layers[0].input, 
									outputs = model_hal.layers[extract_2D_hal].output)
	intermediate_model_hal.name = "HAL_EXTRACT_MDL"
	# intermediate_model_hal.layers[len(intermediate_model_hal.layers)-1].name = "HAL_SOFTMAX_OUT"
	in_hal = intermediate_model_hal(input_2D)
	pool_window_shape = K.int_shape(in_2D)
	output_2D_hal = Average()([in_2D, in_hal])
	# output_2D_hal = AveragePooling1D(pool_window_shape[ROW_AXIS],
 #                           strides=1)(output_2D_hal)
	# output_2D_hal = Flatten()(output_2D_hal)
	# output_2D_hal = Dense(units=n_classes,
 #        				kernel_initializer="he_normal",
 #        				activation="softmax", name = '2DHAL_AVE_OUT')(output_2D_hal)
 	output_2D_hal = Activation("softmax", name = '2DHAL')(output_2D_hal)
	return output_2D_hal
Beispiel #16
0
    print('error:', str((1. - score[1]) * 100) + '%')

    return score


if __name__ == "__main__":

    model, val, val_label = train(True)
    ensemble = []
    for weights in glob.glob("models/keras/*.h5"):
        model = build_network()
        model.load_weights(weights)
        ensemble.append(model)
    x = Input((img_rows * img_cols, ))
    outputs = [model(x) for model in ensemble]
    y = Average()(outputs)

    ensemble_model = Model(x, y)

    preds = ensemble_model.predict(val)

    preds = np.argmax(preds, axis=1)
    val_label = np.argmax(val_label, axis=1)
    metrics = [accuracy_score]
    #, recall_score, precision_score, f1_score]

    for metric in metrics:
        print(type(metric).__name__, ":", metric(np.array(val_label), preds))

    #for i in range(20):
    #    train()
def load_source_model(models=['xception','resnet50'], \
        pred_models=None, \
        is_targeted=False, \
        # output_avg_pred=False, \
        loss_pow=None, \
        aug_or_rotate='aug', \
        grad_dict=None, \
        make_model=False, \
        load_weights=True):

    # the gradients are supposed to be subtracted
    grad_sign = 1 if is_targeted else -1

    if grad_dict is not None:
        grad_dict.clear()

    input_img = Input(shape=(299, 299, 3), name='src_input_img')
    noise_input = Input(batch_shape=(1, ), name='augmentation_scale')

    if aug_or_rotate not in ['aug', 'rot', 'none']:
        raise ValueError('invalid value for aug_or_rotate')

    target_labels = Input(shape=(1000, ), name='target_labels')
    pred_max = Lambda(lambda x: K.max(x, axis=1, keepdims=True),
                      name='target_labels_pred_max')(target_labels)
    y = Equal(name='target_pred_equal')([target_labels, pred_max])
    y = Lambda(lambda x: K.cast(x, K.floatx()), name='type_cast')(y)
    y = Lambda(lambda x: x / K.sum(x, axis=1, keepdims=True),
               name='pseudo_label')(y)

    model_outputs = []
    for model_name in models:
        print('preparing', model_name)
        model_pred = load_branch(model_name,
                                 input_img,
                                 aug_or_rotate,
                                 noise_input,
                                 False,
                                 load_weights=load_weights)
        if pred_models is not None and model_name in pred_models:
            model_outputs.append(model_pred)
        print('preparing gradients', model_name)
        branch_loss = CategoricalCrossEntropy(from_logits=False,
                                              name=model_name +
                                              '_ce')([y, model_pred])
        #if loss_pow:
        #    branch_loss = K.pow(branch_loss, loss_pow)

        branch_grads = Gradients(grad_sign, name='grad_' +
                                 model_name)([input_img, branch_loss])
        if grad_dict is not None:
            if make_model:
                grad_dict[model_name] = branch_grads
            else:
                grad_dict[model_name] = K.function(\
                        [input_img, noise_input, target_labels, K.learning_phase()], \
                        [branch_grads])
        print('finished', model_name)

    print('loading of source models finished')
    if pred_models and grad_dict is not None:
        if make_model:
            if len(model_outputs) == 1:
                grad_dict['PRED'] = Lambda(lambda x: x,
                                           name='PRED')(model_outputs[0])
            else:
                grad_dict['PRED'] = Average(name='PRED')(model_outputs)
        else:
            pred_func = K.function(
                [input_img, noise_input,
                 K.learning_phase()],
                [sum(model_outputs) / len(model_outputs)])
            grad_dict['PRED'] = pred_func

    if make_model:
        output_list = models
        if pred_models:
            output_list.append('PRED')
        #learning_phase = Input(batch_shape=(), \
        #    dtype='bool', \
        #    tensor=K.learning_phase(), \
        #    name='learning_phase')
        #grad_inputs = [input_img, noise_input, target_labels, learning_phase]
        grad_inputs = [input_img, noise_input, target_labels]
        model = Model(inputs=grad_inputs, \
                outputs=[grad_dict[m] \
                for m in output_list])
    else:
        model = None

    print('gradient computation finished')
    return model
Beispiel #18
0
    def neighModule(self,
                    kpm=True,
                    neighEnc=True,
                    visEnc=False,
                    socPool=False,
                    vStt=False):
        self.obsModule(kpm=kpm, neighEnc=neighEnc, visEnc=visEnc, vStt=vStt)
        self.weight = []
        self.modeN_all = []
        self.pose_all = []
        self.gaze_all = []
        self.pose_stt = []
        self.gaze_stt = []
        self.visP_all = []
        self.visG_all = []

        observations = []
        for n in range(self.n_neighbors):
            obs_input = []
            if (kpm):
                shape = (self.n_kpm, 2) if (self.n_kpm > 1) else (2, )
                self.pose_all.append(Input(shape=shape, name='pose_%d' % n))
                self.gaze_all.append(Input(shape=shape, name='gaze_%d' % n))
                obs_input.append(self.pose_all[n])
                obs_input.append(self.gaze_all[n])
            if (vStt):
                self.pose_stt.append(
                    Input(shape=(self.n_enc, 2), name='pStt_%d' % n))
                self.gaze_stt.append(
                    Input(shape=(self.n_enc, 2), name='gStt_%d' % n))
                obs_input.append(self.pose_stt[n])
                obs_input.append(self.gaze_stt[n])
            if (neighEnc):
                self.modeN_all.append(
                    Input(shape=(self.n_enc, self.n_states),
                          name='mode_%d' % n))
                obs_input.append(self.modeN_all[n])
            if (visEnc):
                self.visP_all.append(
                    Input(shape=(self.n_vis, 2), name='visP_%d' % n))
                self.visG_all.append(
                    Input(shape=(self.n_vis, 2), name='visG_%d' % n))
                obs_input.append(self.visP_all[n])
                obs_input.append(self.visG_all[n])
            observations.append(self.obs_mod(obs_input))

        if (socPool):
            observations = Cat(axis=2)([
                Reshape((int_shape(observations[i])[1], 1))(observations[i])
                for i in range(len(observations))
            ])
            self.weight = [
                Input(shape=(self.n_neighbors, ))
                for _ in range(self.d_socPool**2)
            ]
            neigh_avg = Cat()([
                Flatten()(Dot(2)([
                    observations,
                    Reshape((1, self.n_neighbors))(self.weight[i])
                ])) for i in range(self.d_socPool**2)
            ])
        else:
            neigh_avg = Average()(observations)

        self.nmod_input = (self.weight + self.pose_stt + self.gaze_stt +
                           self.pose_all + self.gaze_all + self.visP_all +
                           self.visG_all + self.modeN_all)
        self.neigh_mod = Container(self.nmod_input, neigh_avg)
Beispiel #19
0
def get_model(I, E, M=5):
    """Returns a compiled model described in Appendix section C.

    Arguments:
        I (int): number of inputs in each program. input count is
            padded to I with null type and vals.
        E (int): embedding dimension
        M (int): number of examples per program. default 5.

    Returns:
        keras.model compiled keras model as described in Appendix
        section C
    """
    embed = Embedding(constants.NULL + 1, E, input_length=1, name='embedding')
    l1 = Dense(K, activation='sigmoid', name='layer1')
    l2 = Dense(K, activation='sigmoid', name='layer2')
    l3 = Dense(K, activation='sigmoid', name='layer3')

    input_layers = []
    concat_layers = []
    for i in range(M):
        # example inputs
        for j in range(I):
            type_input = Input(shape=(2, ),
                               name=encoding.get_input_typename(i, j))
            val_inputs = []
            for k in range(L):
                val_input = Input(shape=(1, ),
                                  name=encoding.get_input_valname(i, j, k))
                val_inputs.append(val_input)

            input_layers += [type_input] + val_inputs

            embed_list = [embed(x) for x in val_inputs]
            flattened_list = [Flatten()(x) for x in embed_list]
            concat_layer = Concatenate()([type_input] + flattened_list)
            concat_layers.append(concat_layer)

        # example output
        type_input = Input(shape=(2, ), name=encoding.get_output_typename(i))
        val_inputs = []
        for j in range(L):
            val_input = Input(shape=(1, ),
                              name=encoding.get_output_valname(i, j))
            val_inputs.append(val_input)

        input_layers += [type_input] + val_inputs

        embed_list = [embed(x) for x in val_inputs]
        flattened_list = [Flatten()(x) for x in embed_list]
        concat_layer = Concatenate()([type_input] + flattened_list)
        concat_layers.append(concat_layer)
    l1_layers = [l1(x) for x in concat_layers]
    l2_layers = [l2(x) for x in l1_layers]
    l3_layers = [l3(x) for x in l2_layers]
    ave = Average()(l3_layers)
    pred = Dense(len(impl.FUNCTIONS), activation='softmax')(ave)

    model = Model(inputs=input_layers, outputs=pred)
    model.compile('adam', 'categorical_crossentropy')
    return model
Beispiel #20
0
        ], Y_batch
        counter += 1
        if counter % 50 == 0:
            print("i=" + str(counter))
        if counter == int((1 - splits) * num_batches):
            counter = 0
            if shuffle:
                np.random.shuffle(smp_idx)


nb_hidden = 1000
input1 = Input(shape=(1000, ))
input2 = Input(shape=(1000, ))
input3 = Input(shape=(1000, ))

avgL = Average()([input1, input2, input3])
maxL = Maximum()([input1, input2, input3])
minL = Minimum()([input1, input2, input3])
concatL = Concatenate()([avgL, maxL])
layer1 = Dense(nb_hidden,
               kernel_regularizer=regularizers.l1(0.00001),
               bias_initializer='zeros',
               activation='relu')(concatL)
#layer1=Dropout(0.5)(layer1)
out = Dense(nb_classes,
            kernel_regularizer=regularizers.l1(0.00001),
            bias_initializer='zeros',
            kernel_initializer='identity',
            activation='softmax')(layer1)
model = Model([input1, input2, input3], out)
#model=load_model('genFit_ens450.h5');
Beispiel #21
0
        Conv1D(filters=nb_filter1, kernel_size=filter_len1, activation='relu'),
        #Dropout(dropout_cnn),
        GlobalMaxPooling1D(),
        # https://github.com/fchollet/keras/issues/1920
        # https://stats.stackexchange.com/questions/257321/what-is-global-max-pooling-layer-and-what-is-its-advantage-over-maxpooling-layer
        #MaxPooling1D(pool_len1),
        Dropout(dropout_pool),
        #Flatten(),
        Dense(nb_dense, activation='relu'),
        Dropout(dropout_dense),
        Dense(1, activation='sigmoid')
    ]
    forward_output = get_output(forward_input, hidden_layers)
    reverse_output = get_output(reverse_input, hidden_layers)
    #output = merge([forward_output, reverse_output], mode='ave')
    output = Average()([forward_output, reverse_output])
    model = Model(inputs=[forward_input, reverse_input], outputs=output)
    model.compile(Adam(lr=learningrate),
                  'binary_crossentropy',
                  metrics=['accuracy'])
    #model.summary()

print("...fitting model...")
print(contigLengthk + 'k_fl' + str(filter_len1) + '_fn' + str(nb_filter1) +
      '_dn' + str(nb_dense) + '_ep' + str(epochs))
model.fit(x = [X_tr_shuf_fw, X_tr_shuf_bw], y = Y_tr_shuf, \
            batch_size=batch_size, epochs=epochs, verbose=1, \
            validation_data=([X_val_shuf_fw, X_val_shuf_bw], Y_val_shuf), \
            callbacks=[checkpointer, earlystopper])

### produce AUC ###
Beispiel #22
0
def load_source_model(models=['xception','resnet50'], \
        pred_models=None, \
        is_targeted=False, \
        aug_or_rotate='aug', \
        grad_dict=None, \
        make_model=False, \
        load_weights=True):
    """
    Loads the models and set up grad_dict. grad_dict is a dictionary
    indexed by model names, and the values are tuples 
    (input, output, K.function(input, output)). There is a special
    entry PRED that is for the prediction function. All other
    functions are gradients w.r.t. input pixels.
    The gradients are always supposed to be subtracted, so the sign
    will be different depending on whether it is a targeted attack.

    Arguments
        models      models used in the ensemble
        pred_models models used for prediction
        is_targeted
        aug_or_rotate
        grad_dict   the main output of this function
        make_model
        load_weights
    """

    grad_sign = 1 if is_targeted else -1

    ph = K.learning_phase()

    if grad_dict is not None:
        grad_dict.clear()

    input_img = Input(shape=(299, 299, 3), name='src_input_img')
    noise_input = Input(batch_shape=(1, ), name='augmentation_scale')

    if aug_or_rotate not in ['aug', 'rot', 'none']:
        raise ValueError('invalid value for aug_or_rotate')

    target_labels = Input(shape=(1000, ), name='target_labels')
    pred_max = Lambda(lambda x: K.max(x, axis=1, keepdims=True),
                      name='target_labels_pred_max')(target_labels)
    y = Equal(name='target_pred_equal')([target_labels, pred_max])
    y = Lambda(lambda x: K.cast(x, K.floatx()), name='type_cast')(y)
    y = Lambda(lambda x: x / K.sum(x, axis=1, keepdims=True),
               name='pseudo_label')(y)

    model_outputs = []
    for model_name in models:
        print('preparing', model_name)
        model_pred = load_branch(model_name,
                                 input_img,
                                 aug_or_rotate,
                                 noise_input,
                                 False,
                                 load_weights=load_weights)
        if pred_models is not None and model_name in pred_models:
            model_outputs.append(model_pred)
        print('preparing gradients', model_name)
        branch_loss = CategoricalCrossEntropy(from_logits=False,
                                              name=model_name +
                                              '_ce')([y, model_pred])

        branch_grads = Gradients(grad_sign, name='grad_' +
                                 model_name)([input_img, branch_loss])
        if grad_dict is not None:
            if make_model:
                grad_dict[model_name] = branch_grads
            else:
                grad_dict[model_name] = (\
                        [input_img, noise_input, target_labels, ph], \
                        [branch_grads], \
                        K.function(\
                        [input_img, noise_input, target_labels, ph], \
                        [branch_grads]))
        print('finished', model_name)

    print('loading of source models finished')
    if pred_models and grad_dict is not None:
        if make_model:
            if len(model_outputs) == 1:
                grad_dict['PRED'] = Lambda(lambda x: x,
                                           name='PRED')(model_outputs[0])
            else:
                grad_dict['PRED'] = Average(name='PRED')(model_outputs)
        else:
            pred_avg = sum(model_outputs) / len(model_outputs)
            pred_func = K.function([input_img, noise_input, K.learning_phase()], \
                    [pred_avg])
            grad_dict['PRED'] = ([input_img, noise_input, K.learning_phase()], \
                    [pred_avg], pred_func)

    if make_model:
        output_list = models
        if pred_models:
            output_list.append('PRED')
        grad_inputs = [input_img, noise_input, target_labels]
        model = Model(inputs=grad_inputs, \
                outputs=[grad_dict[m] \
                for m in output_list])
    else:
        model = None

    print('gradient computation finished')
    return model