def testGetExpectedImageSizeFromImageModuleInfo(self): with tf.Graph().as_default(): spec = native_module.create_module_spec(image_module_fn_with_info) self.assertAllEqual(image_util.get_expected_image_size(spec), [2, 4]) m = module.Module(spec) self.assertAllEqual(image_util.get_expected_image_size(m), [2, 4])
def _check_module_is_image_embedding(module_spec, check_image_size): """Raises ValueError if `module_spec` is not usable as image embedding. Args: module_spec: A `_ModuleSpec` to test. check_image_size: Whether to check for compatibility with get_expected_image_size. Raises: ValueError: if `module_spec` default signature is not compatible with mappingan "images" input to a Tensor(float32, shape=(_,K)). """ issues = [] # Find issues with "default" signature inputs. The common signatures for # image models prescribe a specific name; we trust it if we find it # and if we can do the necessary inference of input shapes from it. input_info_dict = module_spec.get_input_info_dict() if (list(input_info_dict.keys()) != ["images"] or input_info_dict["images"].dtype != tf.float32): issues.append( "Module 'default' signature must require a single input, " "which must have type float32 and name 'images'.") else: try: if check_image_size: image_util.get_expected_image_size(module_spec) except ValueError as e: issues.append( "Module does not support hub.get_expected_image_size(); " "original error was:\n" + str(e)) # Raised again below. # Find issues with "default" signature outputs. We test that the dtype and # shape is appropriate for use in input_layer(). output_info_dict = module_spec.get_output_info_dict() if "default" not in output_info_dict: issues.append( "Module 'default' signature must have a 'default' output.") else: output_type = output_info_dict["default"].dtype output_shape = output_info_dict["default"].get_shape() if not (output_type == tf.float32 and output_shape.ndims == 2 and output_shape.dims[1].value): issues.append( "Module 'default' signature must have a 'default' output " "of tf.Tensor(shape=(_,K), dtype=float32).") if issues: raise ValueError("Module is not usable as image embedding: %r" % issues)
def parse_example_spec(self): """Returns a `tf.Example` parsing spec as dict.""" if self.image_size: height, width = self.image_size else: height, width = image_util.get_expected_image_size(self.module_spec) input_shape = [height, width, 3] return {self.key: tf.compat.v1.FixedLenFeature(input_shape, tf.float32)}
def _check_module_is_image_embedding(module_spec): """Raises ValueError if `module_spec` is not usable as image embedding. Args: module_spec: A `_ModuleSpec` to test. Raises: ValueError: if `module_spec` default signature is not compatible with mappingan "images" input to a Tensor(float32, shape=(_,K)). """ issues = [] # Find issues with "default" signature inputs. The common signatures for # image models prescribe a specific name; we trust it if we find it # and if we can do the necessary inference of input shapes from it. input_info_dict = module_spec.get_input_info_dict() if (list(input_info_dict.keys()) != ["images"] or input_info_dict["images"].dtype != tf.float32): issues.append("Module 'default' signature must require a single input, " "which must have type float32 and name 'images'.") else: try: image_util.get_expected_image_size(module_spec) except ValueError as e: issues.append("Module does not support hub.get_expected_image_size(); " "original error was:\n" + str(e)) # Raised again below. # Find issues with "default" signature outputs. We test that the dtype and # shape is appropriate for use in input_layer(). output_info_dict = module_spec.get_output_info_dict() if "default" not in output_info_dict: issues.append("Module 'default' signature must have a 'default' output.") else: output_type = output_info_dict["default"].dtype output_shape = output_info_dict["default"].get_shape() if not (output_type == tf.float32 and output_shape.ndims == 2 and output_shape.dims[1].value): issues.append("Module 'default' signature must have a 'default' output " "of tf.Tensor(shape=(_,K), dtype=float32).") if issues: raise ValueError("Module is not usable as image embedding: %r" % issues)
def _parse_example_spec(self): """Returns a `tf.Example` parsing spec as dict.""" height, width = image_util.get_expected_image_size(self.module_spec) input_shape = [height, width, 3] return {self.key: tf.FixedLenFeature(input_shape, tf.float32)}
def testGetExpectedImageSizeFromShape(self): spec = native_module.create_module_spec(image_module_fn) self.assertAllEqual(image_util.get_expected_image_size(spec), [2, 4]) m = module.Module(spec) self.assertAllEqual(image_util.get_expected_image_size(m), [2, 4])