示例#1
0
    def build_paths_and_labels_iog_test(self):
        # initialize list of image paths and labels
        paths, face_centers, labels = [], [], []

        mat = loadmat(self.config.MAT_TEST_PATH)

        mat_paths, mat_centers, mat_labels = [
            p[0].split('\\')[-1] for p in mat['tecoll'][0][0]['name'][0]
        ], mat['tecoll'][0][0]['facePosSize'][:, [4, 5]], mat['tecoll'][0][0][
            'ageClass'][:, 0] if self.config.DATASET_TYPE == 'age' else mat[
                'tecoll'][0][0]['genClass'][:, 0]

        paths_info = {}

        for i, mat_path in enumerate(mat_paths):
            path_info = paths_info.get(mat_path, {
                'face_centers': [],
                'labels': []
            })
            path_info['face_centers'].append(mat_centers[i].tolist())
            path_info['labels'].append(
                self.to_label(str(mat_labels[i]), str(mat_labels[i])))
            paths_info[mat_path] = path_info

        for unique_path, path_info in paths_info.items():
            path = os.path.sep.join(
                [self.config.BASE_PATH, '*', f'{unique_path}'])
            unique_path = glob.glob(path)[0]
            paths.append(unique_path)

            face_centers.append(path_info['face_centers'])
            labels.append(path_info['labels'])

        return paths, face_centers, labels
示例#2
0
def get_paths_and_labels(splits):
    result = []
    paths = []
    for data in splits:
        buff = [x.split("/")[-1] for x in data]
        label = [int(x.split("?")[0]) for x in buff]
        buff1 = [str(x.split("?")[1]) for x in buff]
        path = ["../train/" + x for x in buff1]
        result.append(label)
        paths.append(path)
    print(
        "[INFO]There are {} training, {} validation and {} test labels".format(
            len(result[0]), len(result[1]), len(result[2])))
    labels = [result[ind] for ind, x in enumerate(result)]
    return paths, labels
示例#3
0
    def build_paths_and_labels_iog(self):
        # initialize list of image paths and labels
        paths, face_coords, labels = [], [], []

        # grab all person data files
        person_data_paths = os.path.sep.join(
            [self.config.BASE_PATH, '*', 'PersonData.txt'])
        person_data_paths = glob.glob(person_data_paths)

        # loop over person data files
        for person_data_path in person_data_paths:
            # get folder containing person data file
            folder = os.path.sep.join(
                [p for p in person_data_path.split(os.path.sep)[:-1]])

            # load contents of file
            rows = open(person_data_path).read().strip().split('\n')

            # loop over rows
            for i, row in enumerate(rows):
                # check to see the line is image name, if so append it to image paths
                if 'jpg' in row:
                    if i > 0:
                        face_coords.append(face_coord)
                        labels.append(label)
                    paths.append(os.path.sep.join([folder, row]))

                    # initialize lists containing face coords and age, gender label of each image
                    face_coord, label = [], []

                else:
                    # get face coords and append to face corrds list
                    face_coord.append([int(c) for c in row.split()[:4]])

                    # get age and gender and append to list
                    age, gender = row.split()[4:]
                    label.append(self.to_label(age, gender))

                if i == len(rows) - 1:
                    face_coords.append(face_coord)
                    labels.append(label)

        return paths, face_coords, labels
示例#4
0
    def build_paths_and_labels_adience(self):
        # initialize list of image paths and labels
        cross_paths, cross_labels, cross_paths_frontal, cross_labels_frontal = [], [], [], []

        # grab paths to folds file
        fold_paths = os.path.sep.join(
            [self.config.LABELS_PATH, 'fold_[0-9]_data.txt'])
        fold_paths_frontal = os.path.sep.join(
            [self.config.LABELS_PATH, 'fold_frontal_*_data.txt'])
        fold_paths = glob.glob(fold_paths)
        fold_paths_frontal = glob.glob(fold_paths_frontal)
        fold_paths.sort()
        fold_paths_frontal.sort()

        # loop over folds paths
        for fold_path, fold_path_frontal in zip(fold_paths,
                                                fold_paths_frontal):
            paths, labels, paths_frontal, labels_frontal = [], [], [], []
            # load contents of folds file, skipping header
            rows = open(fold_path).read().strip().split('\n')[1:]
            rows_frontal = open(fold_path_frontal).read().strip().split(
                '\n')[1:]

            # loop over rows
            for row in rows:
                # unpack needed components of row
                user_id, image_path, face_id, age, gender = row.split('\t')[:5]

                if self.config.DATASET_TYPE == 'age' and age[0] != '(':
                    continue

                if self.config.DATASET_TYPE == 'gender' and gender not in (
                        'f', 'm'):
                    continue

                # construct path to input image and build class label
                p = os.path.sep.join([
                    self.config.IMAGES_PATH, user_id,
                    f'landmark_aligned_face.{face_id}.{image_path}'
                ])
                label = self.to_label(age, gender)

                if label is None:
                    continue

                paths.append(p)
                labels.append(label)

            cross_paths.append(paths)
            cross_labels.append(labels)

            # loop over rows frontal
            for row_frontal in rows_frontal:
                # unpack needed components of row
                user_id, image_path, face_id, age, gender = row_frontal.split(
                    '\t')[:5]

                if self.config.DATASET_TYPE == 'age' and age[0] != '(':
                    continue

                if self.config.DATASET_TYPE == 'gender' and gender not in (
                        'f', 'm'):
                    continue

                # construct path to input image and build class label
                p = os.path.sep.join([
                    self.config.IMAGES_PATH, user_id,
                    f'landmark_aligned_face.{face_id}.{image_path}'
                ])
                label = self.to_label(age, gender)

                if label is None:
                    continue

                paths_frontal.append(p)
                labels_frontal.append(label)

            cross_paths_frontal.append(paths_frontal)
            cross_labels_frontal.append(labels_frontal)

        for i, (test_paths, test_labels, test_paths_frontal,
                test_labels_frontal) in enumerate(
                    zip(cross_paths, cross_labels, cross_paths_frontal,
                        cross_labels_frontal)):
            train_paths, train_labels = [], []
            for j in range(len(fold_paths)):
                if j != i:
                    train_paths.extend(cross_paths[j])
                    train_labels.extend(cross_labels[j])

            yield train_paths, train_labels, test_paths, test_labels, test_paths_frontal, test_labels_frontal
示例#5
0
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os

# grab the paths to the input images in our dataset
paths = []
cats = [f.path for f in os.scandir("Dataset/") if f.is_dir()]
# cats = [p.split("/")[-1] for p in cats]
print(cats)
for cat in cats:
    image_paths = [f.path for f in os.scandir(cat)]
    for ip in image_paths:
        paths.append(ip)
print(paths)
# initialize the list of known encodings and known names
knownEncodings = []
knownNames = []

# loop over the image paths
for (i, imagePath) in enumerate(paths):
    # extract the person name from the image path
    print("[INFO] processing image {}/{}".format(i + 1, len(paths)))
    name = imagePath.split(os.path.sep)[-2]
    # load the input image and convert it from BGR (OpenCV ordering)
    # to dlib ordering (RGB)
    image = cv2.imread(imagePath)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # detect the (x, y)-coordinates of the bounding boxes
示例#6
0
total_duplicates = 0

# First part, loop through the input images and get their hashes
print("Generating hashes...")
for img_path in img_paths:
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.resize(img, (hash_size + 1, hash_size))
    # Compute a horizontal gradient between adjacent column pixels
    diff = img[:, 1:] > img[:, :-1]
    # Convert the difference image to a hash
    img_hash = sum([2**i for (i, v) in enumerate(diff.flatten()) if v])
    # Find any other image paths with the same hash and add the current image
    paths = hashes.get(img_hash, [])
    # And store the list of paths back in the hashes dictionary
    paths.append(img_path)
    hashes[img_hash] = paths

# Second part, loop through the hashes and find duplicates
print("Finding duplicates...")
for (img_hash, hashed_paths) in hashes.items():
    # Is there more than one image at this place in the dictionary, these have the same hash
    if len(hashed_paths) > 1:
        # Display the duplicates
        if args["show"]:
            montage = None
            for path in hashed_paths:
                image = cv2.imread(path)
                image = cv2.resize(image, (150, 150))
                if montage is None:
                    montage = image
示例#7
0
def train_PG(logdir, path, sim_mode=False):

    start = time.time()

    # Initialize the ROS/Sim Environment
    env = ros_env.Env(path, train_mode=True, sim_mode=sim_mode)

    # initialize the ROS agent
    agent = Agent(path, sim_mode=sim_mode)

    # Set Up Logger
    setup_logger(logdir, locals())

    # Set random seeds
    tf.set_random_seed(agent.seed)
    np.random.seed(agent.seed)

    # Maximum length for episodes
    max_path_length = agent.max_path_length

    # Observation and action sizes
    ob_dim = agent.ob_dim
    ac_dim = agent.ac_dim
    """
    Placeholders for batch observations/actions/advantages in policy gradient 
    loss function.
    See Agent.build_computation_graph for notation
    
    sy_ob_no: placeholder for observations
    sy_ac_na: placeholder for actions
    sy_adv_n: placeholder for advantages
    """

    sy_ob_no = tf.placeholder(shape=[None, agent.ob_dim],
                              name="ob",
                              dtype=tf.float32)

    sy_ac_na = tf.placeholder(shape=[None, agent.ac_dim],
                              name="ac",
                              dtype=tf.float32)

    sy_adv_n = tf.placeholder(shape=[None], name="adv", dtype=tf.float32)
    """ 
    The policy takes in an observation and produces a distribution 
    over the action space
    Constructs the symbolic operation for the policy network outputs,
    which are the parameters of the policy distribution p(a|s)
    """

    # output activations are left linear by not passing any arg
    sy_mean = build_mlp(sy_ob_no,
                        agent.ac_dim,
                        "policy-ddpg",
                        agent.n_layers,
                        agent.size,
                        activation=tf.tanh)
    #print sy_mean.name

    sy_logstd = tf.Variable(tf.zeros([1, agent.ac_dim]),
                            dtype=tf.float32,
                            name="logstd")
    """ 
    Constructs a symbolic operation for stochastically sampling from 
    the policy distribution
    
    use the reparameterization trick:
    The output from a Gaussian distribution with mean 'mu' and std 
    'sigma' is
    
    mu + sigma * z,         z ~ N(0, I)
    
    This reduces the problem to just sampling z. 
    (use tf.random_normal!)
    """

    sy_sampled_ac = sy_mean + tf.exp(sy_logstd) * tf.random_normal(
        tf.shape(sy_mean))
    """ 
    We can also compute the logprob of the actions that were
    actually taken by the policy. This is used in the loss function.
    
    Constructs a symbolic operation for computing the log probability 
    of a set of actions that were actually taken according to the policy
    use the log probability under a multivariate gaussian.
    """

    action_normalized = (sy_ac_na - sy_mean) / tf.exp(sy_logstd)
    sy_logprob_n = -0.5 * tf.reduce_sum(tf.square(action_normalized), axis=1)

    #=================================================================#
    # Loss Function and Training Operation
    #=================================================================#

    loss = -tf.reduce_mean(tf.multiply(sy_logprob_n, sy_adv_n))
    update_op = tf.train.AdamOptimizer(agent.learning_rate).minimize(loss)

    #==============================================================#
    # Optional Baseline
    #
    # Define placeholders for targets, a loss function and an update op
    # for fitting a neural network baseline. These will be used to fit the
    # neural network baseline.
    #===============================================================#
    if agent.nn_baseline:
        baseline_prediction = tf.squeeze(
            build_mlp(sy_ob_no,
                      1,
                      "nn_baseline",
                      n_layers=agent.n_layers,
                      size=agent.size))
        sy_target_n = tf.placeholder(shape=[None],
                                     name="sy_target_n",
                                     dtype=tf.float32)
        baseline_loss = tf.nn.l2_loss(baseline_prediction - sy_target_n)
        baseline_update_op = tf.train.AdamOptimizer(
            agent.learning_rate).minimize(baseline_loss)

    # tensorflow: config, session, variable initialization
    tf_config = tf.ConfigProto(inter_op_parallelism_threads=1,
                               intra_op_parallelism_threads=1)
    sess = tf.Session(config=tf_config)
    sess.__enter__()  # equivalent to `with sess:`
    tf.global_variables_initializer().run()  #pylint: disable=E1101

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    #====================================================================#
    # Training Loop
    #====================================================================#

    total_timesteps = 0

    for itr in range(agent.n_iter):
        print("********** Iteration %i ************" % itr)
        itr_mesg = "Iteration started at "
        itr_mesg += time.strftime("%d-%m-%Y_%H-%M-%S")
        print(itr_mesg)

        # Collect paths until we have enough timesteps
        timesteps_this_batch = 0
        paths = []
        while True:
            ob = env.reset()
            obs, acs, rewards = [], [], []
            steps = 0
            while True:
                #time.sleep(0.05)
                obs.append(ob)
                ac = sess.run(sy_sampled_ac, feed_dict={sy_ob_no: ob[None]})
                #print sy_sampled_ac.name (add:0)
                #print sy_ob_no.name (ob:0)
                ac = ac[0]
                acs.append(ac)
                # returns obs, reward and done status
                ob, rew, done = env.step(ac)
                rewards.append(rew)
                steps += 1
                if done or steps > agent.max_path_length:
                    break
            path = {
                "observation": np.array(obs, dtype=np.float32),
                "reward": np.array(rewards, dtype=np.float32),
                "action": np.array(acs, dtype=np.float32)
            }
            paths.append(path)
            timesteps_this_batch += pathlength(path)
            if timesteps_this_batch > agent.min_timesteps_per_batch:
                break

        total_timesteps += timesteps_this_batch
        '''
        # Build arrays for observation and action for the 
        # policy gradient update by concatenating across paths
        '''
        ob_no = np.concatenate([path["observation"] for path in paths])
        ac_na = np.concatenate([path["action"] for path in paths])
        re_n = [path["reward"] for path in paths]
        """
        Monte Carlo estimation of the Q function.        
        Estimates the returns over a set of trajectories.
        
        Store the Q-values for all timesteps and all trajectories in a 
        variable 'q_n', like the 'ob_no' and 'ac_na' above. 
        """

        if agent.reward_to_go:
            q_n = []
            for path in paths:
                q = np.zeros(pathlength(path))
                q[-1] = path['reward'][-1]
                for i in reversed(range(pathlength(path) - 1)):
                    q[i] = path['reward'][i] + agent.gamma * q[i + 1]
                q_n.extend(q)
        else:
            q_n = []
            for path in paths:
                ret_tau = 0
                for i in range(pathlength(path)):
                    ret_tau += (agent.gamma**i) * path['reward'][i]
                q = np.ones(shape=[pathlength(path)]) * ret_tau
                q_n.extend(q)
        """
        Compute advantages by (possibly) subtracting a baseline from the 
        estimated Q values let sum_of_path_lengths be the sum of the 
        lengths of the paths sampled.
        """
        #===========================================================#
        #
        # Computing Baselines
        #===========================================================#
        if agent.nn_baseline:
            # If nn_baseline is True, use your neural network to predict
            # reward-to-go at each timestep for each trajectory, and save the
            # result in a variable 'b_n' like 'ob_no', 'ac_na', and 'q_n'.
            #
            # rescale the output from the nn_baseline to match the
            # statistics (mean and std) of the current batch of Q-values.

            b_n = sess.run(baseline_prediction, feed_dict={sy_ob_no: ob_no})
            m1 = np.mean(b_n)
            s1 = np.std(b_n)
            m2 = np.mean(q_n)
            s2 = np.std(q_n)
            b_n = b_n - m1
            b_n = m2 + b_n * (s2 / (s1 + 1e-8))
            adv_n = q_n - b_n
        else:
            adv_n = q_n.copy()

        #=========================================================#
        # Advantage Normalization
        #=========================================================#
        if agent.normalize_advantages:
            # On the next line, implement a trick which is known
            # empirically to reduce variance
            # in policy gradient methods: normalize adv_n to have mean
            # zero and std=1.
            adv_n = preprocessing.scale(adv_n)
        """ 
        Update the parameters of the policy and (possibly) the neural 
        network baseline, which is trained to approximate the value function
        """
        #========================================================#
        # Optimizing Neural Network Baseline
        #========================================================#
        if agent.nn_baseline:
            # If a neural network baseline is used, set up the targets and
            # the inputs for the baseline.
            #
            # Fit it to the current batch in order to use for the next
            # iteration. Use the baseline_update_op you defined earlier.
            #
            # Instead of trying to target raw Q-values directly,
            # rescale the targets to have mean zero and std=1.

            target_n = preprocessing.scale(q_n)
            sess.run(baseline_update_op,
                     feed_dict={
                         sy_target_n: target_n,
                         sy_ob_no: ob_no
                     })

        #=================================================================#
        # Performing the Policy Update
        #=================================================================#

        # Call the update operation necessary to perform the policy
        # gradient update based on the current batch of rollouts.

        _, after_loss = sess.run([update_op, loss],
                                 feed_dict={
                                     sy_ob_no: ob_no,
                                     sy_ac_na: ac_na,
                                     sy_adv_n: adv_n
                                 })

        # Log diagnostics
        returns = [path["reward"].sum() for path in paths]
        ep_lengths = [pathlength(path) for path in paths]
        logz.log_tabular("Time", time.time() - start)
        logz.log_tabular("Iteration", itr)
        logz.log_tabular("AverageReturn", np.mean(returns))
        logz.log_tabular("StdReturn", np.std(returns))
        logz.log_tabular("MaxReturn", np.max(returns))
        logz.log_tabular("MinReturn", np.min(returns))
        logz.log_tabular("EpLenMean", np.mean(ep_lengths))
        logz.log_tabular("EpLenStd", np.std(ep_lengths))
        logz.log_tabular("TimestepsThisBatch", timesteps_this_batch)
        logz.log_tabular("TimestepsSoFar", total_timesteps)
        logz.log_tabular("After-Loss", after_loss)
        logz.dump_tabular()
        logz.pickle_tf_vars()

        model_file = os.path.join(logdir, "model.ckpt")
        save_path = saver.save(sess, model_file)
        print("Model saved in file: %s" % save_path)

    env.close_env_log()
示例#8
0
if __name__ == "__main__":
    parser = ArgumentParser(description='Tool for performing homographies')

    parser.add_argument('--input', required=True,
        help='Input path for a catalog')
    parser.add_argument('--outputDir', required=True,
        help='Path for the output, will be wiped clean before any operation')
    args = vars(parser.parse_args())

    if not os.path.exists(args["outputDir"]):
        os.makedirs(args["outputDir"])
    cap = cv2.VideoCapture(args["input"])
   
    paths = glob.glob(os.path.join(args["input"],"*.jpg"))
    paths.append(glob.glob(os.path.join(args["input"],"*.png")))
    paths.append(glob.glob(os.path.join(args["input"],"*.jpeg")))
    paths.sort()
    liste = []

    for path in paths:
        image = cv2.imread(path)
        liste.append(variance_of_laplacian(image))
    

    for i in range(10):
        index = np.argmin(liste)
        bestImage = cv2.imread(paths[i])
        cv2.imwrite(os.path.join(args["output"],paths[i].split("/")[-1]))
        del liste[i]