def test_model_variables(self): input_shape = (1, 512, 512, 3) model = efficientdet_keras.EfficientDetNet('efficientdet-d0') model.build(input_shape) eager_train_vars = sorted( [var.name for var in model.trainable_variables]) eager_model_vars = sorted([var.name for var in model.variables]) with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) model = efficientdet_keras.EfficientDetNet('efficientdet-d0') model.build(input_shape) keras_train_vars = sorted( [var.name for var in model.trainable_variables]) keras_model_vars = sorted([var.name for var in model.variables]) with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) legacy_arch.efficientdet(feats, 'efficientdet-d0') legacy_train_vars = sorted( [var.name for var in tf.trainable_variables()]) legacy_model_vars = sorted( [var.name for var in tf.global_variables()]) self.assertEqual(keras_train_vars, legacy_train_vars) self.assertEqual(keras_model_vars, legacy_model_vars) self.assertEqual(eager_train_vars, legacy_train_vars) self.assertEqual(eager_model_vars, legacy_model_vars)
def test_model_variables(self): input_shape = (1, 512, 512, 3) model = efficientdet_keras.EfficientDetNet('efficientdet-d0') model.build(input_shape) eager_train_vars = sorted( [var.name for var in model.trainable_variables]) eager_model_vars = sorted([var.name for var in model.variables]) with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) model = efficientdet_keras.EfficientDetNet('efficientdet-d0') model(feats, True) keras_train_vars = sorted( [var.name for var in tf.trainable_variables()]) keras_model_vars = sorted( [var.name for var in tf.global_variables()]) keras_update_ops = [ op.name for op in tf.get_collection(tf.GraphKeys.UPDATE_OPS) ] with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) legacy_arch.efficientdet(feats, 'efficientdet-d0') legacy_train_vars = sorted( [var.name for var in tf.trainable_variables()]) legacy_model_vars = sorted( [var.name for var in tf.global_variables()]) legacy_update_ops = [ op.name for op in tf.get_collection(tf.GraphKeys.UPDATE_OPS) ] self.assertEqual(keras_train_vars, legacy_train_vars) self.assertEqual(keras_model_vars, legacy_model_vars) self.assertEqual(eager_train_vars, legacy_train_vars) self.assertEqual(eager_model_vars, legacy_model_vars) self.assertAllEqual(keras_update_ops, legacy_update_ops)
def test_model_variables(self): with tf.Graph().as_default(): feats = tf.random.uniform([1, 512, 512, 3]) efficientdet_arch_keras.efficientdet('efficientdet-d0')(feats) vars1 = [var.name for var in tf.global_variables()] with tf.Graph().as_default(): feats = tf.random.uniform([1, 512, 512, 3]) legacy_arch.efficientdet(feats, 'efficientdet-d0') vars2 = [var.name for var in tf.global_variables()] self.assertEqual(vars1, vars2)
def build_model(self, keras=False): tf.compat.v1.reset_default_graph() inputs_shape = [1, 512, 512, 3] inputs = tf.ones(shape=inputs_shape, name='input', dtype=tf.float32) if not keras: legacy_arch.efficientdet(inputs, model_name='efficientdet-d0', is_training_bn=False, image_size=512) else: efficientdet_arch_keras.efficientdet(inputs, model_name='efficientdet-d0', is_training_bn=False, image_size=512) return [n.name for n in tf.global_variables()]
def test_model_output(self): inputs_shape = [1, 512, 512, 3] config = hparams_config.get_efficientdet_config('efficientdet-d0') tmp_ckpt = os.path.join(tempfile.mkdtemp(), 'ckpt') with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) model = efficientdet_keras.EfficientDetNet(config=config) outputs = model(feats) sess.run(tf.global_variables_initializer()) keras_class_out, keras_box_out = sess.run(outputs) model.save_weights(tmp_ckpt) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = legacy_arch.efficientdet(feats, config=config) sess.run(tf.global_variables_initializer()) legacy_class_out, legacy_box_out = sess.run(feats) for i in range(3, 8): self.assertAllClose( keras_class_out[i - 3], legacy_class_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose( keras_box_out[i - 3], legacy_box_out[i], rtol=1e-4, atol=1e-4) feats = tf.ones(inputs_shape) model = efficientdet_keras.EfficientDetNet(config=config) model.load_weights(tmp_ckpt) eager_class_out, eager_box_out = model(feats) for i in range(3, 8): self.assertAllClose( eager_class_out[i - 3], legacy_class_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose( eager_box_out[i - 3], legacy_box_out[i], rtol=1e-4, atol=1e-4)
def test_model_output(self): inputs_shape = [1, 512, 512, 3] config = hparams_config.get_efficientdet_config('efficientdet-d0') with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = efficientdet_arch_keras.build_backbone(feats, config) feats = efficientdet_arch_keras.build_feature_network( feats, config) feats = efficientdet_arch_keras.build_class_and_box_outputs( feats, config) # TODO(tanmingxing): Fix the failure for keras Model. # feats = efficientdet_arch_keras.EfficientDetModel(config=config)(feats) sess.run(tf.global_variables_initializer()) keras_class_out, keras_box_out = sess.run(feats) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = legacy_arch.efficientdet(feats, config=config) sess.run(tf.global_variables_initializer()) legacy_class_out, legacy_box_out = sess.run(feats) for i in range(3, 8): self.assertAllEqual(keras_class_out[i - 3], legacy_class_out[i]) self.assertAllEqual(keras_box_out[i - 3], legacy_box_out[i]) feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) model = efficientdet_arch_keras.EfficientDetModel(config=config) eager_class_out, eager_box_out = model(feats) for i in range(3, 8): # TODO(tanmingxing): fix the failing case. self.assertAllEqual(eager_class_out[i - 3], legacy_class_out[i]) self.assertAllEqual(eager_box_out[i - 3], legacy_box_out[i])
def test_model_output(self): inputs_shape = [1, 512, 512, 3] config = hparams_config.get_efficientdet_config('efficientdet-d0') with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) # feats = efficientdet_arch_keras.build_backbone(feats, config) # feats = efficientdet_arch_keras.build_feature_network(feats, config) # v = efficientdet_arch_keras.build_class_and_box_outputs(feats, config) v = efficientdet_arch_keras.EfficientDetModel(config=config)(feats) sess.run(tf.global_variables_initializer()) keras_class_out, keras_box_out = sess.run(v) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = legacy_arch.efficientdet(feats, config=config) sess.run(tf.global_variables_initializer()) legacy_class_out, legacy_box_out = sess.run(feats) for i in range(3, 8): self.assertAllClose(keras_class_out[i - 3], legacy_class_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose(keras_box_out[i - 3], legacy_box_out[i], rtol=1e-4, atol=1e-4) feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) model = efficientdet_arch_keras.EfficientDetModel(config=config) eager_class_out, eager_box_out = model(feats) # pylint: disable=unused-variable
def _model_fn(inputs): return efficientdet_arch.efficientdet( inputs, model_name='efficientdet-d0', is_training_bn=is_training, precision=precision, image_size=512)
def test_model_output(self): inputs_shape = [1, 512, 512, 3] config = hparams_config.get_efficientdet_config('efficientdet-d0') config.heads = ['object_detection', 'segmentation'] with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) model = efficientdet_keras.EfficientDetNet(config=config) outputs = model(feats, True) sess.run(tf.global_variables_initializer()) keras_class_out, keras_box_out, _ = sess.run(outputs) grads = tf.nest.map_structure( lambda output: tf.gradients(output, feats), outputs) keras_class_grads, keras_box_grads, _ = sess.run(grads) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) outputs = legacy_arch.efficientdet(feats, config=config) sess.run(tf.global_variables_initializer()) legacy_class_out, legacy_box_out = sess.run(outputs) grads = tf.nest.map_structure( lambda output: tf.gradients(output, feats), outputs) legacy_class_grads, legacy_box_grads = sess.run(grads) for i in range(3, 8): self.assertAllEqual(keras_class_out[i - 3], legacy_class_out[i]) self.assertAllEqual(keras_box_out[i - 3], legacy_box_out[i]) self.assertAllEqual(keras_class_grads[i - 3], legacy_class_grads[i]) self.assertAllEqual(keras_box_grads[i - 3], legacy_box_grads[i])
def test_model_output(self): inputs_shape = [1, 512, 512, 3] config = hparams_config.get_efficientdet_config('efficientdet-d0') config.heads = ['object_detection', 'segmentation'] tmp_ckpt = os.path.join(tempfile.mkdtemp(), 'ckpt') with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) model = efficientdet_keras.EfficientDetNet(config=config) outputs = model(feats, True) sess.run(tf.global_variables_initializer()) keras_class_out, keras_box_out, keras_seg_out = sess.run(outputs) grads = tf.nest.map_structure( lambda output: tf.gradients(output, feats), outputs) keras_class_grads, keras_box_grads, keras_seg_grads = sess.run( grads) model.save_weights(tmp_ckpt) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) outputs = legacy_arch.efficientdet(feats, config=config) sess.run(tf.global_variables_initializer()) legacy_class_out, legacy_box_out = sess.run(outputs) grads = tf.nest.map_structure( lambda output: tf.gradients(output, feats), outputs) legacy_class_grads, legacy_box_grads = sess.run(grads) for i in range(3, 8): self.assertAllClose(keras_class_out[i - 3], legacy_class_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose(keras_box_out[i - 3], legacy_box_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose(keras_class_grads[i - 3], legacy_class_grads[i], rtol=1e-4, atol=1e-4) self.assertAllClose(keras_box_grads[i - 3], legacy_box_grads[i], rtol=1e-4, atol=1e-4) feats = tf.ones(inputs_shape) model = efficientdet_keras.EfficientDetNet(config=config) model.load_weights(tmp_ckpt) eager_class_out, eager_box_out, eager_seg_out = model(feats, True) for i in range(3, 8): self.assertAllClose(eager_class_out[i - 3], legacy_class_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose(eager_box_out[i - 3], legacy_box_out[i], rtol=1e-4, atol=1e-4) self.assertAllClose(eager_seg_out, keras_seg_out, rtol=1e-4, atol=1e-4)
def test_model_variables(self): feats = tf.ones([1, 512, 512, 3]) model = efficientdet_arch_keras.efficientdet('efficientdet-d0') model(feats) vars1 = [var.name for var in model.trainable_variables] vars3 = [var.name for var in model.variables] with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) legacy_arch.efficientdet(feats, 'efficientdet-d0') vars2 = [var.name for var in tf.trainable_variables()] vars4 = [var.name for var in tf.global_variables()] vars1.sort() vars2.sort() self.assertEqual(vars1, vars2) vars3.sort() vars4.sort() self.assertEqual(vars3, vars4)
def build_model(self, model_name, isize, is_training=False, data_format='channels_last'): if isinstance(isize, int): isize = (isize, isize) if data_format == 'channels_first': inputs_shape = [1, 3, isize[0], isize[1]] else: inputs_shape = [1, isize[0], isize[1], 3] inputs = tf.ones(shape=inputs_shape, name='input', dtype=tf.float32) efficientdet_arch.efficientdet(inputs, model_name=model_name, is_training_bn=is_training, image_size=isize, data_format=data_format) return utils.num_params_flops(False)
def build_model(self, model_name, isize=None, is_training=False, data_format='channels_last'): config = hparams_config.get_efficientdet_config(model_name) config.image_size = isize or config.image_size isize = utils.parse_image_size(config.image_size) if data_format == 'channels_first': inputs_shape = [1, 3, isize[0], isize[1]] else: inputs_shape = [1, isize[0], isize[1], 3] inputs = tf.ones(shape=inputs_shape, name='input', dtype=tf.float32) efficientdet_arch.efficientdet(inputs, model_name=model_name, is_training_bn=is_training, image_size=isize, data_format=data_format) return utils.num_params_flops(False)
def test_model_variables(self): # TODO(tanmingxing): Re-enable this code once pass internal tests. # feats = tf.ones([1, 512, 512, 3]) # model = efficientdet_arch_keras.efficientdet('efficientdet-d0') # model(feats) # vars1 = sorted([var.name for var in model.trainable_variables]) # vars2 = sorted([var.name for var in model.variables]) with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) model = efficientdet_arch_keras.efficientdet('efficientdet-d0') model(feats) vars3 = sorted([var.name for var in model.trainable_variables]) vars4 = sorted([var.name for var in model.variables]) with tf.Graph().as_default(): feats = tf.ones([1, 512, 512, 3]) legacy_arch.efficientdet(feats, 'efficientdet-d0') vars5 = sorted([var.name for var in tf.trainable_variables()]) vars6 = sorted([var.name for var in tf.global_variables()]) # self.assertEqual(vars1, vars3) self.assertEqual(vars3, vars5) # self.assertEqual(vars2, vars4) self.assertEqual(vars4, vars6)
def test_model_output(self): # TODO(fsx950223): Fix the test case inputs_shape = [1, 512, 512, 3] with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) _, class_outputs1, box_outputs1 = efficientdet_arch_keras.efficientdet( 'efficientdet-d0')(feats) sess.run(tf.global_variables_initializer()) class_output1, box_output1 = sess.run([class_outputs1, box_outputs1]) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) class_outputs2, box_outputs2 = legacy_arch.efficientdet( feats, 'efficientdet-d0') sess.run(tf.global_variables_initializer()) class_output2, box_output2 = sess.run([class_outputs2, box_outputs2]) for i in range(3, 8): self.assertAllEqual(class_output1[i], class_output2[i]) self.assertAllEqual(box_output1[i], box_output2[i])
def build_model(self, inputs: tf.Tensor, is_training: bool) -> List[tf.Tensor]: """Build model with inputs and labels and print out model stats.""" tf.logging.info('start building model') if self.model_name.startswith('efficientdet'): cls_outputs, box_outputs = efficientdet_arch.efficientdet( inputs, model_name=self.model_name, is_training_bn=is_training, use_bfloat16=False) elif self.model_name.startswith('retinanet'): cls_outputs, box_outputs = retinanet_arch.retinanet( inputs, model_name=self.model_name, is_training_bn=is_training, use_bfloat16=False) print('backbone+fpn+box params/flops = {:.6f}M, {:.9f}B'.format( *utils.num_params_flops())) all_outputs = list(cls_outputs.values()) + list(box_outputs.values()) return all_outputs
def test_model_output(self): inputs_shape = [1, 512, 512, 3] config = hparams_config.get_efficientdet_config('efficientdet-d0') with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = efficientdet_arch_keras.build_backbone(feats, config) feats = efficientdet_arch_keras.build_feature_network( feats, config) feats = efficientdet_arch_keras.build_class_and_box_outputs( feats, config) sess.run(tf.global_variables_initializer()) class_output1, box_output1 = sess.run(feats) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = legacy_arch.efficientdet(feats, config=config) sess.run(tf.global_variables_initializer()) class_output2, box_output2 = sess.run(feats) for i in range(3, 8): self.assertAllEqual(class_output1[i], class_output2[i]) self.assertAllEqual(box_output1[i], box_output2[i]) with tf.Session(graph=tf.Graph()) as sess: feats = tf.ones(inputs_shape) tf.random.set_random_seed(SEED) feats = efficientdet_arch_keras.efficientdet(config=config)(feats) sess.run(tf.global_variables_initializer()) class_list, box_list = sess.run(feats) for i in range(config.min_level, config.max_level + 1): class_output3[i] = class_list[i - config.min_level] box_outputs3[i] = box_list[i - config.min_level] for i in range(3, 8): # Failing. self.assertAllEqual(class_output1[i], class_output3[i]) self.assertAllEqual(box_output1[i], box_output3[i])