Exemplo n.º 1
0
    def test_run_vars(self):
        app_driver = get_initialised_driver()
        test_graph = app_driver.create_graph(app_driver.app, 1, True)
        test_tensor = test_graph.get_tensor_by_name("G/conv_bn_selu/conv_/w:0")
        train_eval_msgs = []
        test_vals = []

        def get_iter_msgs(_sender, **msg):
            """" Captures iter_msg and model values for testing"""
            train_eval_msgs.append(msg['iter_msg'])
            test_vals.append(sess.run(test_tensor))
            print(msg['iter_msg'].to_console_string())

        ITER_FINISHED.connect(get_iter_msgs)

        with self.test_session(graph=test_graph) as sess:
            GRAPH_CREATED.send(app_driver.app, iter_msg=None)
            SESS_STARTED.send(app_driver.app, iter_msg=None)
            iterations = IterationMessageGenerator(initial_iter=0,
                                                   final_iter=3,
                                                   validation_every_n=2,
                                                   validation_max_iter=1,
                                                   is_training_action=True)
            app_driver.loop(app_driver.app, iterations())

            # Check sequence of iterations
            self.assertRegexpMatches(train_eval_msgs[0].to_console_string(),
                                     'training')
            self.assertRegexpMatches(train_eval_msgs[1].to_console_string(),
                                     'training')
            self.assertRegexpMatches(train_eval_msgs[2].to_console_string(),
                                     'validation')
            self.assertRegexpMatches(train_eval_msgs[3].to_console_string(),
                                     'training')

            # Check durations
            for iter_msg in train_eval_msgs:
                self.assertGreater(iter_msg.iter_duration, 0.0)

            # Check training changes test tensor
            self.assertNotAlmostEqual(
                np.mean(np.abs(test_vals[0] - test_vals[1])), 0.0)
            self.assertNotAlmostEqual(
                np.mean(np.abs(test_vals[2] - test_vals[3])), 0.0)

            # Check validation doesn't change test tensor
            self.assertAlmostEqual(
                np.mean(np.abs(test_vals[1] - test_vals[2])), 0.0)

            app_driver.app.stop()

        ITER_FINISHED.disconnect(get_iter_msgs)
    def test_init(self):
        app_driver = get_initialised_driver()
        test_graph = app_driver.create_graph(app_driver.app, 1, True)

        app_driver.app.set_iteration_update = set_iteration_update
        app_driver.app.interpret_output = self.create_interpreter()

        app_driver.load_event_handlers([
            'niftynet.engine.handler_model.ModelRestorer',
            'niftynet.engine.handler_network_output.OutputInterpreter',
            'niftynet.engine.handler_sampler.SamplerThreading'
        ])
        with self.test_session(graph=test_graph) as sess:
            SESS_STARTED.send(app_driver.app, iter_msg=None)

            iterator = IterationMessageGenerator(is_training_action=False)
            app_driver.loop(app_driver.app, iterator())
        app_driver.app.stop()