Contents


Developing cognitive IoT solutions for anomaly detection by using deep learning, Part 2

Generating data for anomaly detection

Use Node-RED to create a test data generator

Comments

Content series:

This content is part # of 5 in the series: Developing cognitive IoT solutions for anomaly detection by using deep learning, Part 2

Stay tuned for additional content in this series.

This content is part of the series:Developing cognitive IoT solutions for anomaly detection by using deep learning, Part 2

Stay tuned for additional content in this series.

Every data project starts with data. Data is a very broad term. It can be structured or unstructured, big or small, fast or slow, and accurate or noisy.

I've written a series of articles on deep learning and developing cognitive IoT solutions starting with Introducing deep learning and long-short term memory networks. The next articles are about using Deeplearning4j, ApacheSystemML, and TensorFlow (TensorSpark) for anomaly detection. My use case is anomaly detection for IoT time-series data from vibration (accelerometer) sensor data. To effectively demo the process of creating a deep learning solution on these different technologies, I need data. I need structured, fast, and big data, which can be noisy too.

To have a simple framework for creating data, I've written a test data simulator, which is part of a bigger time series and machine learning toolkit.

This simulator generates data from sampling various physical models, and you can decide on the degree of noise and switch between different states (healthy and broken) of the physical model for anomaly detection and classification tasks.

For now, I've implemented the Lorenz Attractor model. This is a very simple, but still very interesting, physical model. Lorenz was one of the pioneers of chaos theory and he was able to show that a very simple model that consists of just three equations and four model parameters can create a chaotic system that is highly sensitive to initial conditions and that also oscillates between multiple semi-stable states where state transitions are very hard to predict.

I'm using Node-RED as the runtime platform for the simulator because it is a very fast way of implementing data-centric applications. Node-RED is open source and runs entirely on Node.js. If you want to learn more on Node-RED, check out this excellent tutorial.

Because the data simulator is completely implemented as a Node-RED flow, we can use Node-RED from the IoT Starter Boilerplate on the IBM Cloud. Of course, the data simulator can run on any Node-RED instance, even on a Raspberry Pi, where it can be used to simulate sensor data on the edge.

Creating the test data simulator

While it was challenging to create the test data simulator, you can get the simulator up and running in four main steps:

  1. Deploy the Node-RED IoT Starter boilerplate to the IBM Cloud.
  2. Deploy the test data simulator flow.
  3. Test the test data simulator.
  4. Get the IBM Watson IoT Platform credentials to consume the data using MQTT from any place in the world.

Before you get started, you'll need an IBM Cloud account. (Sign up for an IBM Cloud Lite account, a free account that never expires.)

  1. Create an IBM Cloud app using the Internet of Things Platform Starter. If you are not logged in to IBM Cloud, log in.
  2. Name your application a unique name, and click Create.
    Note: I used romeoiotstarter123 for my app to be sure it was a unique name. The app name becomes part of the URL where your application is accessible. My URL, for example, is romeoiotstarter123.mybluemix.net.
  3. In the left menu, click Connections.
  4. On the Internet of Things Platform tile, click View credentials.
  5. Write down the values of the following properties as you need them later when you work with one of the three technologies (DeepLearning4j, ApacheSystemML, and TensorFlow (TensorSpark)):
    • org
    • apiKey
    • apiToken
  6. Click Close.
  7. Wait until your application is running, and then click Visit App URL.

    Note: If you get a "404 - No route defined" error, reload the page in a couple of minutes. This error is a known issue in the Cloud Foundry open source cloud platform component and occasionally occurs if IBM faces high workloads. Cloud Foundry communicates asynchronously between it's components, and therefore the UI thinks that the app is running but the load balancer has not been updated.

  8. Before you can access and open your Node-RED flow editor, you must secure your Node-RED editor.
    1. Click Next.
    2. Set a user name a password.
      Note: Select the check box to share your application with others. Not selecting the check box will keep your implementation private.
    3. Click Next.
    4. Click Finish.
  9. Click Go to your Node-RED flow editor.
  10. Log in with the user name and password you've just created.
  11. Using your mouse, select all the nodes in Flow 1; click the Delete key to empty it.
  12. From the upper-right menu, click Import > Clipboard.
  13. Open this simulatorflow.json file in my developerWorks GitHub repo; copy the JSON object to the clipboard.
  14. On the Import nodes window, paste the JSON object to the text field, and click Import.

    Note: Make sure that you are pasting a JSON document from your clipboard and not HTML. In its next version, NodeRED will have GIT integration, so this step will become easier.

    The following flow is displayed in the Flow 1 tab.

  15. Click Deploy. The message "Successfully deployed" will display.

The debug tab displays the generated messages. Congratulations! Your Test Data Generator is working.

Understanding this Node-RED flow

You've got it working, but what is going on in this Node-RED flow?

Consider the node labeled with the word timestamp.

This node is an inject node and it is generating messages in defined intervals. This is very useful as a starting point for our simulator. In a real-life scenario, this node would be replaced with some nodes that are connected to accelerometer sensors. Because we are generating the accelerometer values using the Lorenz Attractor Model, we can ignore the timestamp payload on the messages and only react on the message object itself, which we will see later.

Double-click the timestamp node. Notice the sample rate generates 100 messages per second (or a sampling rate of 100 Hz).

Note: The sampling rate has to be twice as high as the highest frequency that we want to capture because of the Nquist theorem. While 100 Hz is actually far too low, for the sake of this tutorial, it is high enough. In a real-world scenario, you should sample at 20 or 40 kHz (every 0.01 second is equal to 100 Hz).

Next, look at the function node. It is the heart of the simulator.

Double-click this node and see the following function code:

var h = context.global.get('h')||0.008;
var a = context.global.get('a')||10;
var b = context.global.get('b')||28;
var c = context.global.get('c')||8/3;
var x = context.global.get('x')||2;
var y = context.global.get('y')||3;
var z = context.global.get('z')||4;

x+=h*a*(y-x)
y+=h*(x*(b-z)-y)
z+=h*(x*y-c*z)

context.global.set("x",x);
context.global.set("y",y);
context.global.set("z",z);
msg.payload={};
msg.payload.x=x;
msg.payload.y=y;
msg.payload.z=z;
return msg;

Note: The initial parameters of the model are h, a, b and c. We also initialize x, y, and z to some values; the equations are the actual model. They are dependent on h, a, b, c, x, y, and z. In every time step (currently 100 per second), the model is advanced one step into the future because x, y, and z are updated using values from constants h, a, b, and c and also from previous x, y, and z values.

You need to set a limit on the output for two reasons:

  • At the current sample rate (100 messages per second) you'll using up the free 200 MB per month on the Watson IoT Platform within a couple of hours.
  • The downstream analysis might not be able to cope with this data rate.

Now let's look at the limit to max 3000 function node. Currently, the maximum is set to 30 seconds worth of data using a simple count.

Double-click the node to see the function code:

var count = context.global.get('count') || 0;
count += 1;
context.global.set('count',count);
if (count <= 3000) {
   return msg; 
}

Now, consider the reset node. The function node associated with this node is set to send the next 30 seconds worth of data to the message queue.

Double-click the function node. It is implemented as follows:

context.global.set('count',0);
msg.payload=context.global.get('count');
return msg;

The next to last steps are to switch this simulator between the broken and healthy states. To simulate faulty or broken data we can click the function node associated with the broken inject node.

The only thing this node does is update the Lorenz Attractor model constants.

context.global.set('h',0.008);
context.global.set('a',30);
context.global.set('b',128);
context.global.set('c',28/3);

return msg;

And, of course, take a look at the function to switch it back to a healthy state.

context.global.set('h',0.008);
context.global.set('a',10);
context.global.set('b',28);
context.global.set('c',8/3);

return msg;

Last, but not least, let's look at how this data travels to the IBM Watson IoT Platform's MQTT message broker.

You can leave the configuration as it is and the credentials are injected for you using Cloud Foundry running on IBM Cloud.

Conclusion

You've successfully deployed a test data simulator creating a time series of events sampled from a physical model. You can also switch between two states (healthy and broken) for anomaly detection and classification.

You can now use this test data in the other tutorials in this series. You'll be able to develop deep learning cognitive IoT solutions for anomaly detection with Deeplearning4j, ApacheSystemML, and TensorFlow (TensorSpark).


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Internet of Things, Cognitive computing, Data and analytics
ArticleID=1047848
ArticleTitle=Developing cognitive IoT solutions for anomaly detection by using deep learning, Part 2: Generating data for anomaly detection
publish-date=07192017