'''
author:
cbz
'''
import keras
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense
from keras.callbacks import ReduceLROnPlateau
from process import makedata
from util import *
import pandas as pd
import numpy as np
from queue import Queue
import time
import json
'''
train method
1:311
2:bikeinflow
3:bikeoutflow
4:taxiinflow
5:taxiouflow
'''
def train1(region):
path = 'model/' + str(region) + '311.h5'
file = open(path,'w')
rawdata = pd.read_csv('model/' + str(region) + '311.csv').values
if len(rawdata) == 0:
print('Region' + str(region) + ' don\'t have data about 311!\nUse the unified model!')
return
dic = makedata(region,'model/' + str(region) + '311.csv')
x = np.array(dic['x'])
y = np.array(dic['y'])
model = None
if os.path.getsize('model/' + str(region) + '311.h5') == 0:
model = Sequential()
model.add(Dense(10,activation='sigmoid',use_bias=True))
model.add(Dense(1,use_bias=True))
model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.RMSprop(0.1))
else:
model = load_model('model/' + str(region) + '311.h5')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')
model.fit(x=x, y=y, epochs=100, validation_split=0.1, verbose = 0, shuffle=True, batch_size=128,
callbacks=[reduce_lr])
model.save('model/' + str(region) + '311.h5')
print(str(region) + '-311:100 epoches have been trained over!')
return
def train2(region):
# path = 'model/' + str(region) + '/bikeinflow.h5'
# file = open(path, 'w')
rawdata = pd.read_csv('model/' + str(region) + '/bikeinflow.csv').values
if len(rawdata) == 0:
print('Region' + str(region) + ' don\'t have data about bikeinflow!\nUse the unified model!')
# return
dic = makedata(region, 'model/' + str(region) + '/bikeinflow.csv')
x = np.array(dic['x'])
y = np.array(dic['y'])
model = None
if os.path.getsize('model/' + str(region) + '/bikeinflow.h5') == 0:
print('new model')
model = Sequential()
model.add(Dense(10, input_shape=(1,), activation='sigmoid', use_bias=True))
model.add(Dense(1, use_bias=True))
model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.RMSprop(0.1))
else:
model = load_model('model/' + str(region) + '/bikeinflow.h5')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')
model.fit(x=x, y=y, epochs=100, validation_split=0.1, verbose = 0, shuffle=True, batch_size=128,
callbacks=[reduce_lr])
model.save('model/' + str(region) + '/bikeinflow.h5')
print(str(region) + '-bikeinflow:100 epoches have been trained over!')
return
def train3(region):
# path = 'model/' + str(region) + '/bikeoutflow.h5'
# file = open(path, 'w')
rawdata = pd.read_csv('model/' + str(region) + '/bikeoutflow.csv').values
if len(rawdata) == 0:
print('Region' + str(region) + ' don\'t have data about bikeoutflow!\nUse the unified model!')
# return
dic = makedata(region, 'model/' + str(region) + '/bikeoutflow.csv')
x = np.array(dic['x'])
y = np.array(dic['y'])
model = None
if os.path.getsize('model/' + str(region) + '/bikeoutflow.h5') == 0:
model = Sequential()
model.add(Dense(10, input_shape=(1,),activation='sigmoid', use_bias=True))
model.add(Dense(1, use_bias=True))
model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.RMSprop(0.1))
else:
model = load_model('model/' + str(region) + '/bikeoutflow.h5')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')
model.fit(x=x, y=y, epochs=100, validation_split=0.1, verbose = 0, shuffle=True, batch_size=128,
callbacks=[reduce_lr])
model.save('model/' + str(region) + '/bikeoutflow.h5')
print(str(region) + '-bikeoutflow:100 epoches have been trained over!')
return
def train4(region):
# path = 'model/' + str(region) + '/taxiinflow.h5'
# file = open(path, 'w')
rawdata = pd.read_csv('model/' + str(region) + '/taxiinflow.csv').values
if len(rawdata) == 0:
print('Region' + str(region) + ' don\'t have data about taxiinflow!\nUse the unified model!')
# return
dic = makedata(region, 'model/' + str(region) + '/taxiinflow.csv')
x = np.array(dic['x'])
y = np.array(dic['y'])
model = None
if os.path.getsize('model/' + str(region) + '/taxiinflow.h5') == 0:
model = Sequential()
model.add(Dense(10, input_shape=(1,),activation='sigmoid', use_bias=True))
model.add(Dense(1, use_bias=True))
model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.RMSprop(0.1))
else:
model = load_model('model/' + str(region) + '/taxiinflow.h5')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')
model.fit(x=x, y=y, epochs=100, validation_split=0.1, verbose = 0, shuffle=True, batch_size=128,
callbacks=[reduce_lr])
model.save('model/' + str(region) + '/taxiinflow.h5')
print(str(region) + '-taxiinflow:100 epoches have been trained over!')
return
def train5(region):
# path = 'model/' + str(region) + '/taxioutflow.h5'
# file = open(path, 'w')
rawdata = pd.read_csv('model/' + str(region) + '/taxioutflow.csv').values
if len(rawdata) == 0:
print('Region' + str(region) + ' don\'t have data about taxioutflow!\nUse the unified model!')
# return
dic = makedata(region, 'model/' + str(region) + '/taxioutflow.csv')
x = np.array(dic['x'])
y = np.array(dic['y'])
model = None
if os.path.getsize('model/' + str(region) + '/taxioutflow.h5') == 0:
model = Sequential()
model.add(Dense(10, input_shape=(1,),activation='sigmoid', use_bias=True))
model.add(Dense(1, use_bias=True))
model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.RMSprop(0.1))
else:
model = load_model('model/' + str(region) + '/taxioutflow.h5')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')
model.fit(x=x, y=y, epochs=100, validation_split=0.1, verbose = 0, shuffle=True, batch_size=128,
callbacks=[reduce_lr])
model.save('model/' + str(region) + '/taxioutflow.h5')
print(str(region) + '-taxioutflow:100 epoches have been trained over!')
return
def train(region = 0,all = False):
if all == True:
for i in range(1,863):
# train1(i)
train2(i)
train3(i)
train4(i)
train5(i)
print('Have trained all the models in the dataset!')
return
# train1(region)
train2(region)
train3(region)
train4(region)
train5(region)
print('Finished the task to train region' + str(region) + '!')
return
'''
serve method
'''
def serve():
demo = demonstrater()
demo.run()
return
'''
一个服务对象
'''
class demonstrater():
def __init__(self):
self.predictvaluestaxiinflow = [Queue() for x in range(862)]
self.realvaluestaxiinflow = [Queue() for x in range(862)]
self.predictvaluestaxioutflow = [Queue() for x in range(862)]
self.realvaluestaxioutflow = [Queue() for x in range(862)]
self.predictvaluesbikeinflow = [Queue() for x in range(862)]
self.realvaluesbikeinflow = [Queue() for x in range(862)]
self.predictvaluesbikeoutflow = [Queue() for x in range(862)]
self.realvaluesbikeoutflow = [Queue() for x in range(862)]
self.timestamp = 1388505600
self.endtime = self.timestamp + 30 * 24 * 3600
self.id = 0
print('初始化完成!')
return
'''
从时间戳获得日期
日期格式统一为%Y-%m-%d %H:%M:%S
'''
def getdate(self):
return time.strftime