def eval_image(image, height, width, scope=None):
"""Prepare one image for evaluation.
Args:
image: 3-D float Tensor
height: integer
width: integer
scope: Optional scope for name_scope.
Returns:
3-D float Tensor of prepared image.
"""
with tf.name_scope(
values=[image, height, width], name=scope, default_name='eval_image'):
# Crop the central region of the image with an area containing 87.5% of
# the original image.
image = tf.image.central_crop(image, central_fraction=0.875)
# Resize the image to the original height and width.
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(
image, [height, width], align_corners=False)
image = tf.squeeze(image, [0])
return image
python类squeeze()的实例源码
def rgb2yuv(rgb):
"""
Convert RGB image into YUV https://en.wikipedia.org/wiki/YUV
"""
rgb2yuv_filter = tf.constant([[[[0.299, -0.169,
0.499], [0.587, -0.331, -0.418],
[0.114, 0.499, -0.0813]]]])
rgb2yuv_bias = tf.constant([0., 0.5, 0.5])
rgb = tf.expand_dims(rgb, 0)
temp = tf.nn.conv2d(rgb, rgb2yuv_filter, [1, 1, 1, 1], 'SAME')
temp = tf.nn.bias_add(temp, rgb2yuv_bias)
temp = tf.squeeze(temp, [0])
return temp
# Adapted from
# https://github.com/pavelgonchar/colornet/blob/master/train.py
def yuv2rgb(yuv):
"""
Convert YUV image into RGB https://en.wikipedia.org/wiki/YUV
"""
yuv = tf.multiply(yuv, 255)
yuv2rgb_filter = tf.constant([[[[1., 1., 1.], [0., -0.34413999, 1.77199996],
[1.40199995, -0.71414, 0.]]]])
yuv2rgb_bias = tf.constant([-179.45599365, 135.45983887, -226.81599426])
yuv = tf.expand_dims(yuv, 0)
temp = tf.nn.conv2d(yuv, yuv2rgb_filter, [1, 1, 1, 1], 'SAME')
temp = tf.nn.bias_add(temp, yuv2rgb_bias)
temp = tf.maximum(temp, tf.zeros(temp.get_shape(), dtype=tf.float32))
temp = tf.minimum(temp,
tf.multiply(
tf.ones(temp.get_shape(), dtype=tf.float32), 255))
temp = tf.divide(temp, 255)
temp = tf.squeeze(temp, [0])
return temp
def accuracy_op(logits, labels):
"""Define the accuracy between predictions (logits) and labels.
Args:
logits: a [batch_size, 1,1, num_classes] tensor or
a [batch_size, num_classes] tensor
labels: a [batch_size] tensor
Returns:
accuracy: the accuracy op
"""
with tf.variable_scope('accuracy'):
# handle fully convolutional classifiers
logits_shape = logits.shape
if len(logits_shape) == 4 and logits_shape[1:3] == [1, 1]:
top_k_logits = tf.squeeze(logits, [1, 2])
else:
top_k_logits = logits
top_k_op = tf.nn.in_top_k(top_k_logits, labels, 1)
accuracy = tf.reduce_mean(tf.cast(top_k_op, tf.float32))
return accuracy
doc2vec_train_doc_prediction.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def doc2vec_prediction_model(input_vectors, input_gene, input_variation, output_label, batch_size,
is_training, embedding_size, output_classes):
# inputs/outputs
input_vectors = tf.reshape(input_vectors, [batch_size, embedding_size])
input_gene = tf.reshape(input_gene, [batch_size, embedding_size])
input_variation = tf.reshape(input_variation, [batch_size, embedding_size])
targets = None
if output_label is not None:
output_label = tf.reshape(output_label, [batch_size, 1])
targets = tf.one_hot(output_label, axis=-1, depth=output_classes, on_value=1.0,
off_value=0.0)
targets = tf.squeeze(targets, axis=1)
net = tf.concat([input_vectors, input_gene, input_variation], axis=1)
net = layers.fully_connected(net, embedding_size * 2, activation_fn=tf.nn.relu)
net = layers.dropout(net, keep_prob=0.85, is_training=is_training)
net = layers.fully_connected(net, embedding_size, activation_fn=tf.nn.relu)
net = layers.dropout(net, keep_prob=0.85, is_training=is_training)
net = layers.fully_connected(net, embedding_size // 4, activation_fn=tf.nn.relu)
logits = layers.fully_connected(net, output_classes, activation_fn=None)
return logits, targets
def run_bottleneck_on_image(sess, image_data, image_data_tensor,
bottleneck_tensor):
"""Runs inference on an image to extract the 'bottleneck' summary layer.
Args:
sess: Current active TensorFlow Session.
image_data: String of raw JPEG data.
image_data_tensor: Input data layer in the graph.
bottleneck_tensor: Layer before the final softmax.
Returns:
Numpy array of bottleneck values.
"""
bottleneck_values = sess.run(
bottleneck_tensor,
{image_data_tensor: image_data})
bottleneck_values = np.squeeze(bottleneck_values)
return bottleneck_values
def euclidean_distance(self):
x = tf.argmax(tf.reduce_max(self.smoothed_sigm_network, 1), 1)
y = tf.argmax(tf.reduce_max(self.smoothed_sigm_network, 2), 1)
x = tf.cast(x, tf.float32)
y = tf.cast(y, tf.float32)
dy = tf.squeeze(self.desired_points[:, 0, :])
dx = tf.squeeze(self.desired_points[:, 1, :])
sx = tf.squared_difference(x, dx)
sy = tf.squared_difference(y, dy)
l2_dist = tf.sqrt(sx + sy)
return l2_dist
def run_bottleneck_on_image(sess, image_data, image_data_tensor,
bottleneck_tensor):
"""Runs inference on an image to extract the 'bottleneck' summary layer.
Args:
sess: Current active TensorFlow Session.
image_data: String of raw JPEG data.
image_data_tensor: Input data layer in the graph.
bottleneck_tensor: Layer before the final softmax.
Returns:
Numpy array of bottleneck values.
"""
bottleneck_values = sess.run(
bottleneck_tensor,
{image_data_tensor: image_data})
bottleneck_values = np.squeeze(bottleneck_values)
return bottleneck_values
def prepare_label(self, input_batch, new_size):
"""Resize masks and perform one-hot encoding.
Args:
input_batch: input tensor of shape [batch_size H W 1].
new_size: a tensor with new height and width.
Returns:
Outputs a tensor of shape [batch_size h w 21]
with last dimension comprised of 0's and 1's only.
"""
with tf.name_scope('label_encode'):
input_batch = tf.image.resize_nearest_neighbor(input_batch, new_size) # As labels are integer numbers, need to use NN interp.
input_batch = tf.squeeze(input_batch, squeeze_dims=[3]) # Reducing the channel dimension.
input_batch = tf.one_hot(input_batch, depth=21)
return input_batch
def cal_loss(self):
expand_annotations = tf.expand_dims(
self.annotations, -1, name='annotations/expand_dims')
one_hot_annotations = tf.squeeze(
expand_annotations, axis=[self.channel_axis],
name='annotations/squeeze')
one_hot_annotations = tf.one_hot(
one_hot_annotations, depth=self.conf.class_num,
axis=self.channel_axis, name='annotations/one_hot')
losses = tf.losses.softmax_cross_entropy(
one_hot_annotations, self.predictions, scope='loss/losses')
self.loss_op = tf.reduce_mean(losses, name='loss/loss_op')
self.decoded_predictions = tf.argmax(
self.predictions, self.channel_axis, name='accuracy/decode_pred')
self.dice_accuracy_op, self.sub_dice_list = ops.dice_accuracy(self.decoded_predictions,\
self.annotations,self.conf.class_num)
correct_prediction = tf.equal(
self.annotations, self.decoded_predictions,
name='accuracy/correct_pred')
self.accuracy_op = tf.reduce_mean(
tf.cast(correct_prediction, tf.float32, name='accuracy/cast'),
name='accuracy/accuracy_op')
def read_tensor_from_image_file(file_name, input_height=299, input_width=299,
input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)
return result
def eval_image(image, height, width, scope=None):
"""Prepare one image for evaluation.
Args:
image: 3-D float Tensor
height: integer
width: integer
scope: Optional scope for op_scope.
Returns:
3-D float Tensor of prepared image.
"""
with tf.op_scope([image, height, width], scope, 'eval_image'):
# Crop the central region of the image with an area containing 87.5% of
# the original image.
image = tf.image.central_crop(image, central_fraction=0.875)
# Resize the image to the original height and width.
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, [height, width],
align_corners=False)
image = tf.squeeze(image, [0])
return image
def eval_image(image, height, width, scope=None):
"""Prepare one image for evaluation.
Args:
image: 3-D float Tensor
height: integer
width: integer
scope: Optional scope for op_scope.
Returns:
3-D float Tensor of prepared image.
"""
with tf.name_scope(values=[image, height, width], name=scope, default_name='eval_image'):
# Crop the central region of the image with an area containing 87.5% of
# the original image.
image = tf.image.central_crop(image, central_fraction=0.875)
# Resize the image to the original height and width.
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, [height, width],
align_corners=False)
image = tf.squeeze(image, [0])
return image
def squeezenet(inputs,
num_classes=1000,
is_training=True,
keep_prob=0.5,
spatial_squeeze=True,
scope='squeeze'):
"""
squeezenetv1.1
"""
with tf.name_scope(scope, 'squeeze', [inputs]) as sc:
end_points_collection = sc + '_end_points'
# Collect outputs for conv2d, fully_connected and max_pool2d.
with slim.arg_scope([slim.conv2d, slim.max_pool2d,
slim.avg_pool2d, fire_module],
outputs_collections=end_points_collection):
nets = squeezenet_inference(inputs, is_training, keep_prob)
nets = slim.conv2d(nets, num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='logits')
end_points = slim.utils.convert_collection_to_dict(end_points_collection)
if spatial_squeeze:
nets = tf.squeeze(nets, [1, 2], name='logits/squeezed')
return nets, end_points
def get_eval_ops_slim(logits, labels, one_hot=False, scope=''):
slim = tf.contrib.slim
with tf.name_scope(scope + '/Streaming'):
if one_hot:
labels = tf.argmax(labels, 1)
predictions = tf.argmax(logits, 1)
labels = tf.squeeze(labels)
names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
'Accuracy': slim.metrics.streaming_accuracy(predictions, labels),
})
return names_to_values, names_to_updates
########################################################################
def _aspect_preserving_resize(image, smallest_side):
"""Resize images preserving the original aspect ratio.
Args:
image: A 3-D image `Tensor`.
smallest_side: A python integer or scalar `Tensor` indicating the size of
the smallest side after resize.
Returns:
resized_image: A 3-D tensor containing the resized image.
"""
smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)
shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_height, new_width = _smallest_size_at_least(height, width, smallest_side)
image = tf.expand_dims(image, 0)
resized_image = tf.image.resize_bilinear(image, [new_height, new_width],
align_corners=False)
resized_image = tf.squeeze(resized_image)
resized_image.set_shape([None, None, 3])
return resized_image
def _square_resize(image, side_size):
"""
Args:
image: A 3-D image `Tensor`.
smallest_side: A python integer or scalar `Tensor` indicating the size of
the smallest side after resize.
Returns:
resized_image: A 3-D tensor containing the resized image.
"""
image = tf.expand_dims(image, 0)
# resized_image = tf.image.resize_nearest_neighbor(image, [side_size, side_size]) ## YY: changed bilinear to nearest neighbor
resized_image = tf.image.resize_bilinear(image, [side_size, side_size],
align_corners=False)
resized_image = tf.squeeze(resized_image)
resized_image.set_shape([None, None, 3])
return resized_image
def _aspect_preserving_resize(image, smallest_side):
"""Resize images preserving the original aspect ratio.
Args:
image: A 3-D image `Tensor`.
smallest_side: A python integer or scalar `Tensor` indicating the size of
the smallest side after resize.
Returns:
resized_image: A 3-D tensor containing the resized image.
"""
smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)
shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_height, new_width = _smallest_size_at_least(height, width, smallest_side)
image = tf.expand_dims(image, 0)
resized_image = tf.image.resize_bilinear(image, [new_height, new_width],
align_corners=False)
resized_image = tf.squeeze(resized_image)
resized_image.set_shape([None, None, 3])
return resized_image
def _square_resize(image, side_size):
"""
Args:
image: A 3-D image `Tensor`.
smallest_side: A python integer or scalar `Tensor` indicating the size of
the smallest side after resize.
Returns:
resized_image: A 3-D tensor containing the resized image.
"""
image = tf.expand_dims(image, 0)
resized_image = tf.image.resize_bilinear(image, [side_size, side_size],
align_corners=False)
resized_image = tf.squeeze(resized_image)
resized_image.set_shape([None, None, 3])
return resized_image
def linear(args, output_size, bias, bias_start=0.0, scope=None, squeeze=False, wd=0.0, input_keep_prob=1.0,
is_train=None):
if args is None or (nest.is_sequence(args) and not args):
raise ValueError("`args` must be specified")
if not nest.is_sequence(args):
args = [args]
flat_args = [flatten(arg, 1) for arg in args]
if input_keep_prob < 1.0:
assert is_train is not None
flat_args = [tf.cond(is_train, lambda: tf.nn.dropout(arg, input_keep_prob), lambda: arg)
for arg in flat_args]
flat_out = _linear(flat_args, output_size, bias, bias_start=bias_start, scope=scope)
out = reconstruct(flat_out, args[0], 1)
if squeeze:
out = tf.squeeze(out, [len(args[0].get_shape().as_list())-1])
if wd:
add_wd(wd)
return out