Ejemplo n.º 1
0
    def call(self, inputs):
        encoded_passage, merged_context, modeled_passage, span_begin_probabilities = inputs
        weighted_sum = K.sum(
            K.expand_dims(span_begin_probabilities, axis=-1) * modeled_passage,
            -2)
        passage_weighted_by_predicted_span = K.expand_dims(weighted_sum,
                                                           axis=1)
        tile_shape = K.concatenate([[1], [K.shape(encoded_passage)[1]], [1]],
                                   axis=0)
        passage_weighted_by_predicted_span = K.tile(
            passage_weighted_by_predicted_span, tile_shape)
        multiply1 = modeled_passage * passage_weighted_by_predicted_span
        span_end_representation = K.concatenate([
            merged_context, modeled_passage,
            passage_weighted_by_predicted_span, multiply1
        ])

        span_end_representation = self.bilstm_1(span_end_representation)

        span_end_input = K.concatenate(
            [merged_context, span_end_representation])

        span_end_weights = TimeDistributed(self.dense_1)(span_end_input)

        span_end_probabilities = Softmax()(K.squeeze(span_end_weights,
                                                     axis=-1))
        return span_end_probabilities
Ejemplo n.º 2
0
def infusion_layer(x, indexer=None):
    conv2d_name = 'seg_conv'
    batchnorm_name = 'seg_batchnorm'
    output_name = 'seg_output'
    if indexer:
        conv2d_name += '_' + str(indexer)
        batchnorm_name += '_' + str(indexer)
        output_name += '_' + str(indexer)

    y_seg = compose(
        Conv2D(2,(1,1), name=conv2d_name, kernel_initializer='he_normal', bias_initializer='constant',
            kernel_regularizer = l2(5e-4), activity_regularizer = l2(5e-4)
            ),
        # BatchNormalization(name=output_name),

        # BatchNormalization(name=batchnorm_name),
        # ReLU(name=output_name, max_value=1.0),

        # BatchNormalization(name=batchnorm_name),
        # Activation('sigmoid',name=output_name, ),

        BatchNormalization(name=batchnorm_name),
        Softmax(name=output_name, axis=-1)

        )(x)
    return y_seg
Ejemplo n.º 3
0
    def get_model(cls) -> Model:
        model = Sequential()

        model.add(Dense(10, input_shape=(4, ), activation='relu', name='fc1'))
        model.add(Dense(10, activation='relu', name='fc2'))
        model.add(Dense(3, activation='linear', name='fc4'))
        model.add(Softmax(name='output', axis=1))

        # Adam optimizer with learning rate of 0.001
        optimizer = Adam(lr=0.001)
        model.compile(optimizer,
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

        print('Neural Network Model Summary: ')
        print(model.summary())

        # Train the model
        model.fit(train_x, train_y, verbose=2, batch_size=5, epochs=200)

        # Test on unseen data

        results = model.evaluate(test_x, test_y)

        print('Final test set loss: {:4f}'.format(results[0]))
        print('Final test set accuracy: {:4f}'.format(results[1]))

        return model
Ejemplo n.º 4
0
 def call(self, inputs):
     merged_context, modeled_passage = inputs
     span_begin_input = K.concatenate([merged_context, modeled_passage])
     span_begin_weights = TimeDistributed(Dense(units=1))(span_begin_input)
     span_begin_probabilities = K.squeeze(Softmax()(span_begin_weights),
                                          axis=-1)
     return span_begin_probabilities
Ejemplo n.º 5
0
 def call(self, inputs):
     encoded_passage, merged_context, modeled_passage, span_begin_probabilities = inputs
     weighted_sum = K.sum(
         K.expand_dims(span_begin_probabilities, axis=-1) * modeled_passage,
         -2)
     passage_weighted_by_predicted_span = K.expand_dims(weighted_sum,
                                                        axis=1)
     tile_shape = K.concatenate([[1], [K.shape(encoded_passage)[1]], [1]],
                                axis=0)
     passage_weighted_by_predicted_span = K.tile(
         passage_weighted_by_predicted_span, tile_shape)
     multiply1 = modeled_passage * passage_weighted_by_predicted_span
     span_end_representation = K.concatenate([
         merged_context, modeled_passage,
         passage_weighted_by_predicted_span, multiply1
     ])
     emdim = K.int_shape(encoded_passage)[-1] // 2
     span_end_representation = Bidirectional(
         LSTM(emdim, return_sequences=True))(span_end_representation)
     span_end_input = K.concatenate(
         [merged_context, span_end_representation])
     span_end_weights = TimeDistributed(Dense(units=1))(span_end_input)
     span_end_probabilities = Softmax()(K.squeeze(span_end_weights,
                                                  axis=-1))
     return span_end_probabilities
Ejemplo n.º 6
0
 def call(self, inputs):
     similarity_matrix, encoded_question = inputs
     context_to_query_attention = Softmax(axis=-1)(similarity_matrix)
     encoded_question = K.expand_dims(encoded_question, axis=1)
     return K.sum(
         K.expand_dims(context_to_query_attention, axis=-1) *
         encoded_question, -2)
Ejemplo n.º 7
0
	def build(height, width, depth, classes):
		inputShape = (height, width, depth)
		chanDim = -1
		# if we are using "channel first", update the input shape
		if K.image_data_format() == "channels_first":
			inputShape = (depth, height, width)
			chanDim = 1

		# initialize the model
		model = Sequential()

		# CONV => RELU => POOL
		model.add(Conv2D(32, (3, 3), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(3, 3)))
		model.add(Dropout(0.25))

		# (CONV => RELU) * 2 => POOL
		model.add(Conv2D(64, (3, 3), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(Conv2D(64, (3, 3), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		model.add(Dropout(0.25))

		# (CONV => RELU) * 2 => POOL
		model.add(Conv2D(128, (3, 3), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(Conv2D(128, (3, 3), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		model.add(Dropout(0.25))

		# first (and only) set of FC => RELU layers
		model.add(Flatten())
		model.add(Dense(1024))
		model.add(Activation("relu"))
		model.add(BatchNormalization())
		model.add(Dropout(0.5))

		# softmax classifier
		model.add(Dense(classes))
		# model.add(Activation("softmax"))
		model.add(Softmax())

		# return the constructed network architecture
		return model

		pass
Ejemplo n.º 8
0
 def call(self, inputs):
     similarity_matrix, encoded_context = inputs
     max_similarity = K.max(similarity_matrix, axis=-1)
     # by default, axis = -1 in Softmax
     context_to_query_attention = Softmax()(max_similarity)
     weighted_sum = K.sum(K.expand_dims(context_to_query_attention, axis=-1) * encoded_context, -2)
     expanded_weighted_sum = K.expand_dims(weighted_sum, 1)
     num_of_repeatations = K.shape(encoded_context)[1]
     return K.tile(expanded_weighted_sum, [1, num_of_repeatations, 1])
    def build(width, height, depth, classes):
        model = Sequential()
        inputShape = (height, width, depth)
        chanDim = -1

        if K.image_data_format == "channels_first":
            inputShape = (depth, height, width)
            chanDim = 1
        model = Sequential([
            Conv2D(32, (3, 3),
                   padding="same",
                   kernel_initializer="he_normal",
                   input_shape=inputShape),
            ELU(),
            BatchNormalization(axis=chanDim),
            Conv2D(32, (3, 3), kernel_initializer="he_normal", padding="same"),
            ELU(),
            BatchNormalization(axis=chanDim),
            MaxPooling2D(pool_size=(2, 2)),
            Dropout(0, 25),
            Conv2D(64, (3, 3),
                   padding="same",
                   kernel_initializer="he_normal",
                   input_shape=inputShape),
            ELU(),
            BatchNormalization(axis=chanDim),
            Conv2D(64, (3, 3), kernel_initializer="he_normal", padding="same"),
            ELU(),
            BatchNormalization(axis=chanDim),
            MaxPooling2D(pool_size=(2, 2)),
            Dropout(0, 25),
            Conv2D(64, (3, 3),
                   padding="same",
                   kernel_initializer="he_normal",
                   input_shape=inputShape),
            ELU(),
            BatchNormalization(axis=chanDim),
            Conv2D(64, (3, 3), kernel_initializer="he_normal", padding="same"),
            ELU(),
            BatchNormalization(axis=chanDim),
            MaxPooling2D(pool_size=(2, 2)),
            Dropout(0, 25),
            Flatten(),
            Dense(64, kernel_initializer="he_normal"),
            ELU(),
            BatchNormalization(),
            Dropout(0.25),
            Dense(64, kernel_initializer="he_normal"),
            ELU(),
            BatchNormalization(),
            Dropout(0.25),
            Dense(classes, kernel_initializer="he_normal"),
            Softmax()
        ])
        return model
Ejemplo n.º 10
0
    def __call__(self, answer_encodings):
        score_matrix = tf.matmul(answer_encodings, tf.transpose(answer_encodings, perm=(0, 2, 1)))
        mask = tf.cast(tf.logical_not(tf.cast(tf.matrix_diag([1] * 5), tf.bool)), tf.float32)
        score_matrix = K.dot(score_matrix, mask)
        weights = Softmax()(score_matrix)
        answer_encoding_hat = tf.matmul(weights, answer_encodings)

        answer_representation = K.concatenate([answer_encodings, answer_encoding_hat, answer_encodings*answer_encoding_hat])
        answer_probability = Dense(1, activation="softmax")(answer_representation)

        return K.squeeze(answer_probability, axis=-1)
Ejemplo n.º 11
0
def run_model():
    iris_data = load_iris()  # load the iris dataset

    print('Example data: ')
    print(iris_data.data[:5])
    print('Example labels: ')
    print(iris_data.target[:5])

    x = iris_data.data
    y_ = iris_data.target.reshape(-1, 1)  # Convert data to a single column

    # One Hot encode the class labels
    encoder = OneHotEncoder(sparse=False)
    y = encoder.fit_transform(y_)

    # Split the data for training and testing
    train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.20)

    # Build the model

    model = Sequential()

    model.add(Dense(10, input_shape=(4, ), activation='relu', name='fc1'))
    model.add(Dense(10, activation='relu', name='fc2'))
    model.add(Dense(3, activation='linear', name='fc4'))
    model.add(Softmax(name='output', axis=1))

    # Adam optimizer with learning rate of 0.001
    optimizer = Adam(lr=0.001)
    model.compile(optimizer,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    print('Neural Network Model Summary: ')
    print(model.summary())

    # Train the model
    model.fit(train_x, train_y, verbose=2, batch_size=5, epochs=200)

    # Test on unseen data

    results = model.evaluate(test_x, test_y)

    print('Final test set loss: {:4f}'.format(results[0]))
    print('Final test set accuracy: {:4f}'.format(results[1]))

    print('X for testing:')
    print(test_x[:3, :])
    print('Y for testing:')
    print(test_y[:3, :])

    model.save('../../artifacts/iris.h5')
Ejemplo n.º 12
0
def add_context2query_layer(query_embedding, biattention_matrix,
                            attention_level, max_query_len, max_doc_len):
    # Following the context-to-query implementation of BiDAF model
    # query_embedding: batch_size * max_query_len * nb_filters
    # biattention_matrix: batch_size * max_query_len * max_doc_len
    norm_biattention = Softmax(axis=-2)(biattention_matrix)
    # Activation('softmax', axis=-2)(biattention_matrix)
    reshape_norm_biatt = Reshape((
        max_doc_len,
        max_query_len,
    ))(norm_biattention)
    context_embedding = Dot(axes=[-1, -2],
                            name="context2query-%d" % attention_level)(
                                [reshape_norm_biatt, query_embedding])
    return context_embedding
Ejemplo n.º 13
0
    def build_mechanism(self):

        model = Sequential()

        model.add(Flatten(input_shape=self.vote_shape))
        model.add(Dense(512))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dense(256))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dense(self.n_alters))
        model.add(Softmax(axis=-1))

        print("\n\n\nMechanism model summary")
        model.summary()

        vote = Input(shape=self.vote_shape)
        winner = model(vote)

        return Model(vote, winner)
Ejemplo n.º 14
0
def dense_cnn(input_, nclass):

    _weight_decay = 1e-4

    _nb_filter = 64
    # conv 64 5*5 s=2

    x = Conv2D(32 * 2, (3, 3), activation='relu')(input_)
    x = Conv2D(32 * 2, (3, 3), activation='relu')(x)
    x = Flatten(name='flatten')(x)
    x = Dense(1, name='out')(x)
    y_pred = Dense(nclass, name='out', activation='sigmoid')(x)
    y_pred = Reshape((10, nclass // 10))(y_pred)
    y_pred = Softmax(axis=-1)(y_pred)

    # basemodel = Model(inputs=input, outputs=y_pred)
    # basemodel.summary()

    return y_pred
Ejemplo n.º 15
0
def dense_cnn2(input_, nclass):

    _dropout_rate = 0.2
    _weight_decay = 1e-4

    _nb_filter = 64
    # conv 64 5*5 s=2
    x = Conv2D(_nb_filter, (5, 5),
               strides=(2, 2),
               kernel_initializer='he_normal',
               padding='same',
               use_bias=False,
               kernel_regularizer=l2(_weight_decay))(input_)

    # 64 + 8 * 8 = 128
    x, _nb_filter = dense_block(x, 8, _nb_filter, 8, None, _weight_decay)
    # 128
    x, _nb_filter = transition_block(x, 128, _dropout_rate, 2, _weight_decay)

    # 128 + 8 * 8 = 192
    x, _nb_filter = dense_block(x, 8, _nb_filter, 8, None, _weight_decay)
    # 192 -> 128
    x, _nb_filter = transition_block(x, 128, _dropout_rate, 2, _weight_decay)

    # 128 + 8 * 8 = 192
    x, _nb_filter = dense_block(x, 8, _nb_filter, 8, None, _weight_decay)

    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    x = Permute((2, 1, 3), name='permute')(x)
    #x = TimeDistributed(Flatten(), name='flatten')(x)
    x = Flatten(name='flatten')(x)

    y_pred = Dense(nclass, name='out', activation='sigmoid')(x)
    y_pred = Reshape((10, nclass // 10))(y_pred)
    y_pred = Softmax(axis=-1)(y_pred)

    # basemodel = Model(inputs=input, outputs=y_pred)
    # basemodel.summary()

    return y_pred
Ejemplo n.º 16
0
 def selfAttention(self, x, n_channels, k=8):
     x_shape = K.int_shape(x)
     f = Reshape(
         (-1, x_shape[-1]))(ConvSN2D(n_channels // k, 1,
                                     padding='same')(x))  # [b, n, c/k]
     g = Reshape(
         (-1, x_shape[-1]))(ConvSN2D(n_channels // k, 1,
                                     padding='same')(x))  # [b, n, c/k]
     g = Permute((1, 2))(g)  # [b,c/k,n]
     s = multiply([f, g])
     s = Softmax(axis=-1)(s)  #[b,n,n]
     h = Reshape((-1, x_shape[-1]))(ConvSN2D(n_channels // k,
                                             1,
                                             padding='same')(x))  #[b,n,c/k]
     v = multiply([s, h])  #[b,n,c/k]
     v = Reshape((x_shape[1], x_shape[2], n_channels // k))(v)
     o = ConvSN2D(n_channels, 1, padding='same')(v)  #[b,h,w,c]
     gamma = K.variable(value=0)
     ret = Lambda(lambda x: x * gamma)(o)
     ret = Add()([ret, x])
     return ret
Ejemplo n.º 17
0
def Generator(dim):
    model = Sequential()
    model.add(Dense(256, activation='linear', input_shape=noise_shape))
    model.add(Reshape((-1, 2, 128)))
    model.add(ResBlock(dim))
    model.add(ResBlock(dim))
    model.add(ResBlock(dim))
    model.add(ResBlock(dim))
    model.add(ResBlock(dim))
    model.add(Reshape((1, 32, 8)))
    model.add(Conv1D(64, 32, 1, padding='valid'))
    model.add(Flatten())
    model.add(Dense(np.product(pass_shape), activation='linear'))
    model.add(Softmax(axis=1))
    model.add(Reshape(pass_shape))

    noise = Input(shape=noise_shape)
    password = model(noise)

    model.summary()
    return Model(noise, password)
Ejemplo n.º 18
0
    def build_vote_generator(self):

        model = Sequential()

        model.add(Dense(256, input_dim=self.latent_dim))
        model.add(LeakyReLU(alpha=0.2))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Dense(256))
        model.add(LeakyReLU(alpha=0.2))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Dense(np.prod(self.vote_shape)))
        model.add(Reshape(self.vote_shape))
        model.add(Softmax(axis=-1))

        print("build_vote_generator model summary")
        model.summary()

        noise = Input(shape=(self.latent_dim, ))
        votes = model(noise)

        return Model(noise, votes)
Ejemplo n.º 19
0
def DilatedCNN2D_002(inputDim=(116, 177, 1), nClasses=33, perms=100):

    def bn_block(x):
        return BatchNormalization()(x)

    def conv_block(x, nb_filter, filter_size, atrous_rate=(1, 1)):
        x = Conv2D(nb_filter, filter_size, dilation_rate=atrous_rate, kernel_initializer='he_normal', padding='same')(x)
        x = bn_block(x)
        x = ReLU()(x)
        return x

    atrousRates = [(1,1), (1,1), (2,2), (3,3), (5,5), (8,8), (13,13), (21,21)] 
    numFilters = [32, 64, 64, 128, 64, 64, 64, 128]
    featList = []

    i = Input(shape=inputDim)

    # learnabale remapping 
    x = Dense(perms)(i)
    x = bn_block(x)

    # convolutional blocks
    for idx, (nFilter, dilationRate) in enumerate(zip(numFilters, atrousRates)):
        x = conv_block(x, nFilter, (3,3), dilationRate)
        if idx % 2 ==0 and idx > 0:
            x = MaxPooling2D()(x)

    # bottlenecking
    x = conv_block(x, 32, (3, 3))
    x = conv_block(x, 128, (1,1))
    x = SpatialDropout2D(0.25)(x)

    # classifying
    x = conv_block(x, nClasses, (1, 1))
    x = GlobalMaxPooling2D()(x)
    o = Softmax()(x)

    model = Model(inputs=i, outputs=o)

    return model
Ejemplo n.º 20
0
def discriminator_model(shp):
    # model = Graph()
    input = Input(shape=shp)
    x = Convolution2D(64, (5, 5),
                      border_mode='same',
                      data_format="channels_first")(input)
    x = Activation('tanh')(x)
    x = MaxPooling2D((2, 2), data_format="channels_first")(x)
    x = Convolution2D(128, (5, 5),
                      border_mode='same',
                      data_format="channels_first")(x)
    x = Activation('tanh')(x)
    # x = MaxPooling2D(2, 2)(x)
    x = Flatten()(x)
    x = Dense(1024, activation='tanh')(x)
    # x = Activation('tanh')(x)
    d = Dense(1, activation='sigmoid')(x)
    # d = Activation('sigmoid')(d)
    cls = Dense(15)(x)
    x = Softmax()(x)
    model = Model(inputs=input, outputs=[d, cls])
    return model
Ejemplo n.º 21
0
                            weights=None,
                            pooling=None)
    output = inception.layers[-1].output

    # add last layers
    net = AveragePooling2D(
        pool_size=(8, 8),  # K.int_shape(output),
        strides=(2, 2),
        padding='valid',
        data_format='channels_last',
        name='pool')(output)
    net = Dropout(rate=0.2, name='dropout')(net)
    net = Flatten(data_format='channels_last', name='flatten')(net)

    logits = Dense(NUM_CLASSES, activation=None, name="logits")(net)
    predictions = Softmax(name='predictions')(logits)

    # aux net
    if args.with_aux:
        output = inception.get_layer(name="mixed7").output
        aux_logits = AveragePooling2D(pool_size=(5, 5),
                                      strides=(3, 3),
                                      padding='valid')(output)
        aux_logits = Conv2D(128, (1, 1), name='proj')(aux_logits)
        aux_logits = Conv2D(
            768,
            K.int_shape(aux_logits)[1:3],
            kernel_initializer=initializers.TruncatedNormal(stddev=0.001),
            padding='valid')(aux_logits)
        aux_logits = Flatten(data_format='channels_last')(aux_logits)
        aux_logits = Dense(NUM_CLASSES,
Ejemplo n.º 22
0
x = BatchNormalization()(x)
x = layers.Conv2D(512, (3, 3),
                  activation='relu',
                  padding='same',
                  name='block6_conv2')(x)
x = BatchNormalization()(x)
x = layers.Conv2D(512, (3, 3),
                  activation='softmax',
                  padding='same',
                  name='block6_conv3')(x)
x = BatchNormalization()(x)

# Block 7
skip_connection = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_21', use_bias=False)(skip_connection)
skip_connection = BatchNormalization()(skip_connection)
skip_connection = Softmax(axis=-1)(skip_connection)
skip_connection = Lambda(space_to_depth_x2)(skip_connection)


# Merge
x = concatenate([skip_connection, x])



# Block 8
x = Conv2D(BOX * (4 + 1 + CLASS), (1,1), strides=(1,1), padding='same', name='conv_23')(x) 
output = Reshape((GRID_H, GRID_W, BOX, 4 + 1 + CLASS))(x)


# small hack to allow true_boxes to be registered when Keras build the model 
# for more information: https://github.com/fchollet/keras/issues/2790
Ejemplo n.º 23
0
    def build_strategic_vote_generator(self):

        model = Sequential()

        model.add(Flatten(input_shape=self.vote_shape))
        model.add(Dense(256))
        model.add(LeakyReLU(alpha=0.2))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Dense(256))
        model.add(LeakyReLU(alpha=0.2))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Dense(256))
        model.add(LeakyReLU(alpha=0.2))
        model.add(BatchNormalization(momentum=0.8))

        print("\n\n\nStrategic_vote_generator base model summary")
        model.summary()

        model1 = Sequential()
        model1.add(Dense(256, input_dim=256))
        model1.add(LeakyReLU(alpha=0.2))
        model1.add(BatchNormalization(momentum=0.8))
        model1.add(Dense(self.n_alters))
        model1.add(Reshape((1, self.n_alters)))
        model1.add(Softmax(axis=-1))

        print("\n\n\nStrategic_vote_generator derived model1 summary")
        model1.summary()

        model2 = Sequential()
        model2.add(Dense(256, input_dim=256))
        model2.add(LeakyReLU(alpha=0.2))
        model2.add(BatchNormalization(momentum=0.8))
        model2.add(Dense(self.n_agents))
        model2.add(Softmax(axis=-1))

        print("\n\n\nStrategic_vote_generator derived model2 summary")
        model2.summary()

        votes = Input(shape=self.vote_shape)
        intermediate_votes = model(votes)
        # Gets the strategic vote.
        strategic_vote = model1(intermediate_votes)
        # Gets the probability dist for voter.
        strategic_voter = model2(intermediate_votes)

        # Custom layer for strategic vote generator
        def strategic_vote_generator_layer(tensors):
            votes = tensors[0]
            strategic_vote = tensors[1]
            strategic_votes = []
            for i in range(self.n_agents):
                strategic_votes.append(
                    K.concatenate(
                        [votes[:, :i, :], strategic_vote, votes[:, i + 1:, :]],
                        axis=1))
                # print(strategic_votes[i].shape)
            # Seperating the votes by the weights.
            all_votes = [votes] + [tensors[2]] + strategic_votes
            return all_votes

        def strategic_vote_generator_layer_output_shape(input_shapes):
            return [input_shapes[0]] + [input_shapes[2]
                                        ] + [input_shapes[0]] * self.n_agents

        layer = Lambda(strategic_vote_generator_layer,
                       strategic_vote_generator_layer_output_shape)
        all_votes = layer([votes, strategic_vote, strategic_voter])

        final_model = Model(votes, all_votes)
        print("\n\n\nStrategic_vote_generator final model summary")
        final_model.summary()

        return final_model
Ejemplo n.º 24
0
def main():
    import pynvml
    pynvml.nvmlInit()
    # 这里的0是GPU id
    handle2 = pynvml.nvmlDeviceGetHandleByIndex(2)
    handle3 = pynvml.nvmlDeviceGetHandleByIndex(3)
    # meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)

    # print(meminfo.used)

    parser = argparse.ArgumentParser(
        description='simple 3D convolution for action recognition')
    parser.add_argument('--batch', type=int, default=128)
    parser.add_argument('--epoch', type=int, default=100)
    parser.add_argument('--videos',
                        type=str,
                        default='UCF101',
                        help='directory where videos are stored')
    parser.add_argument('--nclass', type=int, default=101)
    parser.add_argument('--output', type=str, required=True)
    parser.add_argument('--color', type=bool, default=False)
    parser.add_argument('--skip', type=bool, default=True)
    parser.add_argument('--depth', type=int, default=10)
    parser.add_argument('--dataset', type=str, default='ucf101')
    args = parser.parse_args()

    img_rows, img_cols, frames = 64, 64, args.depth
    channel = 3 if args.color else 1
    fname_npz = 'dataset_{}_{}_{}_{}.npz'.format(args.dataset, args.nclass,
                                                 args.depth, args.skip)

    vid3d = videoto3d.Videoto3D(img_rows, img_cols, frames, args.dataset)
    nb_classes = args.nclass

    if os.path.exists(fname_npz):
        loadeddata = np.load(fname_npz)
        X, Y = loadeddata["X"], loadeddata["Y"]
    else:
        x, y = loaddata(args.videos, vid3d, args.nclass, args.output,
                        args.dataset, frames, args.color, args.skip)
        X = x.reshape((x.shape[0], img_rows, img_cols, frames, channel))
        Y = np_utils.to_categorical(y, nb_classes)

        X = X.astype('float32')
        np.savez(fname_npz, X=X, Y=Y)
        print('Saved dataset to dataset.npz.')
    print('X_shape:{}\nY_shape:{}'.format(X.shape, Y.shape))

    # Define model

    # conv3D + Relu + Conv3D + Softmax + Pooling3D + DropOut
    input_x = Input(shape=(img_rows, img_cols, frames, channel))

    #
    # # C3D-conv1
    # convLayer = Conv3D(32, kernel_size= (3, 3, 3),padding='same')(input_x)
    # convLayer = ReLU()(convLayer)
    #
    # convLayer = Conv3D(32, kernel_size= (3, 3, 3), padding='same')(convLayer)
    # convLayer = Softmax()(convLayer)
    # convLayer = MaxPooling3D(pool_size=(3,3,3), padding='same')(convLayer)
    # convLayer = Dropout(0.25)(convLayer)
    #
    # # C3D-conv2
    # convLayer = Conv3D(64, kernel_size= (3, 3, 3),padding='same')(convLayer)
    # convLayer = ReLU()(convLayer)
    #
    # convLayer = Conv3D(64, kernel_size= (3, 3, 3), padding='same')(convLayer)
    # convLayer = Softmax()(convLayer)
    # convLayer = MaxPooling3D(pool_size=(3,3,3), padding='same')(convLayer)
    # convLayer = Dropout(0.25)(convLayer)
    #
    #
    # maskLayer = Conv3D(64*frames, kernel_size=(3,3,2), padding='same')(convLayer)
    # maskLayer = Lambda(mean_filter)(maskLayer)      # [None,1, 64], each point represent a mask of input region of 8x8 points
    # # maskLayer = BatchNormalization()(maskLayer)
    # maskLayer = Lambda(K.sigmoid)(maskLayer)
    # # maskLayer = ReLU()(maskLayer)
    # # maskLayer = Lambda(bi_trans, arguments={'th':0.5})(maskLayer)
    # maskLayer = Reshape(( 8, 8, frames, 1))(maskLayer)  #reshape_filter(maskLayer, shape=[None,8,8,1,1])
    # # maskLayer = Lambda(normalize)(maskLayer)
    # maskLayerForLoss = maskLayer
    # maskLayer = Lambda(repeat_filter,arguments={'rep':8, 'axis':1})(maskLayer)
    # maskLayer = Lambda(repeat_filter,arguments={'rep':8, 'axis':2})(maskLayer)
    # # maskLayer = Lambda(repeat_filter,arguments={'rep':frames, 'axis':3})(maskLayer)
    # maskLayer = Lambda(repeat_filter,arguments={'rep':channel, 'axis':4})(maskLayer)
    #
    # # maskLayer = Lambda(repeat_filter,arguments={'rep':2, 'axis':3})(maskLayer)
    # # maskLayer = Lambda(repeat_filter,arguments={'rep':64, 'axis':4})(maskLayer)
    #
    #
    # convLayer = Multiply()([maskLayer,input_x])
    #
    #

    # C3D-conv1
    convLayer = Conv3D(32, kernel_size=(3, 3, 3), padding='same')(input_x)
    convLayer = ReLU()(convLayer)

    convLayer = Conv3D(32, kernel_size=(3, 3, 3), padding='same')(convLayer)
    convLayer = Softmax()(convLayer)
    convLayer = MaxPooling3D(pool_size=(3, 3, 3), padding='same')(convLayer)
    convLayer = Dropout(0.25)(convLayer)

    # C3D-conv2
    convLayer = Conv3D(64, kernel_size=(3, 3, 3), padding='same')(convLayer)
    convLayer = ReLU()(convLayer)

    convLayer = Conv3D(64, kernel_size=(3, 3, 3), padding='same')(convLayer)
    convLayer = Softmax()(convLayer)
    convLayer = MaxPooling3D(pool_size=(3, 3, 3), padding='same')(convLayer)
    convLayer = Dropout(0.25)(convLayer)

    fc1 = Flatten()(convLayer)

    fc = Dense(512, activation='sigmoid')(fc1)
    fc = Dropout(0.5)(fc)
    dense_out = Dense(nb_classes, activation='softmax')(fc)
    dense_out_converse = Dense(nb_classes)(fc)

    # model = Model(input_x, [dense_out, dense_out_converse])
    model = Model(input_x, [dense_out, dense_out_converse])

    # loss of 2 parts
    losses = {'dense_2': K.categorical_crossentropy, 'dense_3': unlikely_loss}
    lossWeights = {'dense_2': 1, 'dense_3': 1}
    model.compile(loss=losses,
                  loss_weights=lossWeights,
                  optimizer=Adam(lr=0.001),
                  metrics=['accuracy'])
    # model.compile(loss=categorical_crossentropy, optimizer=Adam(lr=0.001),metrics=['accuracy'])
    model.summary()
    plot_model(model,
               show_shapes=True,
               to_file=os.path.join(args.output, 'model.png'))

    X_train, X_test, Y_train, Y_test = train_test_split(X,
                                                        Y,
                                                        test_size=0.1,
                                                        random_state=43)
    X_train, X_val, Y_train, Y_val = train_test_split(X_train,
                                                      Y_train,
                                                      test_size=0.1,
                                                      random_state=43)

    # history = model.fit_generator(myGenerator(X_train, X_test, Y_train, Y_test, nb_classes, args.batch),
    #                               samples_per_epoch=X_train.shape[0], epochs=args.epoch, verbose=1,
    #                               callbacks=callbacks_list,
    # shuffle=True)
    # check GPUs status , once a GPU is available, change os environment parameters and break
    # is none of the GPUs are ready ,sleep for 2 secs and retry.
    cnt = 0
    while True:
        cnt += 1
        processinfo = pynvml.nvmlDeviceGetComputeRunningProcesses(handle2)
        if len(processinfo) == 0:
            os.environ['CUDA_VISIBLE_DEVICES'] = '2'
            print('GPU 2 is available, use GPU 2\n')
            break
        processinfo = pynvml.nvmlDeviceGetComputeRunningProcesses(handle3)
        if len(processinfo) == 0:
            os.environ['CUDA_VISIBLE_DEVICES'] = '3'
            print('GPU 3 is available, use GPU 3\n')
            break
        sleep(2)
        print('\rretry time: {}'.format(cnt), end='')

    history = model.fit(X_train, [Y_train, Y_train],
                        validation_data=(X_val, [Y_val, Y_val]),
                        batch_size=args.batch,
                        epochs=args.epoch,
                        verbose=1,
                        shuffle=True)
    # history = model.fit(X_train, Y_train,
    #                     validation_data=(X_val, Y_val),
    #                     batch_size=args.batch,
    #                     epochs=args.epoch, verbose=1, shuffle=True)
    # loss, acc = model.evaluate(X_test, Y_test, verbose=0)
    model_json = model.to_json()
    if not os.path.isdir(args.output):
        os.makedirs(args.output)
    with open(
            os.path.join(
                args.output, '{}_{}_{}_ucf101_3dcnnmodel.json'.format(
                    current_time, nb_classes, args.depth)), 'w') as json_file:
        json_file.write(model_json)
    model.save_weights(
        os.path.join(
            args.output,
            '{}_{}_{}_ucf101_3dcnnmodel.hd5'.format(current_time, nb_classes,
                                                    args.depth)))
    loss = model.evaluate(X_test, [Y_test, Y_test], verbose=0)
    # loss, acc = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', loss)
    plot_history(history, args.output)
    save_history(history, args.output)

    print('Test loss:', loss)