def __init__(self):
        super(CollectOutputAndTarget, self).__init__()
        self.inputs = []  # collect x_input batches
        self.targets = []  # collect y_true batches
        self.outputs = []  # collect y_pred batches

        # the shape of these 2 variables will change according to batch shape
        # to handle the "last batch", specify `validate_shape=False`
        self.var_x_input = tf.Variable(0., validate_shape=False)
        self.var_y_true = tf.Variable(0., validate_shape=False)
        self.var_y_pred = tf.Variable(0., validate_shape=False)

        self.train_plotObj = Plot('train', title='Train Predictions')
示例#2
0
    def __init__(self, config, num_agents, num_landmarks, folder_dir):
        super(GameModule, self).__init__()

        self.batch_size = config.batch_size  # scalar: num games in this batch
        self.using_utterances = config.use_utterances  # bool: whether current batch allows utterances
        self.using_cuda = config.use_cuda
        self.num_agents = num_agents  # scalar: number of agents in this batch
        self.num_landmarks = num_landmarks  # scalar: number of landmarks in this batch
        self.num_entities = self.num_agents + self.num_landmarks  # type: int
        self.world_dim = config.world_dim
        self.time_horizon = config.time_horizon
        self.num_epochs = config.num_epochs
        self.folder_dir = folder_dir
        if self.using_cuda:
            self.Tensor = torch.cuda.FloatTensor
        else:
            self.Tensor = torch.FloatTensor

        locations = torch.rand(self.batch_size, self.num_entities,
                               2) * config.world_dim
        self.colors = (torch.rand(self.batch_size, self.num_entities, 1) *
                       config.num_colors).floor()
        self.shapes = (torch.rand(self.batch_size, self.num_entities, 1) *
                       config.num_shapes).floor()

        goal_agents = self.Tensor(self.batch_size, self.num_agents, 1)
        goal_entities = (torch.rand(self.batch_size, self.num_agents, 1) *
                         self.num_landmarks).floor().long() + self.num_agents
        goal_locations = self.Tensor(self.batch_size, self.num_agents, 2)

        if self.using_cuda:
            locations = locations.cuda()
            self.colors = self.colors.cuda()
            self.shapes = self.shapes.cuda()
            goal_entities = goal_entities.cuda()

        # [batch_size, num_entities, 2]
        self.locations = Variable(locations)
        # [batch_size, num_entities, 2]
        self.physical = Variable(
            torch.cat((self.colors, self.shapes), 2).float())

        for b in range(self.batch_size):
            goal_agents[b, :, 0] = torch.randperm(self.num_agents)

        for b in range(self.batch_size):
            goal_locations[b] = self.locations.data[b][
                goal_entities[b].squeeze()]

        # [batch_size, num_agents, 3]
        self.goals = Variable(torch.cat((goal_locations, goal_agents), 2))
        goal_agents = Variable(goal_agents)

        if self.using_cuda:
            self.memories = {
                "physical":
                Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                self.num_entities, config.memory_size).cuda()),
                "action":
                Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                config.memory_size).cuda())
            }
        else:
            self.memories = {
                "physical":
                Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                self.num_entities, config.memory_size)),
                "action":
                Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                config.memory_size))
            }

        if self.using_utterances:
            if self.using_cuda:
                self.utterances = Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                config.vocab_size).cuda())
                self.memories["utterance"] = Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                self.num_agents, config.memory_size).cuda())
            else:
                self.utterances = Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                config.vocab_size))
                self.memories["utterance"] = Variable(
                    torch.zeros(self.batch_size, self.num_agents,
                                self.num_agents, config.memory_size))

        agent_baselines = self.locations[:, :self.num_agents, :]
        goals_by_landmark = torch.cat(
            (goal_entities.type(torch.FloatTensor), goal_agents), 2).float()

        sort_idxs = torch.sort(self.goals[:, :, 2])[1]
        self.sorted_goals = Variable(self.Tensor(self.goals.size()))
        # TODO: Bad for loop?
        for b in range(self.batch_size):
            self.sorted_goals[b] = self.goals[b][sort_idxs[b]]
        self.sorted_goals = self.sorted_goals[:, :, :2]

        # [batch_size, num_agents, num_entities, 2]
        self.observations = self.locations.unsqueeze(
            1) - agent_baselines.unsqueeze(2)

        new_obs = self.goals[:, :, :2] - agent_baselines

        # [batch_size, num_agents, 2] [batch_size, num_agents, 1]
        self.observed_goals = torch.cat((new_obs, goal_agents), dim=2)

        self.plots_matrix = Plot(self.batch_size, self.time_horizon,
                                 self.num_entities, locations.shape[2],
                                 self.world_dim, self.num_agents,
                                 goals_by_landmark, self.folder_dir)
        self.plots_matrix.save_plot_matrix("start", locations, self.colors,
                                           self.shapes)
def test():
    # Local Variables
    num_test_images = None

    # -----------------------------------------
    #  Network Testing Model - Importing Graph
    # -----------------------------------------
    # Loads the dataset and restores a specified trained model.
    data = Dataloader()

    # Searches dataset images filenames
    if SELECTED_EVALUATION_SUBSET == 'test':
        _, _, _, _, num_test_images = data.get_test_data(
            test_split=args.test_split, test_file_path=args.test_file_path)
    elif SELECTED_EVALUATION_SUBSET == 'train':
        data.test_image_filenames, data.test_depth_filenames, _, _, num_test_images = data.get_train_data(
        )

    model = Test(data)

    with tf.Session() as sess:
        print('\n[Test] Loading the model...')
        load_model(saver=tf.train.Saver(), sess=sess)

        # ==============
        #  Testing Loop
        # ==============
        if args.debug:
            num_test_images = 5  # Only for testing!

        # TODO: Criar uma classe de test assim como fiz para train e valid, e declarar este objeto dentro dela
        if args.show_test_results:
            test_plot_obj = Plot(args.mode, title='Test Predictions')

        print("[Test] Generating Predictions...")
        pred_list, gt_list = [], []

        timer = -time.time()
        for i in tqdm(range(num_test_images)):
            timer2 = -time.time()

            # Evalute the network for the given image
            # data.test_depth_filenames = [] # Only for testing the following condition!!! # FIXME: Atualmente, o código não dá suporte para esta situação
            if data.test_depth_filenames:  # It's not empty
                feed_test = {
                    model.tf_image_key: data.test_image_filenames[i],
                    model.tf_depth_key: data.test_depth_filenames[i]
                }

                _, depth, depth_resized = sess.run(model.depth_op, feed_test)

            else:
                feed_test = {model.tf_image_key: data.test_image_filenames[i]}

            _, image, image_resized, pred, pred_up = sess.run(
                model.image_op + model.pred_op, feed_test)

            # Clips Predictions at 50, 80 meters
            try:
                pred_50, pred_80 = sess.run(
                    [model.tf_pred_50, model.tf_pred_80], feed_test)
            except AttributeError:
                pred_50 = np.zeros((model.batch_size, ) +
                                   model.output_size.get_size())
                pred_80 = np.zeros((model.batch_size, ) +
                                   model.output_size.get_size())

            # Fill arrays for later on metrics evaluation
            if args.eval_tool == 'monodepth':
                pred_list.append(pred_up[0, :, :, 0])
                gt_list.append(depth[:, :, 0])

            # Saves the predictions and ground truth depth images as uint16 PNG Images
            if SAVE_TEST_DISPARITIES or args.eval_tool == 'monodepth' or args.eval_tool == 'kitti_depth':
                # Convert the Predictions Images from float32 to uint16

                # print(data.test_image_filenames[i])

                imsave_as_uint16_png(
                    settings.output_tmp_pred_dir + 'pred' + str(i) + '.png',
                    pred_up[0])
                imsave_as_uint16_png(
                    settings.output_tmp_gt_dir + 'gt' + str(i) + '.png', depth)

            timer2 += time.time()

            # Show Results
            if args.show_test_results:
                # TODO: Fazer um lista the 'images_to_display' e dar append das imagens na lista
                test_plot_obj.show_test_results(
                    image=image,
                    depth=depth[:, :, 0],
                    image_resized=image_resized,
                    depth_resized=depth_resized[:, :, 0],
                    pred=pred[0, :, :, 0],
                    pred_up=pred_up[0, :, :, 0],
                    pred_50=pred_50[0, :, :, 0],
                    pred_80=pred_80[0, :, :, 0],
                    i=i + 1)

            # input("Continue...")

        # Testing Finished.
        timer += time.time()
        print("Time elapsed: {} s\n".format(timer))

        # =========
        #  Results
        # =========
        # Calculates Metrics
        if args.eval_tool:
            if data.test_depth_filenames:
                print(
                    "[Test/Metrics] Calculating Metrics based on Test Predictions..."
                )

                print('args.test_split:', args.test_split)
                print('args.test_file_path:', args.test_file_path)
                print('dataset_path:', data.dataset.dataset_path)
                print()

                # Invokes Evaluation Tools
                if args.eval_tool == 'monodepth':
                    pred_depths, gt_depths = metrics.generate_depth_maps(
                        pred_list, gt_list, data.dataset.dataset_path)
                    metrics.evaluation_tool_monodepth(pred_depths, gt_depths)
                elif args.eval_tool == 'kitti_depth':
                    metrics.evaluation_tool_kitti_depth(num_test_images)
            else:
                print(
                    "[Test/Metrics] It's not possible to calculate metrics. There are no corresponding labels for generated predictions!"
                )
        else:
            print("[Test/Metrics] Metrics calculation wasn't requested!")
示例#4
0
def test(args):
    print('[%s] Selected mode: Test' % appName)

    # Local Variables
    numSamples = None

    # -----------------------------------------
    #  Network Testing Model - Importing Graph
    # -----------------------------------------
    # Loads the dataset and restores a specified trained model.
    data = Dataloader(args)

    # Searches dataset images filenames
    if TEST_EVALUATE_SUBSET == 0:
        _, _, _, _, numSamples = data.getTestData()
    elif TEST_EVALUATE_SUBSET == 1:
        data.test_image_filenames, data.test_depth_filenames, tf_test_image_filenames, tf_test_depth_filenames, numSamples = data.getTrainData(
        )

    model = Test(args, data)

    with tf.Session() as sess:
        print('\n[network/Testing] Loading the model...')

        # Use to load from *.ckpt file
        saver = tf.train.Saver()
        saver.restore(sess, args.model_path)

        # Use to load from npy file
        # net.load(model_data_path, sess)

        # ==============
        #  Testing Loop
        # ==============
        if args.show_test_results:
            test_plotObj = Plot(args.mode, title='Test Predictions')

        timer = -time.time()
        pred_list, gt_list = [], []
        for i in range(numSamples):
            # for i in range(5): # Only for testing!

            timer2 = -time.time()

            # Evalute the network for the given image
            # data.test_depth_filenames = [] # Only for testing the following condition!!! # FIXME: Atualmente, o código não dá suporte para esta situação
            if data.test_depth_filenames:  # It's not empty
                feed_test = {
                    model.tf_image_key: data.test_image_filenames[i],
                    model.tf_depth_key: data.test_depth_filenames[i]
                }

                _, depth, depth_resized = sess.run(model.depth_op, feed_test)

            else:
                feed_test = {model.tf_image_key: data.test_image_filenames[i]}

            _, image, image_resized = sess.run(model.image_op, feed_test)
            pred, pred_up = sess.run(model.pred_op, feed_test)

            # Clips Predictions at 50, 80 meters
            try:
                pred_50, pred_80 = sess.run(
                    [model.tf_pred_50, model.tf_pred_80], feed_test)
            except AttributeError:
                pred_50 = np.zeros((model.batch_size, ) +
                                   model.output_size.getSize())
                pred_80 = np.zeros((model.batch_size, ) +
                                   model.output_size.getSize())

            # Fill arrays for later on metrics evaluation
            pred_list.append(pred_up[0, :, :, 0])
            gt_list.append(depth[:, :, 0])

            # Saves the Test Predictions as uint16 PNG Images
            if SAVE_TEST_DISPARITIES:
                # Convert the Predictions Images from float32 to uint16
                pred_up_uint16 = exposure.rescale_intensity(pred_up[0],
                                                            out_range='float')
                pred_up_uint16 = img_as_uint(pred_up_uint16)

                depth_uint16 = exposure.rescale_intensity(depth,
                                                          out_range='float')
                depth_uint16 = img_as_uint(depth_uint16)

                # Save PNG Images
                imageio.imsave("output/tmp/pred/pred" + str(i) + ".png",
                               pred_up_uint16)
                imageio.imsave("output/tmp/gt/gt" + str(i) + ".png",
                               depth_uint16)

            # Prints Testing Progress
            timer2 += time.time()
            print('step: %d/%d | t: %f | size(pred_list+gt_list): %d' %
                  (i + 1, numSamples, timer2,
                   total_size(pred_list) + total_size(gt_list)))
            # break # Test

            # Show Results
            if args.show_test_results:
                test_plotObj.showTestResults(image=image,
                                             depth=depth[:, :, 0],
                                             image_resized=image_resized,
                                             depth_resized=depth_resized[:, :,
                                                                         0],
                                             pred=pred[0, :, :, 0],
                                             pred_up=pred_up[0, :, :, 0],
                                             pred_50=pred_50[0, :, :, 0],
                                             pred_80=pred_80[0, :, :, 0],
                                             i=i + 1)

            # input("Continue...")

        # Testing Finished.
        timer += time.time()
        print("\n[Network/Testing] Testing FINISHED! Time elapsed: %f s" %
              timer)

        # =========
        #  Results
        # =========
        # Calculate Metrics
        if data.test_depth_filenames:
            print(
                "[Network/Testing] Calculating Metrics based on Testing Predictions..."
            )

            pred_array = np.array(pred_list)
            gt_array = np.array(gt_list)

            # LainaMetrics.evaluate(pred_array, gt_array) # FIXME:
            # myMetrics.evaluate(pred_array, gt_array) # FIXME:
            MonodepthMetrics.evaluate(pred_array, gt_array)

        else:
            print(
                "[Network/Testing] It's not possible to calculate Metrics. There are no corresponding labels for Testing Predictions!"
            )