Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making scripts compatible #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions datasets/mars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand All @@ -65,15 +66,14 @@ 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.

"""
reshape_fn = (
(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)
Expand All @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions datasets/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions train_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions train_mars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Expand All @@ -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))))

Expand All @@ -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))))

Expand All @@ -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()
Expand Down