コード例 #1
0
 def calculate_goal_success(self, epoch_range):
     if os.path.isfile('batch_succeed.txt'):
         pass
     else:
         with open('batch_succeed.txt', 'w+') as f:
             pass
     sucess_rate_epoch, sucess_rate_epoch_std = [], []
     error_in_dist = (self.error_in_dist**2 * 2)**0.5  # euclidean distance
     for epoch in epoch_range:
         dist_from_goal_per_agent = torch.tensor(Plot.extract_data(
             epoch, dir=self.dir + os.sep, calculate_dist='ON'),
                                                 dtype=torch.float)
         sucess_rate_batch = torch.sum(
             dist_from_goal_per_agent <= error_in_dist,
             dim=1) / dist_from_goal_per_agent.shape[1]
         idx_of_batches_suceeded = (sucess_rate_batch == 1).nonzero()
         with open('batch_succeed.txt', 'a+') as f:
             f.write(
                 'In Epoch {0} the Total num of batches whos agent succeeded is: {1}\n'
                 .format(epoch, idx_of_batches_suceeded.shape[0]))
             f.write('The batchs whos agents succeeded are:/n')
             for batch in idx_of_batches_suceeded:
                 f.write(str(batch.item()))
                 f.write(',')
             f.write('\n')
         sucess_rate_batch = sucess_rate_batch.type(torch.FloatTensor)
         sucess_rate_epoch += [torch.mean(sucess_rate_batch)]
         sucess_rate_epoch_std += [torch.std(sucess_rate_batch)]
     Plot.create_sucees_reate_plot(sucess_rate_epoch, sucess_rate_epoch_std,
                                   epoch_range)
コード例 #2
0
class CollectOutputAndTarget(Callback):
    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')

    def on_batch_end(self, batch, logs=None):
        # evaluate the variables and save them into lists
        x_input = K.eval(self.var_x_input)
        y_true = K.eval(self.var_y_true)
        y_pred = K.eval(self.var_y_pred)

        # print(y_true)
        # print(y_pred)
        # print(mat2uint8(y_true))
        # print(mat2uint8(y_pred))

        if showImages:
            self.train_plotObj.showTrainResults(x_input[0, :, :, :],
                                                y_true[0, :, :,
                                                       0], y_pred[0, :, :, 0])
コード例 #3
0
class CollectOutputAndTarget(Callback):
    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')

    def on_batch_end(self, batch, logs=None):
        # evaluate the variables and save them into lists
        x_input = K.eval(self.var_x_input)
        y_true = K.eval(self.var_y_true)
        y_pred = K.eval(self.var_y_pred)

        # print(y_true)
        # print(y_pred)
        # print(mat2uint8(y_true))
        # print(mat2uint8(y_pred))

        if showImages:
            self.train_plotObj.showTrainResults(x_input[0, :, :, :], y_true[0, :, :, 0], y_pred[0, :, :, 0])
コード例 #4
0
    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')
コード例 #5
0
    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')
コード例 #6
0
 def calculate(self, epoch_range, batch_range):
     for epoch in epoch_range:
         utterance = Plot.extract_data(epoch,
                                       dir=self.dir,
                                       calculate_utternace='ON')
         total_iterations = utterance.shape[1]
         for batch in batch_range:
             for iteration in range(total_iterations):
                 self.counter += 1
                 cur_utter = utterance[batch, iteration, :, :]
                 for j in range(cur_utter.shape[0]):
                     for i in range(cur_utter.shape[1]):
                         if cur_utter[j, i] >= threshold:
                             self.utterance_stat[i] += 1
     self.utterance_stat = np.true_divide(self.utterance_stat, self.counter)
     print(self.utterance_stat)
コード例 #7
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)
コード例 #8
0
class GameModule(nn.Module):
    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)

    """
    Updates game state given all movements and utterances and returns accrued cost
        - movements: [batch_size, num_agents, config.movement_size]
        - utterances: [batch_size, num_agents, config.utterance_size]
        - goal_predictions: [batch_size, num_agents, num_agents, config.goal_size]
    Returns:
        - scalar: total cost of all games in the batch
    """

    def forward(self, movements, goal_predictions, utterances, t):
        self.locations = self.locations + movements
        self.plots_matrix.save_plot_matrix(t, self.locations, self.colors,
                                           self.shapes)  ####
        agent_baselines = self.locations[:, :self.num_agents]
        self.observations = self.locations.unsqueeze(
            1) - agent_baselines.unsqueeze(2)
        new_obs = self.goals[:, :, :2] - agent_baselines
        goal_agents = self.goals[:, :, 2].unsqueeze(2)
        self.observed_goals = torch.cat((new_obs, goal_agents), dim=2)
        if self.using_utterances:
            self.utterances = utterances
            self.plots_matrix.save_utterance_matrix(utterances, t)  ####

            return self.compute_cost(movements, goal_predictions, utterances)
        else:
            return self.compute_cost(movements, goal_predictions)

    def compute_cost(self, movements, goal_predictions, utterances=None):
        physical_cost = self.compute_physical_cost()
        movement_cost = self.compute_movement_cost(movements)
        goal_pred_cost = self.compute_goal_pred_cost(goal_predictions)
        return physical_cost + goal_pred_cost + movement_cost

    """
    Computes the total cost agents get from being near their goals
    agent locations are stored as [batch_size, num_agents + num_landmarks, entity_embed_size]
    """

    def compute_physical_cost(self):
        return 2 * torch.sum(
            torch.sqrt(
                torch.sum(
                    torch.pow(
                        self.locations[:, :self.num_agents, :] -
                        self.sorted_goals, 2), -1)))

    """
    Computes the total cost agents get from predicting others' goals
    goal_predictions: [batch_size, num_agents, num_agents, goal_size]
    goal_predictions[., a_i, a_j, :] = a_i's prediction of a_j's goal with location relative to a_i
    We want:
        real_goal_locations[., a_i, a_j, :] = a_j's goal with location relative to a_i
    We have:
        goals[., a_j, :] = a_j's goal with absolute location
        observed_goals[., a_j, :] = a_j's goal with location relative to a_j
    Which means we want to build an observed_goals-like tensor but relative to each agent
        real_goal_locations[., a_i, a_j, :] = goals[., a_j, :] - locations[a_i]


    """

    def compute_goal_pred_cost(self, goal_predictions):
        relative_goal_locs = self.goals.unsqueeze(
            1)[:, :, :, :2] - self.locations.unsqueeze(
                2)[:, :self.num_agents, :, :]
        goal_agents = self.goals.unsqueeze(1)[:, :, :, 2:].expand_as(
            relative_goal_locs)[:, :, :, -1:]
        relative_goals = torch.cat((relative_goal_locs, goal_agents), dim=3)
        return torch.sum(
            torch.sqrt(
                torch.sum(torch.pow(goal_predictions - relative_goals, 2),
                          -1)))

    """
    Computes the total cost agents get from moving
    """

    def compute_movement_cost(self, movements):
        return torch.sum(torch.sqrt(torch.sum(torch.pow(movements, 2), -1)))

        #dist, dist_per_agent = game.get_avg_agent_to_goal_distance() #add to tensorboard

    def get_avg_agent_to_goal_distance(self):
        dist_from_goal = self.locations[:, :self.
                                        num_agents, :] - self.sorted_goals
        euclidean_distance_per_batch = torch.sqrt(
            torch.sum(torch.pow(dist_from_goal, 2), -1))
        return torch.sum(
            euclidean_distance_per_batch), euclidean_distance_per_batch
コード例 #9
0
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!")
コード例 #10
0
ファイル: plot_metrics.py プロジェクト: rtackx/Bipartite
def plot_metrics(directory_metrics, parts_directory_plot_metrics, legend):	
	print "\t\t[ Plotting metrics... ]"

	for metric in metrics:
		for i in range(0, 2):
			d = directory_metrics + "/" + parts[i] + "/" + metric + "_" + parts[i]

			print "\tPlotting %s ..." % d

			if not os.path.isdir(d):
				print "[%s directory not found]" % d
			else:
				xlog = False
				ylog = False
				if metric in metrics_log:
					xlog = True
					ylog = True				
				
				########################
				# PLOT DISTRIBUTION
				########################
				data_distribution = {}
				with open(d + "/" + metric + "_" + parts[i] + "_distribution.data", 'r') as file:
					for line in file:						
						line = line.replace("\n", "").split()
						data_distribution[modules.extra.num(line[0])] = modules.extra.num(line[1])
				
				title = ""
				xlabel = metric
				ylabel = ""
				plot_file = parts_directory_plot_metrics[i] + "/" + xlabel + "_distribution"
				plot_parameters = dict(marker="o", markersize=11, linewidth=0, alpha=0.5, color=colorset[0])

				if data_distribution:
					P = Plot(plot_file, title, xlabel, ylabel, xlog, ylog, [legend])
					P.plot_single(data_distribution, plot_parameters)
				else:
					print "[Impossible to plot because no distribution data]"

				########################

				########################
				# PLOT CDF
				########################
				data_reverse_cdf = {}
				with open(d + "/" + metric + "_" + parts[i] + "_reverse_cdf.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_reverse_cdf[modules.extra.num(line[0])] = modules.extra.num(line[1])
				
				title = ""
				xlabel = metric
				ylabel = ""
				plot_file = parts_directory_plot_metrics[i] + "/" + xlabel + "_reverse_cdf"
				plot_parameters = dict(linestyle="-", marker="s", markersize=8, linewidth=2.5, markeredgecolor=colorset[0], color=colorset[0])
				
				if data_reverse_cdf:
					P = Plot(plot_file, title, xlabel, ylabel, False, False, [legend], formatter=True)
					P.plot_single(data_reverse_cdf, plot_parameters)
				else:
					print "[Impossible to plot because no reverse CDF data]"
コード例 #11
0
ファイル: plot_metrics.py プロジェクト: rtackx/Bipartite
def plot_correlations(directory_correlations, parts_directory_plot_correlations, legend):
	print "\t\t[ Plotting correlations... ]"

	for correlation in correlations:
		metrics = correlation.split("-")
		for i in range(0, 2):
			correlation = metrics[0] + "_" + parts[i] + "-" + metrics[1] + "_" + parts[i]
			d = directory_correlations + "/" + parts[i] + "/" + correlation

			if not os.path.isdir(d):
				print "[%s directory not found]" % d
			else:
				xlog = False
				ylog = False
				if metrics[0] in metrics_log:
					xlog = True			
				if metrics[1] in metrics_log:
					ylog = True

				print "\tPlotting %s ..." % d
				
				list_plot_parameters = []
				list_data = []

				########################
				# SCATTER POINTS 
				########################
				data_scatter = {}
				with open(d + "/" + correlation + "_scatter.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_scatter[modules.extra.num(line[0])] = modules.extra.num(line[1])
				highest = max(data_scatter.values())
				list_plot_parameters.append(dict(marker="o", markersize=3, linewidth=0, alpha=0.5, color=colorset[0]))
				list_data.append(data_scatter)

				########################
				# CURVE FITTING (AVG)
				########################
				data_avg_curve = {}
				with open(d + "/" + correlation + "_avg_curve.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_avg_curve[modules.extra.num(line[0])] = modules.extra.num(line[1])
				list_plot_parameters.append(dict(linestyle="--", marker="s", markersize=8, linewidth=2.5, color=colorset[1]))
				list_data.append(data_avg_curve)

				'''data_linear = {}
				with open(d + "/" + correlation + "_linear_regression.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						if line[0] in nodes or not nodes:
							data_linear[num(line[0])] = num(line[1])
				list_plot_parameters.append(dict(linestyle="-", marker="o", markersize=6, linewidth=1.5, alpha=0.75))
				list_data.append(data_linear)'''

				'''data_fitting_curve = {}
				with open(d + "/" + correlation + "_fitting_curve.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						if line[0] in nodes or not nodes:
							data_fitting_curve[num(line[0])] = num(line[1])
				list_plot_parameters.append(dict(linestyle="-", marker="o", markersize=6, linewidth=1.5, alpha=0.75))
				list_data.append(data_fitting_curve)'''

				title = ""
				xlabel = metrics[0]
				ylabel = metrics[1]
				plot_file = parts_directory_plot_correlations[i] + "/" + correlation

				try:
					P = Plot(plot_file, title, xlabel, ylabel, True, True, [legend], ylimit=[0, highest])
					P.plot_multiple(list_data, list_plot_parameters)
				except Exception, e:
					print "Error during plotting process : %s" % e
コード例 #12
0
ファイル: plot_datasets.py プロジェクト: rtackx/Bipartite
def plot_metrics(main_directory, list_directories, directory_dest_metrics, main_directory_paired = "", list_directories_paired = []):
	print "\t\t[ Plotting metrics... ]"	

	for metric in metrics:
		for j in range(0, 2):
			m = metric + "_" + parts[j]

			xlog = False
			ylog = False
			if metric in metrics_log:
				xlog = True
				ylog = True

			print "\tPlotting %s ..." % m
						
			xlabel = parts[j].title() + " " + metric
			
			########################
			# PLOT DISTRIBUTION
			########################
			list_plot_parameters = []
			list_data = []
			list_legend = []
			
			i = 0
			ci = 0
			for directory in list_directories:
				data_distribution = {}
				with open(main_directory + "/" + directory + "/metrics/" + parts[j] + "/" + m + "/" + m + "_distribution.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_distribution[modules.extra.num(line[0])] = modules.extra.num(line[1])
				list_data.append(data_distribution)
				if paired:
					list_plot_parameters.append(dict(marker=list_markers[i], markersize=12, linewidth=0, alpha=0.5, color=colorset_paired[ci]))
				else:
					list_plot_parameters.append(dict(marker=list_markers[i], markersize=12, linewidth=0, alpha=0.5, color=colorset[i]))
				list_legend.append(directory)
				
				if paired:
					data_distribution = {}
					with open(main_directory_paired + "/" + list_directories_paired[i] + "/metrics/" + parts[j] + "/" + m + "/" + m + "_distribution.data", 'r') as file:
						for line in file:
							line = line.replace("\n", "").split()
							data_distribution[modules.extra.num(line[0])] = modules.extra.num(line[1])
					list_data.append(data_distribution)
					list_plot_parameters.append(dict(marker=list_markers[i], markersize=12, linewidth=0, alpha=0.5, color=colorset_paired[ci + 1]))
					list_legend.append(list_directories_paired[i])
				
				i += 1
				ci += 2
			
			title = ""
			ylabel = ""
			plot_file = directory_dest_metrics + "/" + parts[j] + "/distribution_" + m

			P = Plot(plot_file, title, xlabel, ylabel, xlog, ylog, list_legend)
			P.plot_multiple(list_data, list_plot_parameters)
			########################

			########################
			# PLOT CDF
			########################
			list_plot_parameters = []
			list_data = []
			list_legend = []
			
			i = 0
			ci = 0
			for directory in list_directories:
				data_reverse_cdf = {}
				with open(main_directory + "/" + directory + "/metrics/" + parts[j] + "/" + m + "/" + m + "_reverse_cdf.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_reverse_cdf[modules.extra.num(line[0])] = modules.extra.num(line[1])
				list_data.append(data_reverse_cdf)
				if paired:				
					list_plot_parameters.append(dict(linestyle="-", marker=list_markers[i], markersize=14, linewidth=2.5, alpha=0.5, markeredgecolor=colorset_paired[ci], color=colorset_paired[ci]))
				else:
					list_plot_parameters.append(dict(linestyle="-", marker=list_markers[i], markersize=14, linewidth=2.5, alpha=0.5, markeredgecolor=colorset[i], color=colorset[i]))
				list_legend.append(directory)				

				if paired:
					data_reverse_cdf = {}
					with open(main_directory_paired + "/" + list_directories_paired[i] + "/metrics/" + parts[j] + "/" + m + "/" + m + "_reverse_cdf.data", 'r') as file:
						for line in file:
							line = line.replace("\n", "").split()
							data_reverse_cdf[modules.extra.num(line[0])] = modules.extra.num(line[1])
					list_data.append(data_reverse_cdf)
					list_plot_parameters.append(dict(linestyle="-", marker=list_markers[i], markersize=14, linewidth=2.5, alpha=0.5, markeredgecolor=colorset_paired[ci + 1], color=colorset_paired[ci + 1]))
					list_legend.append(list_directories_paired[i])

				i += 1
				ci += 2

			title = ""
			ylabel = ""
			plot_file = directory_dest_metrics + "/" + parts[j] + "/reverse_cdf_" + m

			P = Plot(plot_file, title, xlabel, ylabel, False, False, list_legend, formatter=True)
			P.plot_multiple(list_data, list_plot_parameters)
コード例 #13
0
ファイル: plot_datasets.py プロジェクト: rtackx/Bipartite
def plot_correlations(main_directory, list_directories, directory_dest_correlations, main_directory_paired = "", list_directories_paired = []):
	print "\t\t[ Plotting correlations... ]"

	for correlation in correlations:
		metrics = correlation.split("-")
		for j in range(0, 2):
			correlation = metrics[0] + "_" + parts[j] + "-" + metrics[1] + "_" + parts[j]

			xlog = False
			ylog = False
			if metrics[0] in metrics_log:
				xlog = True			
			if metrics[1] in metrics_log:
				ylog = True

			print "\tPlotting %s ..." % correlation

			xlabel = parts[j].title() + " " + metrics[0]
			ylabel = parts[j].title() + " " + metrics[1]

			list_plot_parameters = []
			list_data = []
			list_data_avg_curve = []
			list_plot_parameters_avg_curve = []
			list_legend = []
			highest = 0.0

			i = 0
			ci = 0
			for directory in list_directories:
				maximum = 0.0
				data_scatter = {}
				with open(main_directory + "/" + directory + "/correlations/" + parts[j] + "/" + correlation + "/" + correlation + "_scatter.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_scatter[modules.extra.num(line[0])] = modules.extra.num(line[1])
				maximum = max(data_scatter.values())
				if highest < maximum:
					highest = maximum				
				if paired:
					list_plot_parameters.append(dict(marker=list_markers[i], markersize=8, linewidth=0, alpha=0.5, color=colorset_paired[ci]))
				else:
					list_plot_parameters.append(dict(marker=list_markers[i], markersize=8, linewidth=0, alpha=0.5, color=colorset[i]))
				list_data.append(data_scatter)
								
				data_avg_curve = {}
				with open(main_directory + "/" + directory + "/correlations/" + parts[j] + "/" + correlation + "/" + correlation + "_avg_curve.data", 'r') as file:
					for line in file:
						line = line.replace("\n", "").split()
						data_avg_curve[modules.extra.num(line[0])] = modules.extra.num(line[1])
				if paired:					
					list_plot_parameters_avg_curve.append(dict(linestyle="--", marker=list_markers[i], markersize=13, linewidth=2.5, color=colorset_paired[ci]))
				else:
					list_plot_parameters_avg_curve.append(dict(linestyle="--", marker=list_markers[i], markersize=13, linewidth=2.5, color=colorset[i]))
				
				if data_avg_curve:
					list_data_avg_curve.append(data_avg_curve)
				else:
						print "[Impossible to retrieve average curve data from '%s']" % directory

				list_legend.append(directory)

				if paired:
					maximum = 0.0
					data_scatter = {}
					with open(main_directory_paired + "/" + list_directories_paired[i] + "/correlations/" + parts[j] + "/" + correlation + "/" + correlation + "_scatter.data", 'r') as file:
						for line in file:
							line = line.replace("\n", "").split()
							data_scatter[modules.extra.num(line[0])] = modules.extra.num(line[1])
					maximum = max(data_scatter.values())
					if highest < maximum:
						highest = maximum
					list_plot_parameters.append(dict(marker=list_markers[i], markersize=8, linewidth=0, alpha=0.5, color=colorset_paired[ci + 1]))
					list_data.append(data_scatter)

					data_avg_curve = {}
					with open(main_directory_paired + "/" + list_directories_paired[i] + "/correlations/" + parts[j] + "/" + correlation + "/" + correlation + "_avg_curve.data", 'r') as file:
						for line in file:
							line = line.replace("\n", "").split()
							data_avg_curve[modules.extra.num(line[0])] = modules.extra(line[1])
					list_plot_parameters_avg_curve.append(dict(linestyle="--", marker=list_markers[i], markersize=13, linewidth=2.5, color=colorset_paired[ci + 1]))
					
					if data_avg_curve:
						list_data_avg_curve.append(data_avg_curve)
					else:
						print "[Impossible to retrieve average curve data from %s]" % list_directories_paired[i]

					list_legend.append(list_directories_paired[i])
					
				i += 1
				ci += 2

			title = ""
			plot_file = directory_dest_correlations + "/" + parts[j] + "/" + correlation

			list_data.extend(list_data_avg_curve)
			list_plot_parameters.extend(list_plot_parameters_avg_curve)
			
			P = Plot(plot_file, title, xlabel, ylabel, xlog, ylog, list_legend, ylimit=[0, highest])
			P.plot_multiple(list_data, list_plot_parameters)
コード例 #14
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!"
            )