-
Notifications
You must be signed in to change notification settings - Fork 66
/
utils_backdoor.py
47 lines (36 loc) · 1.23 KB
/
utils_backdoor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-11-05 11:30:01
# @Author : Bolun Wang ([email protected])
# @Link : http://cs.ucsb.edu/~bolunwang
import h5py
import numpy as np
import tensorflow as tf
from keras.preprocessing import image
def dump_image(x, filename, format):
img = image.array_to_img(x, scale=False)
img.save(filename, format)
return
def fix_gpu_memory(mem_fraction=1):
import keras.backend as K
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=mem_fraction)
tf_config = tf.ConfigProto(gpu_options=gpu_options)
tf_config.gpu_options.allow_growth = True
tf_config.log_device_placement = False
tf_config.allow_soft_placement = True
init_op = tf.global_variables_initializer()
sess = tf.Session(config=tf_config)
sess.run(init_op)
K.set_session(sess)
return sess
def load_dataset(data_filename, keys=None):
''' assume all datasets are numpy arrays '''
dataset = {}
with h5py.File(data_filename, 'r') as hf:
if keys is None:
for name in hf:
dataset[name] = np.array(hf.get(name))
else:
for name in keys:
dataset[name] = np.array(hf.get(name))
return dataset