Exemplo n.º 1
0
def test_pidon_operator_on_ode_with_analytic_solution():
    set_random_seed(0)

    r = 4.
    y_0 = 1.

    diff_eq = PopulationGrowthEquation(r)
    cp = ConstrainedProblem(diff_eq)
    t_interval = (0., .25)
    ic = ContinuousInitialCondition(cp, lambda _: np.array([y_0]))

    sampler = UniformRandomCollocationPointSampler()
    pidon = PIDONOperator(sampler, .001, True)

    training_loss_history, test_loss_history = pidon.train(
        cp,
        t_interval,
        training_data_args=DataArgs(
            y_0_functions=[ic.y_0],
            n_domain_points=25,
            n_batches=5,
            n_ic_repeats=5
        ),
        model_args=ModelArgs(
            latent_output_size=1,
            trunk_hidden_layer_sizes=[50, 50, 50],
        ),
        optimization_args=OptimizationArgs(
            optimizer=optimizers.SGD(),
            epochs=100,
            verbose=False
        ),
        secondary_optimization_args=SecondaryOptimizationArgs(
            max_iterations=100,
            verbose=False
        )
    )

    assert len(training_loss_history) == 101
    assert len(test_loss_history) == 0
    assert training_loss_history[-1].weighted_total_loss.numpy() < 5e-5

    ivp = InitialValueProblem(
        cp,
        t_interval,
        ic,
        lambda _ivp, t, x: np.array([y_0 * np.e ** (r * t)])
    )

    solution = pidon.solve(ivp)

    assert solution.d_t == .001
    assert solution.discrete_y().shape == (250, 1)

    analytic_y = np.array([ivp.exact_y(t) for t in solution.t_coordinates])

    assert np.mean(np.abs(analytic_y - solution.discrete_y())) < 1e-3
    assert np.max(np.abs(analytic_y - solution.discrete_y())) < 2.5e-3
def main(unit, first_conv, second_conv, batch_size, k_size, epochs, max_items):
    shape, channel, compute_flops = 36, 1, True
    losses = {
        "mustache": "categorical_crossentropy",
        "eyeglasses": "categorical_crossentropy",
        "beard": "categorical_crossentropy",
        "hat": "categorical_crossentropy",
        "bald": "categorical_crossentropy"
    }

    lossWeights = {
        "mustache": 1,
        "eyeglasses": 5,
        "beard": 5,
        "hat": 1,
        "bald": 5
    }

    # to have the flops we have to do that with a h5 model
    # problem is that you can't compile model with a f1 measure as it doesn't exist in keras originally
    # so, i retrain the model in one epoch, save it and then, compute flops of the model
    model_filename = root_path + "facenet_bis.h5"
    checkpoint = ModelCheckpoint(filepath=model_filename,
                                 monitor='loss',
                                 mode='min')

    opt = topt.SGD(lr=0.001)

    #Creating the net for all these parameters
    net = FaceNet_NoGender(shape, channel, unit, first_conv, second_conv)
    model = net.build(k_size)
    seq = CelebASequence(attributes_path,
                         images_path,
                         shape,
                         channel,
                         max_items=max_items)
    seq.augment(Mirroring())
    seq.augment(Blurring())
    seq.augment(Blurring(Mirroring()))
    seq.augment(MotionBlur('H'))
    seq.augment(MotionBlur('H', Mirroring()))
    seq.prepare(batch_size)

    # initialize the optimizer and compile the model
    print("[INFO] compiling flop model...")
    model.compile(optimizer=opt,
                  loss=losses,
                  loss_weights=lossWeights,
                  metrics=['accuracy'])
    seq.set_mode_train()
    model.fit(x=seq, epochs=epochs, callbacks=[checkpoint])
    flop = get_flops(model_filename)
    file = open(root_path + "flop_final_model_bis.txt", "w+")
    file.write(f"model flop: {flop}")
def define_optimizer(opt_name, opt_kwargs):

    if opt_name == 'sgd':
        optimizer = optimizers.SGD(**opt_kwargs)
    elif opt_name == 'adam':
        optimizer = optimizers.Adam(**opt_kwargs)
    elif opt_name == 'adagrad':
        optimizer = optimizers.Adagrad(**opt_kwargs)
    elif opt_name == 'rmsprop':
        optimizer = optimizers.RMSprop(**opt_kwargs)
    elif opt_name == 'adadelta':
        optimizer = optimizers.Adadelta(**opt_kwargs)
    elif opt_name == 'nadam':
        optimizer = optimizers.Nadam(**opt_kwargs)
    elif opt_name == 'adamax':
        optimizer = optimizers.Adamax(**opt_kwargs)
    else:
        raise ValueError(f"Optimizer {opt_name} is wrong or not implemented.")
    return optimizer
Exemplo n.º 4
0
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += tf.random.normal(labels.shape, stddev=0.01)
print(features[0], labels[0])

batch_size = 10
# Dataset API:https://zhuanlan.zhihu.com/p/30751039
# 将训练数据的特征和标签组合
dataset = tfdata.Dataset.from_tensor_slices((features, labels))
# 随机读取小批量
dataset = dataset.shuffle(buffer_size=num_examples)
dataset = dataset.batch(batch_size)
# data_iter = iter(dataset)

model = keras.Sequential()
# 第一个参数是units,输出的维度大小
model.add(layers.Dense(1, kernel_initializer=init.RandomNormal(stddev=0.01)))

loss = losses.MeanSquaredError()

trainer = optimizers.SGD(learning_rate=0.03)

num_epochs = 3
for epoch in range(num_epochs):
    for (batch, (X, y)) in enumerate(dataset):
        with tf.GradientTape() as tape:
            l = loss(model(X, training=True), y)
        grads = tape.gradient(l, model.trainable_variables)
        trainer.apply_gradients(zip(grads, model.trainable_variables))
    l = loss(model(features), labels)
    print('epoch %d, loss: %f' % (epoch, l))
Exemplo n.º 5
0
X_train_enc, X_test_enc = prepare_inputs_ohot(X_train, X_test)
print('Finished preparing inputs.')
# prepare output data
y_train_enc, y_test_enc = prepare_targets(y_train, y_test)
print('Finished preparing outputs.')
# define the  model
model = Sequential()
model.add(Dense(187, input_dim=X_train_enc.shape[1], activation="tanh", kernel_initializer='he_normal'))
model.add(Dropout(0.2))
model.add(Dense(64, input_dim=X_train_enc.shape[1], activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, input_dim=X_train_enc.shape[1], activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
opt = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mse', optimizer=opt, metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X_train_enc, y_train_enc, epochs=20, batch_size=128, verbose=1, use_multiprocessing=True)
# evaluate the keras model
_, accuracy = model.evaluate(X_test_enc, y_test_enc, verbose=0)
print('Accuracy: %.2f' % (accuracy * 100))



'''
Accuracy: 49.95
model = Sequential()
model.add(Dense(32, input_dim=X_train_enc.shape[1], activation="tanh", kernel_initializer='he_normal'))
model.add(Dropout(0.4))
model.add(Dense(16, input_dim=X_train_enc.shape[1], activation='relu'))
Exemplo n.º 6
0
    def build_origin(self,
                     print_summary=False,
                     num_classes=5,
                     image_size=(352, 640, 3)):

        input_tensor = keras.layers.Input(image_size)
        conv_0 = self.build_conv2D_block(input_tensor,
                                         filters=24,
                                         kernel_size=1,
                                         strides=1)
        # conv stage 1
        conv_1 = self.build_conv2D_block(conv_0,
                                         filters=64,
                                         kernel_size=3,
                                         strides=1)
        conv_1 = self.build_conv2D_block(conv_1,
                                         filters=64,
                                         kernel_size=3,
                                         strides=1)

        # pool stage 1
        pool1 = MaxPooling2D()(conv_1)
        # conv stage 2
        conv_2 = self.build_conv2D_block(pool1,
                                         filters=128,
                                         kernel_size=3,
                                         strides=1)
        conv_2 = self.build_conv2D_block(conv_2,
                                         filters=128,
                                         kernel_size=3,
                                         strides=1)

        # pool stage 2
        pool2 = MaxPooling2D()(conv_2)
        # conv stage 3
        conv_3 = self.build_conv2D_block(pool2,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1)
        conv_3 = self.build_conv2D_block(conv_3,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1)
        conv_3 = self.build_conv2D_block(conv_3,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1)

        # pool stage 3
        pool3 = MaxPooling2D()(conv_3)
        # conv stage 4
        conv_4 = self.build_conv2D_block(pool3,
                                         filters=512,
                                         kernel_size=3,
                                         strides=1)
        conv_4 = self.build_conv2D_block(conv_4,
                                         filters=512,
                                         kernel_size=3,
                                         strides=1)

        conv_4 = self.build_conv2D_block(conv_4,
                                         filters=512,
                                         kernel_size=3,
                                         strides=1)
        # pool4 = MaxPooling2D()(conv_4)
        ### add dilated convolution ###
        # conv stage 5_1
        conv_5 = self.build_conv2D_block(conv_4,
                                         filters=512,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=2)
        conv_5 = self.build_conv2D_block(conv_5,
                                         filters=512,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=2)
        conv_5 = self.build_conv2D_block(conv_5,
                                         filters=512,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=2)

        # added part of SCNN #
        conv_6_4 = self.build_conv2D_block(conv_5,
                                           filters=1024,
                                           kernel_size=3,
                                           strides=1,
                                           dilation_rate=4)
        conv_6_5 = self.build_conv2D_block(conv_6_4,
                                           filters=128,
                                           kernel_size=1,
                                           strides=1)  # 8 x 36 x 100 x 128

        # add message passing #
        # top to down #

        feature_list_new = self.space_cnn_part(conv_6_5)

        #######################
        dropout_output = Dropout(0.9)(feature_list_new)
        conv_output = K.resize_images(
            dropout_output,
            height_factor=self.IMG_HEIGHT // dropout_output.shape[1],
            width_factor=self.IMG_WIDTH // dropout_output.shape[2],
            data_format="channels_last")
        ret_prob_output = Conv2D(filters=num_classes,
                                 kernel_size=1,
                                 activation='softmax',
                                 name='ctg_out_1')(conv_output)

        ### add lane existence prediction branch ###
        # spatial softmax #
        features = ret_prob_output  # N x H x W x C
        softmax = Activation('softmax')(features)
        avg_pool = AvgPool2D(strides=2)(softmax)
        _, H, W, C = avg_pool.get_shape().as_list()
        reshape_output = tf.reshape(avg_pool, [-1, H * W * C])
        fc_output = Dense(128)(reshape_output)
        relu_output = ReLU(max_value=6)(fc_output)
        existence_output = Dense(4, name='ctg_out_2')(relu_output)

        self.model = Model(inputs=input_tensor,
                           outputs=[ret_prob_output, existence_output])
        # print(self.model.summary())
        adam = optimizers.Adam(lr=0.001)
        sgd = optimizers.SGD(lr=0.001)

        if num_classes == 1:
            self.model.compile(optimizer=sgd,
                               loss="binary_crossentropy",
                               metrics=['accuracy'])
        else:
            self.model.compile(optimizer=sgd,
                               loss={
                                   'ctg_out_1': 'categorical_crossentropy',
                                   'ctg_out_2': 'binary_crossentropy'
                               },
                               loss_weights={
                                   'ctg_out_1': 1.,
                                   'ctg_out_2': 0.2,
                               },
                               metrics=['accuracy', 'mse'])
Exemplo n.º 7
0
    def build(self,
              print_summary=False,
              num_classes=5,
              image_size=(352, 640, 3)):

        input_tensor = keras.layers.Input(image_size)
        conv_0 = self.build_conv2D_block(input_tensor,
                                         filters=24,
                                         kernel_size=1,
                                         strides=1)
        conv_0 = self.build_conv2D_block(conv_0,
                                         filters=24,
                                         kernel_size=3,
                                         strides=1)

        conv_0 = self.build_conv2D_block(conv_0,
                                         filters=24,
                                         kernel_size=3,
                                         strides=1)
        conv_0 = self.build_conv2D_block(conv_0,
                                         filters=24,
                                         kernel_size=3,
                                         strides=1)

        # first conv layer
        conv_1 = self.build_conv2D_block(conv_0,
                                         filters=48,
                                         kernel_size=3,
                                         strides=2)
        conv_1 = self.build_conv2D_block(conv_1,
                                         filters=48,
                                         kernel_size=3,
                                         strides=1)

        conv_1 = self.build_conv2D_block(conv_1,
                                         filters=48,
                                         kernel_size=3,
                                         strides=1)
        conv_1 = self.build_conv2D_block(conv_1,
                                         filters=48,
                                         kernel_size=3,
                                         strides=1)
        conv_1 = self.build_conv2D_block(conv_1,
                                         filters=48,
                                         kernel_size=3,
                                         strides=1)
        # second conv layer
        conv_2 = self.build_conv2D_block(conv_1,
                                         filters=64,
                                         kernel_size=3,
                                         strides=2)
        conv_2 = self.build_conv2D_block(conv_2,
                                         filters=64,
                                         kernel_size=3,
                                         strides=1)

        conv_2 = self.build_conv2D_block(conv_2,
                                         filters=64,
                                         kernel_size=3,
                                         strides=1)
        conv_2 = self.build_conv2D_block(conv_2,
                                         filters=64,
                                         kernel_size=3,
                                         strides=1)
        conv_2 = self.build_conv2D_block(conv_2,
                                         filters=64,
                                         kernel_size=3,
                                         strides=1)
        # third conv layer
        conv_3 = self.build_conv2D_block(conv_2,
                                         filters=96,
                                         kernel_size=3,
                                         strides=2)
        conv_3 = self.build_conv2D_block(conv_3,
                                         filters=96,
                                         kernel_size=3,
                                         strides=1)

        conv_3 = self.build_conv2D_block(conv_3,
                                         filters=96,
                                         kernel_size=3,
                                         strides=1)
        conv_3 = self.build_conv2D_block(conv_3,
                                         filters=96,
                                         kernel_size=3,
                                         strides=1)
        conv_3 = self.build_conv2D_block(conv_3,
                                         filters=96,
                                         kernel_size=3,
                                         strides=1)
        # fourth conv layer
        conv_4 = self.build_conv2D_block(conv_3,
                                         filters=128,
                                         kernel_size=3,
                                         strides=2)
        conv_4 = self.build_conv2D_block(conv_4,
                                         filters=128,
                                         kernel_size=3,
                                         strides=1)

        conv_4 = self.build_conv2D_block(conv_4,
                                         filters=128,
                                         kernel_size=3,
                                         strides=1)
        conv_4 = self.build_conv2D_block(conv_4,
                                         filters=128,
                                         kernel_size=3,
                                         strides=1)
        conv_4 = self.build_conv2D_block(conv_4,
                                         filters=128,
                                         kernel_size=3,
                                         strides=1)
        # fifth conv layer
        conv_5 = self.build_conv2D_block(conv_4,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=1)
        conv_5 = self.build_conv2D_block(conv_5,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=1)

        conv_5 = self.build_conv2D_block(conv_5,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=1)
        conv_5 = self.build_conv2D_block(conv_5,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=1)
        conv_5 = self.build_conv2D_block(conv_5,
                                         filters=256,
                                         kernel_size=3,
                                         strides=1,
                                         dilation_rate=1)
        # added part of SCNN #
        conv_6_4 = self.build_conv2D_block(conv_5,
                                           filters=256,
                                           kernel_size=3,
                                           strides=1,
                                           dilation_rate=1)
        conv_6_5 = self.build_conv2D_block(conv_6_4,
                                           filters=128,
                                           kernel_size=1,
                                           strides=1)  # 8 x 36 x 100 x 128

        scnn_part = self.space_cnn_part(conv_6_5)

        #######################
        # conv2d_deconv5_1 = self.build_conv2D_block(conv_5,filters = 196,kernel_size=3,strides=1)
        # conv2d_deconv4   = self.build_conv2Dtranspose_block(conv2d_deconv5_1, filters=128, kernel_size=4, strides=2)

        Concat_concat4 = concatenate([scnn_part, conv_4], axis=-1)

        conv2d_deconv4_1 = self.build_conv2D_block(Concat_concat4,
                                                   filters=96,
                                                   kernel_size=3,
                                                   strides=1)
        conv2d_deconv3 = self.build_conv2Dtranspose_block(conv2d_deconv4_1,
                                                          filters=96,
                                                          kernel_size=4,
                                                          strides=2)

        Concat_concat3 = concatenate([conv2d_deconv3, conv2d_deconv3], axis=-1)

        conv2d_deconv3_1 = self.build_conv2D_block(Concat_concat3,
                                                   filters=64,
                                                   kernel_size=3,
                                                   strides=1)
        conv2d_deconv2 = self.build_conv2Dtranspose_block(conv2d_deconv3_1,
                                                          filters=64,
                                                          kernel_size=4,
                                                          strides=2)

        Concat_concat2 = concatenate([conv2d_deconv2, conv_2], axis=-1)

        conv2d_deconv2_1 = self.build_conv2D_block(Concat_concat2,
                                                   filters=32,
                                                   kernel_size=3,
                                                   strides=1)
        conv2d_deconv1 = self.build_conv2Dtranspose_block(conv2d_deconv2_1,
                                                          filters=32,
                                                          kernel_size=4,
                                                          strides=2)

        Concat_concat1 = concatenate([conv2d_deconv1, conv_1], axis=-1)

        conv2d_deconv1_1 = self.build_conv2D_block(Concat_concat1,
                                                   filters=16,
                                                   kernel_size=3,
                                                   strides=1)
        conv2d_deconv0 = self.build_conv2Dtranspose_block(conv2d_deconv1_1,
                                                          filters=128,
                                                          kernel_size=4,
                                                          strides=2)

        if num_classes == 1:
            ret_prob_output = Conv2DTranspose(filters=num_classes,
                                              kernel_size=1,
                                              strides=1,
                                              activation='sigmoid',
                                              padding='same',
                                              name='ctg_out_1')(conv2d_deconv0)
        else:
            ret_prob_output = Conv2DTranspose(filters=num_classes,
                                              kernel_size=1,
                                              strides=1,
                                              activation='softmax',
                                              padding='same',
                                              name='ctg_out_1')(conv2d_deconv0)

        ### add lane existence prediction branch ###
        # spatial softmax #
        # features = ret_prob_output  # N x H x W x C
        # softmax = Activation('softmax')(features)
        # avg_pool = AvgPool2D(strides=2)(softmax)
        features = self.build_conv2D_block(ret_prob_output,
                                           filters=num_classes,
                                           kernel_size=1,
                                           strides=2)
        _, H, W, C = features.get_shape().as_list()
        reshape_output = tf.reshape(features, [-1, H * W * C])
        fc_output = Dense(128)(reshape_output)
        relu_output = Activation('relu')(fc_output)
        existence_output = Dense(4)(relu_output)
        existence_output = Activation('softmax',
                                      name='ctg_out_2')(existence_output)
        self.model = Model(inputs=input_tensor,
                           outputs=[ret_prob_output, existence_output])
        # print(self.model.summary())
        adam = optimizers.Adam(lr=0.001)
        sgd = optimizers.SGD(lr=0.001)

        if num_classes == 1:
            self.model.compile(optimizer=sgd,
                               loss="binary_crossentropy",
                               metrics=['accuracy'])
        else:
            self.model.compile(optimizer=sgd,
                               loss=self.my_loss_error,
                               metrics=['accuracy'])
Exemplo n.º 8
0
def main(epochs, max_items):
    shape, channel, compute_flops = 36, 1, True
    losses = {"mustache": "categorical_crossentropy",
              "eyeglasses": "categorical_crossentropy",
              "beard": "categorical_crossentropy",
              "hat": "categorical_crossentropy",
              "bald": "categorical_crossentropy"}

    lossWeights = {"mustache": 1, "eyeglasses": 5, "beard": 5, "hat": 1, "bald": 5}

    # fit labels with images with a ReduceLRonPlateau which divide learning by 10
    # if for 2 consecutive epochs, global validation loss doesn't decrease
    reducelronplateau = ReduceLROnPlateau(monitor='loss', mode='min', patience=2, factor=0.1)
    earlystopping = EarlyStopping(monitor='loss', mode='min', patience=3)

    # to have the flops we have to do that with a h5 model
    # problem is that you can't compile model with a f1 measure as it doesn't exist in keras originally
    # so, i retrain the model in one epoch, save it and then, compute flops of the model
    model_filename = root_path + "facenet_flops_test.h5"
    checkpoint = ModelCheckpoint(filepath=model_filename, monitor='loss', mode='min')

    opt = topt.SGD(lr=0.001)

    seq = CelebASequence(attributes_path, images_path, shape, channel, max_items=max_items)
    seq.augment(Mirroring())
    seq.augment(Blurring())
    seq.augment(Blurring(Mirroring()))
    seq.augment(MotionBlur('H'))
    seq.augment(MotionBlur('H', Mirroring()))
    seq.prepare(batch_size)

    #Creating the net for all these parameters
    net = FaceNet_NoGender(shape, channel, unit, first_conv, second_conv)
    model = net.build(k_size)

    with open(root_path + 'modelsummary_no_gender.txt', 'w') as f:
        with redirect_stdout(f):
            model.summary()    

    # initialize the optimizer and compile the model
    print("[INFO] compiling model...")
    model.compile(optimizer=opt, loss=losses, loss_weights=lossWeights, metrics=[f1, 'accuracy'])

    #Cross Validation 5-Fold
    f1_scores = defaultdict(list)
    for k in range(5):
        #Train on 80%
        seq.set_mode_fold(k)
        seq.set_mode_train()

        model.fit(x=seq, epochs=epochs, callbacks=[reducelronplateau, earlystopping])

        #Test on 20%
        seq.set_mode_test()
        evaluations = model.evaluate(seq)
        print(f"evaluations are: {evaluations}")

        # read evaluations by indexes found in model.metrics_names
        for kk in losses.keys():
            idx = model.metrics_names.index(kk + '_f1')
            f1_scores[kk].append(evaluations[idx])

    scores = {}
    for kk in losses.keys():
        score_array = np.array(f1_scores[kk], dtype='float64')
        average = np.mean(score_array)
        stddev = np.std(score_array)
        scores[kk] = (average, stddev)

    ## Compute flop
    # new sequence
    seq_fold = CelebASequence(attributes_path, images_path, shape, channel, max_items=100)
    seq_fold.prepare(batch_size)
    # initialize the optimizer and compile the model
    print("[INFO] compiling flop model...")
    model.compile(optimizer=opt, loss=losses, loss_weights=lossWeights, metrics=['accuracy'])
    seq_fold.set_mode_fold(0)
    model.fit(x=seq_fold, epochs=1, callbacks=[checkpoint])
    flop = get_flops(model_filename)

    # writing new file with performance and flop
    file = open(root_path + "performance_cv_no_gender.txt", "w+")
    for key, value in losses.items():
        file.write(key + f" average score: {scores[key][0]}\n")
        file.write(key + f" std score: {scores[key][1]}\n")

    file.write(f"model flop: {flop}")
Exemplo n.º 9
0
def main():
    x1 = np.array([
        137.9, 104.5, 100.0, 124.32, 79.20, 99.0, 124.0, 114.0, 106.69, 138.05,
        53.75, 46.91, 48.00, 63.02, 81.26, 86.21
    ])
    x2 = np.array([3, 2, 2, 3, 1, 2, 3, 2, 2, 3, 1, 1, 1, 1, 2, 2])
    y = np.array([
        145.0, 110.0, 93.0, 116.0, 65.32, 104.0, 118.0, 91.0, 62.0, 133.0,
        51.0, 45.0, 78.5, 69.65, 75.69, 95.30
    ])

    x1 = tf.cast(x1, dtype=tf.float32)
    x2 = tf.cast(x2, dtype=tf.float32)
    x = tf.stack([x1, x2], axis=0)
    x = (x - tf.reduce_mean(x)) / np.std(x)
    y = tf.cast(y, dtype=tf.float32)
    print(x.shape, y.shape, type(x), type(y))
    lr = 0.0001
    accs, losses = [], []
    # w1, w2, b = tf.Variable(tf.random.normal([1, 1])), \
    #             tf.Variable(tf.random.normal([1, 1])), \
    #             tf.Variable(tf.zeros([1]))  # random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
    w = tf.Variable(np.random.random([1, 2]), trainable=True, dtype=tf.float32)
    b = tf.Variable(random.random(), trainable=True, dtype=tf.float32)
    print(w, b)
    # print(w, b)
    # for step, (x1, x2, y) in enumerate(train_db):
    # w1 = tf.reshape(w1, [1, 1])
    # w2 = tf.reshape(w2, [1, 1])
    # b = tf.reshape(b, [1, 1])
    # print(x1.shape, x2.shape, y.shape, type(x1), type(x2), type(y))
    # print(w1.shape, w2.shape, b.shape, type(w1), type(w2), type(b))
    num = 10000
    optimizer = optimizers.SGD(lr)
    for i in range(num):
        with tf.GradientTape() as tape:
            out = tf.matmul(w, x) + b

            loss = tf.square(y - out)
            loss = tf.reduce_mean(loss)
            losses.append(loss)
            grads = tape.gradient(loss, [w, b])

            optimizer.apply_gradients(zip(grads, [w, b]))
        # if i % 100 == 0:
        #
        #     print('loss', loss)

    # w1.assign_sub(lr * grads[0])
    # w2.assign_sub(lr * grads[1])
    # b.assign_sub(lr * grads[2])  # 简单点说就是 -=

    # for p, g in zip([w1, w2, b], grads):
    #     p.assign_sub(lr * g)

    # print(step, 'loss:', float(loss))
    plt.figure(1)
    epoches = list(range(0, num))
    plt.plot(epoches, losses, color='C0', marker='s')
    plt.ylabel('MSE')
    plt.xlabel('Step')
    plt.legend()

    fig = plt.figure(2)
    ax = Axes3D(fig)
    ax.scatter3D(x[0, :], x[1, :], y, edgecolors='b')
    mesh1, mesh2 = tf.meshgrid(x[0, :], x[1, :])
    y1 = w[0, 0] * mesh1 + w[0, 1] * mesh2 + b
    ax.plot_surface(mesh1, mesh2, y1, color='r')
    plt.show()
def main(epochs, max_items, folds, skip):
    shape, channel, compute_flops = 36, 1, True
    losses = {"mustache": "categorical_crossentropy",
              "eyeglasses": "categorical_crossentropy",
              "beard": "categorical_crossentropy",
              "hat": "categorical_crossentropy",
              "bald": "categorical_crossentropy"}

    lossWeights = {"mustache": 1, "eyeglasses": 5, "beard": 5, "hat": 1, "bald": 5}

    dict_col = initialize_results(losses, compute_flops=compute_flops)

    # fit labels with images with a ReduceLRonPlateau which divide learning by 10
    # if for 2 consecutive epochs, global validation loss doesn't decrease
    reducelronplateau = ReduceLROnPlateau(monitor='loss', mode='min', patience=2, factor=0.1)
    earlystopping = EarlyStopping(monitor='loss', mode='min', patience=3)

    # to have the flops we have to do that with a h5 model
    # problem is that you can't compile model with a f1 measure as it doesn't exist in keras originally
    # so, I retrain the model in one epoch, save it and then, compute flops of the model
    if compute_flops:
        model_filename = root_path + "facenet_flops_test.h5"
        checkpoint = ModelCheckpoint(filepath=model_filename, monitor='loss', mode='min')

    opt = topt.SGD(lr=0.001)

    # build Sequence with data augmentation
    seq = CelebASequence(attributes_path, images_path, shape, channel, max_items=max_items)
    seq.augment(Mirroring())
    seq.augment(Blurring())
    seq.augment(Blurring(Mirroring()))
    seq.augment(MotionBlur('H'))
    seq.augment(MotionBlur('H', Mirroring()))

    s = 0
    for k_size in k_sizes:
        for batch_size in batch_sizes:
            # (Re)Initialize sequence on new batch size
            seq.prepare(batch_size)

            for first_conv in first_convs:
                for second_conv in second_convs:
                    if first_conv >= second_conv:
                        continue
                    for unit in units:
                        s+=1
                        print(f"Combinaison: {s} || {k_size} {batch_size} {first_conv} {second_conv} {unit}")

                        # Skip already-computed combinations
                        if s <= skip:
                            print('Skipped')
                            continue

                        #Creating the net for all these parameters
                        print("[INFO] compiling model...")
                        net = FaceNet_NoGender(shape, channel, unit, first_conv, second_conv)
                        model = net.build(k_size)
                        model.compile(optimizer=opt, loss=losses, loss_weights=lossWeights, metrics=[f1, 'accuracy'])

                        # Cross Validation 5-Fold
                        f1_scores = defaultdict(list)

                        for k in range(folds):
                            print(f'Fold {k}')
                            #Train
                            seq.set_mode_fold(k)
                            seq.set_mode_train()

                            model.fit(x=seq, epochs=epochs, callbacks=[reducelronplateau, earlystopping])

                            #Test
                            seq.set_mode_test()
                            evaluations = model.evaluate(seq)

                            # read evaluations by indexes found in model.metrics_names
                            for kk in losses.keys():
                                idx = model.metrics_names.index(kk + '_f1')
                                f1_scores[kk].append(evaluations[idx])

                        for key, value in losses.items():
                            score_array = np.array(f1_scores[key], dtype='float64')
                            average = np.mean(score_array)
                            stddev = np.std(score_array)
                            multiple_append([dict_col[key + " cv score"], dict_col[key + " std score"]], [average, stddev])

                        multiple_append([dict_col["number of Conv2D"], dict_col["number of Dense"], dict_col["kernel size"],
                                        dict_col["first conv"], dict_col["second conv"], dict_col["unit"], dict_col['batch_size']],
                                        [2, 1, k_size, first_conv, second_conv, unit, batch_size])

                        if compute_flops:
                            net_flops = FaceNet_NoGender(shape, channel, unit, first_conv, second_conv)
                            model_flops = net_flops.build(k_size)

                            seq_flops = CelebASequence(attributes_path, images_path, shape, channel, max_items=100)
                            seq_flops.prepare(batch_size)
                            opt_flops = topt.SGD(lr=0.001)

                            print("[INFO] compiling flops model...")
                            model_flops.compile(optimizer=opt_flops, loss=losses, loss_weights=lossWeights, metrics=['accuracy'])
                            model_flops.fit(x=seq_flops, epochs=1, callbacks=[checkpoint])

                            flops = get_flops(model_filename)
                            dict_col["flop"].append(flops)

                        print(dict_col)
                        df = pd.DataFrame(data=dict_col)
                        df.to_excel(root_path+"tab_comparison_no_gender_motion.xlsx")
                        print("Updated Dataframe is saved as xlsx")

    print("Final Dataframe is saved as xlsx")