def tf_model_eval_distance(sess, x, model1, model2, X_test):
"""
Compute the L1 distance between prediction of original and squeezed data.
:param sess: TF session to use when training the graph
:param x: input placeholder
:param model1: model output original predictions
:param model2: model output squeezed predictions
:param X_test: numpy array with training inputs
:return: a float vector with the distance value
"""
# Define sympbolic for accuracy
# acc_value = keras.metrics.categorical_accuracy(y, model)
l2_diff = tf.sqrt( tf.reduce_sum(tf.square(tf.sub(model1, model2)),
axis=1))
l_inf_diff = tf.reduce_max(tf.abs(tf.sub(model1, model2)), axis=1)
l1_diff = tf.reduce_sum(tf.abs(tf.sub(model1, model2)), axis=1)
l1_dist_vec = np.zeros((len(X_test)))
with sess.as_default():
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_test)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_test)
for batch in range(nb_batches):
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Must not use the `batch_indices` function here, because it
# repeats some examples.
# It's acceptable to repeat during training, but not eval.
start = batch * FLAGS.batch_size
end = min(len(X_test), start + FLAGS.batch_size)
cur_batch_size = end - start
l1_dist_vec[start:end] = l1_diff.eval(feed_dict={x: X_test[start:end],keras.backend.learning_phase(): 0})
assert end >= len(X_test)
return l1_dist_vec
python类backend()的实例源码
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None):
name = _prepare_name(name, 'placeholder')
if sparse:
raise Exception('Sparse tensors are not supported yet :( ')
if dtype is None:
dtype = keras.backend.floatx()
ktorch_tensor = Tensor(name=name, shape=shape, ndim=ndim, dtype=dtype)
make_keras_tensor(ktorch_tensor)
ktorch_tensor._ktorch_placeholder = True
return ktorch_tensor
def decode(y, relu_max):
print 'decoder input shape:', y._keras_shape
assert len(y._keras_shape) == 2
if relu_max:
x = GaussianNoise(0.2)(y)
# x = Activation(utils.relu_n(1))(x)
else:
x = y
x = Reshape((1, 1, LATENT_DIM))(x)
# 1, 1, LATENT_DIM
if relu_max:
print 'in decode: relu_max:', relu_max
x = Activation(utils.scale_up(relu_max))(x)
# x = BN(mode=2, axis=3)(x) # this bn seems not good? NOT VERIFIED
# why use 512 instead of 256 here?
batch_size = keras.backend.shape(x)[0]
x = Deconv2D(512, 4, 4, output_shape=[batch_size, 4, 4, 512],
activation='relu', border_mode='same', subsample=(4,4))(x)
x = BN(mode=2, axis=3)(x)
# 4, 4, 512
x = Deconv2D(256, 5, 5, output_shape=[batch_size, 8, 8, 256],
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 8, 8, 256
x = Deconv2D(128, 5, 5, output_shape=(batch_size, 16, 16, 128),
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 16, 16, 256
x = Deconv2D(64, 5, 5, output_shape=(batch_size, 32, 32, 64),
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 32, 32, 64
x = Deconv2D(3, 5, 5, output_shape=(batch_size, 32, 32, 3),
activation='linear', border_mode='same', subsample=(1,1))(x)
# 32, 32, 3
x = BN(mode=2, axis=3)(x)
return x
def new_session():
if K.backend() == 'tensorflow': # pragma: no cover
import tensorflow as tf
K.clear_session()
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
K.set_session(session)
def get_loss():
def return_loss():
import keras.backend as K
def cat_cross(y_true, y_pred):
'''A test of custom loss function
'''
return K.categorical_crossentropy(y_pred, y_true)
return cat_cross
return return_loss
def get_metric():
def return_metric():
import keras.backend as K
def cosine_proximity(y_true, y_pred):
y_true = K.l2_normalize(y_true, axis=-1)
y_pred = K.l2_normalize(y_pred, axis=-1)
return -K.mean(y_true * y_pred)
return cosine_proximity
return return_metric
def test_experiment_fit(self, get_model, get_loss_metric,
get_custom_l, get_callback_fix):
new_session()
data, data_val = make_data(train_samples, test_samples)
model, metrics, cust_objects = prepare_model(get_model(get_custom_l),
get_loss_metric,
get_custom_l)
expe = Experiment(model)
for mod in [None, model]:
for data_val_loc in [None, data_val]:
expe.fit([data], [data_val_loc], model=mod, nb_epoch=2,
batch_size=batch_size, metrics=metrics,
custom_objects=cust_objects, overwrite=True,
callbacks=get_callback_fix)
expe.backend_name = 'another_backend'
expe.load_model()
expe.load_model(expe.mod_id, expe.data_id)
assert expe.data_id is not None
assert expe.mod_id is not None
assert expe.params_dump is not None
if K.backend() == 'tensorflow':
K.clear_session()
print(self)
def test_experiment_fit_gen(self, get_model, get_loss_metric,
get_custom_l, get_callback_fix):
new_session()
model, metrics, cust_objects = prepare_model(get_model(get_custom_l),
get_loss_metric,
get_custom_l)
model_name = model.__class__.__name__
_, data_val_use = make_data(train_samples, test_samples)
expe = Experiment(model)
for val in [1, data_val_use]:
gen, data, data_stream = make_gen(batch_size)
if val == 1:
val, data_2, data_stream_2 = make_gen(batch_size)
expe.fit_gen([gen], [val], nb_epoch=2,
model=model,
metrics=metrics,
custom_objects=cust_objects,
samples_per_epoch=64,
nb_val_samples=128,
verbose=2, overwrite=True,
callbacks=get_callback_fix)
close_gens(gen, data, data_stream)
if val == 1:
close_gens(val, data_2, data_stream_2)
if K.backend() == 'tensorflow':
K.clear_session()
print(self)
def test_experiment_fit_gen_async(self, get_model, get_loss_metric,
get_custom_l):
new_session()
model, metrics, cust_objects = prepare_model(get_model(get_custom_l),
get_loss_metric,
get_custom_l)
_, data_val_use = make_data(train_samples, test_samples)
expe = Experiment(model)
expected_value = 2
for val in [None, 1, data_val_use]:
gen, data, data_stream = make_gen(batch_size)
if val == 1:
val, data_2, data_stream_2 = make_gen(batch_size)
_, thread = expe.fit_gen_async([gen], [val], nb_epoch=2,
model=model,
metrics=metrics,
custom_objects=cust_objects,
samples_per_epoch=64,
nb_val_samples=128,
verbose=2, overwrite=True)
thread.join()
for k in expe.full_res['metrics']:
if 'iter' not in k:
assert len(
expe.full_res['metrics'][k]) == expected_value
close_gens(gen, data, data_stream)
if val == 1:
close_gens(val, data_2, data_stream_2)
if K.backend() == 'tensorflow':
K.clear_session()
print(self)
def test_deserialization(self):
new_session()
model = sequential()
model.compile(optimizer='sgd', loss='categorical_crossentropy')
ser_mod = to_dict_w_opt(model)
custom_objects = {'test_loss': [1, 2]}
custom_objects = {k: serialize(custom_objects[k])
for k in custom_objects}
model_from_dict_w_opt(ser_mod, custom_objects=custom_objects)
if K.backend() == 'tensorflow':
K.clear_session()
print(self)
def tf_model_eval_distance_dual_input(sess, x, model, X_test1, X_test2):
"""
Compute the L1 distance between prediction of original and squeezed data.
:param sess: TF session to use when training the graph
:param x: input placeholder
:param y: output placeholder (for labels)
:param model: model output predictions
:param X_test: numpy array with training inputs
:param Y_test: numpy array with training outputs
:return: a float with the accuracy value
"""
# Define sympbolic for accuracy
# acc_value = keras.metrics.categorical_accuracy(y, model)
# l2_diff = tf.sqrt( tf.reduce_sum(tf.square(tf.sub(model1, model2)),
# axis=1))
# l_inf_diff = tf.reduce_max(tf.abs(tf.sub(model1, model2)), axis=1)
# l1_diff = tf.reduce_sum(tf.abs(tf.sub(model1, model2)), axis=1)
l1_dist_vec = np.zeros((len(X_test1)))
with sess.as_default():
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_test1)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_test1)
for batch in range(nb_batches):
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Must not use the `batch_indices` function here, because it
# repeats some examples.
# It's acceptable to repeat during training, but not eval.
start = batch * FLAGS.batch_size
end = min(len(X_test1), start + FLAGS.batch_size)
cur_batch_size = end - start
pred_1 = model.eval(feed_dict={x: X_test1[start:end],keras.backend.learning_phase(): 0})
pred_2 = model.eval(feed_dict={x: X_test2[start:end],keras.backend.learning_phase(): 0})
l1_dist_vec[start:end] = np.sum(np.abs(pred_1 - pred_2), axis=1)
assert end >= len(X_test1)
return l1_dist_vec
def tf_model_eval_dist_tri_input(sess, x, model, X_test1, X_test2, X_test3, mode = 'max'):
"""
Compute the accuracy of a TF model on some data
:param sess: TF session to use when training the graph
:param x: input placeholder
:param model: model output predictions
:param X_test[1,2,3]: numpy array with testing inputs
:param Y_test: numpy array with training outputs
:return: a float with the accuracy value
"""
l1_dist_vec = np.zeros((len(X_test1)))
with sess.as_default():
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_test1)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_test1)
for batch in range(nb_batches):
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Must not use the `batch_indices` function here, because it
# repeats some examples.
# It's acceptable to repeat during training, but not eval.
start = batch * FLAGS.batch_size
end = min(len(X_test1), start + FLAGS.batch_size)
cur_batch_size = end - start
pred_1 = model.eval(feed_dict={x: X_test1[start:end],keras.backend.learning_phase(): 0})
pred_2 = model.eval(feed_dict={x: X_test2[start:end],keras.backend.learning_phase(): 0})
pred_3 = model.eval(feed_dict={x: X_test3[start:end],keras.backend.learning_phase(): 0})
l11 = np.sum(np.abs(pred_1 - pred_2), axis=1)
l12 = np.sum(np.abs(pred_1 - pred_3), axis=1)
l13 = np.sum(np.abs(pred_2 - pred_3), axis=1)
if mode == 'max':
l1_dist_vec[start:end] = np.max(np.array([l11, l12, l13]), axis=0)
elif mode == 'mean':
l1_dist_vec[start:end] = np.mean(np.array([l11, l12, l13]), axis=0)
assert end >= len(X_test1)
# Divide by number of examples to get final value
return l1_dist_vec
jsmacifar.py 文件源码
项目:AdversarialMachineLearning_COMP551
作者: arunrawlani
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def cnn_model(logits=False, input_ph=None, img_rows=28, img_cols=28,
channels=1, nb_filters=64, nb_classes=10):
"""
Defines a CNN model using Keras sequential model
:param logits: If set to False, returns a Keras model, otherwise will also
return logits tensor
:param input_ph: The TensorFlow tensor for the input
(needed if returning logits)
("ph" stands for placeholder but it need not actually be a
placeholder)
:param img_rows: number of row in the image
:param img_cols: number of columns in the image
:param channels: number of color channels (e.g., 1 for MNIST)
:param nb_filters: number of convolutional filters per layer
:param nb_classes: the number of output classes
:return:
"""
model = Sequential()
# Define the layers successively (convolution layers are version dependent)
if keras.backend.image_dim_ordering() == 'th':
input_shape = (channels, img_rows, img_cols)
else:
input_shape = (img_rows, img_cols, channels)
layers = [Dropout(0.2, input_shape=input_shape),
conv_2d(nb_filters, (8, 8), (2, 2), "same"),
Activation('relu'),
conv_2d((nb_filters * 2), (6, 6), (2, 2), "valid"),
Activation('relu'),
conv_2d((nb_filters * 2), (5, 5), (1, 1), "valid"),
Activation('relu'),
Dropout(0.5),
Flatten(),
Dense(nb_classes)]
for layer in layers:
model.add(layer)
if logits:
logits_tensor = model(input_ph)
model.add(Activation('softmax'))
if logits:
return model, logits_tensor
else:
return model
jsmastl.py 文件源码
项目:AdversarialMachineLearning_COMP551
作者: arunrawlani
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def cnn_model(logits=False, input_ph=None, img_rows=28, img_cols=28,
channels=1, nb_filters=64, nb_classes=10):
"""
Defines a CNN model using Keras sequential model
:param logits: If set to False, returns a Keras model, otherwise will also
return logits tensor
:param input_ph: The TensorFlow tensor for the input
(needed if returning logits)
("ph" stands for placeholder but it need not actually be a
placeholder)
:param img_rows: number of row in the image
:param img_cols: number of columns in the image
:param channels: number of color channels (e.g., 1 for MNIST)
:param nb_filters: number of convolutional filters per layer
:param nb_classes: the number of output classes
:return:
"""
model = Sequential()
# Define the layers successively (convolution layers are version dependent)
if keras.backend.image_dim_ordering() == 'th':
input_shape = (channels, img_rows, img_cols)
else:
input_shape = (img_rows, img_cols, channels)
layers = [Dropout(0.2, input_shape=input_shape),
conv_2d(nb_filters, (8, 8), (2, 2), "same"),
Activation('relu'),
conv_2d((nb_filters * 2), (6, 6), (2, 2), "valid"),
Activation('relu'),
conv_2d((nb_filters * 2), (5, 5), (1, 1), "valid"),
Activation('relu'),
Dropout(0.5),
Flatten(),
Dense(nb_classes)]
for layer in layers:
model.add(layer)
if logits:
logits_tensor = model(input_ph)
model.add(Activation('softmax'))
if logits:
return model, logits_tensor
else:
return model
def tf_model_eval_distance_dual_input(sess, x, model, X_test1, X_test2):
"""
Compute the L1 distance between prediction of original and squeezed data.
:param sess: TF session to use when training the graph
:param x: input placeholder
:param y: output placeholder (for labels)
:param model: model output predictions
:param X_test: numpy array with training inputs
:param Y_test: numpy array with training outputs
:return: a float with the accuracy value
"""
# Define sympbolic for accuracy
# acc_value = keras.metrics.categorical_accuracy(y, model)
# l2_diff = tf.sqrt( tf.reduce_sum(tf.square(tf.sub(model1, model2)),
# axis=1))
# l_inf_diff = tf.reduce_max(tf.abs(tf.sub(model1, model2)), axis=1)
# l1_diff = tf.reduce_sum(tf.abs(tf.sub(model1, model2)), axis=1)
l1_dist_vec = np.zeros((len(X_test1)))
with sess.as_default():
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_test1)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_test1)
for batch in range(nb_batches):
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Must not use the `batch_indices` function here, because it
# repeats some examples.
# It's acceptable to repeat during training, but not eval.
start = batch * FLAGS.batch_size
end = min(len(X_test1), start + FLAGS.batch_size)
cur_batch_size = end - start
pred_1 = model.eval(feed_dict={x: X_test1[start:end],keras.backend.learning_phase(): 0})
pred_2 = model.eval(feed_dict={x: X_test2[start:end],keras.backend.learning_phase(): 0})
l1_dist_vec[start:end] = np.sum(np.abs(pred_1 - pred_2), axis=1)
assert end >= len(X_test1)
return l1_dist_vec
def tf_model_eval_dist_tri_input(sess, x, model, X_test1, X_test2, X_test3, mode = 'max'):
"""
Compute the accuracy of a TF model on some data
:param sess: TF session to use when training the graph
:param x: input placeholder
:param model: model output predictions
:param X_test[1,2,3]: numpy array with testing inputs
:param Y_test: numpy array with training outputs
:return: a float with the accuracy value
"""
l1_dist_vec = np.zeros((len(X_test1)))
with sess.as_default():
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_test1)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_test1)
for batch in range(nb_batches):
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Must not use the `batch_indices` function here, because it
# repeats some examples.
# It's acceptable to repeat during training, but not eval.
start = batch * FLAGS.batch_size
end = min(len(X_test1), start + FLAGS.batch_size)
cur_batch_size = end - start
pred_1 = model.eval(feed_dict={x: X_test1[start:end],keras.backend.learning_phase(): 0})
pred_2 = model.eval(feed_dict={x: X_test2[start:end],keras.backend.learning_phase(): 0})
pred_3 = model.eval(feed_dict={x: X_test3[start:end],keras.backend.learning_phase(): 0})
l11 = np.sum(np.abs(pred_1 - pred_2), axis=1)
l12 = np.sum(np.abs(pred_1 - pred_3), axis=1)
l13 = np.sum(np.abs(pred_2 - pred_3), axis=1)
if mode == 'max':
l1_dist_vec[start:end] = np.max(np.array([l11, l12, l13]), axis=0)
elif mode == 'mean':
l1_dist_vec[start:end] = np.mean(np.array([l11, l12, l13]), axis=0)
assert end >= len(X_test1)
# Divide by number of examples to get final value
return l1_dist_vec
def bias_add(x, bias, data_format=None):
def _bias_add(X, data_format):
x, bias = X
from keras.backend import image_data_format, ndim, reshape
if data_format is None:
data_format = image_data_format()
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Unknown data_format ' + str(data_format))
if ndim(bias) != 1 and ndim(bias) != ndim(x) - 1:
raise ValueError('Unexpected bias dimensions %d, '
'expect to be 1 or %d dimensions'
% (ndim(bias), ndim(x) - 1))
bias_shape = tuple(bias.size())
ndim_x = len(x.size())
ndim_bias = len(bias_shape)
if ndim_x == 5:
if data_format == 'channels_first':
if ndim_bias == 1:
bias = reshape(bias, (1, bias_shape[0], 1, 1, 1))
else:
bias = reshape(bias, (1, bias_shape[3]) + bias_shape[:3])
elif data_format == 'channels_last':
if ndim_bias == 1:
bias = reshape(bias, (1, 1, 1, 1, bias_shape[0]))
else:
bias = reshape(bias, (1,) + bias_shape)
elif ndim_x == 4:
if data_format == 'channels_first':
if ndim_bias == 1:
bias = reshape(bias, (1, bias_shape[0], 1, 1))
else:
bias = reshape(bias, (1, bias_shape[2]) + bias_shape[:2])
elif data_format == 'channels_last':
if ndim_bias == 1:
bias = reshape(bias, (1, 1, 1, bias_shape[0]))
else:
bias = reshape(bias, (1,) + bias_shape)
elif ndim_x == 3:
if data_format == 'channels_first':
if ndim_bias == 1:
bias = reshape(bias, (1, bias_shape[0], 1))
else:
bias = reshape(bias, (1, bias_shape[1], bias_shape[0]))
elif data_format == 'channels_last':
if ndim_bias == 1:
bias = reshape(bias, (1, 1, bias_shape[0]))
else:
bias = reshape(bias, (1,) + bias_shape)
return x.add(bias.expand_as(x))
def _compute_output_shape(X):
return _get_shape(X[0])
return get_op(_bias_add, output_shape=_compute_output_shape, arguments=[data_format])([x, bias])
def decode(y, relu_max):
print 'decoder input shape:', y._keras_shape
assert len(y._keras_shape) == 2
if relu_max:
x = GaussianNoise(0.2)(y)
x = Activation(utils.relu_n(1))(x)
else:
x = y
x = Reshape((1, 1, LATENT_DIM))(x)
# 1, 1, LATENT_DIM
if relu_max:
print 'in decode: relu_max:', relu_max
x = Activation(utils.scale_up(relu_max))(x)
# x = BN(mode=2, axis=3)(x) # this bn seems not good? NOT VERIFIED
# why use 512 instead of 256 here?
batch_size = keras.backend.shape(x)[0]
x = Deconv2D(512, 6, 6, output_shape=[batch_size, 6, 6, 512],
activation='relu', border_mode='same', subsample=(6,6))(x)
x = BN(mode=2, axis=3)(x)
# 6, 6, 512
x = Deconv2D(256, 5, 5, output_shape=[batch_size, 12, 12, 256],
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 12, 12, 256
x = Deconv2D(128, 5, 5, output_shape=(batch_size, 24, 24, 128),
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 24, 24, 128
x = Deconv2D(64, 5, 5, output_shape=(batch_size, 48, 48, 64),
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 48, 48, 64
x = Deconv2D(32, 5, 5, output_shape=(batch_size, 96, 96, 32),
activation='relu', border_mode='same', subsample=(2,2))(x)
x = BN(mode=2, axis=3)(x)
# 96, 96, 32
x = Deconv2D(3, 5, 5, output_shape=(batch_size, 96, 96, 3),
activation='linear', border_mode='same', subsample=(1,1))(x)
# 32, 32, 3
x = BN(mode=2, axis=3)(x)
return x