Skip to content

Commit

Permalink
adding compute_baselines improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverGrainge committed Jul 12, 2024
1 parent 7b411b3 commit 71c1671
Show file tree
Hide file tree
Showing 10 changed files with 538 additions and 450 deletions.
10 changes: 2 additions & 8 deletions PlaceRec/Methods/hybridnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def __call__(self, tensor):

class HybridNet(SingleStageBaseModelWrapper):
def __init__(self, pretrained: bool = True):
name = "alexnet_hybridnet"
name = "hybridnet"
weight_path = join(config["weights_directory"], name + ".ckpt")
self.model = HybridNetModel()
if pretrained:
Expand All @@ -204,10 +204,4 @@ def __init__(self, pretrained: bool = True):
model=self.model, preprocess=preprocess, name=name, weight_path=None
)
# some layers not implemented on metal
self.set_device(self.device)

def set_device(self, device: str) -> None:
if "mps" in device:
device = "cpu"
self.device = device
self.model.to(device)
self.set_device("cpu")
5 changes: 2 additions & 3 deletions compute_baselines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
from PlaceRec.Evaluate import Eval

datasets = ["Pitts30k_Val"]
methods = ["ResNet34_NetVLAD", "ResNet34_MixVPR", "ResNet34_GeM", "ResNet34_ConvAP"]

columns = ["method_name", "model_memory"]
methods = ["MixVPR", "DinoSalad", "EigenPlaces", "SFRS"]

batch_size = 24

columns = ["method_name", "model_memory"]
for ds in datasets:
columns += [f"{ds}_total_memory", f"{ds}_R1", f"{ds}_map_memory"]
df = pd.DataFrame(columns=columns)
Expand Down
16 changes: 8 additions & 8 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Desktop
datasets_directory: "/media/oliver/9e8e8649-c1f2-4524-9025-f2c751d67f57/home/oliver/Documents/datasets/Datasets"
weights_directory: "/media/oliver/9e8e8649-c1f2-4524-9025-f2c751d67f57/home/oliver/Documents/VisualLoc_Weights"
#datasets_directory: "/media/oliver/9e8e8649-c1f2-4524-9025-f2c751d67f57/home/oliver/Documents/datasets/Datasets"
#weights_directory: "/media/oliver/9e8e8649-c1f2-4524-9025-f2c751d67f57/home/oliver/Documents/VisualLoc_Weights"
# Laptop
#weights_directory: "/Users/olivergrainge/Documents/github/VisualLoc_Weights"
#datasets_directory: "/Users/olivergrainge/Documents/github/Datasets"
weights_directory: "/Users/olivergrainge/Documents/github/VisualLoc_Weights"
datasets_directory: "/Users/olivergrainge/Documents/github/Datasets"
#HPC
#datasets_directory: "/scratch/oeg1n18/datasets"
#weights_directory: "/scratch/oeg1n18/VisualLoc_Weights"
Expand All @@ -16,10 +16,10 @@ weights_directory: "/media/oliver/9e8e8649-c1f2-4524-9025-f2c751d67f57/home/oliv
run:
datasets: ["Pitts30k"]
methods: ["MixVPR"]
num_workers: 16
num_workers: 0
pin_memory: False
batchsize: 198
device: "cuda"
batchsize: 32
device: "mps"

eval:
datasets: ["pitts30k"]
Expand All @@ -39,7 +39,7 @@ train:
eval_distance: "cosine"
finetune: False
checkpoint: False
num_workers: 16
num_workers: 0

# optimization
optimizer: "adam"
Expand Down
54 changes: 29 additions & 25 deletions pruning_plots/memory/R1_total_memory.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import matplotlib.pyplot as plt
import pandas as pd
from brokenaxes import brokenaxes
import matplotlib.gridspec as gridspec
import numpy as np

aggregation = 1.5
# Define parameters
aggregation = 0.75
dataset = "Pitts30k_Val"

# Read data from CSV files
df = pd.read_csv("../results.csv")
baselines = pd.read_csv("../baselines.csv")

# Replace the method_name entries
df["method_name"] = df["method_name"].replace("ResNet50_GeM", "GeM")

# Filter the dataframe based on the specified aggregation
df = df[["method_name", f"{dataset}_total_memory", f"{dataset}_R1", "agg_rate"]]
df = df[df["agg_rate"] == aggregation]

# Create a figure with broken x-axis
fig = plt.figure(figsize=(7, 4))
gs = gridspec.GridSpec(1, 1)
bax = brokenaxes(xlims=((0, 400), (600, 700)), subplot_spec=gs[0], d=0.05)
# Filter the baselines dataframe for the same dataset
baselines = baselines[["method_name", f"{dataset}_total_memory", f"{dataset}_R1"]]

# Manually plotting
# Create a figure with normal axis
fig, ax = plt.subplots(figsize=(7, 4))

# Define color palette
color_palette = plt.get_cmap("viridis")(np.linspace(0, 1, df["method_name"].nunique()))
method_colors = dict(zip(df["method_name"].unique(), color_palette))

# Plot data on the normal axis
for name, group in df.groupby("method_name"):
bax.plot(
ax.plot(
group[f"{dataset}_total_memory"],
group[f"{dataset}_R1"],
label=name,
Expand All @@ -35,21 +38,22 @@
color=method_colors[name],
)

# Add baseline points
for i in range(len(baselines)):
row = baselines.iloc[i]
memory = float(row[f"{dataset}_total_memory"])
name = str(row["method_name"])
if "dino" not in name.lower():
# Plot baseline data points
for name, group in baselines.groupby("method_name"):
if name == "CosPlace":
continue
r1 = row[f"{dataset}_R1"]
bax.plot(memory, r1, "o", color="red") # Red dot for baseline
bax.text(memory - 5, r1 - 0.02, name, ha="center", va="bottom", fontsize=11)

bax.set_title(f"R@1 vs Memory Consumption for Pitts30k Val")
bax.set_xlabel("Memory (Mb)", fontsize=13, labelpad=20)
bax.set_ylabel("R@1", fontsize=13, labelpad=42)
bax.grid(True)
bax.legend(title="VPR Model", loc="center right")
plt.savefig("total_mem.jpg", dpi=500, format="jpg")
ax.scatter(
group[f"{dataset}_total_memory"],
group[f"{dataset}_R1"],
label=f"{name} (baseline)",
marker="x",
color="black",
)

# Add legend and labels
ax.legend()
ax.set_xlabel(f"{dataset}_total_memory")
ax.set_ylabel(f"{dataset}_R1")

# Show plot
plt.show()
13 changes: 12 additions & 1 deletion pruning_plots/preprocess_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@

df = pd.read_csv("../results.csv")

print(df["weight_path"])
# Function to extract sparsity value
def extract_sparsity(path):
parts = path.split("_")
num = float(parts[5])
return num


# Apply the function to the weight_path column
df["sparsity"] = df["weight_path"].apply(extract_sparsity)

# Convert the extracted values to numeric type
df["sparsity"] = pd.to_numeric(df["sparsity"])
df["agg_rate"] = df["weight_path"].str.extract("agg_([0-9]+\.?[0-9]*)")
df["agg_rate"] = pd.to_numeric(df["agg_rate"])

Expand Down
Loading

0 comments on commit 71c1671

Please sign in to comment.