예제 #1
0
def google_vgg16_finetune(classes=3, size=256):

    input_layer = Input(shape=(size, size, 3), name='image_input')
    base_model = VGG16(weights='imagenet', include_top=False, input_tensor=input_layer)

    x = Conv2D(name='squeeze', filters=256, kernel_size=(1, 1))(base_model.output)  # squeeze channels
    x = Flatten(name='avgpool')(x)
    x = Dense(256, name='features', kernel_regularizer=regularizers.l2(0.01))(x)
    x = Activation('relu')(x)
    x = Dense(classes, activation='softmax', name='out')(x)

    model = Model(inputs=base_model.input, outputs=x)
    for layer in model.layers:
        if layer.name in ['block5_conv1', 'block5_conv2', 'block5_conv3', 'features', 'out']:
            layer.trainable = True
        else:
            layer.trainable = False

    print(model.summary())

    model.compile(
        loss='categorical_crossentropy',
        optimizer=tf.keras.optimizers.RMSprop(lr=1e-4),
        metrics=['accuracy'])

    return model
예제 #2
0
    # but the semantics are unclear, so to be sure we use
    # the loss across the entire 2-rank tensor, we reduce it
    # to a single scalar with the mean function.
    loss_mean = tf.reduce_mean(loss)

    return loss_mean


#######################################################################
# Compile the model.
#######################################################################

optimizer = RMSprop(lr=1e-3)  ### Adam / Adagrad dosent work well with RNN's
decoder_target = tf.placeholder(dtype='int32', shape=(None, None))
model_train.compile(optimizer=optimizer,
                    loss=sparse_cross_entropy,
                    target_tensors=[decoder_target])

# Callbacks
path_checkpoint = '21_checkpoint.keras'
callback_checkpoint = ModelCheckpoint(filepath=path_checkpoint,
                                      monitor='val_loss',
                                      verbose=1,
                                      save_weights_only=True,
                                      save_best_only=True)
callback_early_stopping = EarlyStopping(monitor='val_loss',
                                        patience=3,
                                        verbose=1)
callback_tensorboard = TensorBoard(log_dir='./21_logs/',
                                   histogram_freq=0,
                                   write_graph=False)
예제 #3
0
class BertModel_for_Simsent(NLUModel):
    def     __init__(self):
        self.model=None
        self.bert_model_2()
        self.compile_model()


    def load_bert(self,ex_name = None):
        config_path = os.path.join(config.model_dir, config.bert_config)
        ckpt_path = os.path.join(config.model_dir, config.bert_ckpt)
        model =build_transformer_model(config_path=config_path,checkpoint_path=ckpt_path,model='bert')
        if not ex_name == None:
            for index in range(len(model.layers)):
                layer = model.get_layer(index=index)
                layer._name = '{}_{}'.format(layer._name,ex_name)
        self.model = model
        return model

    def bert_model_2(self):
        bert = self.load_bert()
        seq, seg = bert.input

        bert_out = bert.output
        bert_sent = bert_out[:, 0, :]
        bert_sent_drop = Dropout(rate=config.dropout, name="bert_sent_drop")(bert_sent)

        sent_tc = Dense(config.class_num,activation='softmax',name='sim_classifier')(bert_sent_drop)
        self.model = Model(inputs=[seq, seg], outputs=[sent_tc])
        pass

    # def build_model(self):
    #     bert1 = self.load_bert(ex_name='sent_1')
    #     bert2 = self.load_bert(ex_name='sent_2')
    #
    #     seq1,seg1 = bert1.input
    #     seq2,seg2 = bert2.input
    #
    #     bert_out_1 = bert1.output
    #     bert_out_2 = bert2.output
    #
    #     bert_sent_1 = bert_out_1[:,0,:]
    #     bert_sent_2 = bert_out_2[:, 0, :]
    #
    #     bert_sent_drop_1 = Dropout(rate=config.dropout,name="bert_sent1_drop")(bert_sent_1)
    #     bert_sent_drop_2 = Dropout(rate=config.dropout, name="bert_sent2_drop")(bert_sent_2)
    #
    #     bert_sent_merge = concatenate([bert_sent_drop_1,bert_sent_drop_2], axis=1)
    #     bert_sent_merge = Dropout(rate=config.dropout, name="bert_sent_merge_drop")(bert_sent_merge)
    #
    #     fc = Dense(300,activation='relu',name='hidden_sim')(bert_sent_merge)
    #
    #     output = Dense(config.class_num,activation='softmax',name='sim_classifier')(fc)
    #
    #     self.model = Model(inputs=[seq1,seg1,seq2,seg2],outputs=[output])


    def compile_model(self):
        opt = tf.keras.optimizers.Adam(lr=config.learning_rate)
        loss = {
            'sim_classifier':'sparse_categorical_crossentropy'
        }
        loss_weight = {'sim_classifier':1.0}
        metrics = {'sim_classifier':'acc'}
        self.model.compile(optimizer=opt,loss=loss,metrics=metrics,loss_weights=loss_weight)

    def fit(self,X,Y,valid_data=None,epochs=6,batch_size=32):
        self.model.fit(X,Y,validation_data=valid_data,epochs=epochs,batch_size=batch_size)

    def save(self,model_path,model_name):
        self.model.save(os.path.join(model_path,'{}.h5'.format(model_name)))
예제 #4
0
def define_nmt(hidden_size, batch_size, en_timesteps, en_vsize, sp_timesteps, sp_vsize):

    """ Defining a NMT model """

    # Define an input sequence and process it.

    if batch_size:
        encoder_inputs = Input(batch_shape=(batch_size, en_timesteps, en_vsize), name='encoder_inputs')
        decoder_inputs = Input(batch_shape=(batch_size, sp_timesteps - 1, sp_vsize), name='decoder_inputs')

    else:
        encoder_inputs = Input(shape=(en_timesteps, en_vsize), name='encoder_inputs')
        decoder_inputs = Input(shape=(sp_timesteps - 1, sp_vsize), name='decoder_inputs')



    # Encoder GRU

    encoder_gru = GRU(hidden_size, return_sequences=True, return_state=True, name='encoder_gru')
    encoder_out, encoder_state = encoder_gru(encoder_inputs)


    # Set up the decoder GRU, using `encoder_states` as initial state.
    decoder_gru = GRU(hidden_size, return_sequences=True, return_state=True, name='decoder_gru')
    decoder_out, decoder_state = decoder_gru(decoder_inputs, initial_state=encoder_state)



    # Attention layer
    attn_layer = AttentionLayer(name='attention_layer')
    attn_out, attn_states = attn_layer([encoder_out, decoder_out])



    # Concat attention input and decoder GRU output
    decoder_concat_input = Concatenate(axis=-1, name='concat_layer')([decoder_out, attn_out])


    # Dense layer

    dense = Dense(sp_vsize, activation='softmax', name='softmax_layer')
    dense_time = TimeDistributed(dense, name='time_distributed_layer')
    decoder_pred = dense_time(decoder_concat_input)


    # Full model
    full_model = Model(inputs=[encoder_inputs, decoder_inputs], outputs=decoder_pred)
    full_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics = ['accuracy'])
    full_model.summary()



    """ Inference model """

    batch_size = 1



    """ Encoder (Inference) model """

    encoder_inf_inputs = Input(batch_shape=(batch_size, en_timesteps, en_vsize), name='encoder_inf_inputs')
    encoder_inf_out, encoder_inf_state = encoder_gru(encoder_inf_inputs)
    encoder_model = Model(inputs=encoder_inf_inputs, outputs=[encoder_inf_out, encoder_inf_state])



    """ Decoder (Inference) model """
    decoder_inf_inputs = Input(batch_shape=(batch_size, 1, sp_vsize), name='decoder_word_inputs')
    encoder_inf_states = Input(batch_shape=(batch_size, en_timesteps, hidden_size), name='encoder_inf_states')
    decoder_init_state = Input(batch_shape=(batch_size, hidden_size), name='decoder_init')



    decoder_inf_out, decoder_inf_state = decoder_gru(decoder_inf_inputs, initial_state=decoder_init_state)
    attn_inf_out, attn_inf_states = attn_layer([encoder_inf_states, decoder_inf_out])
    decoder_inf_concat = Concatenate(axis=-1, name='concat')([decoder_inf_out, attn_inf_out])
    decoder_inf_pred = TimeDistributed(dense)(decoder_inf_concat)

    decoder_model = Model(inputs=[encoder_inf_states, decoder_init_state, decoder_inf_inputs],
                          outputs=[decoder_inf_pred, attn_inf_states, decoder_inf_state])



    return full_model, encoder_model, decoder_model
inp = Input(shape=img_shape_full)
model = InceptionResNetV2(weights='imagenet',
                          include_top=False,
                          input_shape=img_shape_full,
                          input_tensor=inp)

# Anadir capa para nuestro numero de clases
x = model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=model.input, outputs=predictions)

# TOTAL CAPAS = 782
LAYERS_TO_FREEZE = 700
for layer in model.layers[:LAYERS_TO_FREEZE]:
    layer.trainable = False

model.compile(optimizer="adam",
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.fit(images_train,
          labels_train,
          batch_size=128,
          epochs=1,
          verbose=1,
          validation_split=0.1)

score = model.evaluate(images_test, labels_test, verbose=0)

print('Testing set accuracy:', score[1])
def main(args):

    dataset = args.dataset
    data_type = args.data_type

    # data dir, image size and batch size
    DATA_DIR = 'data'
    TRAIN_DIR = os.path.join(DATA_DIR, 'train')
    VALID_DIR = os.path.join(DATA_DIR, 'valid')
    SIZE = (224, 224)
    BATCH_SIZE = 2

    # remove files
    try:
        shutil.rmtree(DATA_DIR)
    except:
        pass

    train_ratio = 0.8  # 80% data for training, the rest for testing

    # get the list of filenames and corresponding list of labels for training et validation
    train_filenames = []
    train_labels = []
    val_filenames = []
    val_labels = []

    # read files into label and frame lists
    with open('../data/labels/' + dataset + '_' + data_type +
              '_label.csv') as f:
        frames_labels = [(line.strip().split(',')[0],
                          line.strip().split(',')[1]) for line in f]

    # re-organize data by labels
    file_dir = '../data/' + dataset + '/' + data_type + '/jpg/'
    file_format = '.jpeg'
    dict_frame = {
    }  # key: label value, value: a list of indice in the original file
    for fr_lb in frames_labels:
        fr, lb = fr_lb
        if (lb not in dict_frame):
            dict_frame[lb] = []
        dict_frame[lb].append(file_dir + fr + file_format)

    random.seed()  # using current time as the seed
    # generate filenames and labels for training and validation dataset
    for lb in dict_frame:
        # pick random indices for training data for lb in dict_frame
        train_index = random.sample(range(0, len(dict_frame[lb])),
                                    int(train_ratio * len(dict_frame[lb])))
        for index in range(len(dict_frame[lb])):
            # training data
            if (index in train_index):
                train_filenames.append(dict_frame[lb][index])
                train_labels.append(int(lb) - 1)
            # validation data
            else:
                val_filenames.append(dict_frame[lb][index])
                val_labels.append(int(lb) - 1)

    assert set(train_labels) == set(
        val_labels), "Train and val labels don't correspond:\n{}\n{}".format(
            set(train_labels), set(val_labels))

    # create new dir data/train/label_x and data/valid/label_x
    for label in set(train_labels):
        os.makedirs(os.path.join(TRAIN_DIR, str(label)), exist_ok=True)
        os.makedirs(os.path.join(VALID_DIR, str(label)), exist_ok=True)

    # copy files
    for tr_file, label in zip(train_filenames, train_labels):
        shutil.copy2(tr_file, os.path.join(TRAIN_DIR, str(label)))
    for val_file, label in zip(val_filenames, val_labels):
        shutil.copy2(val_file, os.path.join(VALID_DIR, str(label)))

    # train models
    num_train_samples = sum([len(files) for r, d, files in os.walk(TRAIN_DIR)])
    num_valid_samples = sum([len(files) for r, d, files in os.walk(VALID_DIR)])

    num_train_steps = math.floor(num_train_samples / BATCH_SIZE)
    num_valid_steps = math.floor(num_valid_samples / BATCH_SIZE)

    gen = image.ImageDataGenerator()
    val_gen = image.ImageDataGenerator(horizontal_flip=True,
                                       vertical_flip=True)

    batches = gen.flow_from_directory(TRAIN_DIR,
                                      target_size=SIZE,
                                      class_mode='categorical',
                                      shuffle=True,
                                      batch_size=BATCH_SIZE)
    val_batches = val_gen.flow_from_directory(VALID_DIR,
                                              target_size=SIZE,
                                              class_mode='categorical',
                                              shuffle=True,
                                              batch_size=BATCH_SIZE)

    model = ResNet50()

    classes = list(iter(batches.class_indices))
    model.layers.pop()
    for layer in model.layers:
        layer.trainable = False
    last = model.layers[-1].output
    x = Dense(len(classes), activation="softmax")(last)
    finetuned_model = Model(model.input, x)
    finetuned_model.compile(optimizer=Adam(lr=0.0001),
                            loss='categorical_crossentropy',
                            metrics=['accuracy'])
    for c in batches.class_indices:
        classes[batches.class_indices[c]] = c
    finetuned_model.classes = classes

    early_stopping = EarlyStopping(patience=450)
    checkpointer = ModelCheckpoint('./resnet_model/resnet_50_best.h5',
                                   verbose=1,
                                   save_best_only=True)

    finetuned_model.fit_generator(batches,
                                  steps_per_epoch=num_train_steps,
                                  epochs=450,
                                  callbacks=[early_stopping, checkpointer],
                                  validation_data=val_batches,
                                  validation_steps=num_valid_steps)
    finetuned_model.save('./resnet_model/resnet_50_final.h5')
예제 #7
0
def UNet64(input_shape,
           n_predictions=1,
           lossfunction="mean_squared_error",
           simpleclassification=None,
           flatten_output=True,
           optimizer="adam",
           activation_hidden="relu",
           activation_output="relu",
           metrics=None):
    inputs = Input(shape=input_shape)

    conv01 = Conv2D(10, kernel_size=(3, 3),
                    padding="same")(inputs)  # 10 x 64x64
    conv01 = Activation(activation_hidden)(conv01)
    conv01_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv01)  # 10 x 32x32
    print("0)", conv01_pool.shape, "10 x 32x32")

    conv02 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv01_pool)  # 20 x 32x32
    conv02 = Activation(activation_hidden)(conv02)
    conv02_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv02)  # 20 x 16x16
    print("1)", conv02_pool.shape, "20 x 16x16")

    conv03 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv02_pool)  # 20 x 16x16
    conv03 = Activation(activation_hidden)(conv03)
    conv03_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv03)  # 20 x 8x8
    print("2)", conv03_pool.shape, "20 x 8x8")

    conv04 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv03_pool)  # 20 x 8x8
    conv04 = Activation(activation_hidden)(conv04)
    conv04_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv04)  # 20 x 4x4
    print("3)", conv04_pool.shape, "20 x 4x4")

    ### UPSAMPLING:
    up04 = UpSampling2D((2, 2))(conv04_pool)  # 20 x 8x8
    up04 = concatenate([conv04, up04], axis=3)  # 20+20 x 8x8
    print("4)", up04.shape, "40 x 8x8")

    up03 = UpSampling2D((2, 2))(up04)  # 40 x 16x16
    up03 = concatenate([conv03, up03], axis=3)  # 20+40 x 16x16
    print("5)", up03.shape, "60 x 16x16")

    up02 = UpSampling2D((2, 2))(up03)  # 60 x 32x32
    up02 = concatenate([conv02, up02], axis=3)  # 20+60 x 32x32
    print("6)", up02.shape, "80 x 32x32")

    up01 = UpSampling2D((2, 2))(up02)  # 80 x 64x64
    up01 = concatenate([conv01, up01], axis=3)  # 10+80 x 64x64
    print("7)", up01.shape, "90 x 64x64")

    output = Conv2D(n_predictions, (1, 1),
                    activation=activation_output)(up01)  # 1 x 64x64
    print("8)", output.shape, "{} x 64x64".format(n_predictions))
    if flatten_output:
        output = Flatten()(output)
        print("output flattened to {}".format(output.shape))
        if simpleclassification is not None:
            output = Dense(simpleclassification, activation='softmax')(output)
            print(
                "9)", output.shape,
                "zur Klassifikation von {} Klassen (mit softmax)".format(
                    simpleclassification))

    model = Model(inputs=inputs, outputs=output)
    if metrics is not None:
        model.compile(loss=lossfunction, optimizer=optimizer, metrics=metrics)
    else:
        model.compile(loss=lossfunction, optimizer=optimizer)
    return model
예제 #8
0
    def build_autoencoder(self, param):
        autoencoder = None
        input_img = Input(shape=(param.get('image_size'),
                                 param.get('image_size'),
                                 param.get('image_channels')),
                          name='input')
        x = Conv2D(256,
                   (param.get('cae_conv_size'), param.get('cae_conv_size')),
                   activation='relu',
                   padding='same')(input_img)  # tanh?
        x = MaxPooling2D(
            (param.get('cae_max_pool_size'), param.get('cae_max_pool_size')),
            padding='same')(x)
        x = Conv2D(128,
                   (param.get('cae_conv_size'), param.get('cae_conv_size')),
                   activation='relu',
                   padding='same')(x)
        x = MaxPooling2D(
            (param.get('cae_max_pool_size'), param.get('cae_max_pool_size')),
            padding='same')(x)
        x = Conv2D(128,
                   (param.get('cae_conv_size'), param.get('cae_conv_size')),
                   activation='relu',
                   padding='same')(x)
        x = MaxPooling2D(
            (param.get('cae_max_pool_size'), param.get('cae_max_pool_size')),
            padding='same')(x)
        x = Flatten()(x)
        encoded = Dense(param.get('code_size'), name='encoded')(x)

        print('encoded shape ', encoded.shape)
        ims = 8
        first = True
        x = Dense(int(ims * ims), activation='relu')(encoded)
        x = Reshape(target_shape=(ims, ims, 1))(x)  # -12
        while ims != param.get('image_size'):
            x = Conv2D(
                int(ims * ims / 2),
                (param.get('cae_conv_size'), param.get('cae_conv_size')),
                activation='relu',
                padding='same')(x)
            x = UpSampling2D((param.get('cae_max_pool_size'),
                              param.get('cae_max_pool_size')))(x)
            ims = ims * param.get('cae_max_pool_size')
        decoded = Conv2D(
            param.get('image_channels'),
            (param.get('cae_conv_size'), param.get('cae_conv_size')),
            activation='sigmoid',
            padding='same',
            name='decoded')(x)

        print('decoded shape ', decoded.shape)

        autoencoder = Model(input_img, decoded)
        autoencoder.compile(optimizer='adam', loss='mean_squared_error')

        # Create a separate encoder model
        encoder = Model(input_img, encoded)
        encoder.compile(optimizer='adam', loss='mean_squared_error')
        encoder.summary()

        # Create a separate decoder model
        decoder_inp = Input(shape=(param.get('code_size'), ))
        #		decoder_inp = Input(shape=encoded.output_shape)
        enc_layer_idx = utils.getLayerIndexByName(autoencoder, 'encoded')
        print('encoder layer idx ', enc_layer_idx)
        decoder_layer = autoencoder.layers[enc_layer_idx + 1](decoder_inp)
        for i in range(enc_layer_idx + 2, len(autoencoder.layers)):
            decoder_layer = autoencoder.layers[i](decoder_layer)
        decoder = Model(decoder_inp, decoder_layer)
        decoder.compile(optimizer='adam', loss='mean_squared_error')
        decoder.summary()

        return autoencoder, encoder, decoder
예제 #9
0
    def init_model(self, input_shape, num_classes, **kwargs):
        layers = 5
        filters_size = [64, 128, 256, 512, 512]
        kernel_size = (3, 3)
        pool_size = [(2, 2), (2, 2), (2, 2), (4, 1), (4, 1)]

        freq_axis = 2
        channel_axis = 3

        channel_size = 128
        min_size = min(input_shape[:2])
        melgram_input = Input(shape=input_shape)
        # x = ZeroPadding2D(padding=(0, 37))(melgram_input)

        x = Reshape((input_shape[0], input_shape[1], 1))(melgram_input)
        x = BatchNormalization(axis=freq_axis, name='bn_0_freq')(x)

        # Conv block 1
        x = Convolution2D(filters=filters_size[0],
                          kernel_size=kernel_size,
                          padding='same',
                          name='conv1')(x)
        x = ELU()(x)
        x = BatchNormalization(axis=channel_axis, name='bn1')(x)
        x = MaxPooling2D(pool_size=pool_size[0],
                         strides=pool_size[0],
                         name='pool1')(x)
        x = Dropout(0.1, name='dropout1')(x)

        min_size = min_size // pool_size[0][0]

        for layer in range(1, layers):
            min_size = min_size // pool_size[layer][0]
            if min_size < 1:
                break
            x = Convolution2D(filters=filters_size[layer],
                              kernel_size=kernel_size,
                              padding='same',
                              name=f'conv{layer + 1}')(x)
            x = ELU()(x)
            x = BatchNormalization(axis=channel_axis, name=f'bn{layer + 1}')(x)
            x = MaxPooling2D(pool_size=pool_size[layer],
                             strides=pool_size[layer],
                             name=f'pool{layer + 1}')(x)
            x = Dropout(0.1, name=f'dropout{layer + 1}')(x)

        x = Reshape((-1, channel_size))(x)

        gru_units = 32
        if num_classes > 32:
            gru_units = int(num_classes * 1.5)
        # GRU block 1, 2, output
        x = CuDNNGRU(gru_units, return_sequences=True, name='gru1')(x)
        x = CuDNNGRU(gru_units, return_sequences=False, name='gru2')(x)
        x = Dropout(0.3)(x)
        outputs = Dense(num_classes, activation='softmax', name='output')(x)

        model = TFModel(inputs=melgram_input, outputs=outputs)
        optimizer = optimizers.Adam(
            # learning_rate=1e-3,
            lr=1e-3,
            beta_1=0.9,
            beta_2=0.999,
            epsilon=1e-08,
            decay=1e-4,
            amsgrad=True)
        model.compile(optimizer=optimizer,
                      loss="sparse_categorical_crossentropy",
                      metrics=['accuracy'])
        model.summary()
        self._model = model
        self.is_init = True
예제 #10
0
# negative_context_product = Dense(1, activation = "sigmoid")(negative_context_product)
boost = 1
import sys
if len(sys.argv) > 5:
    boost = float(sys.argv[5])
if boost > 1:
    negative_context_product = Lambda(lambda x: x * boost)(
        negative_context_product)
negative_context_product = Lambda(lambda x: tf.math.sigmoid(x))(
    negative_context_product)
# The dot products are outputted

model = Model(inputs=[word_index, context, negative_samples],
              outputs=[word_context_product, negative_context_product])
# binary crossentropy is applied on the output
model.compile(optimizer='rmsprop', loss='binary_crossentropy')
print(model.summary())
plot_model(model, to_file='model.png')
print(V_gen.getStepsPerEpoch(sentences, batchSize=1))
# model.fit_generator(V_gen.pretraining_batch_generator(sentences, vocabulary, reverse_vocabulary), samples_per_epoch=G.train_words, nb_epoch=1)
test = next(
    V_gen.pretraining_batch_generator(sentences, vocabulary,
                                      reverse_vocabulary))
model.fit_generator(V_gen.pretraining_batch_generator(sentences, vocabulary,
                                                      reverse_vocabulary),
                    epochs=1,
                    steps_per_epoch=V_gen.getStepsPerEpoch(sentences,
                                                           batchSize=1))
# Save the trained embedding

# outputs = [layer.output for layer in model.layers]
# merge feature extractors
merge = Concatenate()([flat1, flat2, flat3])

# interpretation layer
hidden1 = Dense(512, activation='relu')(merge)

# prediction output
hidden2 = Dense(256, activation='relu')(hidden1)
output = Dense(2, activation='softmax')(hidden2)

model = Model(inputs=[input1, input2, input3], outputs=output)

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer=SGD(lr=0.001),
              metrics=['accuracy'])

NAME = 'Late Merging'

# filepathdest_incep = "/content/gdrive/My Drive/Colab Notebooks/Results/BaseCNN.hdf5"

# callback_setting = [ModelCheckpoint(filepath=filepathdest_incep, verbose=1, monitor='val_loss', mode='min', save_best_only=True)]

# log_dir = "logs/fit/" + NAME +' ' + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
# tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, write_images=False)

history = model.fit_generator(three_inp,
                              steps_per_epoch=len(X_train1) / 50,
                              epochs=650,
                              verbose=1,
예제 #12
0
def MixModel(input_size=INPUT_SIZE, kernel_size=KERNEL_SIZE,
             activation=ACTIVATION,
             padding=PADDING,
             strides=STRIDES,
             kernel_initializer=KERNEL_INITIALIZER,
             pool_size=POOL_SIZE,
             model_depth=3,
             metrics=dice):
    inputs = tf.keras.Input(input_size, name="inputs")
    layer_list = list()
    conv2 = None
    current_filters = 32
    # down sampling
    for depth in range(model_depth):
        if depth == 0:
            inputs2 = Conv2D(64, 3, padding="same", activation="relu", kernel_initializer="he_normal")(inputs)
            continue
        # resnet block
        if depth > 1:
            #     cut = pol1
            current_filters = current_filters * (2 * depth)
        # if depth >1 and depth%2 == 0:
        #     cut2 = pol1

        #         print("depth and num_filters:",(depth,current_filters))

        if conv2 != None:
            conv1 = ConvBlock(pol1,
                              num_filters=current_filters,
                              kernel_size=kernel_size,
                              activation=activation,
                              padding=padding,
                              strides=strides,
                              kernel_initializer=kernel_initializer,
                              BN=True)
        else:
            conv1 = ConvBlock(inputs2,
                              num_filters=current_filters,
                              kernel_size=kernel_size,
                              activation=activation,
                              padding=padding,
                              strides=strides,
                              kernel_initializer=kernel_initializer,
                              BN=True)

        #         pol1 = MaxPooling2D(pool_size=POOL_SIZE)(conv1)

        # current_filters = current_filters * (depth**2)
        conv2 = ConvBlock(conv1,
                          num_filters=current_filters * 2,
                          kernel_size=kernel_size,
                          activation=activation,
                          padding=padding,
                          strides=strides,
                          kernel_initializer=kernel_initializer,
                          BN=True)

        #         print("conv1:",conv1.shape)
        #         print("conv2:", conv2.shape)

        if depth == model_depth - 1:
            conv2 = tf.keras.layers.Dropout(0.5)(conv2)

        pol1 = MaxPooling2D(pool_size=POOL_SIZE)(conv2)

        #         print("polshape:",pol1.shape)

        layer_list.append([conv1, conv2, pol1])
    #         print(layer_list)

    current_filters = pol1.shape[3]

    conv3 = Conv2D(1024, 3, padding="same", kernel_initializer="he_normal", activation="relu")(pol1)
    pol2 = MaxPooling2D(pool_size=POOL_SIZE)(conv3)
    conv4 = Conv2D(1024, 3, padding="same", kernel_initializer="he_normal", activation="relu")(pol2)
    up1 = UpSampling2D(size=POOL_SIZE)(conv4)
    conv5 = Conv2D(512, 3, padding="same", kernel_initializer="he_normal", activation="relu")(up1)

    # upsampling
    for depth in range(model_depth):
        if depth == 0:
            continue
        if depth == 1:
            up1 = UpSampling2D(size=POOL_SIZE)(conv5)

        if depth > 1:
            current_filters = int(current_filters / (2 * depth))
            up1 = UpSampling2D(size=POOL_SIZE)(up_conv2)
        #         print("up1shape:",up1.shape)
        #         print("num_filters:",(current_filters,depth))

        up_conv1 = upConvBlock(up1,
                               num_filters=current_filters,
                               kernel_size=kernel_size,
                               activation=activation,
                               padding=padding,
                               strides=strides,
                               kernel_initializer=kernel_initializer,
                               BN=True)
        #         print(layer_list)
        #         print("compare:",(up_conv1.shape,layer_list[model_depth-depth-1][1].shape))
        merge = tf.keras.layers.concatenate([up_conv1, layer_list[model_depth - depth - 1][0]], axis=3)

        up_conv2 = upConvBlock(merge,
                               num_filters=current_filters,
                               kernel_size=kernel_size,
                               activation=activation,
                               padding=padding,
                               strides=strides,
                               kernel_initializer=kernel_initializer,
                               BN=True)
        # if depth>1:
        #     up_conv2 += cut
        # if depth>1 and cut2 != None:
        #     conv2 += cut2
        #     cut2 = None
    up_conv2 = Conv2D(64, 3, padding="same", kernel_initializer="he_normal", activation="relu")(up_conv2)
    up_conv2 = Conv2D(3, 3, padding="same", kernel_initializer="he_normal", activation="relu")(up_conv2)

    model = Model(inputs, up_conv2)

    if not isinstance(metrics, list):
        metrics = [metrics]

    #     label_wise_dice_metrics = [get_label_dice_coefficient_function(index) for index in range(BATCH_SIZE)]
    #     if metrics:
    #         metrics = metrics + label_wise_dice_metrics
    #     else:
    #         metrics = label_wise_dice_metrics

    #     with tf.device('/cpu:0'):
    #         parallel_model = multi_gpu_model(model, gpus=2)
    #     model.compile(optimizer="adam",loss=dice_coefficient_loss, metrics=metrics)
    model.compile(optimizer="adam", loss=dice_loss, metrics=[dice])
    # model.summary()
    return model
예제 #13
0
predictions = (Dense(6,
                     activation="linear",
                     name="predictions",
                     input_shape=(10, )))(input_next)

# Use the Adam method for training the network.
# We want to find the best learning-rate for the Adam method.
optimizer = Adam(lr=1e-4)
model = Model(inputs=[input_first], outputs=predictions)
homepath = os.environ["HOME"]
path_best_model = '{}/10_17_tvweights_LSTM_Regression_Model_SAIL_SPEECH.keras'.format(
    homepath)
# model = Model(inputs=[input_first], outputs=predictions)
model = load_model(path_best_model)
# In Keras we need to compile the model so it can be trained.
model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['mse'])

mfcc = mfccsList[0]
track = trackList[0]
mfccs = (np.load(mfcc))
sixDistances = np.load(track)

mfcc_array = []
sixDistances_array = []

path_stdscaler = '{}/10_17_tvweight_StandardScaler.pkl'.format(homepath)
path_mmscaler = '{}/10_17_tvweight_MinMaxScaler.pkl'.format(homepath)
fitter = joblib.load(path_stdscaler)
scaler = joblib.load(path_mmscaler)

mfccs = fitter.fit_transform(mfccs)
예제 #14
0
파일: dmn.py 프로젝트: shahnizam1/SA-DL
    def __init__(self):
        self.HOPS = 5
        self.DATASET = 'twitter'  # 'restaurant', 'laptop'
        self.POLARITIES_DIM = 3
        self.EMBEDDING_DIM = 200
        self.LEARNING_RATE = 0.01
        self.LSTM_PARAMS = {
            'units':
            200,
            'activation':
            'tanh',
            'recurrent_activation':
            'sigmoid',
            'kernel_initializer':
            initializers.RandomUniform(minval=-0.003, maxval=0.003),
            'recurrent_initializer':
            initializers.RandomUniform(minval=-0.003, maxval=0.003),
            'bias_initializer':
            initializers.RandomUniform(minval=-0.003, maxval=0.003),
            'kernel_regularizer':
            regularizers.l2(0.001),
            'recurrent_regularizer':
            regularizers.l2(0.001),
            'bias_regularizer':
            regularizers.l2(0.001),
            'dropout':
            0,
            'recurrent_dropout':
            0,
        }
        self.MAX_SEQUENCE_LENGTH = 40
        self.MAX_ASPECT_LENGTH = 2
        self.ITERATION = 500
        self.BATCH_SIZE = 200

        self.texts_raw_indices, self.texts_left_indices, self.aspects_indices, self.texts_right_indices, \
        self.polarities_matrix, \
        self.embedding_matrix, \
        self.tokenizer = \
            read_dataset(type=self.DATASET,
                         mode='train',
                         embedding_dim=self.EMBEDDING_DIM,
                         max_seq_len=self.MAX_SEQUENCE_LENGTH, max_aspect_len=self.MAX_ASPECT_LENGTH)

        if os.path.exists('dmn_saved_model.h5'):
            print('loading saved model...')
            self.model = load_model('dmn_saved_model.h5')
        else:
            print('Build model...')
            inputs_sentence = Input(shape=(self.MAX_SEQUENCE_LENGTH * 2 +
                                           self.MAX_ASPECT_LENGTH, ),
                                    name='inputs_sentence')
            inputs_aspect = Input(shape=(self.MAX_ASPECT_LENGTH, ),
                                  name='inputs_aspect')
            memory = Embedding(input_dim=len(self.tokenizer.word_index) + 1,
                               output_dim=self.EMBEDDING_DIM,
                               input_length=self.MAX_SEQUENCE_LENGTH * 2 +
                               self.MAX_ASPECT_LENGTH,
                               weights=[self.embedding_matrix],
                               trainable=False,
                               name='sentence_embedding')(inputs_sentence)
            memory = Lambda(self.locationed_memory,
                            name='locationed_memory')(memory)
            aspect = Embedding(input_dim=len(self.tokenizer.word_index) + 1,
                               output_dim=self.EMBEDDING_DIM,
                               input_length=self.MAX_ASPECT_LENGTH,
                               weights=[self.embedding_matrix],
                               trainable=False,
                               name='aspect_embedding')(inputs_aspect)
            x = Lambda(lambda xin: K.mean(xin, axis=1),
                       name='aspect_mean')(aspect)
            SharedAttention = Attention(name='shared_attention')
            for i in range(self.HOPS):
                x = SharedAttention((memory, x))
            x = Dense(self.POLARITIES_DIM)(x)
            predictions = Activation('softmax')(x)
            model = Model(inputs=[inputs_sentence, inputs_aspect],
                          outputs=predictions)
            model.summary()
            model.compile(loss='categorical_crossentropy',
                          optimizer=optimizers.Adam(lr=self.LEARNING_RATE),
                          metrics=['acc'])
            # plot_model(model, to_file='model.png')
            self.model = model
예제 #15
0
class HybridCNN:
    def __init__(self, word_embedding_map, code_embedding_map, word_tokenizer, code_tokenizer, model_config):
        self.model_config = model_config
        self.word_tokenizer = word_tokenizer
        self.code_tokenizer = code_tokenizer
        self.word_embedding_map = word_embedding_map
        self.code_embedding_map = code_embedding_map
        self.model = None

    def create_model(self):
        # Declaration for KimCNN-based word encoder
        word_encoder_input = Input(shape=(self.model_config.max_word_len,), dtype='int32')
        word_embedding_layer = Embedding(len(self.word_tokenizer.word_index) + 1, self.model_config.word_embedding_dim,
                                         weights=[self.word_embedding_map], input_length=self.model_config.max_word_len,
                                         trainable=False)
        embedded_word_sequences = word_embedding_layer(word_encoder_input)

        w_conv1 = Conv1D(100, 3, activation='relu', padding='same')(embedded_word_sequences)
        w_pool1 = GlobalMaxPool1D()(w_conv1)
        w_conv2 = Conv1D(100, 4, activation='relu', padding='same')(embedded_word_sequences)
        w_pool2 = GlobalMaxPool1D()(w_conv2)
        w_conv3 = Conv1D(100, 5, activation='relu', padding='same')(embedded_word_sequences)
        w_pool3 = GlobalMaxPool1D()(w_conv3)
        w_concat1 = Concatenate()([w_pool1, w_pool2, w_pool3])
        word_encoder = Model(word_encoder_input, w_concat1)

        # Declaration for KimCNN-based code encoder
        code_encoder_input = Input(shape=(self.model_config.max_code_len,), dtype='int32')
        code_embedding_layer = Embedding(len(self.code_tokenizer.word_index) + 1, self.model_config.code_embedding_dim,
                                         weights=[self.code_embedding_map], input_length=self.model_config.max_code_len,
                                         trainable=False)
        embedded_code_sequences = code_embedding_layer(code_encoder_input)

        c_conv1 = Conv1D(100, 3, activation='relu', padding='same')(embedded_code_sequences)
        c_pool1 = GlobalMaxPool1D()(c_conv1)
        c_conv2 = Conv1D(100, 4, activation='relu', padding='same')(embedded_code_sequences)
        c_pool2 = GlobalMaxPool1D()(c_conv2)
        c_conv3 = Conv1D(100, 5, activation='relu', padding='same')(embedded_code_sequences)
        c_pool3 = GlobalMaxPool1D()(c_conv3)
        c_concat1 = Concatenate()([c_pool1, c_pool2, c_pool3])
        code_encoder = Model(code_encoder_input, c_concat1)

        # Similarity classifier using the word and code encoders
        word_input1 = Input(shape=(self.model_config.max_word_len,), dtype='int32')
        word_input2 = Input(shape=(self.model_config.max_word_len,), dtype='int32')
        code_input1 = Input(shape=(self.model_config.max_code_len,), dtype='int32')
        code_input2 = Input(shape=(self.model_config.max_code_len,), dtype='int32')
        l_concat1 = Concatenate()([word_encoder(word_input1), word_encoder(word_input2),
                                   code_encoder(code_input1), code_encoder(code_input2)])
        l_dense1 = Dense(self.model_config.hidden_dim, activation='relu')(l_concat1)
        l_dropout1 = Dropout(self.model_config.dropout)(l_dense1)
        l_dense2 = Dense(self.model_config.hidden_dim, activation='relu')(l_dropout1)
        l_dropout2 = Dropout(self.model_config.dropout)(l_dense2)
        preds = Dense(self.model_config.num_classes, activation='softmax')(l_dropout2)
        self.model = Model([word_input1, word_input2, code_input1, code_input2], preds)
        self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        self.model.summary()

    def train(self, train_xw1, train_xw2, train_xc1, train_xc2, train_y, evaluate_xw1, evaluate_xw2, evaluate_xc1,
              evaluate_xc2, evaluate_y, **kwargs):
        iteration = 0
        if 'iteration' in kwargs:
            iteration = kwargs['iteration']
        early_stopping_callback = EarlyStopping(patience=self.model_config.patience, monitor='val_acc')
        checkpoint_callback = ModelCheckpoint(filepath="data/checkpoints/hybrid_cnn/%s_%d.hdf5" %
                                                       (self.model_config.dataset, iteration),
                                                       monitor='val_acc', verbose=1, save_best_only=True)
        self.model.fit([train_xw1, train_xw2, train_xc1, train_xc2], train_y,
                       validation_data=([evaluate_xw1, evaluate_xw2, evaluate_xc1, evaluate_xc2], evaluate_y),
                       epochs=self.model_config.epochs, batch_size=self.model_config.batch_size,
                       callbacks=[early_stopping_callback, checkpoint_callback])
        self.model.load_weights(filepath="data/checkpoints/hybrid_cnn/%s_%d.hdf5" % (self.model_config.dataset, iteration))

    def predict(self, predict_xw1, predict_xw2, predict_xc1, predict_xc2):
        return self.model.predict([predict_xw1, predict_xw2, predict_xc1, predict_xc2])

    def evaluate(self, evaluate_xw1, evaluate_xw2, evaluate_xc1, evaluate_xc2, evaluate_y):
        predict_y = self.predict(evaluate_xw1, evaluate_xw2, evaluate_xc1, evaluate_xc2).argmax(axis=1)
        evaluate_y = evaluate_y.argmax(axis=1)
        return {"individual": precision_recall_fscore_support(evaluate_y, predict_y),
                "micro-average": precision_recall_fscore_support(evaluate_y, predict_y, average="micro")}

    def cross_val(self, data_xw1, data_xw2, data_xc1, data_xc2, data_y, n_splits=5):
        skf = StratifiedKFold(n_splits, shuffle=False, random_state=157)
        print("Performing cross validation (%d-fold)..." % n_splits)
        iteration = 1
        mean_accuracy = 0
        recall_list = [0 for _ in range(self.model_config.num_classes)]
        precision_list = [0 for _ in range(self.model_config.num_classes)]
        for train_index, test_index in skf.split(data_xw1, data_y.argmax(axis=1)):
            self.create_model()
            print("Iteration %d of %d" % (iteration, n_splits))
            self.train(data_xw1[train_index], data_xw2[train_index], data_xc1[train_index], data_xc2[train_index],
                       data_y[train_index], data_xw1[test_index], data_xw2[test_index], data_xc1[test_index],
                       data_xc2[test_index], data_y[test_index], iteration=iteration)
            metrics = self.evaluate(data_xw1[test_index], data_xw2[test_index], data_xc1[test_index],
                                    data_xc2[test_index], data_y[test_index])
            precision_list = [x + y for x, y in zip(metrics['individual'][0], precision_list)]
            recall_list = [x + y for x, y in zip(metrics['individual'][1], recall_list)]
            mean_accuracy += metrics['micro-average'][0]
            print("Precision, Recall, F_Score, Support")
            iteration += 1
            print(metrics)
        print("Mean accuracy: %s Mean precision: %s, Mean recall: %s" % (mean_accuracy/n_splits,
                                                                         [precision/n_splits for precision in precision_list],
                                                                         [recall/n_splits for recall in recall_list]))
예제 #16
0
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense, Input
from tensorflow.python.keras import optimizers, losses
from tensorflow.python.keras.utils import to_categorical
from modelhelpers import GradientActivationStore
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'


(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784).astype('float32')
x_test = x_test.reshape(10000, 784).astype('float32')

y_train_hot = to_categorical(y_train, 10)
y_test_hot = to_categorical(y_test, 10)


x_in = Input(shape=(784,))
l = Dense(1000, activation='relu')(x_in)
for i in range(2):
    l = Dense(1000, activation='relu')(l)
output = Dense(10, activation='softmax')(l)

model = Model(inputs=x_in, outputs=output)

model.compile(loss=losses.categorical_crossentropy, optimizer=optimizers.SGD(lr=0.001))

cbk = GradientActivationStore(DIR='logs', filename='test', num_classes=10, record_every=1, only_weights=True)

fitting = model.fit(x=x_train[:100], y=y_train_hot[:100], batch_size=32,
                 epochs=3, callbacks=[cbk], validation_data=(x_test[:100], y_test_hot[:100]), verbose=1)
             kernel_size=(3, 3),
             padding='same',
             activation='relu')(out)
out = Conv2D(filters=128, kernel_size=(3, 3), activation='relu')(out)
out = BatchNormalization()(out)
out = MaxPooling2D(pool_size=(2, 2))(out)
out = Dropout(0.5)(out)
out = Conv2D(filters=256, kernel_size=(3, 3), activation='relu')(out)
out = BatchNormalization()(out)
out = MaxPooling2D(pool_size=(2, 2))(out)
out = Flatten()(out)
out = Dropout(0.5)(out)
out = Dense(1, name='6digit', activation='sigmoid')(out)
model = Model(inputs=inp, outputs=out)
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()

print("Reading training data...")
traincsv = open(TRAINING_DIR + '/data/56_imitate_train_set/len_train.csv',
                'r',
                encoding='utf8')
train_data = np.stack([
    np.array(
        Image.open(TRAINING_DIR + "/data/56_imitate_train_set/" + row[0] +
                   ".jpg")) / 255.0 for row in csv.reader(traincsv)
])
traincsv = open(TRAINING_DIR + '/data/56_imitate_train_set/len_train.csv',
                'r',
                encoding='utf8')
예제 #18
0
파일: keras_train.py 프로젝트: ifuding/TC
    def small_densenet(self,
                       img_input_shape=(64, 64, 3),
                       blocks=[6, 12, 24, 16],
                       weight_decay=1e-4,
                       kernel_initializer='he_normal',
                       init_filters=None,
                       reduction=None,
                       growth_rate=None,
                       init_stride=None):
        img_input = Input(shape=(img_input_shape))

        # x = layers.Conv2D(init_filters, 3, strides=1, use_bias=False,
        #     kernel_initializer = kernel_initializer,
        #     kernel_regularizer = l2(weight_decay),
        #     name='conv1/conv')(img_input)

        x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
        x = layers.Conv2D(init_filters,
                          3,
                          strides=init_stride,
                          use_bias=False,
                          kernel_initializer=kernel_initializer,
                          kernel_regularizer=l2(weight_decay),
                          name='conv1/conv')(x)
        x = layers.BatchNormalization(axis=3,
                                      epsilon=1.001e-5,
                                      name='conv1/bn')(x)
        x = layers.Activation('relu', name='conv1/relu')(x)
        x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
        x = layers.AveragePooling2D(3, strides=2, name='pool1')(x)

        for i, block in enumerate(blocks):
            scope_num_str = str(i + 2)
            x = self.dense_block(x,
                                 block,
                                 name='conv' + scope_num_str,
                                 growth_rate=growth_rate,
                                 weight_decay=weight_decay,
                                 kernel_initializer=kernel_initializer)
            if i != len(blocks) - 1:
                x = self.transition_block(
                    x,
                    reduction,
                    name='pool' + scope_num_str,
                    weight_decay=weight_decay,
                    kernel_initializer=kernel_initializer)
        x = layers.BatchNormalization(axis=3, epsilon=1.001e-5, name='bn')(x)
        x = layers.Activation('relu', name='relu')(x)

        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        # x = Lambda(lambda x: x, name = 'densenet_features')(x)
        # x = self.full_connect_layer(x, self.hidden_dim, weight_decay = weight_decay, kernel_initializer = kernel_initializer)
        x = layers.Dense(
            self.cat_max,
            activation='softmax',
            kernel_initializer=kernel_initializer,
            # kernel_regularizer = l2(weight_decay),
            name='fc')(x)

        model = Model(img_input, x)
        # print (model.summary())
        model.compile(optimizer=Adam(lr=self.lr),
                      loss='categorical_crossentropy',
                      metrics=['categorical_accuracy'])

        return model
예제 #19
0
def UNet64_2x2core_large(input_shape):
    """wie UNet64_out_expansed, aber downsampling bis 2x2"""
    inputs = Input(shape=input_shape)

    conv01 = Conv2D(10, kernel_size=(3, 3),
                    padding="same")(inputs)  # 10 x 64x64
    conv01 = Activation('relu')(conv01)
    conv01_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv01)  # 10 x 32x32
    print("0)", conv01_pool.shape, "10 x 32x32")

    conv02 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv01_pool)  # 20 x 32x32
    conv02 = Activation('relu')(conv02)
    conv02_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv02)  # 20 x 16x16
    print("1)", conv02_pool.shape, "20 x 16x16")

    conv03 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv02_pool)  # 20 x 16x16
    conv03 = Activation('relu')(conv03)
    conv03_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv03)  # 20 x 8x8
    print("2)", conv03_pool.shape, "20 x 8x8")

    conv04 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv03_pool)  # 20 x 8x8
    conv04 = Activation('relu')(conv04)
    conv04_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv04)  # 20 x 4x4
    print("3)", conv04_pool.shape, "20 x 4x4")

    conv05 = Conv2D(20, kernel_size=(3, 3),
                    padding="same")(conv04_pool)  # 20 x 4x4
    conv05 = Activation('relu')(conv05)
    conv05_pool = MaxPooling2D((2, 2), strides=(2, 2))(conv05)  # 20 x 2x2
    print("4)", conv05_pool.shape, "20 x 2x2")

    ### UPSAMPLING:
    up05 = UpSampling2D((2, 2))(conv05_pool)  # 20 x 4x4
    up05 = concatenate([conv05, up05], axis=3)  # 40 x 4x4
    print("4)", up05.shape, "40 x 4x4")

    up04 = UpSampling2D((2, 2))(up05)  # 10 x 8x8
    up04 = concatenate([conv04, up04], axis=3)  # 20+40 x 8x8
    print("4)", up04.shape, "60 x 8x8")

    up03 = UpSampling2D((2, 2))(up04)  # 30 x 16x16
    up03 = concatenate([conv03, up03], axis=3)  # 20+60 x 16x16
    print("5)", up03.shape, "80 x 16x16")

    up02 = UpSampling2D((2, 2))(up03)  # 80 x 32x32
    up02 = concatenate([conv02, up02], axis=3)  # 20+80 x 32x32
    print("6)", up02.shape, "100 x 32x32")

    up01 = UpSampling2D((2, 2))(up02)  # 100 x 64x64
    up01 = concatenate([conv01, up01], axis=3)  # 10+100 x 64x64
    print("7)", up01.shape, "110 x 64x64")

    output = Conv2D(1, (3, 3), activation='relu',
                    padding="same")(up01)  # 1 x 64x64
    print("8)", output.shape, "1 x 64x64")
    output = Flatten()(output)
    model = Model(inputs=inputs, outputs=output)
    model.compile(loss="mean_squared_error", optimizer='adam')
    return model
예제 #20
0
파일: model.py 프로젝트: wangcj05/sciann
    def __init__(self,
                 inputs=None,
                 targets=None,
                 loss_func="mse",
                 optimizer="adam",
                 load_weights_from=None,
                 plot_to_file=None,
                 **kwargs):
        # strictly check for inputs to be of type variable.
        inputs = to_list(inputs)
        if not all([is_variable(x) for x in inputs]):
            raise ValueError(
                'Please provide a `list` of `Variable` or `RadialBasis` objects for inputs. '
            )
        # prepare input tensors.
        input_vars = []
        for var in inputs:
            input_vars += var.inputs
        # check outputs if of correct type.
        if targets is None:
            if 'constraints' in kwargs:
                targets = kwargs.get('constraints')
            elif 'conditions' in kwargs:
                targets = kwargs.get('conditions')
        else:
            if 'conditions' in kwargs or 'constraints' in kwargs:
                raise TypeError(
                    'Inconsistent inputs: `constraints`, `conditions`, and `targets` are all equivalent keywords '
                    '- pass all targets as a list to `SciModel`. ')
        # setup constraints.
        targets = to_list(targets)
        for i, y in enumerate(targets):
            if not is_constraint(y):
                if is_functional(y):
                    # Case of Data-type constraint.
                    targets[i] = Data(y)
                elif isinstance(y, tuple) and \
                        len(y) == 2 and \
                        is_functional(y[0]) and is_functional(y[1]):
                    # Case of Tie-type constraint.
                    targets[i] = Tie(y[0], y[1])
                else:
                    # Not recognised.
                    raise ValueError(
                        'The {}th target entry is not of type `Constraint` or `Functional` - '
                        'received \n ++++++ {} '.format(i, y))
        # prepare network outputs.
        output_vars = []
        for cond in targets:
            output_vars += cond().outputs
        # prepare loss_functions.
        if isinstance(loss_func, str):
            loss_func = SciModel.loss_functions(loss_func)
        elif not callable(loss_func):
            raise TypeError(
                'Please provide a valid loss function from ("mse", "mae") ' +
                "or a callable function for input of tensor types. ")
        # Initialize the Model form super class.
        model = Model(inputs=input_vars, outputs=output_vars, **kwargs)
        # compile the model.
        model.compile(
            loss=loss_func,
            optimizer=optimizer,
        )
        # model.train_function = True

        # set initial state of the model.
        if load_weights_from is not None:
            if os.path.exists(load_weights_from):
                model.load_weights(load_weights_from)
            else:
                raise Warning("File not found - load_weights_from: {}".format(
                    load_weights_from))
        # Set the variables.
        self._model = model
        self._inputs = inputs
        self._constraints = targets
        self._loss_func = loss_func
        # Plot to file if requested.
        if plot_to_file is not None:
            plot_model(self._model, to_file=plot_to_file)
    def fit(self,
            learning_rate=1e-4,
            epochs=5,
            activation='relu',
            dropout=0,
            hidden_size=1024,
            nb_layers=1,
            include_class_weight=False,
            save_augmented=False,
            batch_size=20,
            save_model=False,
            verbose=True,
            fine_tuning=False,
            NB_IV3_LAYERS_TO_FREEZE=279,
            use_TPU=False,
            transfer_model='Inception',
            min_accuracy=None,
            cutoff_regularization=False,
            extract_SavedModel=False):

        #if we want stop training when no sufficient improvement in accuracy has been achieved
        if min_accuracy is not None:
            callback = EarlyStopping(monitor='categorical_accuracy',
                                     baseline=min_accuracy)
            callback = [callback]
        else:
            callback = None

        #load the pretrained model, without the classification (top) layers
        if transfer_model == 'Xception':
            base_model = Xception(weights='imagenet',
                                  include_top=False,
                                  input_shape=(299, 299, 3))
            target_size = (299, 299)
        elif transfer_model == 'Inception_Resnet':
            base_model = InceptionResNetV2(weights='imagenet',
                                           include_top=False,
                                           input_shape=(299, 299, 3))
            target_size = (299, 299)
        elif transfer_model == 'Resnet':
            base_model = ResNet50(weights='imagenet',
                                  include_top=False,
                                  input_shape=(224, 224, 3))
            target_size = (224, 224)
        else:
            base_model = InceptionV3(weights='imagenet',
                                     include_top=False,
                                     input_shape=(299, 299, 3))
            target_size = (299, 299)

        #We expect the classes to be the name of the folders in the training set
        self.categories = os.listdir(TRAIN_DIR)

        #Add the classification layers using Keras functional API
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        for _ in range(nb_layers):
            x = Dense(hidden_size, activation=activation)(
                x)  #Hidden layer for classification
            if dropout > 0:
                x = Dropout(rate=dropout)(x)

        predictions = Dense(len(self.categories),
                            activation='softmax')(x)  #Output layer
        model = Model(inputs=base_model.input, outputs=predictions)

        #Set only the top layers as trainable (if we want to do fine-tuning,
        #we can train the base layers as a second step)
        for layer in base_model.layers:
            layer.trainable = False

        #Define the optimizer and the loss, and compile the model
        loss = 'categorical_crossentropy'
        if use_TPU:
            #if we want to try out the TPU, it looks like we currently need to use
            #tensorflow optimizers...see https://stackoverflow.com/questions/52940552/valueerror-operation-utpu-140462710602256-varisinitializedop-has-been-marked
            #...and https://www.youtube.com/watch?v=jgNwywYcH4w
            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
            tpu_optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer)
            model.compile(optimizer=tpu_optimizer,
                          loss=loss,
                          metrics=['categorical_accuracy'])

            TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
            model = tf.contrib.tpu.keras_to_tpu_model(
                model,
                strategy=tf.contrib.tpu.TPUDistributionStrategy(
                    tf.contrib.cluster_resolver.TPUClusterResolver(
                        TPU_WORKER)))
            tf.logging.set_verbosity(tf.logging.INFO)

        else:
            optimizer = Adam(lr=learning_rate)
            model.compile(optimizer=optimizer,
                          loss=loss,
                          metrics=['categorical_accuracy'])

        datagen_train = ImageDataGenerator(rotation_range=180,
                                           rescale=1. / 255,
                                           width_shift_range=0.1,
                                           height_shift_range=0.1,
                                           shear_range=0.1,
                                           zoom_range=[0.9, 1.5],
                                           horizontal_flip=True,
                                           vertical_flip=True,
                                           fill_mode='nearest')

        datagen_val = ImageDataGenerator(rescale=1. / 255)

        #Save the augmented images if we want to
        if save_augmented:
            save_to_dir = AUGMENTED_DIR
        else:
            save_to_dir = None

        self.generator_train = datagen_train.flow_from_directory(
            directory=TRAIN_DIR,
            target_size=target_size,
            batch_size=batch_size,
            shuffle=True,
            save_to_dir=save_to_dir)

        self.generator_val = datagen_val.flow_from_directory(
            directory=VAL_DIR,
            target_size=target_size,
            batch_size=batch_size,
            shuffle=False)

        steps_per_epoch = self.generator_train.n / batch_size
        self.val_steps_per_epoch = self.generator_val.n / batch_size

        #if we want to weight the classes given the imbalanced number of images
        if include_class_weight:
            from sklearn.utils.class_weight import compute_class_weight
            cls_train = self.generator_train.classes
            class_weight = compute_class_weight(class_weight='balanced',
                                                classes=np.unique(cls_train),
                                                y=cls_train)
        else:
            class_weight = None

        #Fit the model
        history = model.fit_generator(
            generator=self.generator_train,
            epochs=epochs,
            steps_per_epoch=steps_per_epoch,
            verbose=verbose,
            class_weight=class_weight,
            validation_data=self.generator_val,
            validation_steps=self.val_steps_per_epoch,
            callbacks=callback)

        #Fine-tune the model, if we wish so
        if fine_tuning and not model.stop_training:
            print('============')
            print('Begin fine-tuning')
            print('============')

            #declare the first layers as trainable
            for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]:
                layer.trainable = False
            for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]:
                layer.trainable = True

            model.compile(optimizer=Adam(lr=learning_rate * 0.1),
                          loss=loss,
                          metrics=['categorical_accuracy'])

            #Fit the model
            history = model.fit_generator(
                generator=self.generator_train,
                epochs=epochs,
                steps_per_epoch=steps_per_epoch,
                verbose=verbose,
                class_weight=class_weight,
                validation_data=self.generator_val,
                validation_steps=self.val_steps_per_epoch)

        #Evaluate the model, just to be sure
        self.fitness = history.history['val_categorical_accuracy'][-1]

        #Save the model
        if save_model:
            if not os.path.exists(parentdir + '/data/trained_models'):
                os.makedirs(parentdir + '/data/trained_models')
            model.save(parentdir + '/data/trained_models/trained_model.h5')
            print('Model saved!')

        #save model in production format
        if extract_SavedModel:
            export_path = "./image_classifier/1/"

            with K.get_session() as sess:
                tf.saved_model.simple_save(
                    sess,
                    export_path,
                    inputs={'input_image': model.input},
                    outputs={t.name: t
                             for t in model.outputs})

        else:
            self.model = model
            del history
            del model
예제 #22
0
def layer_test(layer_cls,
               kwargs={},
               input_shape=None,
               input_dtype=None,
               input_data=None,
               expected_output=None,
               expected_output_dtype=None,
               fixed_batch_size=False,
               supports_masking=False):
    # generate input data

    if input_data is None:

        if not input_shape:
            raise AssertionError()

        if not input_dtype:

            input_dtype = K.floatx()

        input_data_shape = list(input_shape)

        for i, e in enumerate(input_data_shape):

            if e is None:

                input_data_shape[i] = np.random.randint(1, 4)
        input_mask = []
        if all(isinstance(e, tuple) for e in input_data_shape):
            input_data = []

            for e in input_data_shape:
                input_data.append(
                    (10 * np.random.random(e)).astype(input_dtype))
                if supports_masking:
                    a = np.full(e[:2], False)
                    a[:, :e[1] // 2] = True
                    input_mask.append(a)

        else:

            input_data = (10 * np.random.random(input_data_shape))

            input_data = input_data.astype(input_dtype)
            if supports_masking:
                a = np.full(input_data_shape[:2], False)
                a[:, :input_data_shape[1] // 2] = True

                print(a)
                print(a.shape)
                input_mask.append(a)

    else:

        if input_shape is None:

            input_shape = input_data.shape

        if input_dtype is None:

            input_dtype = input_data.dtype

    if expected_output_dtype is None:

        expected_output_dtype = input_dtype

    # instantiation

    layer = layer_cls(**kwargs)

    # test get_weights , set_weights at layer level

    weights = layer.get_weights()

    layer.set_weights(weights)

    try:
        expected_output_shape = layer.compute_output_shape(input_shape)
    except Exception:
        expected_output_shape = layer._compute_output_shape(input_shape)

    # test in functional API
    if isinstance(input_shape, list):
        if fixed_batch_size:

            x = [Input(batch_shape=e, dtype=input_dtype) for e in input_shape]
            if supports_masking:
                mask = [
                    Input(batch_shape=e[0:2], dtype=bool) for e in input_shape
                ]

        else:

            x = [Input(shape=e[1:], dtype=input_dtype) for e in input_shape]
            if supports_masking:
                mask = [Input(shape=(e[1], ), dtype=bool) for e in input_shape]

    else:
        if fixed_batch_size:

            x = Input(batch_shape=input_shape, dtype=input_dtype)
            if supports_masking:
                mask = Input(batch_shape=input_shape[0:2], dtype=bool)

        else:

            x = Input(shape=input_shape[1:], dtype=input_dtype)
            if supports_masking:
                mask = Input(shape=(input_shape[1], ), dtype=bool)

    if supports_masking:

        y = layer(Masking()(x), mask=mask)
    else:
        y = layer(x)

    if not (K.dtype(y) == expected_output_dtype):
        raise AssertionError()

    # check with the functional API
    if supports_masking:
        model = Model([x, mask], y)

        actual_output = model.predict([input_data, input_mask[0]])
    else:
        model = Model(x, y)

        actual_output = model.predict(input_data)

    actual_output_shape = actual_output.shape
    for expected_dim, actual_dim in zip(expected_output_shape,
                                        actual_output_shape):

        if expected_dim is not None:

            if not (expected_dim == actual_dim):
                raise AssertionError("expected_shape", expected_output_shape,
                                     "actual_shape", actual_output_shape)

    if expected_output is not None:

        assert_allclose(actual_output, expected_output, rtol=1e-3)

    # test serialization, weight setting at model level

    model_config = model.get_config()

    recovered_model = model.__class__.from_config(model_config)

    if model.weights:

        weights = model.get_weights()

        recovered_model.set_weights(weights)

        _output = recovered_model.predict(input_data)

        assert_allclose(_output, actual_output, rtol=1e-3)

    # test training mode (e.g. useful when the layer has a

    # different behavior at training and testing time).

    if has_arg(layer.call, 'training'):

        model.compile('rmsprop', 'mse')

        model.train_on_batch(input_data, actual_output)

    # test instantiation from layer config

    layer_config = layer.get_config()

    layer_config['batch_input_shape'] = input_shape

    layer = layer.__class__.from_config(layer_config)

    # for further checks in the caller function

    return actual_output
예제 #23
0
    def get_model(self, num_keys):

        #
        encoder_input = Input(shape=(None, ), name='encoder_input')
        embedding_size = 128

        encoder_embedding = Embedding(input_dim=num_words,
                                      output_dim=embedding_size,
                                      name='encoder_embedding')

        state_size = 512

        encoder_gru1 = GRU(state_size, name='encoder_gru1',
                           return_sequences=True)
        encoder_gru2 = GRU(state_size, name='encoder_gru2',
                           return_sequences=True)
        encoder_gru3 = GRU(state_size, name='encoder_gru3',
                           return_sequences=False)

        encoder_dense_1 = Dense(state_size,
                              activation='relu',
                              name='encoded_output_1')

        encoder_dense_2 = Dense(state_size,
                              activation='relu',
                              name='encoded_output_2')

        encoder_embedding = Embedding(input_dim=state_size,
                                      output_dim=embedding_size,
                                      name='encoded_embedding')

        encoder_dense_out = Dense(num_keys,
                              activation='linear',
                              name='encoded_output_3')


        def connect_encoder():
            # Start the neural network with its input-layer.
            net = encoder_input

            # Connect the embedding-layer.
            net = encoder_embedding(net)

            # Connect all the GRU-layers.
            net = encoder_gru1(net)
            net = encoder_gru2(net)
            net = encoder_gru3(net)

            net = encoder_dense_1(net)
            net = encoder_dense_2(net)

            # This is the output of the encoder.
            emdedding = encoder_embedding(net)

            encoder_output = encoder_dense_out(net)

            return encoder_output, emdedding

        encoder_output, emdedding = connect_encoder()


        f_model_train = Model(inputs=[encoder_input],
                                outputs=[encoder_output])

        f_model_encoder = Model(inputs=[encoder_input],
                                    outputs=[emdedding])

        optimizer = RMSprop(lr=1e-3)

        decoder_target = tf.placeholder(dtype='int32', shape=(None, None))

        f_model_train.compile(optimizer=optimizer,
                            loss=self.sparse_cross_entropy,
                            target_tensors=[decoder_target])


        return f_model_train, f_model_encoder
예제 #24
0
class Pix2Pix():
    def __init__(self):
        # Input shape
        self.img_rows = 256
        self.img_cols = 256
        self.channels = 3
        self.img_shape = (self.img_rows, self.img_cols, self.channels)

        # Configure data loader
        self.dataset_name = 'facades'
        self.data_loader = DataLoader(dataset_name=self.dataset_name,
                                      img_res=(self.img_rows, self.img_cols))

        # Calculate output shape of D (PatchGAN)
        patch = int(self.img_rows / 2**4)
        self.disc_patch = (patch, patch, 1)

        # Number of filters in the first layer of G and D
        self.gf = 64
        self.df = 64

        optimizer = Adam(0.0002, 0.5)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='mse',
                                   optimizer=optimizer,
                                   metrics=['accuracy'])

        #-------------------------
        # Construct Computational
        #   Graph of Generator
        #-------------------------

        # Build the generator
        self.generator = self.build_generator()

        # Input images and their conditioning images
        img_A = Input(shape=self.img_shape)
        img_B = Input(shape=self.img_shape)

        # By conditioning on B generate a fake version of A
        fake_A = self.generator(img_B)

        # For the combined model we will only train the generator
        #self.discriminator.trainable = False

        # Discriminators determines validity of translated images / condition pairs
        valid = self.discriminator([fake_A, img_B])

        self.combined = Model(inputs=[img_A, img_B], outputs=[valid, fake_A])
        self.combined.compile(loss=['mse', smoothL1],
                              loss_weights=[2, 100],
                              optimizer=optimizer)

        ################# Perceptual loss and L1 loss ######################
        self.vggmodel = VGG19(
            weights="vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5",
            include_top=False)
        #print(vggmodel.get_layer('block4_pool'))
        #print(self.combined.output[1])
        #print(vggmodel.get_layer('block4_pool').output)
        #lossOut = vggmodel(inputs=self.combined.output[1], output = vggmodel.get_layer('block4_pool').output)
        lossOut = self.vggmodel(inputs=self.combined.output[1])

        self.vggmodel.trainable = False
        for l in self.vggmodel.layers:
            l.trainable = False

        self.vgg_combined = Model(inputs=self.combined.input, outputs=lossOut)
        self.vgg_combined.compile(loss='mse',
                                  optimizer='adam',
                                  loss_weights=[10])

        valid.trainable = False

    def build_generator(self):

        layer_per_block = [4, 4, 4, 4, 4, 15, 4, 4, 4, 4, 4]

        tiramisu = Tiramisu(layer_per_block)
        tiramisu.summary()

        #d0 = Input(shape=self.img_shape)

        return tiramisu

    def build_discriminator(self):
        def d_layer(layer_input, filters, f_size=4, bn=True):
            """Discriminator layer"""
            d = Conv2D(filters, kernel_size=f_size, strides=2,
                       padding='same')(layer_input)
            d = LeakyReLU(alpha=0.2)(d)
            if bn:
                d = BatchNormalization(momentum=0.8)(d)
            return d

        img_A = Input(shape=self.img_shape)
        img_B = Input(shape=self.img_shape)

        # Concatenate image and conditioning image by channels to produce input
        combined_imgs = Concatenate(axis=-1)([img_A, img_B])

        d1 = d_layer(combined_imgs, self.df, bn=False)
        d2 = d_layer(d1, self.df * 2)
        d3 = d_layer(d2, self.df * 4)
        d4 = d_layer(d3, self.df * 8)

        validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(d4)

        return Model([img_A, img_B], validity)

    def train(self, epochs, batch_size=1, sample_interval=50):

        start_time = datetime.datetime.now()

        # Adversarial loss ground truths
        valid = np.ones((batch_size, ) + self.disc_patch)
        fake = np.zeros((batch_size, ) + self.disc_patch)

        for epoch in range(epochs):
            for batch_i, (imgs_A, imgs_B) in enumerate(
                    self.data_loader.load_batch(batch_size)):

                # ---------------------
                #  Train Discriminator
                # ---------------------

                # Condition on B and generate a translated version
                fake_A = self.generator.predict(imgs_B)

                # Train the discriminators (original images = real / generated = Fake)
                d_loss_real = self.discriminator.train_on_batch(
                    [imgs_A, imgs_B], valid)
                d_loss_fake = self.discriminator.train_on_batch(
                    [fake_A, imgs_B], fake)
                d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

                # -----------------
                #  Train Generator
                # -----------------

                # Train the generators
                g_loss = self.combined.train_on_batch([imgs_A, imgs_B],
                                                      [valid, imgs_A])

                full_vgg = self.vggmodel.predict(imgs_A)

                vgg_loss = self.vgg_combined.train_on_batch([imgs_A, imgs_B],
                                                            full_vgg)

                elapsed_time = datetime.datetime.now() - start_time
                # Plot the progress
                print(
                    "[Epoch %d/%d] [Batch %d/%d] [D loss: %f, acc: %3d%%] [G loss: %f] time: %s"
                    % (epoch, epochs, batch_i, self.data_loader.n_batches,
                       d_loss[0], 100 * d_loss[1], g_loss[0], elapsed_time))

                # If at save interval => save generated image samples
                if batch_i % sample_interval == 0:
                    self.sample_images(epoch, batch_i)

            self.combined.save_weights("Weights/" + str(epoch) + ".h5")

    def img_to_frame(self, imgA, imgB, fakeA):
        no_images = imgA.shape[0]
        img_height = imgA.shape[1]
        img_width = imgA.shape[2]
        pad = 20
        title_pad = 20
        pad_top = pad + title_pad
        frame = np.zeros((no_images * (img_height + pad_top),
                          no_images * (img_width + pad), 3))
        count = 0
        gen_imgs = np.concatenate([imgB, fakeA, imgA])
        gen_imgs = 0.5 * gen_imgs + 0.5
        titles = ['Condition', 'Generated', 'Original']
        for r in range(no_images):
            for c in range(no_images):
                im = gen_imgs[count]
                count = count + 1
                y0 = r * (img_height + pad_top) + pad // 2
                x0 = c * (img_width + pad) + pad // 2
                # print(frame[y0:y0+img_height,x0:x0+img_width,:].shape)
                frame[y0:y0 + img_height, x0:x0 + img_width, :] = im * 255
                frame = cv2.putText(frame, titles[r],
                                    (x0, y0 - title_pad // 4),
                                    cv2.FONT_HERSHEY_COMPLEX, .5,
                                    (255, 255, 255))
        return frame

    def sample_images(self, epoch, batch_i):
        os.makedirs('images/%s' % self.dataset_name, exist_ok=True)
        os.makedirs('images/dehazed', exist_ok=True)
        os.makedirs('images/haze', exist_ok=True)
        os.makedirs('images/original', exist_ok=True)
        r, c = 3, 3

        imgs_A, imgs_B, or_A, or_B = self.data_loader.load_data(
            batch_size=3, is_testing=True)

        fake_A = self.generator.predict(imgs_B)

        cv2.imwrite(
            "images/dehazed" + "/" + "Img:" + str(epoch) + "_" + str(batch_i) +
            ".jpg", (fake_A[0] * 0.5 + 0.5) * 255)
        cv2.imwrite(
            "images/haze" + "/" + "Img:" + str(epoch) + "_" + str(batch_i) +
            ".jpg", (or_B[0] * 0.5 + 0.5) * 255)
        cv2.imwrite(
            "images/original" + "/" + "Img:" + str(epoch) + "_" +
            str(batch_i) + ".jpg", (or_A[0] * 0.5 + 0.5) * 255)

        frame = self.img_to_frame(imgs_A, imgs_B, fake_A)

        cv2.imwrite(
            "images/" + self.dataset_name + "/" + "Img:" + str(epoch) + "_" +
            str(batch_i) + ".png", frame)
                                                      color_mode="rgb",
                                                      batch_size=32,
                                                      class_mode="categorical",
                                                      shuffle=False,
                                                      seed=42)

    if mode == 'training':

        from tensorflow.python.keras.optimizers import Adam
        from tensorflow.python.keras.losses import categorical_crossentropy

        adam = Adam(lr=0.0)
        metrics = ['accuracy']

        model.compile(optimizer=adam,
                      metrics=metrics,
                      loss=categorical_crossentropy)

        model.fit_generator(
            train_generator,
            validation_data=test_generator,
            epochs=100,
            use_multiprocessing=False,
            workers=10,
            callbacks=[
                ModelCheckpoint(
                    '%s/model_{epoch:d}_loss{loss:.4f}_acc_{acc:.4f}.h5' %
                    folder,
                    monitor='val_loss',
                    verbose=1,
                    save_best_only=False,
예제 #26
0
          kernel_regularizer=regularizers.l2(0.01))(x)
x = Dropout(0.4)(x)
x = Dense(units=256,
          activation="relu",
          kernel_regularizer=regularizers.l2(0.01))(x)
x = Dropout(0.4)(x)
x = Flatten()(x)
output = Dense(units=4, activation="softmax")(x)

model = Model(inputs=model.input, outputs=output)

# summarize
model.summary()

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])  #optimizer=adam

image_size = 224
data_generator = ImageDataGenerator(
    preprocessing_function=
    preprocess_input,  #Generate batches of tensor image data with real-time data augmentation.
    rescale=1. /
    255,  #we multiply the data by the value provided (after applying all other transformations)
    zoom_range=0.2
)  # Float...Range for random zoom...If a float, [lower, upper] = [1-zoom_range, 1+zoom_range]

train_generator = data_generator.flow_from_directory(
    "D:/PYCodes/Arrow224/train",
    target_size=(image_size, image_size),
    batch_size=32,  #16
예제 #27
0
# model.add(tf.keras.layers.Dense(64, activation='relu'))
# model.add(tf.keras.layers.Dense(64, activation='relu'))
# model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

# model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])

# model.fit(X, Y, batch_size=1, nb_epoch=100, verbose=0)

inputs = Input(shape=INPUT_SHAPE)
x = extractor(inputs)
# x = extractor_new(inputs)
center_output = Dense(units=1, activation='sigmoid', name='center')(x)

model = Model(inputs=inputs, outputs=[center_output], name='CenterNet')
optimizer = Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=optimizer)
model.summary()

checkpoint = ModelCheckpoint('models/' + model_name + '.h5',
                             monitor='loss',
                             verbose=1,
                             save_best_only=True,
                             mode='min')

early_stopping = EarlyStopping(monitor='loss',
                               mode='min',
                               verbose=1,
                               patience=50)

history = model.fit_generator(generator(X, Y, batch_size),
                              epochs=epochs,
예제 #28
0
                     dropout=dropout_rate))(temporal_input_layer)

            # add a dense layer
            ts_output = Dense(32, activation="relu")(lstm_1)
            # temporal part ends here

            # and a softmax layer -- num_classes
            predictions = Dense(num_classes, activation='softmax')(ts_output)

            optimizer = Adam(lr=learning_rate)

            # this is the model we will train
            model = Model(inputs=temporal_input_layer, outputs=predictions)

            model.compile(loss='categorical_crossentropy',
                          optimizer=optimizer,
                          metrics=['accuracy'])

            print(model.summary())

            ## NN ends here

            # callbacks
            # tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
            model_name = str(config[parser_args.task]['MODEL_FOLDER']
                             ) + '_temporal' + curr_time + '.h5'
            model_checkpoint = ModelCheckpoint(model_name,
                                               verbose=1,
                                               monitor='val_loss',
                                               save_best_only=True,
                                               mode='auto')
input_img = Input(shape=(784, ))
encoding_dim = 32

encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)

autoencoder = Model(inputs=input_img, outputs=encoded)

encoder = Model(inputs=input_img, outputs=encoded)
encoded_input = Input(shape=(encoding_dim, ))
decoder_layer = autoencoder.layers[-1]

decoder = Model(inputs=encoded_input, outputs=decoder_layer(encoded_input))

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder.fit(x_train,
                x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))

encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)

n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
    ax = plt.subplot(2, n, i + 1)
예제 #30
0
    def __init__(self):
        self.HOPS = 7
        self.SCORE_FUNCTION = 'mlp'  # scaled_dot_product / mlp (concat) / bi_linear (general dot)
        self.DATASET = 'twitter'  # 'twitter', 'restaurant', 'laptop'
        self.POLARITIES_DIM = 3
        self.EMBEDDING_DIM = 300
        self.LEARNING_RATE = 0.01
        self.INITIALIZER = initializers.RandomUniform(minval=-0.003,
                                                      maxval=0.003)
        self.REGULARIZER = regularizers.l2(0.001)
        self.MAX_SEQUENCE_LENGTH = 80
        self.MAX_ASPECT_LENGTH = 10
        self.BATCH_SIZE = 200
        self.EPOCHS = 100

        self.texts_raw_indices, self.texts_raw_without_aspects_indices, self.texts_left_indices, self.texts_left_with_aspects_indices, \
        self.aspects_indices, self.texts_right_indices, self.texts_right_with_aspects_indices, \
        self.polarities_matrix, \
        self.embedding_matrix, \
        self.tokenizer = \
            read_dataset(type=self.DATASET,
                         mode='train',
                         embedding_dim=self.EMBEDDING_DIM,
                         max_seq_len=self.MAX_SEQUENCE_LENGTH, max_aspect_len=self.MAX_ASPECT_LENGTH)

        if os.path.exists('dmn_saved_model.h5'):
            print('loading saved model...')
            self.model = load_model('dmn_saved_model.h5')
        else:
            print('Build model...')
            inputs_sentence = Input(shape=(self.MAX_SEQUENCE_LENGTH, ),
                                    name='inputs_sentence')
            inputs_aspect = Input(shape=(self.MAX_ASPECT_LENGTH, ),
                                  name='inputs_aspect')
            nonzero_count = Lambda(lambda xin: tf.count_nonzero(
                xin, dtype=tf.float32))(inputs_aspect)
            memory = Embedding(input_dim=len(self.tokenizer.word_index) + 1,
                               output_dim=self.EMBEDDING_DIM,
                               input_length=self.MAX_SEQUENCE_LENGTH,
                               mask_zero=True,
                               weights=[self.embedding_matrix],
                               trainable=False,
                               name='sentence_embedding')(inputs_sentence)
            memory = Lambda(self.locationed_memory,
                            name='locationed_memory')(memory)
            aspect = Embedding(input_dim=len(self.tokenizer.word_index) + 1,
                               output_dim=self.EMBEDDING_DIM,
                               input_length=self.MAX_ASPECT_LENGTH,
                               mask_zero=True,
                               weights=[self.embedding_matrix],
                               trainable=False,
                               name='aspect_embedding')(inputs_aspect)
            x = Lambda(lambda xin: K.sum(xin[0], axis=1) / xin[1],
                       name='aspect_mean')([aspect, nonzero_count])
            shared_attention = Attention(score_function=self.SCORE_FUNCTION,
                                         initializer=self.INITIALIZER,
                                         regularizer=self.REGULARIZER,
                                         name='shared_attention')
            for i in range(self.HOPS):
                x = shared_attention((memory, x))
            x = Flatten()(x)
            x = Dense(self.POLARITIES_DIM)(x)
            predictions = Activation('softmax')(x)
            model = Model(inputs=[inputs_sentence, inputs_aspect],
                          outputs=predictions)
            model.summary()
            model.compile(loss='categorical_crossentropy',
                          optimizer=optimizers.Adam(lr=self.LEARNING_RATE),
                          metrics=['acc', f1])
            # plot_model(model, to_file='model.png')
            self.model = model
예제 #31
0
def layer_test(layer_cls, kwargs={}, input_shape=None, input_dtype=None,

               input_data=None, expected_output=None,

               expected_output_dtype=None, fixed_batch_size=False):
    # generate input data

    if input_data is None:

        if not input_shape:
            raise AssertionError()

        if not input_dtype:

            input_dtype = K.floatx()

        input_data_shape = list(input_shape)

        for i, e in enumerate(input_data_shape):

            if e is None:

                input_data_shape[i] = np.random.randint(1, 4)

        if all(isinstance(e, tuple) for e in input_data_shape):
            input_data = []
            for e in input_data_shape:
                input_data.append(
                    (10 * np.random.random(e)).astype(input_dtype))

        else:

            input_data = (10 * np.random.random(input_data_shape))

            input_data = input_data.astype(input_dtype)

    else:

        if input_shape is None:

            input_shape = input_data.shape

        if input_dtype is None:

            input_dtype = input_data.dtype

    if expected_output_dtype is None:

        expected_output_dtype = input_dtype

    # instantiation

    layer = layer_cls(**kwargs)

    # test get_weights , set_weights at layer level

    weights = layer.get_weights()

    layer.set_weights(weights)

    try:
        expected_output_shape = layer.compute_output_shape(input_shape)
    except Exception:
        expected_output_shape = layer._compute_output_shape(input_shape)

    # test in functional API
    if isinstance(input_shape, list):
        if fixed_batch_size:

            x = [Input(batch_shape=e, dtype=input_dtype) for e in input_shape]

        else:

            x = [Input(shape=e[1:], dtype=input_dtype) for e in input_shape]
    else:
        if fixed_batch_size:

            x = Input(batch_shape=input_shape, dtype=input_dtype)

        else:

            x = Input(shape=input_shape[1:], dtype=input_dtype)

    y = layer(x)

    if not (K.dtype(y) == expected_output_dtype):
        raise AssertionError()

    # check with the functional API

    model = Model(x, y)

    actual_output = model.predict(input_data)

    actual_output_shape = actual_output.shape

    for expected_dim, actual_dim in zip(expected_output_shape,

                                        actual_output_shape):

        if expected_dim is not None:

            if not (expected_dim == actual_dim):
                raise AssertionError()

    if expected_output is not None:

        assert_allclose(actual_output, expected_output, rtol=1e-3)

    # test serialization, weight setting at model level

    model_config = model.get_config()

    recovered_model = model.__class__.from_config(model_config)

    if model.weights:

        weights = model.get_weights()

        recovered_model.set_weights(weights)

        _output = recovered_model.predict(input_data)

        assert_allclose(_output, actual_output, rtol=1e-3)

    # test training mode (e.g. useful when the layer has a

    # different behavior at training and testing time).

    if has_arg(layer.call, 'training'):

        model.compile('rmsprop', 'mse')

        model.train_on_batch(input_data, actual_output)

    # test instantiation from layer config

    layer_config = layer.get_config()

    layer_config['batch_input_shape'] = input_shape

    layer = layer.__class__.from_config(layer_config)

    # for further checks in the caller function

    return actual_output
예제 #32
0
def train_top_model_densenet121():



    # DesneNet generators

    # Set up generators
    train_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        train_path,
        target_size=(image_size, image_size),
        batch_size=train_batch_size)

    valid_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        valid_path,
        target_size=(image_size, image_size),
        batch_size=val_batch_size)

    test_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        test_path,
        target_size=(image_size, image_size),
        batch_size=test_batch_size,
        shuffle=False)

    input_tensor = Input(shape = (224,224,3))

    #Loading the model
    model = DenseNet121(input_tensor= input_tensor,weights='imagenet',include_top=False)

    # add a global spatial average pooling layer
    x = model.output
    x = GlobalAveragePooling2D()(x)
    # add relu layer
    x = Dense(1024, activation='relu')(x)
    # and a softmax layer for 7 classes
    predictions = Dense(7, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=model.input, outputs=predictions)


    def top_3_accuracy(y_true, y_pred):
        return top_k_categorical_accuracy(y_true, y_pred, k=3)

    def top_2_accuracy(y_true, y_pred):
        return top_k_categorical_accuracy(y_true, y_pred, k=2)


    model.compile(optimizer=optimizers.SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False),
                          loss="categorical_crossentropy",
                          metrics=[categorical_accuracy, top_2_accuracy, top_3_accuracy])

    print(model.summary())

    # Declare a checkpoint to save the best version of the model
    checkpoint = ModelCheckpoint("modelDenseNet121.h5", monitor='val_categorical_accuracy', verbose=1,
                                 save_best_only=True, mode='max')

    # Reduce the learning rate as the learning stagnates
    reduce_lr = ReduceLROnPlateau(monitor='val_categorical_accuracy', factor=0.5, patience=2,
                                  verbose=1, mode='max', min_lr=0.00001)

    early_stopping = EarlyStopping(monitor='val_categorical_accuracy', patience=10, verbose=1, mode='max')

    callbacks_list = [checkpoint, reduce_lr,
                      early_stopping
                      ]

    history = model.fit_generator(train_batches,
                                  # class_weight = class_weights,
                                  epochs=epochs, shuffle=True, validation_data = valid_batches, steps_per_epoch=train_steps, validation_steps = val_steps,  verbose=1, callbacks=callbacks_list)


    # # Evaluation of the best epoch
    model.load_weights('modelDenseNet.h5')

    val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \
        model.evaluate_generator(valid_batches, steps=val_steps)

    print('val_loss:', val_loss)
    print('val_cat_acc:', val_cat_acc)
    print('val_top_2_acc:', val_top_2_acc)
    print('val_top_3_acc:', val_top_3_acc)