Skip to content

Commit

Permalink
reformat code using black -l 200
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiyangZhou committed Oct 11, 2021
1 parent 1e5ec09 commit 61a9cd3
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 82 deletions.
4 changes: 1 addition & 3 deletions datasets/caltech101.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def __init__(self, cfg):
if os.path.exists(self.split_path):
train, val, test = OxfordPets.read_split(self.split_path, self.image_dir)
else:
train, val, test = DTD.read_and_split_data(
self.image_dir, ignored=IGNORED, new_cnames=NEW_CNAMES
)
train, val, test = DTD.read_and_split_data(self.image_dir, ignored=IGNORED, new_cnames=NEW_CNAMES)
OxfordPets.save_split(train, val, test, self.split_path, self.image_dir)

num_shots = cfg.DATASET.NUM_SHOTS
Expand Down
12 changes: 3 additions & 9 deletions datasets/dtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def __init__(self, cfg):
root = os.path.abspath(os.path.expanduser(cfg.DATASET.ROOT))
self.dataset_dir = os.path.join(root, self.dataset_dir)
self.image_dir = os.path.join(self.dataset_dir, "images")
self.split_path = os.path.join(
self.dataset_dir, "split_zhou_DescribableTextures.json"
)
self.split_path = os.path.join(self.dataset_dir, "split_zhou_DescribableTextures.json")

if os.path.exists(self.split_path):
train, val, test = OxfordPets.read_split(self.split_path, self.image_dir)
Expand All @@ -33,9 +31,7 @@ def __init__(self, cfg):
super().__init__(train_x=train, val=val, test=test)

@staticmethod
def read_and_split_data(
image_dir, p_trn=0.5, p_val=0.2, ignored=[], new_cnames=None
):
def read_and_split_data(image_dir, p_trn=0.5, p_val=0.2, ignored=[], new_cnames=None):
# The data are supposed to be organized into the following structure
# =============
# images/
Expand All @@ -48,9 +44,7 @@ def read_and_split_data(
categories.sort()

p_tst = 1 - p_trn - p_val
print(
f"Splitting into {p_trn:.0%} train, {p_val:.0%} val, and {p_tst:.0%} test"
)
print(f"Splitting into {p_trn:.0%} train, {p_val:.0%} val, and {p_tst:.0%} test")

def _collate(ims, y, c):
items = []
Expand Down
8 changes: 2 additions & 6 deletions datasets/eurosat.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def __init__(self, cfg):
if os.path.exists(self.split_path):
train, val, test = OxfordPets.read_split(self.split_path, self.image_dir)
else:
train, val, test = DTD.read_and_split_data(
self.image_dir, new_cnames=NEW_CNAMES
)
train, val, test = DTD.read_and_split_data(self.image_dir, new_cnames=NEW_CNAMES)
OxfordPets.save_split(train, val, test, self.split_path, self.image_dir)

num_shots = cfg.DATASET.NUM_SHOTS
Expand All @@ -49,8 +47,6 @@ def update_classname(self, dataset_old):
for item_old in dataset_old:
cname_old = item_old.classname
cname_new = NEW_CLASSNAMES[cname_old]
item_new = Datum(
impath=item_old.impath, label=item_old.label, classname=cname_new
)
item_new = Datum(impath=item_old.impath, label=item_old.label, classname=cname_new)
dataset_new.append(item_new)
return dataset_new
8 changes: 2 additions & 6 deletions datasets/oxford_flowers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def __init__(self, cfg):
self.image_dir = os.path.join(self.dataset_dir, "jpg")
self.label_file = os.path.join(self.dataset_dir, "imagelabels.mat")
self.lab2cname_file = os.path.join(self.dataset_dir, "cat_to_name.json")
self.split_path = os.path.join(
self.dataset_dir, "split_zhou_OxfordFlowers.json"
)
self.split_path = os.path.join(self.dataset_dir, "split_zhou_OxfordFlowers.json")

if os.path.exists(self.split_path):
train, val, test = OxfordPets.read_split(self.split_path, self.image_dir)
Expand Down Expand Up @@ -50,9 +48,7 @@ def read_data(self):
def _collate(ims, y, c):
items = []
for im in ims:
item = Datum(
impath=im, label=y - 1, classname=c # convert to 0-based label
)
item = Datum(impath=im, label=y - 1, classname=c) # convert to 0-based label
items.append(item)
return items

Expand Down
4 changes: 1 addition & 3 deletions datasets/stanford_cars.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def __init__(self, cfg):
if os.path.exists(self.split_path):
train, val, test = OxfordPets.read_split(self.split_path, self.dataset_dir)
else:
trainval_file = os.path.join(
self.dataset_dir, "devkit", "cars_train_annos.mat"
)
trainval_file = os.path.join(self.dataset_dir, "devkit", "cars_train_annos.mat")
test_file = os.path.join(self.dataset_dir, "cars_test_annos_withlabels.mat")
meta_file = os.path.join(self.dataset_dir, "devkit", "cars_meta.mat")
trainval = self.read_data("cars_train", trainval_file, meta_file)
Expand Down
12 changes: 3 additions & 9 deletions lpclip/feat_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,19 @@ def main(args):
parser = argparse.ArgumentParser()
parser.add_argument("--root", type=str, default="", help="path to dataset")
parser.add_argument("--output-dir", type=str, default="", help="output directory")
parser.add_argument(
"--config-file", type=str, default="", help="path to config file"
)
parser.add_argument("--config-file", type=str, default="", help="path to config file")
parser.add_argument(
"--dataset-config-file",
type=str,
default="",
help="path to config file for dataset setup",
)
parser.add_argument("--num-shot", type=int, default=1, help="number of shots")
parser.add_argument(
"--split", type=str, choices=["train", "val", "test"], help="which split"
)
parser.add_argument("--split", type=str, choices=["train", "val", "test"], help="which split")
parser.add_argument("--trainer", type=str, default="", help="name of trainer")
parser.add_argument("--backbone", type=str, default="", help="name of CNN backbone")
parser.add_argument("--head", type=str, default="", help="name of head")
parser.add_argument(
"--seed", type=int, default=-1, help="only positive value enables a fixed seed"
)
parser.add_argument("--seed", type=int, default=-1, help="only positive value enables a fixed seed")
parser.add_argument("--eval-only", action="store_true", help="evaluation only")
args = parser.parse_args()
main(args)
44 changes: 11 additions & 33 deletions lpclip/linear_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
parser.add_argument("--dataset", type=str, default="", help="path to dataset")
parser.add_argument("--num_step", type=int, default=8, help="number of steps")
parser.add_argument("--num_run", type=int, default=10, help="number of runs")
parser.add_argument(
"--feature_dir", type=str, default="clip_feat", help="feature dir path"
)
parser.add_argument("--feature_dir", type=str, default="clip_feat", help="feature dir path")
args = parser.parse_args()

dataset = args.dataset
Expand All @@ -30,17 +28,13 @@
test_acc_step_list = np.zeros([args.num_run, args.num_step])
for seed in range(1, args.num_run + 1):
np.random.seed(seed)
print(
f"-- Seed: {seed} --------------------------------------------------------------"
)
print(f"-- Seed: {seed} --------------------------------------------------------------")
# Sampling
all_label_list = np.unique(train_label)
selected_idx_list = []
for label in all_label_list:
label_collection = np.where(train_label == label)[0]
selected_idx = np.random.choice(
label_collection, size=num_shot, replace=False
)
selected_idx = np.random.choice(label_collection, size=num_shot, replace=False)
selected_idx_list.extend(selected_idx)

fewshot_train_feature = train_feature[selected_idx_list]
Expand All @@ -50,9 +44,7 @@
val_selected_idx_list = []
for label in all_label_list:
label_collection = np.where(val_label == label)[0]
selected_idx = np.random.choice(
label_collection, size=val_num_shot, replace=False
)
selected_idx = np.random.choice(label_collection, size=val_num_shot, replace=False)
val_selected_idx_list.extend(selected_idx)

fewshot_val_feature = val_feature[val_selected_idx_list]
Expand All @@ -62,9 +54,7 @@
search_list = [1e6, 1e4, 1e2, 1, 1e-2, 1e-4, 1e-6]
acc_list = []
for c_weight in search_list:
clf = LogisticRegression(
solver="lbfgs", max_iter=1000, penalty="l2", C=c_weight
).fit(fewshot_train_feature, fewshot_train_label)
clf = LogisticRegression(solver="lbfgs", max_iter=1000, penalty="l2", C=c_weight).fit(fewshot_train_feature, fewshot_train_label)
pred = clf.predict(fewshot_val_feature)
acc_val = sum(pred == fewshot_val_label) / len(fewshot_val_label)
acc_list.append(acc_val)
Expand All @@ -77,16 +67,12 @@
c_left, c_right = 1e-1 * c_peak, 1e1 * c_peak

def binary_search(c_left, c_right, seed, step, test_acc_step_list):
clf_left = LogisticRegression(
solver="lbfgs", max_iter=1000, penalty="l2", C=c_left
).fit(fewshot_train_feature, fewshot_train_label)
clf_left = LogisticRegression(solver="lbfgs", max_iter=1000, penalty="l2", C=c_left).fit(fewshot_train_feature, fewshot_train_label)
pred_left = clf_left.predict(fewshot_val_feature)
acc_left = sum(pred_left == fewshot_val_label) / len(fewshot_val_label)
print("Val accuracy (Left): {:.2f}".format(100 * acc_left), flush=True)

clf_right = LogisticRegression(
solver="lbfgs", max_iter=1000, penalty="l2", C=c_right
).fit(fewshot_train_feature, fewshot_train_label)
clf_right = LogisticRegression(solver="lbfgs", max_iter=1000, penalty="l2", C=c_right).fit(fewshot_train_feature, fewshot_train_label)
pred_right = clf_right.predict(fewshot_val_feature)
acc_right = sum(pred_right == fewshot_val_label) / len(fewshot_val_label)
print("Val accuracy (Right): {:.2f}".format(100 * acc_right), flush=True)
Expand All @@ -110,13 +96,9 @@ def binary_search(c_left, c_right, seed, step, test_acc_step_list):
print("Test Accuracy: {:.2f}".format(test_acc), flush=True)
test_acc_step_list[seed - 1, step] = test_acc

saveline = "{}, seed {}, {} shot, weight {}, test_acc {:.2f}\n".format(
dataset, seed, num_shot, c_final, test_acc
)
saveline = "{}, seed {}, {} shot, weight {}, test_acc {:.2f}\n".format(dataset, seed, num_shot, c_final, test_acc)
with open(
"./report/{}_s{}r{}_details.txt".format(
args.feature_dir, args.num_step, args.num_run
),
"./report/{}_s{}r{}_details.txt".format(args.feature_dir, args.num_step, args.num_run),
"a+",
) as writer:
writer.write(saveline)
Expand All @@ -133,16 +115,12 @@ def binary_search(c_left, c_right, seed, step, test_acc_step_list):
f"{dataset}, {num_shot} Shot, Round {step}: {c_left}/{c_right}",
flush=True,
)
c_left, c_right, seed, step, test_acc_step_list = binary_search(
c_left, c_right, seed, step, test_acc_step_list
)
c_left, c_right, seed, step, test_acc_step_list = binary_search(c_left, c_right, seed, step, test_acc_step_list)
# save results of last step
test_acc_list = test_acc_step_list[:, -1]
acc_mean = np.mean(test_acc_list)
acc_std = np.std(test_acc_list)
save_line = "{}, {} Shot, Test acc stat: {:.2f} ({:.2f})\n".format(
dataset, num_shot, acc_mean, acc_std
)
save_line = "{}, {} Shot, Test acc stat: {:.2f} ({:.2f})\n".format(dataset, num_shot, acc_mean, acc_std)
print(save_line, flush=True)
with open(
"./report/{}_s{}r{}.txt".format(args.feature_dir, args.num_step, args.num_run),
Expand Down
13 changes: 3 additions & 10 deletions trainers/coop.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def __init__(self, cfg, classnames, clip_model):
ctx_dim = clip_model.ln_final.weight.shape[0]
clip_imsize = clip_model.visual.input_resolution
cfg_imsize = cfg.INPUT.SIZE[0]
assert (
cfg_imsize == clip_imsize
), f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"

if ctx_init:
# use given words to initialize context vectors
Expand Down Expand Up @@ -242,9 +240,7 @@ def build_model(self):
# NOTE: only give prompt_learner to the optimizer
self.optim = build_optimizer(self.model.prompt_learner, cfg.OPTIM)
self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
self.register_model(
"prompt_learner", self.model.prompt_learner, self.optim, self.sched
)
self.register_model("prompt_learner", self.model.prompt_learner, self.optim, self.sched)

device_count = torch.cuda.device_count()
if device_count > 1:
Expand Down Expand Up @@ -304,9 +300,6 @@ def load_model(self, directory, epoch=None):
if "token_suffix" in state_dict:
del state_dict["token_suffix"]

print(
"Loading weights to {} "
'from "{}" (epoch = {})'.format(name, model_path, epoch)
)
print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
# set strict=False
self._models[name].load_state_dict(state_dict, strict=False)
4 changes: 1 addition & 3 deletions trainers/zsclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def build_model(self):
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
mean_text_features = mean_text_features + text_features
mean_text_features = mean_text_features / num_temp
mean_text_features = mean_text_features / mean_text_features.norm(
dim=-1, keepdim=True
)
mean_text_features = mean_text_features / mean_text_features.norm(dim=-1, keepdim=True)

self.text_features = mean_text_features
self.clip_model = clip_model

0 comments on commit 61a9cd3

Please sign in to comment.