diff --git a/house_prices_estimation_example_data.py b/house_prices_estimation_example_data.py index daffbba509de3e1e74ba6dc45622978931b0bb0e..c51f2cc4d49f76f8488d1cd444c47e58528d9bf8 100644 --- a/house_prices_estimation_example_data.py +++ b/house_prices_estimation_example_data.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- - +from tensorflow_core.python.keras.engine.input_layer import Input from tensorflow_core.python.keras.layers.core import Dense, Dropout -from tensorflow_core.python.keras.models import Sequential +from tensorflow_core.python.keras.models import Sequential, Model from tensorflow_core.python.keras.optimizer_v2.adam import Adam from datasets import load_house_dataset_data @@ -10,13 +10,21 @@ import matplotlib.pyplot as plt __author__ = 106360 -def generate_simple_regression_model(input_shape): - model = Sequential() - model.add(Dense(16, input_dim=input_shape, activation="relu")) - # model.add(Dropout(0.25)) - model.add(Dense(6, activation="relu")) - # model.add(Dropout(0.25)) - model.add(Dense(1, activation="sigmoid")) +def generate_simple_regression_model(input_shape,weights='',remove_head=False): + # define the model input + inputs = Input(shape=(input_shape)) + # loop over the number of filters + + x = Dense(16, input_dim=input_shape, activation="relu", name='layer1')(inputs) + x = Dense(6, activation="relu",name='layer2')(x) + + if not remove_head: + x= Dense(1, activation="sigmoid",name='layer3')(x) + + model = Model(inputs,x) + if weights!='': + model.load_weights(weights,by_name=True) + return model diff --git a/house_prices_estimation_example_image.py b/house_prices_estimation_example_image.py index a2d1343d80d2cc52f3b865e053cb9f11e1957ded..24fa7098833b48ecfe12948f0f96d40bd7d94b71 100644 --- a/house_prices_estimation_example_image.py +++ b/house_prices_estimation_example_image.py @@ -11,13 +11,13 @@ from tensorflow_core.python.keras.models import Sequential, Model, load_model from tensorflow_core.python.keras.optimizer_v2.adam import Adam import tensorflow as tf -from train_and_evaluation import evaluate_regression_model +from train_and_evaluation import evaluate_regression_model, train_model from datasets import load_house_dataset_data, DatasetType import matplotlib.pyplot as plt __author__ = 106360 -def generate_simple_cnn_regression_model(input_shape,n_blocks=2,weights='',is_regression=True,num_classes=1,freeze=False): +def generate_simple_cnn_regression_model(input_shape,n_blocks=2,weights='',is_regression=True,num_classes=1,freeze=False,remove_head=False): # define the model input inputs = Input(shape=(input_shape,input_shape,3)) # loop over the number of filters @@ -29,12 +29,14 @@ def generate_simple_cnn_regression_model(input_shape,n_blocks=2,weights='',is_re x = MaxPooling2D()(x) x= Flatten()(x) - - if is_regression: - y = Dense(num_classes, activation="sigmoid",name='last_dense_reg')(x) + x = Dense(6, activation="relu", name='pre_last_dense_reg')(x) + if not remove_head: + if is_regression: + y = Dense(num_classes, activation="sigmoid",name='last_dense_reg')(x) + else: + y = Dense(num_classes, activation="softmax", name='last_dense_clf')(x) else: - y = Dense(num_classes, activation="softmax", name='last_dense_clf')(x) - + y=x model = Model(inputs,y) if weights!='': @@ -115,7 +117,7 @@ def train_cifar100(num_classes=100,batch_size=32): if __name__ == "__main__": - pre_train_with_cifar100 = False + pre_train_with_cifar100 = True weights = '' if pre_train_with_cifar100: file_weight_cifar100 = 'pretrained_cifar100.h5' diff --git a/house_prices_estimation_example_image_and_data.py b/house_prices_estimation_example_image_and_data.py index 88cd8f7f69bac2be33fe9304eb63fcc25c93b5f6..98b6da53a10aa37be3517ea1d22014b8a2c5eb25 100644 --- a/house_prices_estimation_example_image_and_data.py +++ b/house_prices_estimation_example_image_and_data.py @@ -1,4 +1,11 @@ # -*- coding: utf-8 -*- +from tensorflow_core.python.keras.layers.merge import Concatenate + + +from house_prices_estimation_example_data import generate_simple_regression_model +from house_prices_estimation_example_image import generate_simple_cnn_regression_model + +__author__ = 106360 import os from tensorflow_core.python.keras.datasets import cifar10 @@ -11,80 +18,43 @@ from tensorflow_core.python.keras.models import Sequential, Model, load_model from tensorflow_core.python.keras.optimizer_v2.adam import Adam import tensorflow as tf -from utils import plot_regression, load_house_dataset_data, DatasetType +from datasets import load_house_dataset_data, DatasetType import matplotlib.pyplot as plt -__author__ = 106360 - - -def train_model(trainX, trainY, testX, testY, model, epochs=200, batch_size=16,show_plot=True): - history = model.fit(trainX, trainY, validation_data=(testX, testY), - epochs=epochs, batch_size=batch_size) +from train_and_evaluation import train_model, evaluate_regression_model - if show_plot: - acc = history.history['mean_absolute_percentage_error'] - val_acc = history.history['val_mean_absolute_percentage_error'] - loss = history.history['loss'] - val_loss = history.history['val_loss'] - epochs_range = range(epochs) - plt.figure(figsize=(8, 8)) - plt.subplot(1, 2, 1) - plt.plot(epochs_range, acc, label='Training mean_absolute_percentage_error') - plt.plot(epochs_range, val_acc, label='Validation mean_absolute_percentage_error') - plt.legend(loc='upper right') - plt.title('Training and Validation \% error') - - plt.subplot(1, 2, 2) - plt.plot(epochs_range, loss, label='Training Loss') - plt.plot(epochs_range, val_loss, label='Validation Loss') - plt.legend(loc='upper right') - plt.title('Training and Validation Loss') - plt.show() - - return model +if __name__ == "__main__": + (trainX_data,trainX_img, trainY, testX_data,testX_img,testY), normalizer = load_house_dataset_data(test_size=0.2,random_state=666,type=DatasetType.Both) -if __name__ == "__main__": - pre_train_with_cifar100 = False - weights = '' - if pre_train_with_cifar100: - file_weight_cifar100 = 'pretrained_cifar100.h5' + trainX = [trainX_data,trainX_img['frontal_img']] + testX = [testX_data,testX_img['frontal_img']] + input_shape_data = trainX[0].shape[1] + input_shape_img = trainX[1].shape[1] - try: - model = load_model(file_weight_cifar100) - except: - model = train_cifar100() - model.save(file_weight_cifar100) - weights = file_weight_cifar100 - (trainX_data,trainX_img, trainY, testX_data,testX_img,testY), normalizer = load_house_dataset_data(test_size=0.2,random_state=666,type=DatasetType.Both) + model_data = generate_simple_regression_model(input_shape_data, weights='regression_model_data.h5',remove_head=True) + model_img = generate_simple_cnn_regression_model(input_shape_img, weights='regression_model_image_pretrained.h5',remove_head=False) + input_data = [Input(input_shape_data),Input((input_shape_img,input_shape_img,3))] + # y_data = model_data.layers[-2]#(input_data[0]) + # y_img = model_img.layers[-2]#(input_data[1]) + y_data = model_data(input_data[0]) + y_img = model_img(input_data[1]) - trainX = trainX_img['frontal_img'] - testX = testX_img['frontal_img'] - input_shape = trainX.shape[1] + y = Concatenate()([y_data, y_img]) + y = Dense(1,activation='sigmoid')(y) - if pre_train_with_cifar100: - file_weight_finetune = 'regression_model_image_finetune.h5' - model = generate_simple_cnn_regression_model(input_shape,weights=weights,freeze=True) - opt = Adam(lr=1e-3, decay=1e-3 / 200) - model.compile(loss='mean_squared_error',metrics=['mean_absolute_percentage_error','mean_absolute_error','mean_squared_error'], optimizer=opt) - model.summary() - model = train_model(trainX, trainY, testX, testY,model,show_plot=True,epochs=500,batch_size=32) - evaluate_regression_model(model,testX,testY,normalizer,show_plot=True) - model.save(file_weight_finetune) - weights = file_weight_finetune - final_model_weight = 'regression_model_image_pretrained.h5' - else: - final_model_weight = 'regression_model_image_from_scratch.h5' + for layer in model_img.layers: + layer.trainable = False + model = Model(input_data,y) - model = generate_simple_cnn_regression_model(input_shape, weights=weights) opt = Adam(lr=1e-3, decay=1e-3 / 200) model.compile(loss='mean_squared_error', metrics=['mean_absolute_percentage_error', 'mean_absolute_error', 'mean_squared_error'], @@ -92,4 +62,4 @@ if __name__ == "__main__": model.summary() model = train_model(trainX, trainY, testX, testY, model, show_plot=True, epochs=500, batch_size=32) evaluate_regression_model(model, testX, testY, normalizer, show_plot=True) - model.save(final_model_weight) + model.save('regression_model_combined.h5')