Exemplo n.º 1
0
def get_compiled_multi_io_model_temporal(sample_weight_mode):
    model = get_multi_io_temporal_model()
    model.compile(optimizer=optimizer_v2.gradient_descent.SGD(0.1),
                  loss='mae',
                  metrics=[metrics.MeanAbsoluteError(name='mae')],
                  weighted_metrics=[metrics.MeanAbsoluteError(name='mae_2')],
                  sample_weight_mode=sample_weight_mode,
                  run_eagerly=test_utils.should_run_eagerly())
    return model
Exemplo n.º 2
0
 def test_weighted(self):
     mae_obj = metrics.MeanAbsoluteError()
     y_true = K.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),
                          (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))
     y_pred = K.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),
                          (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))
     sample_weight = K.constant((1., 1.5, 2., 2.5))
     result = mae_obj(y_true, y_pred, sample_weight=sample_weight)
     assert np.allclose(0.54285, K.eval(result), atol=1e-5)
Exemplo n.º 3
0
    def test_unweighted(self):
        mae_obj = metrics.MeanAbsoluteError()
        y_true = K.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),
                             (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))
        y_pred = K.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),
                             (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))

        result = mae_obj(y_true, y_pred)
        assert np.allclose(0.5, K.eval(result), atol=1e-5)
Exemplo n.º 4
0
    def test_config(self):
        mae_obj = metrics.MeanAbsoluteError(name='my_mae', dtype='int32')
        assert mae_obj.name == 'my_mae'
        assert mae_obj.dtype == 'int32'

        # Check save and restore config
        mae_obj2 = metrics.MeanAbsoluteError.from_config(mae_obj.get_config())
        assert mae_obj2.name == 'my_mae'
        assert mae_obj2.dtype == 'int32'
def build_model():

    # Build network architecture
    model = models.Sequential()
    model.add(
        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(50, 50, 1)))
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(layers.Flatten())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(16, activation='relu'))
    model.add(layers.Dense(1, activation=None))

    # Configure model optimization
    model.compile(
        optimizer='rmsprop',
        loss='mse',
        metrics=[metrics.MeanAbsoluteError(),
                 metrics.RootMeanSquaredError()])

    return model
Exemplo n.º 6
0
def models(labelarr, imgarr):
    pretrainmodel = load_model(
        '/home/som/lab/seed-yzj/newpaper4/laboratory/model/labotary_nose_att.hdf5',
        compile=False)
    pretrainmodel.compile(optimizer=Adam(lr=1e-4),
                          loss=losses.mean_squared_error,
                          metrics=[
                              metrics.MeanAbsoluteError(),
                              metrics.MeanAbsolutePercentageError(),
                              metrics.RootMeanSquaredError(), pearson_r
                          ])

    x_train, x_test, y_train, y_test = train_test_split(imgarr,
                                                        labelarr,
                                                        test_size=0.4,
                                                        random_state=3)
    testlen = x_test.shape[0]
    x_val = x_test[:int(testlen / 2)]
    y_val = y_test[:int(testlen / 2)]
    x_test = x_test[int(testlen / 2):]
    y_test = y_test[int(testlen / 2):]
    # input
    input = Input(shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3]))

    x = ZeroPadding2D((3, 3))(input)
    x = Conv2d_BN(x,
                  nb_filter=64,
                  kernel_size=(7, 7),
                  strides=(2, 2),
                  padding='valid')
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)
    # (56,56,64)
    x = Conv_Block(x, nb_filter=64, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=64, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=64, kernel_size=(3, 3))
    # (28,28,128)
    x = Conv_Block(x,
                   nb_filter=128,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   with_conv_shortcut=True)
    x = Conv_Block(x, nb_filter=128, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=128, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=128, kernel_size=(3, 3))
    # (14,14,256)
    x = Conv_Block(x,
                   nb_filter=256,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   with_conv_shortcut=True)
    x = Conv_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=256, kernel_size=(3, 3))
    # (7,7,512)
    x = Conv_Block(x,
                   nb_filter=512,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   with_conv_shortcut=True)
    x = Conv_Block(x, nb_filter=512, kernel_size=(3, 3))
    x = Conv_Block(x, nb_filter=512, kernel_size=(3, 3))
    x = AveragePooling2D(pool_size=(3, 3))(x)
    x = Flatten()(x)

    fc6 = Dense(units=labelarr.shape[1],
                weights=pretrainmodel.get_layer('dense_27').get_weights(),
                trainable=False)(x)

    model = Model(input=input, output=fc6)
    model.summary()

    # model = multi_gpu_model(model, gpus=4)

    model.compile(optimizer=Adam(lr=1e-4),
                  loss=losses.mean_squared_error,
                  metrics=[
                      metrics.MeanAbsoluteError(),
                      metrics.MeanAbsolutePercentageError(),
                      metrics.RootMeanSquaredError(), pearson_r
                  ])

    model_checkpoint = ModelCheckpoint(
        '/home/som/lab/seed-yzj/newpaper4/laboratory/model/labotary_left_att.hdf5',
        monitor='val_loss',
        verbose=1,
        save_best_only=True,
        save_weights_only=False)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                  patience=15,
                                  mode='auto',
                                  factor=0.7,
                                  min_lr=1e-6)

    # csv_logger = CSVLogger('training.csv')

    model.fit(x_train,
              y_train,
              batch_size=32,
              epochs=200,
              verbose=1,
              callbacks=[model_checkpoint, reduce_lr],
              validation_data=(x_val, y_val))

    return 0
Exemplo n.º 7
0
         'use_bias': True,
         'kernel_initializer': 'Zeros',
         'kernel_regularizer': None,
         'bias_regularizer': None,
         'activity_regularizer': None,
         'kernel_constraint': None,
         'bias_constraint': None
     },
 },
 'model': {
     'compilation': {
         'optimizer': optimizers.Adam(
             learning_rate=0.001
             ),
         'loss': losses.MeanSquaredError(),
         'metrics': [metrics.MeanSquaredError(), metrics.MeanAbsoluteError()],
     }
 },
 'fit': {
     'batch_size': 32,
     'epochs': 100,
     'callbacks' : [
         callbacks.EarlyStopping(monitor='loss', patience=7), 
         callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10)
     ]
 },
 'Monte-Carlo': {
     'temperature': 100,
     'Number_of_steps': 100000,
     'box_size': 0.02
 }
Exemplo n.º 8
0
    def __init__(self):
        self.nosearr = np.load(self.nosearrpath)
        self.nosefrearr = np.load(self.nosefrearrpath)
        self.nosemodel = load_model(self.nosemodelpath, compile=False)
        self.nosemodel.compile(optimizer=Adam(lr=1e-4),
                               loss=losses.mean_squared_error,
                               metrics=[
                                   metrics.MeanAbsoluteError(),
                                   metrics.MeanAbsolutePercentageError(),
                                   metrics.RootMeanSquaredError(), pearson_r
                               ])
        # self.nosemodel.summary()

        self.foreheadarr = np.load(self.foreheadarrpath)
        self.foreheadfrearr = np.load(self.foreheadfrearrpath)
        self.foreheadmodel = load_model(self.foreheadmodelpath, compile=False)
        self.foreheadmodel.compile(optimizer=Adam(lr=1e-4),
                                   loss=losses.mean_squared_error,
                                   metrics=[
                                       metrics.MeanAbsoluteError(),
                                       metrics.MeanAbsolutePercentageError(),
                                       metrics.RootMeanSquaredError(),
                                       pearson_r
                                   ])
        # self.foreheadmodel.summary()

        self.leftarr = np.load(self.leftarrpath)
        self.leftfrearr = np.load(self.leftfrearrpath)
        self.leftmodel = load_model(self.leftmodelpath, compile=False)
        self.leftmodel.compile(optimizer=Adam(lr=1e-4),
                               loss=losses.mean_squared_error,
                               metrics=[
                                   metrics.MeanAbsoluteError(),
                                   metrics.MeanAbsolutePercentageError(),
                                   metrics.RootMeanSquaredError(), pearson_r
                               ])

        self.rightarr = np.load(self.rightarrpath)
        self.rightfrearr = np.load(self.rightfrearrpath)
        self.rightmodel = load_model(self.rightmodelpath, compile=False)
        self.rightmodel.compile(optimizer=Adam(lr=1e-4),
                                loss=losses.mean_squared_error,
                                metrics=[
                                    metrics.MeanAbsoluteError(),
                                    metrics.MeanAbsolutePercentageError(),
                                    metrics.RootMeanSquaredError(), pearson_r
                                ])

        self.thermalnosearr = np.load(self.thermalnosearrpath)
        self.thermalnosefrearr = np.load(self.thermalnosefrearrpath)
        self.thermalnosemodel = load_model(self.thermalnosemodelpath,
                                           compile=False)
        self.thermalnosemodel.compile(
            optimizer=Adam(lr=1e-4),
            loss=losses.mean_squared_error,
            metrics=[
                metrics.MeanAbsoluteError(),
                metrics.MeanAbsolutePercentageError(),
                metrics.RootMeanSquaredError(), pearson_r
            ])

        self.thermalforeheadarr = np.load(self.thermalforeheadarrpath)
        self.thermalforeheadfrearr = np.load(self.thermalforeheadfrearrpath)
        self.thermalforeheadmodel = load_model(self.thermalforeheadmodelpath,
                                               compile=False)
        self.thermalforeheadmodel.compile(
            optimizer=Adam(lr=1e-4),
            loss=losses.mean_squared_error,
            metrics=[
                metrics.MeanAbsoluteError(),
                metrics.MeanAbsolutePercentageError(),
                metrics.RootMeanSquaredError(), pearson_r
            ])

        self.thermalleftarr = np.load(self.thermalleftarrpath)
        self.thermalleftfrearr = np.load(self.thermalleftfrearrpath)
        self.thermalleftmodel = load_model(self.thermalleftmodelpath,
                                           compile=False)
        self.thermalleftmodel.compile(
            optimizer=Adam(lr=1e-4),
            loss=losses.mean_squared_error,
            metrics=[
                metrics.MeanAbsoluteError(),
                metrics.MeanAbsolutePercentageError(),
                metrics.RootMeanSquaredError(), pearson_r
            ])

        self.thermalrightarr = np.load(self.thermalrightarrpath)
        self.thermalrightfrearr = np.load(self.thermalrightfrearrpath)
        self.thermalrightmodel = load_model(self.thermalrightmodelpath,
                                            compile=False)
        self.thermalrightmodel.compile(
            optimizer=Adam(lr=1e-4),
            loss=losses.mean_squared_error,
            metrics=[
                metrics.MeanAbsoluteError(),
                metrics.MeanAbsolutePercentageError(),
                metrics.RootMeanSquaredError(), pearson_r
            ])