def test_inference_methods(self): # test predict, evaluate, test_on_batch, predict_on_batch # on different input types: list, dict num_classes = (2, 3) num_samples = 100 input_dim = 50 x1 = np.ones((num_samples, input_dim)) x2 = np.ones((num_samples, input_dim)) y1 = np.zeros((num_samples, num_classes[0])) y2 = np.zeros((num_samples, num_classes[1])) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) model.compile(loss='mse', optimizer='rmsprop', run_eagerly=testing_utils.should_run_eagerly(), experimental_run_tf_function=testing_utils. should_run_tf_function()) model.evaluate([x1, x2], [y1, y2]) model.test_on_batch([x1, x2], [y1, y2]) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) model.predict([x1, x2]) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) model.predict_on_batch([x1, x2])
def test_training_methods(self): # test fit, train_on_batch # on different input types: list, dict num_classes = (2, 3) num_samples = 100 input_dim = 50 x1 = np.ones((num_samples, input_dim)) x2 = np.ones((num_samples, input_dim)) y1 = np.zeros((num_samples, num_classes[0])) y2 = np.zeros((num_samples, num_classes[1])) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) model.compile(loss='mse', optimizer='rmsprop', run_eagerly=testing_utils.should_run_eagerly(), experimental_run_tf_function=testing_utils. should_run_tf_function()) model.fit([x1, x2], [y1, y2], epochs=2, batch_size=32, verbose=0) model.fit({ 'input_1': x1, 'input_2': x2 }, { 'output_1': y1, 'output_2': y2 }, epochs=2, batch_size=32) model.fit([x1, x2], [y1, y2], epochs=2, batch_size=32, verbose=0, validation_data=([x1, x2], [y1, y2])) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) model.compile(loss='mse', optimizer='rmsprop', run_eagerly=testing_utils.should_run_eagerly(), experimental_run_tf_function=testing_utils. should_run_tf_function()) model.train_on_batch([x1, x2], [y1, y2]) model.train_on_batch({ 'input_1': x1, 'input_2': x2 }, { 'output_1': y1, 'output_2': y2 })
def test_summary(self): class ToString(object): def __init__(self): self.contents = '' def __call__(self, msg): self.contents += msg + '\n' # Single-io model = model_util.SimpleTestModel(num_classes=4, use_bn=True, use_dp=True) model._set_inputs(np.ones((3, 4))) # need to build model first print_fn = ToString() model.summary(print_fn=print_fn) self.assertTrue('Trainable params: 356' in print_fn.contents) # Multi-io model = model_util.MultiIOTestModel(num_classes=(5, 6), use_bn=True, use_dp=True) model._set_inputs([np.ones((3, 4)), np.ones( (3, 4))]) # need to build model first print_fn = ToString() model.summary(print_fn=print_fn) self.assertTrue('Trainable params: 587' in print_fn.contents)
def test_attributes(self): # layers, weights, trainable_weights, non_trainable_weights, inputs, outputs num_classes = (2, 3) num_samples = 100 input_dim = 50 model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) x1 = np.ones((num_samples, input_dim)) x2 = np.ones((num_samples, input_dim)) y1 = np.zeros((num_samples, num_classes[0])) y2 = np.zeros((num_samples, num_classes[1])) self.assertEqual(model.name, 'test_model') self.assertEqual(model.built, False) self.assertEqual(len(model.weights), 0) model.compile(loss='mse', optimizer='rmsprop', run_eagerly=testing_utils.should_run_eagerly(), experimental_run_tf_function=testing_utils. should_run_tf_function()) model.train_on_batch([x1, x2], [y1, y2]) self.assertEqual(model.built, True) self.assertEqual(len(model.layers), 4) self.assertEqual(len(model.weights), 10) self.assertEqual(len(model.trainable_weights), 8) self.assertEqual(len(model.non_trainable_weights), 2) self.assertEqual(len(model.inputs), 2) self.assertEqual(len(model.outputs), 2)
def test_saving(self): num_classes = (2, 3) num_samples = 100 input_dim = 50 x1 = np.ones((num_samples, input_dim)) x2 = np.ones((num_samples, input_dim)) y1 = np.zeros((num_samples, num_classes[0])) y2 = np.zeros((num_samples, num_classes[1])) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) model.compile(loss='mse', optimizer='rmsprop', run_eagerly=testing_utils.should_run_eagerly(), experimental_run_tf_function=testing_utils. should_run_tf_function()) model.fit([x1, x2], [y1, y2], epochs=2, batch_size=32, verbose=0) y_ref_1, y_ref_2 = model.predict([x1, x2]) tf_format_name = os.path.join(self.get_temp_dir(), 'ckpt') model.save_weights(tf_format_name) if h5py is not None: hdf5_format_name = os.path.join(self.get_temp_dir(), 'weights.h5') model.save_weights(hdf5_format_name) model = model_util.MultiIOTestModel(num_classes=num_classes, use_bn=True) if h5py is not None: with self.assertRaises(ValueError): model.load_weights(hdf5_format_name) model.load_weights(tf_format_name) y1, y2 = model.predict([x1, x2]) self.assertAllClose(y_ref_1, y1, atol=1e-5) self.assertAllClose(y_ref_2, y2, atol=1e-5) if h5py is not None: model.load_weights(hdf5_format_name) y1, y2 = model.predict([x1, x2]) self.assertAllClose(y_ref_1, y1, atol=1e-5) self.assertAllClose(y_ref_2, y2, atol=1e-5)
def test_multi_io_workflow_with_tensors(self): num_classes = (2, 3) num_samples = 10 input_dim = 50 with self.cached_session(): model = model_util.MultiIOTestModel( num_classes=num_classes, use_dp=True, use_bn=True) model.compile(loss='mse', optimizer='rmsprop') x1 = array_ops.ones((num_samples, input_dim)) x2 = array_ops.ones((num_samples, input_dim)) y1 = array_ops.zeros((num_samples, num_classes[0])) y2 = array_ops.zeros((num_samples, num_classes[1])) model.fit([x1, x2], [y1, y2], epochs=2, steps_per_epoch=10, verbose=0) _ = model.evaluate(steps=10, verbose=0)
def test_multi_io_subclass_build(self): batch_size = None num_samples = 1000 input_dim = 50 model = model_util.MultiIOTestModel() self.assertFalse(model.built, 'Model should not have been built') self.assertFalse(model.weights, ('Model should have no weights since it ' 'has not been built.')) batch_input_shape = tensor_shape.TensorShape((batch_size, input_dim)) model.build( input_shape=[batch_input_shape, batch_input_shape]) self.assertTrue(model.weights, ('Model should have weights now that it ' 'has been properly built.')) self.assertTrue(model.built, 'Model should be built after calling `build`.') x1 = array_ops.ones((num_samples, input_dim)) x2 = array_ops.ones((num_samples, input_dim)) model([x1, x2])
def test_multi_io_workflow_with_numpy_arrays_and_custom_placeholders(self): num_classes = (2, 3) num_samples = 1000 input_dim = 50 with self.cached_session(): model = model_util.MultiIOTestModel( num_classes=num_classes, use_dp=True, use_bn=True) model.compile(loss='mse', optimizer='rmsprop') x1 = np.ones((num_samples, input_dim)) x2 = np.ones((num_samples, input_dim)) y1 = np.zeros((num_samples, num_classes[0])) y2 = np.zeros((num_samples, num_classes[1])) x2_placeholder = array_ops.placeholder( dtype='float32', shape=(None, input_dim)) model._set_inputs([x1, x2_placeholder]) model.fit([x1, x2], [y1, y2], epochs=2, batch_size=32, verbose=0) _ = model.evaluate([x1, x2], [y1, y2], verbose=0)
def test_multi_io_workflow_with_np_arrays(self): num_classes = (2, 3) num_samples = 1000 input_dim = 50 model = model_util.MultiIOTestModel(num_classes=num_classes, use_dp=True, use_bn=True) model.compile(loss='mse', optimizer='rmsprop', metrics=['acc'], run_eagerly=testing_utils.should_run_eagerly(), experimental_run_tf_function=testing_utils. should_run_tf_function()) x1 = np.ones((num_samples, input_dim)) x2 = np.ones((num_samples, input_dim)) y1 = np.zeros((num_samples, num_classes[0])) y2 = np.zeros((num_samples, num_classes[1])) model.fit([x1, x2], [y1, y2], epochs=2, batch_size=32, verbose=0) _ = model.evaluate([x1, x2], [y1, y2], verbose=0)