From a394b145f948c5dc2b053a9436a9846e744d6651 Mon Sep 17 00:00:00 2001 From: jazzminewang Date: Fri, 7 Dec 2018 22:15:08 +0000 Subject: [PATCH] making scripts compatible --- datasets/mars.py | 33 ++++++++++++++++----------------- datasets/util.py | 5 ++--- train_app.py | 4 ++-- train_mars.py | 14 ++++++-------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/datasets/mars.py b/datasets/mars.py index 958d3ee7..8cd4b5cc 100644 --- a/datasets/mars.py +++ b/datasets/mars.py @@ -33,19 +33,20 @@ def read_train_test_directory_to_str(directory): def to_label(x): return int(x) if x.isdigit() else -1 - dirnames = os.listdir(directory) - image_filenames, ids, camera_indices, tracklet_indices = [], [], [], [] - for dirname in dirnames: - filenames = os.listdir(os.path.join(directory, dirname)) - filenames = [ - f for f in filenames if os.path.splitext(f)[1] == ".jpg"] - image_filenames += [ - os.path.join(directory, dirname, f) for f in filenames] - ids += [to_label(dirname) for _ in filenames] - camera_indices += [int(f[5]) for f in filenames] - tracklet_indices += [int(f[7:11]) for f in filenames] - - return image_filenames, ids, camera_indices, tracklet_indices + filenames = os.listdir(directory) + print("filenames") + print(filenames) + image_filenames, ids, tracklet_indices = [], [], [] + # for dirname in dirnames: + # filenames = os.listdir(os.path.join(directory, dirname)) + filenames = [ + f for f in filenames if os.path.splitext(f)[1] == ".jpg"] + image_filenames += [ + os.path.join(directory, f) for f in filenames] + ids += [int(f[0]) for f in filenames] + tracklet_indices += [int(f[f.find('_') + 1: f.find('.')]) for f in filenames] + + return image_filenames, ids, tracklet_indices def read_train_test_directory_to_image(directory, image_shape=(128, 64)): @@ -65,7 +66,6 @@ def read_train_test_directory_to_image(directory, image_shape=(128, 64)): * Tensor of images in BGR color space. * One dimensional array of unique IDs for the individuals in the images. - * One dimensional array of camera indices. * One dimensional array of tracklet indices. """ @@ -73,7 +73,7 @@ def read_train_test_directory_to_image(directory, image_shape=(128, 64)): (lambda x: x) if image_shape == IMAGE_SHAPE[:2] else (lambda x: cv2.resize(x, image_shape[::-1]))) - filenames, ids, camera_indices, tracklet_indices = ( + filenames, ids, tracklet_indices = ( read_train_test_directory_to_str(directory)) images = np.zeros((len(filenames), ) + image_shape + (3, ), np.uint8) @@ -83,9 +83,8 @@ def read_train_test_directory_to_image(directory, image_shape=(128, 64)): image = cv2.imread(filename, cv2.IMREAD_COLOR) images[i] = reshape_fn(image) ids = np.asarray(ids, dtype=np.int64) - camera_indices = np.asarray(camera_indices, dtype=np.int64) tracklet_indices = np.asarray(tracklet_indices, dtype=np.int64) - return images, ids, camera_indices, tracklet_indices + return images, ids, tracklet_indices def read_train_split_to_str(dataset_dir): diff --git a/datasets/util.py b/datasets/util.py index f2087c9b..e8666b0e 100644 --- a/datasets/util.py +++ b/datasets/util.py @@ -113,7 +113,7 @@ def limit_num_elements_per_identity(data_y, max_num_images_per_id, seed=None): return valid_mask -def create_cmc_probe_and_gallery(data_y, camera_indices=None, seed=None): +def create_cmc_probe_and_gallery(data_y, seed=None): """Create probe and gallery images for evaluation of CMC top-k statistics. For every identity, this function selects one image as probe and one image @@ -138,8 +138,7 @@ def create_cmc_probe_and_gallery(data_y, camera_indices=None, seed=None): """ data_y = np.asarray(data_y) - if camera_indices is None: - camera_indices = np.zeros_like(data_y, dtype=np.int) + camera_indices = np.zeros_like(data_y, dtype=np.int) camera_indices = np.asarray(camera_indices) random_generator = np.random.RandomState(seed=seed) diff --git a/train_app.py b/train_app.py index 105887fa..de59fdd2 100644 --- a/train_app.py +++ b/train_app.py @@ -292,7 +292,7 @@ def create_trainer(preprocess_fn, network_factory, read_from_file, image_shape, return trainer, train_op -def eval_loop(preprocess_fn, network_factory, data_x, data_y, camera_indices, +def eval_loop(preprocess_fn, network_factory, data_x, data_y, log_dir, eval_log_dir, image_shape=None, run_id=None, loss_mode="cosine-softmax", num_galleries=10, random_seed=4321): """Evaluate a running training session using CMC metric averaged over @@ -358,7 +358,7 @@ def eval_loop(preprocess_fn, network_factory, data_x, data_y, camera_indices, probes, galleries = [], [] for i in range(num_galleries): probe_indices, gallery_indices = util.create_cmc_probe_and_gallery( - data_y, camera_indices, seed=random_seed + i) + data_y, seed=random_seed + i) probes.append(probe_indices) galleries.append(gallery_indices) probes, galleries = np.asarray(probes), np.asarray(galleries) diff --git a/train_mars.py b/train_mars.py index 2595de4a..03f8b772 100644 --- a/train_mars.py +++ b/train_mars.py @@ -20,15 +20,14 @@ def __init__(self, dataset_dir, num_validation_y=0.1, seed=1234): self._seed = seed def read_train(self): - filenames, ids, camera_indices, _ = mars.read_train_split_to_str( + filenames, ids, _ = mars.read_train_split_to_str( self._dataset_dir) train_indices, _ = util.create_validation_split( np.asarray(ids, np.int64), self._num_validation_y, self._seed) filenames = [filenames[i] for i in train_indices] ids = [ids[i] for i in train_indices] - camera_indices = [camera_indices[i] for i in train_indices] - return filenames, ids, camera_indices + return filenames, ids def read_validation(self): filenames, ids, camera_indices, _ = mars.read_train_split_to_str( @@ -38,8 +37,7 @@ def read_validation(self): filenames = [filenames[i] for i in valid_indices] ids = [ids[i] for i in valid_indices] - camera_indices = [camera_indices[i] for i in valid_indices] - return filenames, ids, camera_indices + return filenames, ids def read_test_filenames(self): filename = os.path.join(self._dataset_dir, "info", "test_name.txt") @@ -60,7 +58,7 @@ def main(): dataset = Mars(args.dataset_dir, num_validation_y=0.1, seed=1234) if args.mode == "train": - train_x, train_y, _ = dataset.read_train() + train_x, train_y = dataset.read_train() print("Train set size: %d images, %d identities" % ( len(train_x), len(np.unique(train_y)))) @@ -72,7 +70,7 @@ def main(): net.preprocess, network_factory, train_x, train_y, num_images_per_id=4, image_shape=IMAGE_SHAPE, **train_kwargs) elif args.mode == "eval": - valid_x, valid_y, camera_indices = dataset.read_validation() + valid_x, valid_y = dataset.read_validation() print("Validation set size: %d images, %d identities" % ( len(valid_x), len(np.unique(valid_y)))) @@ -81,7 +79,7 @@ def main(): add_logits=args.loss_mode == "cosine-softmax") eval_kwargs = train_app.to_eval_kwargs(args) train_app.eval_loop( - net.preprocess, network_factory, valid_x, valid_y, camera_indices, + net.preprocess, network_factory, valid_x, valid_y, image_shape=IMAGE_SHAPE, num_galleries=20, **eval_kwargs) elif args.mode == "export": filenames = dataset.read_test_filenames()