Guest User

Untitled

a guest
Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. import gc
  2. import os
  3. import shutil
  4. import tracemalloc
  5. from pathlib import Path
  6.  
  7. import numpy as np
  8.  
  9. from keras import backend as K
  10. from keras import callbacks
  11. from keras.layers import BatchNormalization
  12. from keras.layers import Input, Dense, Dropout, Layer
  13. from keras.models import Model
  14. from keras.utils import np_utils
  15. from keras.utils.generic_utils import to_list
  16. from keras.utils.generic_utils import unpack_singleton
  17. from keras.utils.test_utils import get_test_data
  18.  
  19.  
  20. input_dim = 2
  21. num_hidden = 4
  22. num_classes = 2
  23. batch_size = 5
  24. train_samples = 20
  25. test_samples = 20
  26.  
  27.  
  28. def data_generator(x, y, batch_size):
  29. x = to_list(x)
  30. y = to_list(y)
  31. max_batch_index = len(x[0]) // batch_size
  32. i = 0
  33. while 1:
  34. x_batch = [array[i * batch_size: (i + 1) * batch_size] for array in x]
  35. x_batch = unpack_singleton(x_batch)
  36.  
  37. y_batch = [array[i * batch_size: (i + 1) * batch_size] for array in y]
  38. y_batch = unpack_singleton(y_batch)
  39. yield x_batch, y_batch
  40. i += 1
  41. i = i % max_batch_index
  42.  
  43.  
  44. # Changing the default arguments of get_test_data.
  45. def get_data_callbacks(num_train=train_samples,
  46. num_test=test_samples,
  47. input_shape=(input_dim,),
  48. classification=True,
  49. num_classes=num_classes):
  50. return get_test_data(num_train=num_train,
  51. num_test=num_test,
  52. input_shape=input_shape,
  53. classification=classification,
  54. num_classes=num_classes)
  55.  
  56.  
  57. def investigate_TensorBoard(tmpdir, update_freq):
  58. np.random.seed(np.random.randint(1, 1e7))
  59. filepath = str(tmpdir / 'logs')
  60.  
  61. (X_train, y_train), (X_test, y_test) = get_data_callbacks()
  62. y_test = np_utils.to_categorical(y_test)
  63. y_train = np_utils.to_categorical(y_train)
  64.  
  65. class DummyStatefulMetric(Layer):
  66.  
  67. def __init__(self, name='dummy_stateful_metric', **kwargs):
  68. super(DummyStatefulMetric, self).__init__(name=name, **kwargs)
  69. self.stateful = True
  70. self.state = K.variable(value=0, dtype='int32')
  71.  
  72. def reset_states(self):
  73. pass
  74.  
  75. def __call__(self, y_true, y_pred):
  76. return self.state
  77.  
  78. inp = Input((input_dim,))
  79. hidden = Dense(num_hidden, activation='relu')(inp)
  80. hidden = Dropout(0.1)(hidden)
  81. hidden = BatchNormalization()(hidden)
  82. output = Dense(num_classes, activation='softmax')(hidden)
  83. model = Model(inputs=inp, outputs=output)
  84. model.compile(loss='categorical_crossentropy',
  85. optimizer='sgd',
  86. metrics=['accuracy', DummyStatefulMetric()])
  87.  
  88. # we must generate new callbacks for each test, as they aren't stateless
  89. def callbacks_factory(histogram_freq, embeddings_freq=1):
  90. return [callbacks.TensorBoard(log_dir=filepath,
  91. histogram_freq=histogram_freq,
  92. write_images=True, write_grads=True,
  93. embeddings_freq=embeddings_freq,
  94. embeddings_layer_names=['dense_1'],
  95. embeddings_data=X_test,
  96. batch_size=5,
  97. update_freq=update_freq)]
  98.  
  99. # fit without validation data
  100. model.fit(X_train, y_train, batch_size=batch_size,
  101. callbacks=callbacks_factory(histogram_freq=0, embeddings_freq=0),
  102. epochs=3)
  103.  
  104. # fit with validation data and accuracy
  105. model.fit(X_train, y_train, batch_size=batch_size,
  106. validation_data=(X_test, y_test),
  107. callbacks=callbacks_factory(histogram_freq=0), epochs=2)
  108.  
  109. # fit generator without validation data
  110. train_generator = data_generator(X_train, y_train, batch_size)
  111. model.fit_generator(train_generator, len(X_train), epochs=2,
  112. callbacks=callbacks_factory(histogram_freq=0,
  113. embeddings_freq=0))
  114.  
  115. # fit generator with validation data and accuracy
  116. train_generator = data_generator(X_train, y_train, batch_size)
  117. model.fit_generator(train_generator, len(X_train), epochs=2,
  118. validation_data=(X_test, y_test),
  119. callbacks=callbacks_factory(histogram_freq=1))
  120. train_generator.close()
  121.  
  122. assert os.path.isdir(filepath)
  123. shutil.rmtree(filepath)
  124.  
  125.  
  126. class Testing:
  127. def __init__(self):
  128. self.snapshots = []
  129.  
  130. def collect_stats(self):
  131. self.snapshots.append(tracemalloc.take_snapshot())
  132. if len(self.snapshots) > 1:
  133. stats = self.snapshots[-1].filter_traces(filters).compare_to(self.snapshots[-2], 'filename')
  134. for stat in stats[:10]:
  135. print("{} new KiB {} total KiB {} new {} total memory blocks: ".format(stat.size_diff / 1024,
  136. stat.size / 1024,
  137. stat.count_diff, stat.count))
  138. for line in stat.traceback.format():
  139. print(line)
  140.  
  141.  
  142. tmpdir = Path('temp')
  143. if not os.path.exists('temp'):
  144. os.mkdir('temp')
  145. # Keep 10 frames
  146. tracemalloc.start(10)
  147.  
  148. # We are looking for everything at first
  149. filters = []
  150.  
  151. t = Testing()
  152. for _ in range(10):
  153. K.clear_session()
  154. investigate_TensorBoard(tmpdir, 'batch')
  155. gc.collect()
  156. K.clear_session()
  157. t.collect_stats()
  158.  
  159. # Filter for tensorflow
  160. filters = [tracemalloc.Filter(inclusive=True, filename_pattern="*tensorflow*")]
  161. snapshot = t.snapshots[-1]
  162. old_snapshot = t.snapshots[-2]
  163. stats = snapshot.filter_traces(filters).compare_to(old_snapshot.filter_traces(filters), 'traceback')
  164. top_k = sorted([i for i in stats if i.size_diff > 0], key=lambda j: j.size_diff)[::-1][:10]
  165.  
  166. for k in top_k:
  167. print('Leaked', k.size_diff, 'KB')
  168. for f in k.traceback:
  169. print(f)
Add Comment
Please, Sign In to add comment