Skip to content

Commit e423f8f

Browse files
committed
first commit
1 parent a0871a6 commit e423f8f

File tree

17 files changed

+5585
-0
lines changed

17 files changed

+5585
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Energy disaggregation demo featuring GCP products
2+
The popularization of IoT devices and the evolvement of machine learning technologies have brought tremendous opportunities for new businesses. We demonstrate how home appliances’ (e.g. kettle and washing machine) working status (on/off) can be inferred from gross power readings collected by a smart meter together with state-of-art machine learning techniques. An end-to-end demo system is developed entirely on Google Cloud Platform as shown in the following figure. It includes
3+
* Data collection and ingesting through Cloud IoT Core and Cloud Pub/Sub
4+
* Machine learning model development using Tensorflow and training using Cloud Machine Learning Engine (CMLE)
5+
* Machine Learning model serving using CMLE together with App Engine as frontend
6+
* Data visualization and exploration using Colab
7+
![system architecture](https://storage.googleapis.com/gcp_blog/img/arch.jpg)
8+
9+
## Steps to deploy the demo system
10+
11+
### Step 0. Prerequisite
12+
Before you follow the instructions below to deploy our demo system, you need a Google cloud project if you don't have one. You can find detailed instructions [here](https://cloud.google.com/dataproc/docs/guides/setup-project).
13+
14+
After you have created a google cloud project, follow the instructions below:
15+
```shell
16+
# clone the repository storing all the necessary codes
17+
git clone [REPO_URL]
18+
19+
cd e2e_demo
20+
21+
# remember your project's id in an environment variable
22+
GOOGLE_PROJECT_ID=[your-google-project-id]
23+
24+
# create and download a service account
25+
gcloud --project ${GOOGLE_PROJECT_ID} iam service-accounts create e2e-demo-sc
26+
gcloud --project ${GOOGLE_PROJECT_ID} projects add-iam-policy-binding ${GOOGLE_PROJECT_ID} \
27+
--member "serviceAccount:e2e-demo-sc@${GOOGLE_PROJECT_ID}.iam.gserviceaccount.com" \
28+
--role "roles/owner"
29+
gcloud --project ${GOOGLE_PROJECT_ID} iam service-accounts keys create e2e_demo_credential.json \
30+
--iam-account e2e-demo-sc@${GOOGLE_PROJECT_ID}.iam.gserviceaccount.com
31+
GOOGLE_APPLICATION_CREDENTIALS=${PWD}"/e2e_demo_credential.json"
32+
33+
# create a new GCS bucket if you don't have one
34+
BUCKET_NAME=[your-bucket-name]
35+
gsutil mb -p ${GOOGLE_PROJECT_ID} gs://${BUCKET_NAME}/
36+
```
37+
38+
You also need to enable the following APIs in the APIs & Services menu.
39+
* Cloud ML Engine API
40+
* Cloud IoT API
41+
* Cloud PubSub API
42+
43+
### Step 1. Deploy a trained ML model in Cloud ML Engine.
44+
You can download our trained model [here](https://storage.googleapis.com/gcp_blog/e2e_demo/model.tar) or you can train your own model using the `ml/start.sh`.
45+
Notice: you need to enable CLoud ML Engine API first.
46+
47+
If you are using our trained model:
48+
```shell
49+
# download our trained model
50+
# download using curl: curl -o model.tar https://storage.googleapis.com/gcp_blog/e2e_demo/model.tar
51+
wget https://storage.googleapis.com/gcp_blog/e2e_demo/model.tar --no-check-certificate
52+
tar xvf model.tar
53+
54+
# upload the model to your bucket
55+
gsutil cp -r model gs://${BUCKET_NAME}
56+
```
57+
58+
If you want to train your own model:
59+
```shell
60+
pip install ml/
61+
cd ml
62+
63+
# use one of the following commands and your model should be saved in your cloud storage bucket
64+
65+
# train locally with default parameter
66+
bash start.sh -l
67+
# train locally with specified parameters
68+
bash start.sh -l learning-rate=0.00001 lstm-size=128
69+
# train on Cloud ML Engine with default parameter
70+
bash start.sh -p ${GOOGLE_PROJECT_ID} -b ${BUCKET_NAME}
71+
# train on Cloud ML Engine with specified parameters
72+
bash start.sh -p ${GOOGLE_PROJECT_ID} -b ${BUCKET_NAME} learning-rate=0.00001 lstm-size=128
73+
# run hyper-parameter tuning on Cloud ML Engine
74+
bash start.sh -p ${GOOGLE_PROJECT_ID} -b ${BUCKET_NAME} -t
75+
```
76+
77+
Finally let's deploy our model to ML engine:
78+
```shell
79+
# Set up an appropriate region
80+
# Available regions: https://cloud.google.com/ml-engine/docs/tensorflow/regions
81+
REGION="your-application-region"
82+
83+
# create a model
84+
gcloud ml-engine models create EnergyDisaggregationModel \
85+
--regions ${REGION} \
86+
--project ${GOOGLE_PROJECT_ID}
87+
88+
# create a model version
89+
gcloud ml-engine versions create v01 \
90+
--model EnergyDisaggregationModel \
91+
--origin gs://${BUCKET_NAME}/model \
92+
--runtime-version 1.12 \
93+
--framework TensorFlow \
94+
--python-version 3.5 \
95+
--project ${GOOGLE_PROJECT_ID}
96+
```
97+
98+
### Step 2. Deploy server.
99+
Type in the following commands to start server in app engine.
100+
```shell
101+
cd e2e_demo/server
102+
cp ${GOOGLE_APPLICATION_CREDENTIALS} .
103+
echo " GOOGLE_APPLICATION_CREDENTIALS: '${GOOGLE_APPLICATION_CREDENTIALS##*/}'" >> app.yaml
104+
echo " GOOGLE_CLOUD_PROJECT: '${GOOGLE_PROJECT_ID}'" >> app.yaml
105+
106+
# deploy application engine, choose any region that suits and answer yes at the end.
107+
gcloud --project ${GOOGLE_PROJECT_ID} app deploy
108+
109+
# create a pubsub topic "data" and a subscription in the topic.
110+
# this is the pubsub between IoT devices and the server.
111+
gcloud --project ${GOOGLE_PROJECT_ID} pubsub topics create data
112+
gcloud --project ${GOOGLE_PROJECT_ID} pubsub subscriptions create sub0 \
113+
--topic=data --push-endpoint=https://${GOOGLE_PROJECT_ID}.appspot.com/upload
114+
115+
# create a pubsub topic "pred" and a subscription in the topic.
116+
# this is the pubsub between the server and a result utilizing client.
117+
gcloud --project ${GOOGLE_PROJECT_ID} pubsub topics create pred
118+
gcloud --project ${GOOGLE_PROJECT_ID} pubsub subscriptions create sub1 --topic=pred
119+
120+
# create BigQuery dataset and tables.
121+
bq --project_id ${GOOGLE_PROJECT_ID} mk \
122+
--dataset ${GOOGLE_PROJECT_ID}:EnergyDisaggregation
123+
bq --project_id ${GOOGLE_PROJECT_ID} load --autodetect \
124+
--source_format=CSV EnergyDisaggregation.ApplianceInfo \
125+
gs://gcp_blog/e2e_demo/appliance_info.csv
126+
bq --project_id ${GOOGLE_PROJECT_ID} load --autodetect \
127+
--source_format=CSV EnergyDisaggregation.ApplianceStatusGroundTruth \
128+
gs://gcp_blog/e2e_demo/appliance_status_ground_truth.csv
129+
bq --project_id ${GOOGLE_PROJECT_ID} mk \
130+
--table ${GOOGLE_PROJECT_ID}:EnergyDisaggregation.ActivePower \
131+
time:TIMESTAMP,device_id:STRING,power:INTEGER
132+
bq --project_id ${GOOGLE_PROJECT_ID} mk \
133+
--table ${GOOGLE_PROJECT_ID}:EnergyDisaggregation.Predictions \
134+
time:TIMESTAMP,device_id:STRING,appliance_id:INTEGER,pred_status:INTEGER,pred_prob:FLOAT
135+
```
136+
137+
### Step 3. Setup your Cloud IoT Client(s)
138+
Follow the instructions below to set up your client(s).
139+
Note: you need to enable the Cloud IoT API first.
140+
```shell
141+
# You need to specify the IDs for cloud iot registry and the devices you want.
142+
# See more details for permitted characters and sizes for each resource:
143+
# https://cloud.google.com/iot/docs/requirements#permitted_characters_and_size_requirements
144+
REGISTRY_ID="your-registry-id"
145+
DEVICE_IDS=("your-device-id1" "your-device-id2" ...)
146+
147+
# create an iot registry with created pubsub topic above
148+
gcloud --project ${GOOGLE_PROJECT_ID} iot registries create ${REGISTRY_ID} \
149+
--region ${REGION} --event-notification-config topic=data
150+
151+
# generates key pair for setting in Cloud IoT device.
152+
# The rs256.key generated by the following command would be used to create JWT.
153+
ssh-keygen -t rsa -b 4096 -f ./rs256.key
154+
# Press "Enter" twice
155+
openssl rsa -in ./rs256.key -pubout -outform PEM -out ./rs256.pub
156+
157+
# create multiple devices
158+
for device in ${DEVICE_IDS[@]}; do
159+
# create an iot device with generated public key and the registry.
160+
gcloud --project ${GOOGLE_PROJECT_ID} iot devices create ${device} \
161+
--region ${REGION} --public-key path=./rs256.pub,type=rsa-pem \
162+
--registry ${REGISTRY_ID} --log-level=debug
163+
done
164+
165+
# download root ca certificates for use in mqtt client communicated with iot server.
166+
# download using curl: curl -o roots.pem https://pki.goog/roots.pem
167+
wget https://pki.goog/roots.pem --no-check-certificate
168+
```
169+
170+
### Complete! Try the demo system in colab or locally.
171+
If you want to use colab, visit https://colab.research.google.com/ and you can import our notebooks either directly from Github or upload from your cloned repository. Follow the instructions in the notebooks and you should be able to reproduce our results.
172+
173+
If you want to run the demo locally:
174+
```
175+
cd notebook
176+
virtualenv env
177+
source env/bin/activate
178+
pip install jupyter
179+
jupyter notebook
180+
```
181+
182+
*notebook/EnergyDisaggregationDemo_View.ipynb* allows you to view raw power consumption data and our model's prediction results in almost real time. It pulls data from our server's pubsub topic for visualization. Fill in the necessary information in the *Configuration* block and run all the cells.
183+
184+
*notebook/EnergyDisaggregationDemo_Client.ipynb* simulates multiple smart meters by reading in power consumption data from a real world dataset and sends the readings to our server. All Cloud IoT Core related code resides in this notebook. Fill in the necessary information in the *Configuration* block and run all the cells, once you see messages being sent you should be able to see plots like the one shown below in *notebook/EnergyDisaggregationDemo_View.ipynb*.
185+
186+
![Demo system sample output](https://storage.googleapis.com/gcp_blog/img/demo03.gif)
187+
188+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trainingInput:
2+
scaleTier: CUSTOM
3+
masterType: standard_gpu
4+
runtimeVersion: "1.12"
5+
pythonVersion: "3.5"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
trainingInput:
2+
scaleTier: CUSTOM
3+
masterType: standard_gpu
4+
runtimeVersion: "1.12"
5+
pythonVersion: "3.5"
6+
hyperparameters:
7+
goal: MAXIMIZE
8+
hyperparameterMetricTag: f_measure
9+
maxTrials: 20
10+
maxParallelTrials: 2
11+
params:
12+
- parameterName: num-epochs
13+
type: INTEGER
14+
minValue: 5
15+
maxValue: 40
16+
- parameterName: num-layers
17+
type: INTEGER
18+
minValue: 1
19+
maxValue: 5
20+
- parameterName: learning-rate
21+
type: DOUBLE
22+
minValue: 0.000001
23+
maxValue: 0.0001
24+
- parameterName: dropout-rate
25+
type: DOUBLE
26+
minValue: 0.0
27+
maxValue: 0.5
28+
- parameterName: filter-prob
29+
type: DOUBLE
30+
minValue: 0.5
31+
maxValue: 1.0
32+
- parameterName: lstm-size
33+
type: INTEGER
34+
minValue: 128
35+
maxValue: 512
36+
- parameterName: seq-len
37+
type: DISCRETE
38+
discreteValues:
39+
- 5
40+
- 10
41+
- 15
42+
- 20
43+
- 30
44+
- 40
45+
- 50
46+
- parameterName: train-batch-size
47+
type: DISCRETE
48+
discreteValues:
49+
- 16
50+
- 32
51+
- 64
52+
- 128
53+
- 256
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from setuptools import find_packages
16+
from setuptools import setup
17+
18+
19+
REQUIRED_PACKAGES = [
20+
"tensorflow==1.12.0",
21+
"google-cloud-storage==1.13.2",
22+
"pandas==0.23.4",
23+
"scikit-learn==0.20.2",
24+
]
25+
26+
27+
setup(
28+
name='energy-disaggregation',
29+
version='0.1',
30+
author='Google',
31+
install_requires=REQUIRED_PACKAGES,
32+
packages=find_packages(),
33+
include_package_data=True,
34+
requires=[]
35+
)

0 commit comments

Comments
 (0)