def test_weighted(self): rmse_obj = metrics.RootMeanSquaredError() y_true = K.constant((2, 4, 6, 8)) y_pred = K.constant((1, 3, 2, 3)) sample_weight = K.constant((0, 1, 0, 1)) result = rmse_obj(y_true, y_pred, sample_weight=sample_weight) assert np.allclose(math.sqrt(13), K.eval(result), atol=1e-3)
def test_config(self): rmse_obj = metrics.RootMeanSquaredError(name='rmse', dtype='int32') assert rmse_obj.name == 'rmse' assert rmse_obj.dtype == 'int32' rmse_obj2 = metrics.RootMeanSquaredError.from_config(rmse_obj.get_config()) assert rmse_obj2.name == 'rmse' assert rmse_obj2.dtype == 'int32'
def test_unweighted(self): rmse_obj = metrics.RootMeanSquaredError() y_true = K.constant((2, 4, 6)) y_pred = K.constant((1, 3, 2)) update_op = rmse_obj(y_true, y_pred) K.eval(update_op) result = rmse_obj(y_true, y_pred) # error = [-1, -1, -4], square(error) = [1, 1, 16], mean = 18/3 = 6 assert np.allclose(math.sqrt(6), K.eval(result), atol=1e-3)
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
int(look_back / sub_sequence), 1) test_data_X = test_data_X.reshape(test_data_X.shape[0], sub_sequence, 1, int(look_back / sub_sequence), 1) test_loss = 0 test_mae = 0 test_rse = 0 for k in range(5): model = Sequential() model.add(ConvLSTM2D(filters=100, kernel_size=(1, 2), activation='relu', input_shape=(sub_sequence, 1, int(look_back / sub_sequence), 1))) model.add(Flatten()) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse', metrics=[metrics.mae, metrics.RootMeanSquaredError()]) train_history = model.fit(training_data_X, training_data_Y, epochs=500, verbose=2, validation_data=(validation_data_X, validation_data_Y)) loss = train_history.history['loss'] val_loss = train_history.history['val_loss'] mae_loss = train_history.history['mean_absolute_error'] mae_val_loss = train_history.history['val_mean_absolute_error'] fig1 = plt.figure(1) fig1.suptitle('Training and validation history ConvLSTM EUR-GBP UNIVARIATE ' + str(look_back) + ' PAST OBSERVATIONS RUN ' + str(k + 1), fontsize=8, y=0.99, verticalalignment='top') plt.plot(loss) plt.plot(val_loss) plt.plot(mae_loss) plt.plot(mae_val_loss) plt.legend(['loss', 'val_loss', 'mae', 'val_mae'])
test_mae = 0 test_rse = 0 for k in range(5): model = Sequential() model.add( LSTM(100, activation='relu', input_shape=(look_back, 1), return_sequences=True)) model.add(LSTM(100, activation='relu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse', metrics=[metrics.mae, metrics.RootMeanSquaredError()]) train_history = model.fit(training_data_X, training_data_Y, epochs=500, verbose=2, validation_data=(validation_data_X, validation_data_Y)) loss = train_history.history['loss'] val_loss = train_history.history['val_loss'] mae_loss = train_history.history['mean_absolute_error'] mae_val_loss = train_history.history['val_mean_absolute_error'] fig1 = plt.figure(1) fig1.suptitle( 'Training and validation history LSTM STACKED EUR-GBP UNIVARIATE ' + str(look_back) + ' PAST OBSERVATIONS RUN ' + str(k + 1),
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
model.add(BatchNormalization()) model.add(Dropout(0.2)) model.add(MaxPooling2D(pool_size=2, padding='same')) model.add(Conv2D(32, 3, activation="relu")) # model.add(MaxPooling2D(pool_size=2, padding='same')) # model.add(Lambda(counting, # name='z')) model.add(Flatten()) model.add(Dense(64, activity_regularizer=l2(0.001))) model.add(Dropout(0.1)) model.add(Dense(32)) model.add(Dense(1, activation="linear")) model.compile(loss=keras.losses.mean_absolute_error, optimizer='nadam', metrics=[metrics.RootMeanSquaredError(), metrics.MAE]) print(model.summary()) print(X_train.shape, Y_train.shape) print(X_test.shape, Y_test2.shape) batch_size = 16 epochs = 150 reduce_lr_acc = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=epochs / 10, verbose=1, min_delta=1e-4, mode='max') model.fit(X_train, Y_train,
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 ])