示例#1
0
 def _build_vae(self):
     ''' builds variational autoencoder network '''
     self.kl_anneal = Input(batch_shape=(self.batch_size, ),
                            name='kl_anneal')
     # build VAE
     if np.all(np.array([self.alpha, self.beta, self.lamb]) == 0):
         self.x_output = self.decoder(self.encoder(self.enc_x_input))
         self.vae = Model(inputs=[self.enc_x_input],
                          outputs=[self.x_output],
                          name='variational_autoencoder')
     elif self.alpha == self.beta == self.lamb:
         self.x_output = self.decoder(self.encoder(self.enc_x_input)[2])
         self.vae = Model(inputs=[self.enc_x_input, self.kl_anneal],
                          outputs=[self.x_output],
                          name='variational_autoencoder')
         tc_loss = self.kl_anneal * self.kullback_leibler_divergence_loss()
         self.vae.add_loss(tc_loss)
         self.vae.add_metric(tc_loss, name='tc_loss', aggregation='mean')
     elif np.any(np.array([self.alpha, self.beta, self.lamb]) > 0):
         self.x_output = self.decoder(self.encoder(self.enc_x_input)[2])
         self.vae = Model(inputs=[self.enc_x_input, self.kl_anneal],
                          outputs=[self.x_output],
                          name='variational_autoencoder')
         tc_loss = self.kl_anneal * self.total_correlation_loss()
         self.vae.add_loss(tc_loss)
         self.vae.add_metric(tc_loss, name='tc_loss', aggregation='mean')
     # define VAE optimizer
     if self.vae_opt_n == 'sgd':
         self.vae_opt = SGD(learning_rate=self.lr)
     elif self.vae_opt_n == 'sgdm':
         self.vae_opt = SGD(learning_rate=self.lr, momentum=0.5)
     elif self.vae_opt_n == 'nsgd':
         self.vae_opt = SGD(learning_rate=self.lr,
                            momentum=0.5,
                            nesterov=True)
     elif self.vae_opt_n == 'rmsprop':
         self.vae_opt = RMSprop(learning_rate=self.lr)
     elif self.vae_opt_n == 'rmsprop_cent':
         self.vae_opt = RMSprop(learning_rate=self.lr, centered=True)
     elif self.vae_opt_n == 'adam':
         self.vae_opt = Adam(learning_rate=self.lr, beta_1=0.5)
     elif self.vae_opt_n == 'adam_ams':
         self.vae_opt = Adam(learning_rate=self.lr,
                             beta_1=0.5,
                             amsgrad=True)
     elif self.vae_opt_n == 'adamax':
         self.vae_opt = Adamax(learning_rate=self.lr, beta_1=0.5)
     elif self.vae_opt_n == 'adamax_ams':
         self.vae_opt = Adamax(learning_rate=self.lr,
                               beta_1=0.5,
                               amsgrad=True)
     elif self.vae_opt_n == 'nadam':
         self.vae_opt = Nadam(learning_rate=self.lr, beta_1=0.5)
     # compile VAE
     rc_loss = self.reconstruction_loss()
     self.vae.add_loss(rc_loss)
     self.vae.add_metric(rc_loss, name='rc_loss', aggregation='mean')
     self.vae.compile(optimizer=self.vae_opt)
示例#2
0
    def __init__(
            self,
            input_shape,
            number_of_classes,
            filtres=16,
            tailleBlock={
                'A': 10,
                'B': 3,
                'C': 3
            },
            optimiseur='Nadam',
            activation='elu',
            beta=1.1,
            initializer='he_normal',
            metrics=['accuracy'],
            learningR=None,  #0.0005,
            nb_gpu=2):

        get_custom_objects()['swish'] = swish
        get_custom_objects()['e_swish'] = e_swish

        self.input_shape = input_shape
        self.number_of_classes = number_of_classes
        self.filtres = filtres
        self.tailleBlock = tailleBlock

        #if learningR is not None :
        self.optimiseur = optimiseur
        if learningR is not None:
            self.optimiseur = {
                'SGD': SGD(learning_rate=learningR),
                'RMSprop': RMSprop(learning_rate=learningR),
                'Adagrad': Adagrad(learning_rate=learningR),
                'Adadelta': Adadelta(learning_rate=learningR),
                'Adam': Adam(learning_rate=learningR),
                'Adamax': Adamax(learning_rate=learningR),
                'Nadam': Nadam(learning_rate=learningR),
            }[optimiseur]
        else:
            self.optimiseur = {
                'SGD': SGD(),
                'RMSprop': RMSprop(),
                'Adagrad': Adagrad(),
                'Adadelta': Adadelta(),
                'Adam': Adam(),
                'Adamax': Adamax(),
                'Nadam': Nadam(),
            }[optimiseur]

        self.activation = activation
        self.initializer = initializer
        self.nb_gpu = nb_gpu
        self.metrics = metrics

        # la valeur 3 indique que les canaux des couleurs sont à la fin
        # autrement -1 (je n'utilise pas cette syntaxe )
        self.channel_axis = 3
    def __init__(self, config):
        self.max_iterations = config['PhotometricOptimizer']['max_iterations']
        self.termination_crit = config['PhotometricOptimizer'][
            'termination_crit']
        self.image_height = config['dataset']['image_height']
        self.image_width = config['dataset']['image_width']
        self.g = Graphics3()
        self.optimizer = Adamax(lr=1e-3)
        self.timer = Timer(config)

        self.angle_th = np.cos(
            (config['PhotometricOptimizer']['angle_th'] / 180.0) * np.pi)

        self.angle_th = tf.constant(self.angle_th, dtype=tf.float32)
示例#4
0
    def init_model(self):

        self.model = Sequential()
        self.model.add(
            Dense(self.hidden_units,
                  input_dim=self.input_units,
                  activation=self.activation))
        self.model.add(
            Dropout(self.dropout, noise_shape=self.noise_shape,
                    seed=self.seed))
        self.model.add(
            Dense(self.output_units, activation=self.activation_last))

        if self.optimizer == 'RMSprop':
            opt = RMSprop(learning_rate=self.learning_rate)
        elif self.optimizer == 'Adadelta':
            opt = kAdadelta(learning_rate=self.learning_rate)
        elif self.optimizer == 'SGD':
            opt = SGD(learning_rate=self.learning_rate)
        elif self.optimizer == 'Adagrad':
            opt = Adagrad(learning_rate=self.learning_rate)
        elif self.optimizer == 'Adamax':
            opt = Adamax(learning_rate=self.learning_rate)
        elif self.optimizer == 'Nadam':
            opt = Nadam(learning_rate=self.learning_rate)
        else:
            opt = Adam(learning_rate=self.learning_rate)

        self.model.compile(optimizer=opt,
                           loss=self.loss,
                           metrics=[self.metrics])
示例#5
0
def build_top_nn(input_shape, summary=False):
    """" Return the custom fully connected classifier """

    w = TruncatedNormal(mean=0.0, stddev=0.0001, seed=None)
    opt = Adamax(learning_rate=0.0001,
                 beta_1=0.9,
                 beta_2=0.999,
                 epsilon=None,
                 decay=0.0)

    model_top = Sequential()
    model_top.add(Flatten(input_shape=input_shape))
    model_top.add(Dense(16, kernel_initializer=w, bias_initializer='zeros'))
    model_top.add(Activation('relu'))
    model_top.add(Dropout(0.5))
    model_top.add(Dense(1, kernel_initializer=w, bias_initializer='zeros'))
    model_top.add(Activation('sigmoid'))

    if summary:
        print("Top classifier:")
        model_top.summary()

    model_top.compile(optimizer=opt,
                      loss='binary_crossentropy',
                      metrics=['accuracy'])

    return model_top
示例#6
0
 def __init__(self,
              img_wt,
              img_ht,
              flow: Flow = None,
              hidden_units=32,
              z_size=64,
              encoder_strides=[2, 2],
              decoder_strides=[2, 2],
              callbacks=[],
              metrics=[],
              output_activation='sigmoid',
              loss='binary_crossentropy',
              beta_update_fn=None):
     super(GatedConvVAE, self).__init__()
     if beta_update_fn is None:
         beta_update_fn = lambda i, beta: 1.0E-2 * i
     self.flow = flow
     self.hidden_units = hidden_units
     self.z_size = z_size
     self.num_downsamples = len(encoder_strides)
     self.num_upsamples = len(decoder_strides)
     self.encoder_strides = encoder_strides
     self.decoder_strides = decoder_strides
     self.output_activation = output_activation
     self.encoder = self._create_encoder(img_wt, img_ht)
     self.decoder, self.flow_layer = self._create_decoder(img_wt, img_ht)
     beta_update = LambdaCallback(on_epoch_begin=lambda i, _:
                                  beta_update_fn(i, self.flow_layer.beta))
     decoder_output = self.decoder(self.encoder(self.encoder.inputs))
     self.model = Model(inputs=self.encoder.inputs,
                        outputs=decoder_output[0])
     self.model.compile(loss=loss,
                        optimizer=Adamax(learning_rate=1.0E-4, clipnorm=1.),
                        callbacks=[beta_update] + callbacks,
                        metrics=metrics)
示例#7
0
    def create_model(self):
        def softmax_ce_logits(y_true, y_pred):
            return tf.compat.v1.nn.softmax_cross_entropy_with_logits_v2(
                y_true, y_pred)

        observation = Input(self.observation_dim, name="o_0")
        value, reward, policy_logits, hidden_state = self.initial_inference(
            observation)

        actions_all, mu_all, loss_all = [], [], []
        mu_all += [value,
                   policy_logits]  # TODO: Shouldn't we also add the reward?
        loss_all += ["mse", softmax_ce_logits]

        for k in range(self.config.num_unroll_steps):
            action = Input(self.action_dim, name=f"a_{k}")
            actions_all.append(action)

            value, reward, policy_logits, hidden_state_n = self.recurrent_inference(
                hidden_state, action)
            mu_all += [value, reward, policy_logits]
            loss_all += ["mse", "mse", softmax_ce_logits]

            hidden_state = hidden_state_n  # Passback

        model = Model([observation] + actions_all, mu_all)
        model.compile(Adamax(self.config.learning_rate), loss_all)
        return model
def build_triplet_classifier_model(extractor_model,
                                   dist_type='eucl',
                                   threshold=1.0):
    anchor_in = Input(shape=(224, 224, 3), name="anchor_in")
    anchor_out = extractor_model(anchor_in)

    compare_in = Input(shape=(224, 224, 3), name="compare_in")
    compare_out = extractor_model(compare_in)

    if dist_type == 'cos':
        dist = CosineDistance(name="dist")([anchor_out, compare_out])
    else:
        dist = EuclidianDistanceSquared(name="dist")([anchor_out, compare_out])

    model = Lambda(lambda x: tf.cast((x < threshold), tf.float32))(dist)
    model = Model([anchor_in, compare_in], model)
    model.compile(optimizer=Adamax(),
                  loss=None,
                  metrics=[
                      BinaryAccuracy(),
                      Precision(),
                      Recall(),
                      TrueNegatives(),
                      FalsePositives(),
                      FalseNegatives(),
                      TruePositives()
                  ])

    return model
示例#9
0
def OptimTypeFunc(x, OpLr, OpMom):
    # Function to determine to get desiered optimezer
    return {
        0: Nadam(lr=OpLr),
        1: Adamax(lr=OpLr),
        2: SGD(lr=OpLr, momentum=OpMom),
        3: Adam(lr=OpLr, clipnorm=1.0),
        4: Nadam(lr=OpLr)
    }[x]
示例#10
0
def build_triplet_training_model(extractor_model,
                                 dist_type='eucl',
                                 alpha=1.0,
                                 optimizer=Adamax()):
    triplet_loss = tfa.losses.TripletSemiHardLoss(
        margin=alpha,
        distance_metric='angular' if (dist_type == 'cos') else 'L2',
        name="triplet_loss")
    extractor_model.compile(optimizer=optimizer, loss=triplet_loss)
    return extractor_model
示例#11
0
def PickOptimizer(option='adam', ParamDic={}):
    """
    Build an optimizer object.

    It gets the type of optimizer needed and the parameters that the users gives.
    It adds all the other default parameters and return the object.

    Notice: SGD and adam support a learning rate decays the other don't

    :param option: string. The type of optimizer desired. Supports: adam
                                                                    Nadam
                                                                    Adamax
                                                                    SGD
    :param ParamDic: dictionary. If a parameter is in the dictionary then take that value, if not then use defaults.
    :return: A keras optimizer object as desired in the option parameter
    """

    # Defaults parameters
    DefltInitLearning = 0.001  # 0.0001
    DefltBeta1 = 0.9
    DefltBeta2 = 0.999
    DefltEpsil = 1e-07
    DefltAmsgrad = False
    DefltAMoment = 0.0
    Defltnesterov = False

    # Find parameters values
    # If the values in the dic. take the value if not then use default
    LR = ParamDic['learning_rate'] if 'learning_rate' in ParamDic else DefltInitLearning
    BT1 = ParamDic['beta_1'] if 'beta_1' in ParamDic else DefltBeta1
    BT2 = ParamDic['beta_2'] if 'beta_2' in ParamDic else DefltBeta2
    Eps = ParamDic['epsilon'] if 'epsilon' in ParamDic else DefltEpsil
    AMS = ParamDic['amsgrad'] if 'amsgrad' in ParamDic else DefltAmsgrad
    MMN = ParamDic['momentum'] if 'momentum' in ParamDic else DefltAMoment
    NES = ParamDic['nesterov'] if 'nesterov' in ParamDic else Defltnesterov

    if option == 'adam':
        opt = Adam(learning_rate=LR, beta_1=BT1, beta_2=BT2, epsilon=Eps, amsgrad=AMS, name='Adam')
    elif option == 'Nadam':  # Like adam but the momentum also get a direction
        opt = Nadam(learning_rate=LR, beta_1=BT1, beta_2=BT2, epsilon=Eps, name='Nadam')
    elif option == 'Adamax':
        # It is a variant of Adam based on the infinity norm.
        # Default parameters follow those provided in the paper.
        # Adamax is sometimes superior to adam, specially in models with embeddings.
        opt = Adamax(learning_rate=LR, beta_1=BT1, beta_2=BT2, epsilon=Eps, name='Adamax')
    elif option == 'SGD':
        opt = SGD(learning_rate=LR, momentum=MMN, nesterov=NES, name='SGD')
    else:
        print('Parameter option was not defined correctly')
        return

    return opt
示例#12
0
def define_generator(latent_dim, n_classes=3):
    print("**********  ENTERED generator  *****************")
    ##### foundation for labels
    in_label = Input(shape=(1, ))
    embedding_layer = Embedding(n_classes, 8)
    embedding_layer.trainable = True
    li = embedding_layer(in_label)
    n_nodes = 5 * 5
    li = Dense(n_nodes)(li)
    li = Reshape((5, 5, 1))(li)
    print("generator...  n_nodes, li.shape: ", n_nodes, li.shape)
    ##### foundation for 5x5 image
    in_lat = Input(shape=(latent_dim, ))
    n_nodes = 128 * 5 * 5
    genX = Dense(n_nodes)(in_lat)
    genX = LeakyReLU(alpha=0.2)(genX)
    genX = Reshape((5, 5, 128))(genX)
    dropout = 0.1
    print("genX.shape: ", genX.shape)
    ##### merge image gen and label input
    merge = Concatenate()([genX, li])
    print("merge.shape: ", merge.shape)
    ##### create merged model
    # upsample to 10x10
    gen = Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same')(merge)
    print("gen after CV2DT.shape: ", gen.shape)
    gen = LeakyReLU(alpha=0.2)(gen)
    gen = Dropout(dropout)(gen)
    print("gen.shape: ", gen.shape)
    # upsample to 20x20
    gen = Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same')(gen)
    gen = LeakyReLU(alpha=0.2)(gen)
    print("gen.shape: ", gen.shape)
    # upsample to 40x40
    gen = Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same')(gen)
    gen = LeakyReLU(alpha=0.2)(gen)
    print("gen.shape: ", gen.shape)
    # upsample to 80x80
    gen = Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same')(gen)
    gen = LeakyReLU(alpha=0.2)(gen)
    print("gen.shape: ", gen.shape)
    # output layer 80x80x3
    out_layer = Conv2D(3, (5, 5), activation='tanh', padding='same')(gen)
    print("out_layer.shape: ", out_layer.shape)
    # define model
    model = Model(inputs=[in_lat, in_label], outputs=out_layer)
    opt = Adamax(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=10e-8)
    model.compile(loss=['binary_crossentropy'], optimizer=opt)
    print("\nembedding_layer.get_weights(): \n", embedding_layer.get_weights())
    model.summary()
    plot_model(model, to_file='cgan/generator_model.png')
    return model
示例#13
0
    def load_pretrained(self, file):
        model = tf.keras.models.load_model(file, compile=False)

        def softmax_ce_logits(y_true, y_pred):
            return tf.compat.v1.nn.softmax_cross_entropy_with_logits_v2(
                y_true, y_pred)

        loss_all = ["mse", softmax_ce_logits]
        for k in range(self.config.num_unroll_steps):
            loss_all += ["mse", "mse", softmax_ce_logits]

        model.compile(Adamax(self.config.learning_rate), loss_all)

        self.model = model
示例#14
0
def build_variational(input_shape,
                      encoder,
                      layers,
                      depth,
                      min_filters=32,
                      max_filters=256,
                      lr=1.0E-4):
    flow = Invert(
        GlowFlow(num_layers=layers,
                 depth=depth,
                 coupling_nn_ctor=coupling_nn_glow(min_filters=min_filters,
                                                   max_filters=max_filters)))
    model = VariationalModel(encoder, normal(), flow)
    model.compile(optimizer=Adamax(lr=lr), output_shape=input_shape)
    return model
示例#15
0
 def test_allowed_slot_names(self):
     opt_and_slots_pairs = [
         (SGD(), []),
         (SGD(momentum=0.2), ["momentum"]),
         (Adam(), ["m", "v"]),
         (Adam(amsgrad=True), ["m", "v", "vhat"]),
         (Adamax(), ["m", "v"]),
         (Nadam(), ["m", "v"]),
         (Adadelta(), ["accum_grad", "accum_var"]),
         (Adagrad(), ["accumulator"]),
         (Ftrl(), ["accumulator", "linear"]),
         (RMSprop(), ["rms"]),
         (RMSprop(momentum=0.2), ["rms", "momentum"]),
         (RMSprop(centered=True), ["rms", "mg"]),
         (RMSprop(momentum=0.2, centered=True), ["rms", "momentum", "mg"]),
     ]
     for opt, expected_slots in opt_and_slots_pairs:
         self._compare_slot_names(opt, expected_slots)
示例#16
0
def define_gan(g_model, d_model):
    print("**********  ENTERED gan  *****************")
    # make weights in the discriminator not trainable
    d_model.trainable = False
    # get noise and label inputs from generator model
    gen_noise, gen_label = g_model.input
    # get image output from the generator model
    gen_output = g_model.output
    # connect image output and label input from generator as inputs to discriminator
    gan_output = d_model([gen_output, gen_label])
    # define gan model as taking noise and label and outputting a classification
    model = Model([gen_noise, gen_label], gan_output)
    # compile model
    opt = Adamax(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=10e-8)
    model.compile(loss='binary_crossentropy', optimizer=opt)
    model.summary()
    plot_model(model, to_file='cgan/gan_model.png')
    return model
示例#17
0
文件: network.py 项目: bluesea0/ditk
def build_multitask_bin_cat_network(nb_classes, char_inputs, char_encoded,
                                    word_inputs, word_encoded, gaze_inputs,
                                    gaze_encoded, gaze):

    if gaze:
        network = concatenate([gaze_encoded, char_encoded, word_encoded],
                              name='concat_layer')
    else:
        network = concatenate([char_encoded, word_encoded],
                              name='concat_layer')
    network = Dense(100, activation='relu', name='common_dense_layer')(network)

    bin_output = Dense(1, activation='sigmoid', name='bin_output')(network)
    cat_output = Dense(nb_classes, activation='softmax',
                       name='cat_output')(network)

    if gaze:
        network_inputs = gaze_inputs + char_inputs + word_inputs
    else:
        network_inputs = char_inputs + word_inputs

    network_outputs = [bin_output, cat_output]

    model = Model(inputs=network_inputs,
                  outputs=network_outputs,
                  name='ne_model')

    adamax = Adamax(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(optimizer=adamax,
                  loss={
                      'bin_output': 'binary_crossentropy',
                      'cat_output': 'categorical_crossentropy'
                  },
                  loss_weights={
                      'bin_output': 1.,
                      'cat_output': 1.
                  },
                  metrics={
                      'bin_output': [utils.fbeta_score, 'accuracy'],
                      'cat_output': [utils.fbeta_score, 'accuracy']
                  })
    return model
示例#18
0
def _get_optimizer(opt_type, learning_rate):
    if opt_type == 'adam':
        return Adam(learning_rate=learning_rate)
    elif opt_type == 'nadam':
        return Nadam(learning_rate=learning_rate)
    elif opt_type == 'sgd':
        return SGD(learning_rate=learning_rate)
    elif opt_type == 'adadelta':
        return Adadelta(learning_rate=learning_rate)
    elif opt_type == 'adagrad':
        return Adagrad(learning_rate=learning_rate)
    elif opt_type == 'adamax':
        return Adamax(learning_rate=learning_rate)
    elif opt_type == 'ftrl':
        return Ftrl(learning_rate=learning_rate)
    elif opt_type == 'rmsprop':
        return RMSprop(learning_rate=learning_rate)
    else:
        raise NotImplementedError('Optimizer type %s is not implemented.' %
                                  opt_type)
示例#19
0
def main(epochs=15, steps_per_epoch=20):
    model = get_basic_model(verbose=False)
    opt = Adamax()
    model.compile(loss="cosine_similarity", optimizer=opt)

    preds = []
    losses = []

    for i in range(1, 6):
        filename = ".../data/captions_" + str(i) + ".txt"
        print("Current file: " + filename)
        batch = next(transfer_generator(filename))
        print(decode_binarized_caption(batch[1][0], filename))

        H = model.fit(transfer_generator(filename),
                      epochs=epochs,
                      steps_per_epoch=steps_per_epoch)

        pred = np.round(model.predict(batch[0]))
        preds.append(pred)

        # print(pred)
        # print(batch[1])
        # for i in range(10):
        #     print(decode_binarized_caption(pred[i], filename), decode_binarized_caption(batch[1][i], filename))

        losses.append(H.history["loss"])
        print('\n')

    plot_loss(losses, epochs)
    avg_loss_by_file = []
    all_loss = []
    for loss_file in losses:
        avg_loss_by_file.append(np.mean(loss_file))
        all_loss = all_loss + loss_file

    print("Avg. Loss by Caption file")
    print(avg_loss_by_file)
    print("Avg. Loss Overall")
    print(np.mean(all_loss))
示例#20
0
def build_triplet_distances_model(extractor_model,
                                  dist_type='eucl',
                                  alpha=1.0,
                                  add_loss=False):
    anchor_in = Input(shape=(224, 224, 3), name="anchor_in")
    anchor_out = extractor_model(anchor_in)

    pos_in = Input(shape=(224, 224, 3), name="pos_in")
    pos_out = extractor_model(pos_in)

    neg_in = Input(shape=(224, 224, 3), name="neg_in")
    neg_out = extractor_model(neg_in)

    if dist_type == 'cos':
        pos_dist = CosineDistance(name="pos_dist")([anchor_out, pos_out])
        neg_dist = CosineDistance(name="neg_dist")([anchor_out, neg_out])
    else:
        pos_dist = EuclidianDistanceSquared(name="pos_dist")(
            [anchor_out, pos_out])
        neg_dist = EuclidianDistanceSquared(name="neg_dist")(
            [anchor_out, neg_out])

    triplet = TripletLoss(alpha=alpha)([pos_dist, neg_dist])
    triplet_model = Model([anchor_in, pos_in, neg_in],
                          [triplet, pos_dist, neg_dist])
    triplet_model.add_metric(pos_dist,
                             aggregation='mean',
                             name="pos_dist_mean")
    triplet_model.add_metric(neg_dist,
                             aggregation='mean',
                             name="neg_dist_mean")
    if add_loss:
        triplet_model.add_loss(triplet)
    else:
        triplet_model.add_metric(triplet,
                                 aggregation='mean',
                                 name="triplet_loss_mean")

    triplet_model.compile(optimizer=Adamax(), loss=None)
    return triplet_model
示例#21
0
def initialize_optimizer(optimizer_name: str,
                         learning_rate: float = None,
                         decay: float = None,
                         beta1: float = None,
                         beta2: float = None,
                         rho: float = None,
                         momentum: float = None,
                         clip_norm=None,
                         clip_value=None) -> OptimizerType:
    """
    Initializes an optimizer based on the user's choices.
    Refer to Keras docs for the parameters and the optimizers.
    Any parameter which is not used from the given compiler, will be ignored.

    :param optimizer_name: the name of the optimizer to be used.
    Available names: 'adam', 'rmsprop', 'sgd', 'adagrad', 'adadelta', 'adamax'
    :return: the optimizer.
    """
    if optimizer_name == 'adam':
        opt = Adam(lr=learning_rate, beta_1=beta1, beta_2=beta2, decay=decay)
    elif optimizer_name == 'rmsprop':
        opt = RMSprop(lr=learning_rate, rho=rho, decay=decay)
    elif optimizer_name == 'sgd':
        opt = SGD(lr=learning_rate, momentum=momentum, decay=decay)
    elif optimizer_name == 'adagrad':
        opt = Adagrad(lr=learning_rate, decay=decay)
    elif optimizer_name == 'adadelta':
        opt = Adadelta(lr=learning_rate, rho=rho, decay=decay)
    elif optimizer_name == 'adamax':
        opt = Adamax(lr=learning_rate, beta_1=beta1, beta_2=beta2, decay=decay)
    else:
        raise ValueError('An unexpected optimizer name has been encountered.')

    if clip_norm is not None:
        opt.clip_norm = clip_norm
    if clip_value is not None:
        opt.clip_value = clip_value
    return opt
示例#22
0
文件: config.py 项目: tuanchien/asd
def get_optimiser(config: dict):
    """
    Choose the correct optimiser.
    """

    # Read config
    optimiser = config['optimiser']
    learning_rate = config['learning_rate']

    if optimiser == 'SGD':
        return SGD(lr=learning_rate, nesterov=True, momentum=0.9, decay=1e-6)
    elif optimiser == 'Adam':
        return Adam(lr=learning_rate)
    elif optimiser == 'RMSprop':
        return RMSprop(lr=learning_rate)
    elif optimiser == 'Adagrad':
        return Adagrad(lr=learning_rate)
    elif optimiser == 'Adadelta':
        return Adadelta(lr=learning_rate)
    elif optimiser == 'Adamax':
        return Adamax(lr=learning_rate)
    elif optimiser == 'Nadam':
        return Nadam(lr=learning_rate)
def create_prm_optimizer(prm):
    if prm['optimizer'] == 'RMSprop':
        prm['optimizer_func'] = RMSprop(lr=prm['learning_rate'],
                                        decay=prm['decay'])

    if prm['optimizer'] == 'Adam':
        prm['optimizer_func'] = Adam(lr=prm['learning_rate'],
                                     decay=prm['decay'])

    if prm['optimizer'] == 'AMSgrad':
        prm['optimizer_func'] = Adam(lr=prm['learning_rate'],
                                     decay=prm['decay'],
                                     amsgrad=True)

    if prm['optimizer'] == 'Adamax':
        prm['optimizer_func'] = Adamax(lr=prm['learning_rate'],
                                       decay=prm['decay'])

    if prm['optimizer'] == 'Nadam':
        prm['optimizer_func'] = Nadam(lr=prm['learning_rate'],
                                      decay=prm['decay'])

    return (prm)
## Design network
model = Sequential()
model.add(Masking(mask_value=-9999, input_shape=(a_timesteps, a_features)))
model.add(
    LSTM(64,
         input_shape=(a_timesteps, a_features),
         dropout=0.2,
         recurrent_dropout=0.2,
         return_sequences=True))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2, return_sequences=True))
model.add(LSTM(32, dropout=0.2, recurrent_dropout=0.2))
#model.add(LSTM(50, input_shape=(a_timesteps, a_features)))
model.add(Dense(1, activation='sigmoid'))
model.compile(
    loss='binary_crossentropy',
    optimizer=Adamax(lr=0.0001),
    metrics=['accuracy'],
)

## Fit network
history = model.fit(train_X,
                    train_Y,
                    epochs=70,
                    batch_size=a_batch_size,
                    validation_data=(test_X, test_Y),
                    shuffle=False)

## Plot 예시
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.legend()
示例#25
0
def main():
    # Obtenemos dos numpy array con las emociones y las imagenes en orden
    emotion_array_np, images_array_np = load_data(CSV_FILE)

    # Triplicamos las imagenes a 3 capas, para tenerlas en formato RGB, shape(NUM,48,48) -->  shape(NUM,48,48,1)
    images_array_np = np.broadcast_to(images_array_np[..., None],
                                      images_array_np.shape + (1, ))
    # Guardamos el formato de entrada al modelo
    initial_inputs = Input(np.shape(images_array_np[0]))

    # FORMATO ENTRADA shape(NUM,48,48,1)
    # SE DIVIDE EN:
    # TRAIN
    emotion_train = emotion_array_np[0:NUM_TRAIN]
    images_train = images_array_np[0:NUM_TRAIN]

    # TEST
    emotion_test = emotion_array_np[NUM_TRAIN + 1:]
    images_test = images_array_np[NUM_TRAIN + 1:]

    # Eliminamos el csv inicial
    del (images_array_np, emotion_array_np)

    # Preprocesado de imágenes para crear nuevas imágenes
    train_datagen = ImageDataGenerator(
        # Inclina y estira la imagen
        shear_range=0.2,
        # Rotaciones aleatorias de 10 grados
        rotation_range=10,
        # Se aplica de forma aleatoria zoom en las imágenes
        zoom_range=0.2,
        # Algunas imágenes se girarán horizontalmente
        horizontal_flip=True,
        # Desplazamientos
        # Horizontal
        width_shift_range=0.1,
        # Vertical
        height_shift_range=0.1)

    # No hace ningún cambio, solo aplicará el nuevo formato con BATCH
    test_datagen = ImageDataGenerator(horizontal_flip=False, zoom_range=0)

    data_train = train_datagen.flow(images_train, emotion_train, BATCH)
    data_test = test_datagen.flow(images_test, emotion_test, BATCH)

    # Generamos estructura secuencial, (capas apiladas)
    CNN_model = Sequential()
    # Capa convolucional 1
    CNN_model.add(
        Conv2D(NUM_CONV1,
               kernel_size=TAM_FILTER,
               activation='relu',
               input_shape=np.shape(images_train[0])))

    # Capa convolucional 2
    CNN_model.add(Conv2D(NUM_CONV2, kernel_size=TAM_FILTER, activation='relu'))
    CNN_model.add(MaxPooling2D(pool_size=(2, 2)))

    # Capa convolucional 3
    CNN_model.add(Conv2D(NUM_CONV3, kernel_size=TAM_FILTER, activation='relu'))
    CNN_model.add(MaxPooling2D(pool_size=(2, 2)))

    # Capa convolucional 4
    CNN_model.add(Conv2D(NUM_CONV4, kernel_size=TAM_FILTER, activation='relu'))
    CNN_model.add(MaxPooling2D(pool_size=(2, 2)))

    # Aplanamiento de los datos
    CNN_model.add(Flatten())
    # RED NEURONAL MULTICAPA
    # Capa 1
    CNN_model.add(Dense(2048, activation='relu'))
    CNN_model.add(Dropout(0.5))
    # Capa 2
    CNN_model.add(Dense(1024, activation='relu'))
    CNN_model.add(Dropout(0.5))
    # Capa output
    CNN_model.add(Dense(NUM_CLASSES, activation='softmax'))

    CNN_model.compile(loss='categorical_crossentropy',
                      optimizer=Adamax(lr=LEARNING_RATE, decay=DECAY),
                      metrics=['accuracy'])

    # Gráficos a tiempo real
    # Se define tensorboard para seguir el entrenamiento e tiempo real
    tensorboard = tf.keras.callbacks.TensorBoard(log_dir=LOG_DIR)

    try:
        # Lanzamos los tensorboard
        process = subprocess.Popen('tensorboard --logdir=./logs',
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        # Abrimos el navegador
        os.system('start chrome http://localhost:6006/')
    except Exception:
        print("No pudo lanzarse tensorboard. Excepción: \n",
              traceback.print_exc())

    # CALLBACKS
    checkpoint_name = 'val_acc_{val_accuracy:.4f}-{epoch:02d}.hdf5'
    checkpoint_filepath = CKPT_DIR + checkpoint_name
    csv_logger = tf.keras.callbacks.CSVLogger(LOG_CSV, append=False)
    # early_stop = tf.keras.callbacks.EarlyStopping('val_loss', patience=PATIENCE)
    model_checkpoint = tf.keras.callbacks.ModelCheckpoint(
        checkpoint_filepath,
        monitor='val_accuracy',
        verbose=1,
        mode='max',
        save_best_only=True)
    callbacks = [myCallback(), tensorboard, model_checkpoint, csv_logger]

    # ENTRENAMIENTO
    model_info = CNN_model.fit(data_train,
                               batch_size=BATCH,
                               epochs=EPOCH,
                               validation_data=data_test,
                               verbose=1,
                               callbacks=callbacks)

    # EVALUACIÓN
    results = CNN_model.evaluate(images_test, emotion_test, batch_size=128)

    # GUARDADO DE LA ESTRUCTURA Y CONFIGURACIÓN EN UN .txt
    file = open(SESSION_PATH + "info_model.txt", "w")
    CNN_model.summary(print_fn=lambda x: file.write(x + '\n'))
    file.write("\n\n" + "Learning rate: " + str(LEARNING_RATE) + "\nDecay: " +
               str(DECAY))
    file.close()

    # CREACIÓN DE LA MATRIZ DE CONFUSIÓN
    emotions = {
        0: 'Enfado',
        1: 'Asco',
        2: 'Miedo',
        3: 'Felicidad',
        4: 'Tristeza',
        5: 'Sorpresa',
        6: 'Neutral'
    }
    predictions = CNN_model.predict(images_test)
    predictions = np.argmax(predictions, axis=1)
    emotions_true = np.argmax(emotion_test, axis=1)

    conf_mat = metrics.confusion_matrix(y_true=emotions_true,
                                        y_pred=predictions)
    fig, ax = plot_confusion_matrix(conf_mat=conf_mat,
                                    show_normed=True,
                                    show_absolute=False,
                                    class_names=emotions.values(),
                                    figsize=(8, 8))
    matrix_name = "MATRIZ_test_images"

    # GUARDADO DEL MODELO Y FIGURA
    accuracy = model_info.history['accuracy']
    epochs = str(len(accuracy))
    a = round(results[1], 3)
    accuracy_value = str(a)

    model_name = "model_e_" + epochs + "-acc_" + accuracy_value + ".hdf5"
    try:
        fig.savefig(SESSION_PATH + matrix_name)
        CNN_model.save(SESSION_PATH + model_name)
    except Exception:
        print("Error al guardar el modelo \n", traceback.print_exc())
Testlabels.pop(0)
Liste_Testlabels = Testlabels
Testlabels = np.asarray(Testlabels)
Testbilder = np.asarray([Testbilder])
Testbilder = Testbilder.reshape(-1, 32, 32, 3)
Testbilder = np.asarray(Testbilder, dtype="float32")
Testlabels = np.asarray(Testlabels, dtype="float32")

model = load_model('model_99,105%.hdf5')

best = 0.99105305
batch_size1 = 64
batch_size2 = 64
batch_size3 = 128
opt = Adamax(lr=0.0004)

for a in range(10):
    for i in range(150):

        model.compile(loss='sparse_categorical_crossentropy',
                      optimizer=opt,
                      metrics=['accuracy'])
        if i < 50:

            model.fit(Trainingsbilder,
                      Trainingslabels,
                      epochs=1,
                      shuffle=True,
                      batch_size=batch_size1)
    with strategy.scope():
        cmsnet = CMSNet(dl_input_shape=(None, height_crop, width_crop,
                                        channels),
                        num_classes=n_classes,
                        output_stride=output_stride,
                        pooling=pooling,
                        residual_shortcut=residual_shortcut)
        cmsnet.summary()
        #cmsnet.mySummary()

        # optimizer = SGD(momentum=0.9, nesterov=True)
        #optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-6)
        #optimizer = Adadelta(lr=0.008, rho=0.95, epsilon=None, decay=0.0)
        optimizer = Adamax(lr=0.002,
                           beta_1=0.9,
                           beta_2=0.999,
                           epsilon=None,
                           decay=0.0)
        #optimizer = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, schedule_decay=0.004)
        miou = MIoU(num_classes=n_classes)

        cmsnet.compile(loss='sparse_categorical_crossentropy',
                       optimizer=optimizer,
                       metrics=['accuracy', miou],
                       sample_weight_mode="temporal")
else:
    cmsnet = CMSNet(dl_input_shape=(None, height_crop, width_crop, channels),
                    num_classes=n_classes,
                    output_stride=output_stride,
                    pooling=pooling,
                    residual_shortcut=residual_shortcut)
                    if not os.path.isfile(os.path.join(aux_exp_dir, "metrics.csv")):
                        if os.path.isdir(aux_exp_dir):
                            shutil.rmtree(aux_exp_dir)
                        if os.path.isdir(aux_tensorboard_dir):
                            shutil.rmtree(aux_tensorboard_dir)

                        train_df, val_df, _ = get_train_val_test_dfs(bboxs_csv, splits_csv)
                        train_datagen = BBoxsGenerator(train_df, imgs_dir=imgs_dir, out_image_size=(img_size, img_size), resize=(img_size!=224))
                        val_datagen = BBoxsGenerator(val_df, imgs_dir=imgs_dir, out_image_size=(img_size, img_size), resize=(img_size!=224))
                        del train_df, val_df
                        gc.collect()

                        model = build_bbox_model(input_size=(img_size, img_size, 3),
                                                n_conv_blocks=n_conv_blocks, base_conv_n_filters=base_conv_n_filters,
                                                n_dense_layers=2, dense_size=dense_size, dropout_rate=0.30,
                                                loss=MeanSquaredError(), optimizer=Adamax(),
                                                metrics=[MeanAbsoluteError(), MeanBBoxIoU(x2y2=True)])

                        run_experiment(model, exp_name, train_datagen, val_datagen,
                                    results_dir=results_dir, tensorboard_logdir=tensorboard_dir,
                                    generator_queue_size=50, generator_workers=8, use_multiprocessing=False)

                        del train_datagen, val_datagen, model
                        gc.collect()

                        tf.keras.backend.clear_session()
                        gc.collect()

                        ### Temporary ###
                        sys.exit(0)
                        ###########
    for j in range(0, len(test_labels)):
        labels.append(list(test_labels[j]).index(1))
        predictions.append(result_fix[j].index(1))

    # store confusion matrix
    with open(result_folder + '/' + model_name + '_cm_' + str(num_classes),
              'wb') as file_pi:
        pickle.dump(confusion_matrix(labels, predictions), file_pi)

    del model, history, predictions, labels, result, test_labels, result_fix


optimizers = {
    'sgd': SGD(lr=0.001, momentum=0.9),
    'adam': Adam(),
    'adamax': Adamax(),
    'adadelta': Adadelta(),
    'adagrad': Adagrad(),
    'ftrl': Ftrl(),
    'nadam': Nadam(),
    'rmsprop': RMSprop()
}

initializers = [
    'constant', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform',
    'lecun_normal', 'lecun_uniform', 'random_normal', 'truncated_normal'
]

# Try lots of different small models, see what works best
fit_evaluate_model(mymodels.get_simple_cnn(IMG_DIM, num_classes), 'mymodel',
                   'baseline')
# CIFAR-10をデータセット化し,Train, Validation, Testに分割
ds_train = tf.data.Dataset.from_tensor_slices(
    (x_train, y_train)).shuffle(x_train.shape[0]).batch(BATCH_SIZE_T)
ds_val = tf.data.Dataset.from_tensor_slices(
    (x_val, y_val)).shuffle(x_val.shape[0]).batch(BATCH_SIZE_T)
ds_test = tf.data.Dataset.from_tensor_slices(
    (x_test, y_test)).shuffle(x_test.shape[0]).batch(BATCH_SIZE_T)
ds = tf.data.Dataset.zip((ds_train, ds_val))

# Teacherモデルの定義
inputs = Input(shape=input_shape)
teacher = KDModel.Teacher(NUM_CLASSES)
teacher_model = teacher.createModel(inputs)

# Teacherモデルの学習
optimizer = Adamax(learning_rate=LR_T)  # 最適化アルゴリズム
history_teacher = LossAccHistory()
training = KDModel.NormalTraining(teacher_model)
teacher_model.summary()
# plot_model(teacher_model, show_shapes=True, to_file='teacher_model.png')
for epoch in range(1, EPOCHS_T + 1):
    loss_metric = Mean()
    loss_metric_val = Mean()
    acc_metric = Mean()
    acc_metric_val = Mean()
    acc = CategoricalAccuracy()
    acc_val = CategoricalAccuracy()

    # 各バッチごとに学習
    for (x_train_, y_train_), (x_val_, y_val_) in ds:
        loss_value, grads = training.grad(x_train_, y_train_)