├── LICENSE
├── README.md
├── alexnet_data.py
├── alexnet_keras.py
├── alexnet_run.py
├── fig
├── 1.png
├── 2.png
├── 3.png
├── cor.png
├── cor_uncor.png
└── uncor.png
├── generate_corr_data.py
├── generate_fake_data.py
├── lstm_keras.py
└── requirements.txt
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Using CNN on 2D Images of Time Series
2 | Because too often time series are fed as 1-D vectors Recurrent Neural Networks (Simple RNN, LSTM, GRU..).
3 |
4 |
5 | Will this time series go up or down in the next time frame?
6 |
7 |
8 |
9 |
10 | Which plot contains highly correlated time series?
11 |
12 |
13 |
14 | ## Possible advantages/drawbacks of such approach:
15 |
16 | ### Advantages
17 | - Almost no pre-processing. Feed raw pixels (be careful of the resolution of the image though)!
18 | - We can add several time series on the same plot or on a different plot and concatenate both images.
19 | - Conv Nets have the reputation of being more stable than Recurrent Neural Networks for many tasks (WaveNet is just one example).
20 | - No vanishing/exploding gradient! Even though, it's less true with LSTM.
21 |
22 | ### Drawbacks
23 | - Input is much bigger than feeding 1-D vectors. Actually it's very very sparse!
24 | - Training will be undoubtedly slower.
25 | - Sometimes it's also hard to train very big conv nets (VGG19 is such an example).
26 |
27 |
28 | ## Let's get started!
29 |
30 | ### Fake data generation
31 | ```
32 | git clone https://github.com/philipperemy/tensorflow-cnn-time-series.git
33 | cd tensorflow-cnn-time-series/
34 | sudo pip3 install -r requirements.txt
35 | python3 generate_data.py
36 | ```
37 |
38 | ### Start the training of the CNN (AlexNet is used here)
39 | ```
40 | python3 alexnet_run.py
41 | ```
42 |
43 | ### Toy example: Binary classification of images of time series
44 |
45 | We consider the following binary classification problem of time series:
46 | - *UP*: If the time series went up in the next time frame.
47 | - *DOWN*: if the time series went down.
48 |
49 | Because it's impossible to classify pure random time series into two distinct classes, we expect a 50% accuracy on the testing set and the model to overfit on the training set. Here are some examples that we feed to the conv net:
50 |
51 |
52 |

53 |
54 |
55 |
56 |

57 |
58 |
59 | ### Keep in mind that LSTM is also good!
60 | ```
61 | python3 lstm_keras.py # on correlation classification task
62 | [...]
63 | [test] loss= 0.021, acc= 100.00
64 | [test] loss= 0.004, acc= 100.00
65 | [test] loss= 0.004, acc= 100.00
66 | ```
67 |
--------------------------------------------------------------------------------
/alexnet_data.py:
--------------------------------------------------------------------------------
1 | import matplotlib
2 |
3 | matplotlib.use('Agg')
4 |
5 | from random import shuffle
6 |
7 | import errno
8 | import os
9 | from glob import glob
10 | import skimage.io
11 | import skimage.transform
12 |
13 | import matplotlib.pyplot as plt
14 | import numpy as np
15 |
16 | DATA_FOLDER = '/tmp/cnn-time-series/'
17 |
18 |
19 | def load_image(path):
20 | try:
21 | img = skimage.io.imread(path).astype(float)
22 | # TODO http://scikit-image.org/docs/dev/api/skimage.color.html rgb2gray
23 | # TODO cropping.
24 | img = skimage.transform.resize(img, (224, 224), mode='constant')
25 | except:
26 | return None
27 | if img is None:
28 | return None
29 | if len(img.shape) < 2:
30 | return None
31 | if len(img.shape) == 4:
32 | return None
33 | if len(img.shape) == 2:
34 | img = np.tile(img[:, :, None], 3)
35 | if img.shape[2] == 4:
36 | img = img[:, :, :3]
37 | if img.shape[2] > 4:
38 | return None
39 |
40 | img /= 255.
41 | return img
42 |
43 |
44 | def next_batch(x_y, index, batch_size):
45 | has_reset = False
46 | index *= batch_size
47 | updated_index = index % len(x_y)
48 | if updated_index + batch_size > len(x_y):
49 | updated_index = 0
50 | has_reset = True
51 | beg = updated_index
52 | end = updated_index + batch_size
53 | output = x_y[beg:end]
54 | x = np.array([e[0] for e in output])
55 | y = np.array([e[1] for e in output])
56 | return x, y, has_reset
57 |
58 |
59 | def read_dataset(folder, max_num_training_images, max_num_testing_images, class_mapper):
60 | training_inputs = read_set(folder, 'train', max_num_training_images, class_mapper)
61 | testing_inputs = read_set(folder, 'test', max_num_testing_images, class_mapper)
62 | return training_inputs, testing_inputs
63 |
64 |
65 | def read_set(folder, phase, max_num_of_images, class_mapper):
66 | images_folder = os.path.join(folder, phase)
67 | inputs = []
68 | list_images = glob(images_folder + '/**/*.png')
69 | shuffle(list_images)
70 | for i, image_name in enumerate(list_images):
71 | if len(inputs) >= max_num_of_images:
72 | break
73 | class_name = image_name.split('/')[-2]
74 | if i % 100 == 0:
75 | print(i)
76 | inputs.append([load_image(image_name), class_mapper[class_name]]) # TODO make them 256x256
77 | return inputs
78 |
79 |
80 | def compute_mean_not_optimised(inputs):
81 | matrix_all_images = []
82 | for image, label in inputs:
83 | matrix_all_images.append(image)
84 | return np.mean(np.array(matrix_all_images), axis=0)
85 |
86 |
87 | def compute_mean(inputs):
88 | image_mean = np.array(inputs[0][0])
89 | image_mean.fill(0)
90 | for i, (image, label) in enumerate(inputs):
91 | image_mean += image
92 | if i % 100 == 0:
93 | print(i)
94 | return image_mean / len(inputs)
95 |
96 |
97 | def subtract_mean(inputs, mean_image):
98 | new_inputs = []
99 | for image, label in inputs:
100 | new_inputs.append([image - mean_image, label])
101 | return new_inputs
102 |
103 |
104 | def mkdir_p(path):
105 | try:
106 | os.makedirs(path)
107 | except OSError as exc:
108 | if exc.errno == errno.EEXIST and os.path.isdir(path):
109 | pass
110 | else:
111 | raise
112 |
113 |
114 | def generate_time_series(arr, filename):
115 | generate_multi_time_series([arr], filename)
116 |
117 |
118 | def generate_multi_time_series(arr_list, filename):
119 | fig = plt.figure()
120 | for arr in arr_list:
121 | plt.plot(arr)
122 | plt.savefig(filename)
123 | plt.close(fig)
124 |
--------------------------------------------------------------------------------
/alexnet_keras.py:
--------------------------------------------------------------------------------
1 | import tensorflow as tf
2 | from keras.layers.convolutional import Conv2D, MaxPooling2D
3 | from keras.layers.core import Dense, Activation, Flatten
4 |
5 |
6 | # import tensorflow as tf
7 | # tf.python.control_flow_ops = tf # some hack to get tf running with Dropout
8 |
9 | # 224x224
10 | # https://gist.github.com/JBed/c2fb3ce8ed299f197eff
11 | def alex_net_keras(x, num_classes=2, keep_prob=0.5):
12 | x = Conv2D(92, kernel_size=(11, 11), strides=(4, 4), padding='same')(x) # conv 1
13 | # x = BatchNormalization()(x)
14 | x = Activation('relu')(x)
15 | # LRN is missing here - Caffe.
16 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x) # pool 1
17 |
18 | x = Conv2D(256, kernel_size=(5, 5), padding='same')(x) # miss group and pad param # conv 2
19 | # x = BatchNormalization()(x)
20 | x = Activation('relu')(x)
21 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x) # pool 2
22 |
23 | x = Conv2D(384, kernel_size=(3, 3), padding='same')(x) # conv 3
24 | # x = BatchNormalization()(x)
25 | x = Activation('relu')(x)
26 | # x = MaxPooling2D(pool_size=(3, 3))(x)
27 |
28 | x = Conv2D(384, kernel_size=(3, 3), padding='same')(x) # conv 4
29 | # x = BatchNormalization()(x)
30 | x = Activation('relu')(x)
31 | # x = MaxPooling2D(pool_size=(3, 3))(x)
32 |
33 | x = Conv2D(256, kernel_size=(3, 3), padding='same')(x) # conv 5
34 | # x = BatchNormalization()(x)
35 | x = Activation('relu')(x)
36 |
37 | x = Flatten()(x)
38 | x = Dense(4096, kernel_initializer='normal')(x) # fc6
39 | # dropout 0.5
40 | x = tf.nn.dropout(x, keep_prob=keep_prob)
41 | # x = BatchNormalization()(x)
42 | x = Activation('relu')(x)
43 | x = Dense(4096, kernel_initializer='normal')(x) # fc7
44 | # dropout 0.5
45 | x = tf.nn.dropout(x, keep_prob=keep_prob)
46 | # x = BatchNormalization()(x)
47 | x = Activation('relu')(x)
48 | x = Dense(num_classes)(x)
49 | # x = BatchNormalization()(x)
50 | # x = Activation('softmax')(x)
51 | return x
52 |
--------------------------------------------------------------------------------
/alexnet_run.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 | import numpy as np
4 | import tensorflow as tf
5 |
6 | from alexnet_data import read_dataset, next_batch, compute_mean, subtract_mean
7 | from generate_fake_data import DATA_FOLDER
8 |
9 | if __name__ == '__main__':
10 |
11 | NUM_TRAINING_IMAGES = 1000
12 | NUM_TESTING_IMAGES = 1000
13 |
14 | names = os.listdir(os.path.join(DATA_FOLDER, 'train'))
15 | print(names)
16 | NUM_CLASSES = len(names)
17 | class_mapper = {names[0]: 0.0, names[1]: 1.0}
18 | print(class_mapper)
19 | BATCH_SIZE = 128
20 | HEIGHT = 224
21 | WIDTH = 224
22 | CHANNELS = 3
23 | LEARNING_RATE = 0.01
24 | data_percentage = 1
25 | num_training_images = data_percentage * NUM_TRAINING_IMAGES
26 | num_testing_images = data_percentage * NUM_TESTING_IMAGES
27 |
28 | print('read_dataset() start')
29 | training_inputs, testing_inputs = read_dataset(DATA_FOLDER, num_training_images, num_testing_images, class_mapper)
30 | print('read_dataset() done')
31 | print('compute_mean() start')
32 | mean_image = compute_mean(training_inputs)
33 | print('compute_mean() done')
34 | training_inputs = subtract_mean(training_inputs, mean_image)
35 | testing_inputs = subtract_mean(testing_inputs, mean_image)
36 | print(len(training_inputs), 'training inputs')
37 | print(len(testing_inputs), 'testing inputs')
38 |
39 | x = tf.placeholder(tf.float32, shape=[None, HEIGHT, WIDTH, CHANNELS])
40 | y = tf.placeholder(tf.int64, [None])
41 | keep_prob = tf.placeholder(tf.float32)
42 |
43 | from alexnet_keras import alex_net_keras
44 |
45 | logits = alex_net_keras(x, num_classes=len(names), keep_prob=keep_prob)
46 |
47 | cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))
48 |
49 | train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(cross_entropy)
50 |
51 | correct_prediction = tf.equal(tf.argmax(logits, 1), y)
52 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
53 |
54 | sess = tf.Session()
55 | sess.run(tf.global_variables_initializer())
56 |
57 | for i in range(int(1e9)):
58 | batch_xs, batch_ys, _ = next_batch(training_inputs, i, BATCH_SIZE)
59 | tr_loss, _ = sess.run([cross_entropy, train_step],
60 | feed_dict={x: batch_xs, y: batch_ys, keep_prob: 0.5})
61 | print('[TRAINING] #batch = {0}, tr_loss = {1:.3f}'.format(i, tr_loss))
62 | if i % 100 == 0:
63 | accuracy_list = []
64 | j = 0
65 | while True:
66 | batch_xt, batch_yt, reset = next_batch(testing_inputs, j, BATCH_SIZE)
67 | if reset:
68 | break
69 | te_loss, te_acc = sess.run([cross_entropy, accuracy],
70 | feed_dict={x: batch_xt, y: batch_yt, keep_prob: 1.0})
71 | print('[TESTING] #batch = {0}, te_loss = {1:.3f}, te_acc = {2:.3f}'.format(i, te_loss, te_acc))
72 | accuracy_list.append(te_acc)
73 | j += 1
74 | print('[ALL] total batches = {0} total mean accuracy on testing set = {1:.2f}'.format(i, np.mean(
75 | accuracy_list)))
76 |
--------------------------------------------------------------------------------
/fig/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipperemy/tensorflow-cnn-time-series/d975e822b824315dfc6fa11a5af0450e42c6b0bf/fig/1.png
--------------------------------------------------------------------------------
/fig/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipperemy/tensorflow-cnn-time-series/d975e822b824315dfc6fa11a5af0450e42c6b0bf/fig/2.png
--------------------------------------------------------------------------------
/fig/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipperemy/tensorflow-cnn-time-series/d975e822b824315dfc6fa11a5af0450e42c6b0bf/fig/3.png
--------------------------------------------------------------------------------
/fig/cor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipperemy/tensorflow-cnn-time-series/d975e822b824315dfc6fa11a5af0450e42c6b0bf/fig/cor.png
--------------------------------------------------------------------------------
/fig/cor_uncor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipperemy/tensorflow-cnn-time-series/d975e822b824315dfc6fa11a5af0450e42c6b0bf/fig/cor_uncor.png
--------------------------------------------------------------------------------
/fig/uncor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipperemy/tensorflow-cnn-time-series/d975e822b824315dfc6fa11a5af0450e42c6b0bf/fig/uncor.png
--------------------------------------------------------------------------------
/generate_corr_data.py:
--------------------------------------------------------------------------------
1 | import shutil
2 |
3 | import progressbar
4 | import scipy.stats as stats
5 |
6 | from alexnet_data import *
7 |
8 |
9 | def generate_two_correlated_time_series(size, rho):
10 | num_samples = size
11 | num_variables = 2
12 | cov = [[1.0, rho], [rho, 1.0]]
13 |
14 | L = np.linalg.cholesky(cov)
15 |
16 | uncorrelated = np.random.standard_normal((num_variables, num_samples))
17 | correlated = np.dot(L, uncorrelated)
18 | x, y = correlated
19 | rho, p_val = stats.pearsonr(x, y)
20 | return x, y, rho
21 |
22 |
23 | def generate():
24 | shutil.rmtree(DATA_FOLDER, ignore_errors=True)
25 | size = 1024
26 | total_num_images = 100000
27 | bar = progressbar.ProgressBar()
28 | for i in bar(range(total_num_images)):
29 | if i % 2 == 0:
30 | class_name = 'CORRELATED'
31 | rho = 0.8
32 | else:
33 | class_name = 'UNCORRELATED'
34 | rho = 0.0
35 |
36 | # more fun to consider non-stationary time series.
37 | # still the correlation holds almost surely.
38 | x_tr, y_tr, _ = generate_two_correlated_time_series(size, rho)
39 | x_tr = np.cumsum(x_tr)
40 | y_tr = np.cumsum(y_tr)
41 |
42 | x_te, y_te, _ = generate_two_correlated_time_series(size, rho)
43 | x_te = np.cumsum(x_te)
44 | y_te = np.cumsum(y_te)
45 |
46 | train_output_dir = os.path.join(DATA_FOLDER, 'train', class_name)
47 | mkdir_p(train_output_dir)
48 | generate_multi_time_series([x_tr, y_tr], os.path.join(train_output_dir, '{}.png'.format(i)))
49 |
50 | test_output_dir = os.path.join(DATA_FOLDER, 'test', class_name)
51 | mkdir_p(test_output_dir)
52 | generate_multi_time_series([x_te, y_te], os.path.join(test_output_dir, '{}.png'.format(i)))
53 |
54 |
55 | if __name__ == '__main__':
56 | generate()
57 |
--------------------------------------------------------------------------------
/generate_fake_data.py:
--------------------------------------------------------------------------------
1 | from alexnet_data import *
2 |
3 |
4 | def generate():
5 | for i in range(200):
6 | if i % 2 == 0:
7 | direction = 'UP'
8 | else:
9 | direction = 'DOWN'
10 | train_output_dir = os.path.join(DATA_FOLDER, 'train', direction)
11 | mkdir_p(train_output_dir)
12 | arr = np.cumsum(np.random.standard_normal(1024))
13 | generate_time_series(arr, os.path.join(train_output_dir, 'img_{}.png'.format(i)))
14 |
15 | test_output_dir = os.path.join(DATA_FOLDER, 'test', direction)
16 | mkdir_p(test_output_dir)
17 | # arr = np.cumsum(np.random.standard_normal(1024))
18 | generate_time_series(arr, os.path.join(test_output_dir, 'img_{}.png'.format(i)))
19 |
20 |
21 | if __name__ == '__main__':
22 | generate()
23 |
--------------------------------------------------------------------------------
/lstm_keras.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from keras.layers import Dense
3 | from keras.layers import LSTM
4 | from keras.models import Sequential
5 |
6 | from generate_corr_data import generate_two_correlated_time_series
7 |
8 | max_len = 1024
9 | batch_size = 512
10 |
11 |
12 | def next_batch():
13 | x = []
14 | y = []
15 | for i in range(batch_size // 2):
16 | x.append(generate_two_correlated_time_series(size=max_len, rho=0.8)[0:2])
17 | y.append(1.0)
18 |
19 | x.append(generate_two_correlated_time_series(size=max_len, rho=0)[0:2])
20 | y.append(0.0)
21 | return np.transpose(np.array(x), (0, 2, 1)), np.array(y)
22 |
23 |
24 | print('Build model...')
25 | model = Sequential()
26 | model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2, batch_input_shape=(batch_size, max_len, 2),
27 | return_sequences=True))
28 | model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
29 | model.add(Dense(128))
30 | model.add(Dense(128))
31 | model.add(Dense(1, activation='sigmoid'))
32 | print(model.summary())
33 |
34 | # try using different optimizers and different optimizer configs
35 | model.compile(loss='binary_crossentropy',
36 | optimizer='adam',
37 | metrics=['accuracy'])
38 |
39 | print('Train...')
40 | while True:
41 | x_train, y_train = next_batch()
42 | train_loss, train_acc = model.train_on_batch(x_train, y_train)
43 | print('[train] loss= {0:.3f}, acc= {1:.2f}'.format(train_loss, train_acc * 100))
44 |
45 | x_test, y_test = next_batch()
46 | test_loss, test_acc = model.test_on_batch(x_test, y_test)
47 | print('[test] loss= {0:.3f}, acc= {1:.2f}'.format(test_loss, test_acc * 100))
48 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | tensorflow
2 | Keras
3 | matplotlib
4 | numpy
5 | scikit_image
6 |
--------------------------------------------------------------------------------