def __tensor__(self, shape, dtype): channels = None h = None w = None if shape is not None: shape_len = len(shape) # NOTE(adamb) Technically we can load images, convert them to # single channel (if they aren't already) and reshape them to # [h, w]. Revisit the code below if we ever want this. if shape_len < 3: return None # NOTE(adamb) Technically we can handle .gif files, which decode to # a shape of length 4. For this to work properly, we'll need to # know which one we're dealing with and return None if the # format doesn't match the requested shape. if shape_len > 3: return TensorShape(shape[-3:]) channels = shape[-1] h = shape[-3] w = shape[-2] contents = as_tensor(self.bytes, None, None) image = tf.image.decode_image(contents, channels=channels) if dtype is not None and dtype != image.dtype: image = tf.image.convert_image_dtype(image, dtype=dtype) if w is not None or h is not None: image = tf.image.resize_image_with_crop_or_pad(image, target_height=h, target_width=w) return image
def terminal_write(o): tensor = as_tensor(o, None, None) accept_mimetypes = {"image/png": imgcat, "text/plain": lambda x: x} encoded = _encode(tensor, accept_mimetypes) if encoded is None: encoded = tensor print(pprint.pformat(encoded), file=sys.stdout, flush=True)
def session_handle_for(value, signature_def_input): if isinstance(value, float): return None # TODO(adamb) This might append to the default graph. These appends # should be cached and idempotent. The added nodes should be # reused if possible. return as_tensor( value, tf.TensorShape(signature_def_input.tensor_shape).as_list(), tf.as_dtype(signature_def_input.dtype))
def __tensor__(self, shape, dtype): channels = None if shape is not None: shape_len = len(shape) if shape_len < 3 or shape_len > 4: raise Exception("Unsupported shape: %s" % shape) channels = shape[-1] contents = as_tensor(self.bytes, None, None) image = tf.image.decode_image(contents, channels=channels) if dtype is not None and dtype != image.dtype: image = tf.image.convert_image_dtype(image, dtype=dtype) h = shape[-3] w = shape[-2] if w is not None or h is not None: image = tf.image.resize_image_with_crop_or_pad(image, target_height=h, target_width=w) return image
def session_handle_for(value, signature_def_input): return as_tensor( value, tf.TensorShape(signature_def_input.tensor_shape).as_list(), tf.as_dtype(signature_def_input.dtype))
def do_test(self, expect, shape, dtype, data): result = as_tensor(data, shape, dtype) self.assertEqual(expect, result)