Esempio n. 1
0
 def testDenseFeatures(self):
     features = {
         "image_a": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                     [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
         "image_b": [[[[0.1, 0.2, 0.1], [0.2, 0.1, 0.2]]],
                     [[[0.1, 0.2, 0.3], [0.3, 0.2, 0.1]]]],
     }
     feature_columns = [
         hub.image_embedding_column("image_a", self.spec),
         hub.image_embedding_column("image_b", self.spec),
     ]
     if not feature_column_v2.is_feature_column_v2(feature_columns):
         self.skipTest(
             "Resources not implemented in the state manager of feature "
             "column v2.")
     with tf.Graph().as_default():
         # We want to test with dense_features_v2.DenseFeatures. This symbol was
         # added in https://github.com/tensorflow/tensorflow/commit/64586f18724f737393071125a91b19adf013cf8a.
         feature_layer = tf.compat.v2.keras.layers.DenseFeatures(
             feature_columns)
         feature_layer_out = feature_layer(features)
         with tf_v1.train.MonitoredSession() as sess:
             output = sess.run(feature_layer_out)
             self.assertAllClose(output, [[0.5, 0.7, 0.9, 0.3, 0.3, 0.3],
                                          [0.8, 0.9, 1.0, 0.4, 0.4, 0.4]])
Esempio n. 2
0
  def testConfig(self):
    module_path = os.path.join(self.get_temp_dir(), "module")
    export_module_spec(self.spec, module_path)
    image_column = hub.image_embedding_column("image", module_path)
    config = image_column.get_config()
    cloned_image_column = hub.feature_column._ImageEmbeddingColumn.from_config(
        config)
    self.assertEqual(cloned_image_column.module_spec_path,
                     image_column.module_spec_path)

    with self.assertRaisesRegexp(NotImplementedError, "Can only generate"):
      image_column = hub.image_embedding_column("image", self.spec)
      config = image_column.get_config()
Esempio n. 3
0
  def testWorksWithCannedEstimator(self):
    image_column = hub.image_embedding_column("image", self.spec)
    other_column = tf_v1.feature_column.numeric_column("number")

    feature_columns = [image_column, other_column]
    estimator = tf_v1.estimator.DNNClassifier(
        hidden_units=[10],
        feature_columns=feature_columns,
        model_dir=self.get_temp_dir())

    # This only tests that estimator apis are working with the feature
    # column without throwing exceptions.
    features = {
        "image":
            np.array([[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                      [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
                     dtype=np.float32),
        "number":
            np.array([[20], [1]]),
    }
    labels = np.array([[1], [0]])
    if hasattr(tf.compat, "v1"):
      numpy_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn
    else:
      numpy_input_fn = tf_v1.estimator.inputs.numpy_input_fn
    input_fn = numpy_input_fn(features, labels, shuffle=True)
    estimator.train(input_fn, max_steps=1)
    estimator.evaluate(input_fn, steps=1)
    estimator.predict(input_fn)
 def testMakeParseExampleSpec(self):
     image_column = hub.image_embedding_column("image", self.spec)
     parsing_spec = tf_v1.feature_column.make_parse_example_spec(
         [image_column])
     self.assertEqual(
         parsing_spec,
         {"image": tf_v1.FixedLenFeature([1, 2, 3], dtype=tf.float32)})
Esempio n. 5
0
    def testDenseFeatures_shareAcrossApplication(self):
        features = {
            "image": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                      [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
        }
        feature_columns = [
            hub.image_embedding_column("image", self.spec),
        ]
        if not feature_column_v2.is_feature_column_v2(feature_columns):
            self.skipTest(
                "Resources not implemented in the state manager of feature "
                "column v2.")
        with tf.Graph().as_default():
            feature_layer = feature_column_v2.DenseFeatures(feature_columns)
            global_vars_before = tf.global_variables()
            feature_layer_out_1 = feature_layer(features)
            global_vars_middle = tf.global_variables()
            feature_layer_out_2 = feature_layer(features)
            global_vars_after = tf.global_variables()

            self.assertLen(global_vars_before, 0)
            self.assertLen(global_vars_middle, 1)
            self.assertLen(global_vars_after, 1)

            with tf_v1.train.MonitoredSession() as sess:
                output_1 = sess.run(feature_layer_out_1)
                output_2 = sess.run(feature_layer_out_2)

                self.assertAllClose(output_1,
                                    [[0.5, 0.7, 0.9], [0.8, 0.9, 1.0]])
                self.assertAllEqual(output_1, output_2)
Esempio n. 6
0
    def testDenseFeatures_shareAcrossApplication(self):
        features = {
            "image": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                      [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
        }
        feature_columns = [
            hub.image_embedding_column("image",
                                       self.randomly_initialized_spec),
        ]
        if not feature_column_v2.is_feature_column_v2(feature_columns):
            self.skipTest(
                "Resources not implemented in the state manager of feature "
                "column v2.")
        with tf.Graph().as_default():
            # We want to test with dense_features_v2.DenseFeatures. This symbol was
            # added in https://github.com/tensorflow/tensorflow/commit/64586f18724f737393071125a91b19adf013cf8a.
            feature_layer = tf.compat.v2.keras.layers.DenseFeatures(
                feature_columns)
            feature_layer_out_1 = feature_layer(features)
            feature_layer_out_2 = feature_layer(features)

            with tf.compat.v1.train.MonitoredSession() as sess:
                output_1 = sess.run(feature_layer_out_1)
                output_2 = sess.run(feature_layer_out_2)

                self.assertAllClose(output_1, output_2)
Esempio n. 7
0
 def testInputLayer(self):
   features = {
       "image_a": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                   [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
       "image_b": [[[[0.1, 0.2, 0.1], [0.2, 0.1, 0.2]]],
                   [[[0.1, 0.2, 0.3], [0.3, 0.2, 0.1]]]],
   }
   feature_columns = [
       hub.image_embedding_column("image_a", self.spec),
       hub.image_embedding_column("image_b", self.spec),
   ]
   with tf.Graph().as_default():
     input_layer = tf.feature_column.input_layer(features, feature_columns)
     with tf.train.MonitoredSession() as sess:
       output = sess.run(input_layer)
       self.assertAllClose(output, [[0.5, 0.7, 0.9, 0.3, 0.3, 0.3],
                                    [0.8, 0.9, 1.0, 0.4, 0.4, 0.4]])
 def testDenseFeatures(self):
     features = {
         "image_a": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                     [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
         "image_b": [[[[0.1, 0.2, 0.1], [0.2, 0.1, 0.2]]],
                     [[[0.1, 0.2, 0.3], [0.3, 0.2, 0.1]]]],
     }
     feature_columns = [
         hub.image_embedding_column("image_a", self.spec),
         hub.image_embedding_column("image_b", self.spec),
     ]
     with tf.Graph().as_default():
         feature_layer = feature_column_lib.DenseFeatures(feature_columns)
         feature_layer_out = feature_layer(features)
         with tf_v1.train.MonitoredSession() as sess:
             output = sess.run(feature_layer_out)
             self.assertAllClose(output, [[0.5, 0.7, 0.9, 0.3, 0.3, 0.3],
                                          [0.8, 0.9, 1.0, 0.4, 0.4, 0.4]])
 def testImageSizeManuallySpecified(self):
     spec = hub.create_module_spec(create_image_module_fn([None, None]))
     image_column = hub.image_embedding_column("image",
                                               spec,
                                               image_size=[229, 229])
     parsing_spec = tf_v1.feature_column.make_parse_example_spec(
         [image_column])
     self.assertEqual(
         parsing_spec,
         {"image": tf_v1.FixedLenFeature([229, 229, 3], dtype=tf.float32)})
Esempio n. 10
0
 def testDenseFeatures(self):
   features = {
       "image_a": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                   [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
       "image_b": [[[[0.1, 0.2, 0.1], [0.2, 0.1, 0.2]]],
                   [[[0.1, 0.2, 0.3], [0.3, 0.2, 0.1]]]],
   }
   feature_columns = [
       hub.image_embedding_column("image_a", self.spec),
       hub.image_embedding_column("image_b", self.spec),
   ]
   if not feature_column_v2.is_feature_column_v2(feature_columns):
     self.skipTest("Resources not implemented in the state manager of feature "
                   "column v2.")
   with tf.Graph().as_default():
     feature_layer = _dense_features_module.DenseFeatures(feature_columns)
     feature_layer_out = feature_layer(features)
     with tf_v1.train.MonitoredSession() as sess:
       output = sess.run(feature_layer_out)
       self.assertAllClose(
           output,
           [[0.5, 0.7, 0.9, 0.3, 0.3, 0.3], [0.8, 0.9, 1.0, 0.4, 0.4, 0.4]])
Esempio n. 11
0
def create_classifier(config, parameters):
    """Creates a DNN classifier.

    Defines features and builds an 'Estimator' with them.

    Args:
        config: `RunConfig` object to configure the runtime of the `Estimator`.
        parameters: Parameters passed to the job.

    Returns:
        `tf.estimator.DNNClassifier` with specified features and architecture.
    """
    # Columns to be used as features.
    subspecies = tf.feature_column.categorical_column_with_vocabulary_list(
        'subspecies',
        vocabulary_list=BEE_SUBSPECIES,
        default_value=0)
    subspecies = tf.feature_column.embedding_column(
        subspecies, dimension=parameters.subspecies_embedding)

    image = hub.image_embedding_column('image', parameters.tf_hub_module)

    feature_cols = [subspecies, image]

    layer = parameters.first_layer_size
    lfrac = parameters.layer_reduction_fraction
    nlayers = parameters.number_layers
    h_units = [layer]
    for _ in range(nlayers - 1):
        h_units.append(math.ceil(layer * lfrac))
        layer = h_units[-1]

    estimator = tf.estimator.DNNClassifier(
        feature_columns=feature_cols,
        hidden_units=h_units,
        optimizer=tf.train.AdagradOptimizer(
            learning_rate=parameters.learning_rate),
        dropout=parameters.dropout, config=config)
    estimator = tf.contrib.estimator.add_metrics(
        estimator, _estimator_metrics)
    estimator = tf.contrib.estimator.forward_features(estimator, 'img_file')
    return estimator
Esempio n. 12
0
  def testDenseFeatures_shareAcrossApplication(self):
    features = {
        "image": [[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]],
                  [[[0.7, 0.7, 0.7], [0.1, 0.2, 0.3]]]],
    }
    feature_columns = [
        hub.image_embedding_column("image", self.randomly_initialized_spec),
    ]
    if not feature_column_v2.is_feature_column_v2(feature_columns):
      self.skipTest("Resources not implemented in the state manager of feature "
                    "column v2.")
    with tf.Graph().as_default():
      feature_layer = _dense_features_module.DenseFeatures(feature_columns)
      feature_layer_out_1 = feature_layer(features)
      feature_layer_out_2 = feature_layer(features)

      with tf_v1.train.MonitoredSession() as sess:
        output_1 = sess.run(feature_layer_out_1)
        output_2 = sess.run(feature_layer_out_2)

        self.assertAllClose(output_1, output_2)
Esempio n. 13
0
 def testVariableShape(self):
   image_column = hub.image_embedding_column("image", self.spec)
   self.assertEqual(image_column._variable_shape, [3])
Esempio n. 14
0
 def testExpectedImageSize(self):
   image_column = hub.image_embedding_column("image", self.spec)
   # The usage comment recommends this code pattern, so we test it here.
   self.assertSequenceEqual(
       hub.get_expected_image_size(image_column.module_spec), [1, 2])
Esempio n. 15
0
 def testParents(self):
   image_column = hub.image_embedding_column("image", self.spec)
   self.assertEqual(["image"], image_column.parents)
Esempio n. 16
0
 def testName(self):
     image_column = hub.image_embedding_column(
         tf.feature_column.numeric_column("image"), self.spec)
     self.assertEqual("image_hub_module_embedding", image_column.name)