alifthi commited on
Commit
692fdae
·
1 Parent(s): 0ee2503

HousePricePrediction: model class

Browse files
Files changed (1) hide show
  1. HousePricePrediction.py +134 -0
HousePricePrediction.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
3
+ import tensorflow as tf
4
+ from matplotlib import pyplot as plt
5
+ from tensorflow.keras import layers as ksl
6
+ import numpy as np
7
+ import pandas as pd
8
+
9
+ class Model():
10
+ def __init__(self,loadWeights = False,loadModel = False,weightAddr = None,modelAddr=None):
11
+ if loadModel:
12
+ self.net = self.loadModel(modelAddr)
13
+ elif not(loadModel and loadWeights):
14
+ self.net = self.buildModel()
15
+ elif loadWeights:
16
+ self.net = self.loadWeights()
17
+ self.loadWeights(weightAddr)
18
+ else :
19
+ print('[Error] incompatible inputs !')
20
+
21
+ self.const = None
22
+
23
+
24
+
25
+
26
+ def myLoss(y_true,y_pred):
27
+ return tf.reduce_mean(tf.square(y_true - y_pred),axis = -1)
28
+ def compile(self,loss,metrics,optim):
29
+ self.net.compile(loss = loss,optimizer = optim ,metrics = metrics)
30
+
31
+ def train(self,e,b,Data,label):
32
+ hist = self.net.fit(Data,label,epochs = e,batch_size = b)
33
+ return hist
34
+
35
+ def buildModel(self):
36
+ inpIm1 = ksl.Input((128,128,3),name = 'image1Input')
37
+ inpIm2 = ksl.Input((128,128,3),name = 'image2Input')
38
+ inpIm3 = ksl.Input((128,128,3),name = 'image3Input')
39
+ inpIm4 = ksl.Input((128,128,3),name = 'image4Input')
40
+
41
+ x = ksl.Conv2D(32,kernel_size=3,strides=1,padding='same')(inpIm1)
42
+ x = ksl.Lambda(self.relu)(x)
43
+ x = ksl.MaxPool2D([2,2])(x)
44
+ x = ksl.Conv2D(64,kernel_size=3,padding='same')(x)
45
+ x = ksl.Lambda(self.relu)(x)
46
+ x = ksl.MaxPool2D([2,2])(x)
47
+ x = ksl.Conv2D(128,kernel_size=3,padding='same')(x)
48
+ x = ksl.Lambda(self.relu)(x)
49
+ x = ksl.MaxPool2D([2,2])(x)
50
+
51
+ w = ksl.Conv2D(32,kernel_size=3,strides=1,padding='same')(inpIm2)
52
+ w = ksl.Lambda(self.relu)(w)
53
+ w = ksl.MaxPool2D([2,2])(w)
54
+ w = ksl.Conv2D(64,kernel_size=3,padding='same')(w)
55
+ w = ksl.Lambda(self.relu)(w)
56
+ w = ksl.MaxPool2D([2,2])(w)
57
+ w = ksl.Conv2D(128,kernel_size=3,padding='same')(w)
58
+ w = ksl.Lambda(self.relu)(w)
59
+ w = ksl.MaxPool2D([2,2])(w)
60
+
61
+ y = ksl.Conv2D(32,kernel_size=3,strides=1,padding='same')(inpIm3)
62
+ y=ksl.Lambda(self.relu)(y)
63
+ y = ksl.MaxPool2D([2,2])(y)
64
+ y = ksl.Conv2D(64,kernel_size=3,padding='same')(y)
65
+ y = ksl.Lambda(self.relu)(y)
66
+ y = ksl.MaxPool2D([2,2])(y)
67
+ y = ksl.Conv2D(128,kernel_size=3,padding='same')(y)
68
+ y = ksl.Lambda(self.relu)(y)
69
+ y = ksl.MaxPool2D([2,2])(y)
70
+
71
+ z = ksl.Conv2D(32,kernel_size=3,strides=1,padding='same')(inpIm4)
72
+ z = ksl.MaxPool2D([2,2])(z)
73
+ z = ksl.Conv2D(64,kernel_size=3,padding='same')(z)
74
+ z = ksl.MaxPool2D([2,2])(z)
75
+ z = ksl.Conv2D(64,kernel_size=3,padding='same')(z)
76
+ z = ksl.MaxPool2D([2,2])(z)
77
+
78
+
79
+ inpTxtFeatures = ksl.Input((4,),name = 'numFeatureInput')
80
+ t = ksl.Dense(1024,'relu')(inpTxtFeatures)
81
+ t = ksl.Dense(512,'relu')(t)
82
+ t = ksl.Dense(256,'relu')(t)
83
+ t = ksl.Reshape((16,16,1))(t)
84
+
85
+
86
+
87
+ concatInput = ksl.concatenate([w,x,y,z,t],axis=3)
88
+ out = ksl.Conv2D(128,kernel_size=3,padding='same')(concatInput)
89
+ out = ksl.MaxPool2D([2,2])(out)
90
+ out = ksl.Conv2D(256,kernel_size=3,padding='same')(out)
91
+ out = ksl.MaxPool2D([2,2])(out)
92
+ out = ksl.Flatten()(out)
93
+ out = ksl.Dense(128,'relu')(out)
94
+ out = ksl.Dense(64,'relu')(out)
95
+ out = ksl.Dense(1,'linear')(out)
96
+ net = tf.keras.Model(inputs = [inpIm1,inpIm2,inpIm3,inpIm4,inpTxtFeatures],outputs = out)
97
+ return net
98
+ def predict(self,inputList,const):
99
+ numericFeatur = inputList[4:]
100
+ numericFeatur = pd.DataFrame(numericFeatur)
101
+ max = numericFeatur.max()
102
+ numericFeatur = numericFeatur/max
103
+ images = inputList[:4]
104
+ images = [self.preProcess(im) for im in images]
105
+ pred = self.net.predict(np.concatenate((images,numericFeatur.to_numpy())))
106
+ return pred*const
107
+
108
+ def saveModel(self):
109
+ self.net.save('./Models/model.h5')
110
+ self.net.save_weights('./Models/modelWeights.h5')
111
+ def loadWeights(self,weightAddr):
112
+ net = self.buildModel()
113
+ net.load_weghts(weightAddr)
114
+ return net
115
+ @staticmethod
116
+ def loadModel(modelAddr):
117
+ net = tf.kerasl.models.load_model(modelAddr)
118
+ return net
119
+
120
+
121
+ return image
122
+ @staticmethod
123
+ def relu(x):
124
+ return tf.maximum( 0.0,x)
125
+
126
+ @staticmethod
127
+ def plotHistory(Hist):
128
+ # plot History
129
+ plt.plot(Hist.history['loss'])
130
+ plt.title('model loss')
131
+ plt.show()
132
+ plt.plot(Hist.history['mse'])
133
+ plt.title('model mse')
134
+ plt.show()