Added functionality to create clean dataset.

little technicality change in delf. does not change anything as features are renormalized before pca anyway
Todos:
process output information
parent 55bebe64
......@@ -48,9 +48,9 @@ def create_class_subset(source_csv_path, target_csv_path, number_of_desired_clas
class DatasetDownloader:
def __init__(self, source_csv_path, output_directory, image_base_width=500, number_of_processes=50):
def __init__(self, source_csv_path, output_directory, image_base_size=900, number_of_processes=50):
source_csv_path = Path(source_csv_path)
self.image_base_width = image_base_width
self.image_base_size = image_base_size
self.output_directory = Path(output_directory)
assert source_csv_path.is_file(), f"no source source_csv_path at {source_csv_path}!"
......@@ -85,8 +85,17 @@ class DatasetDownloader:
return
try:
pil_image_rgb = pil_image.convert('RGB')
image_height = int(pil_image_rgb.size[1]*(self.image_base_width/pil_image_rgb.size[0]))
pil_resized = pil_image_rgb.resize((self.image_base_width, image_height), Image.ANTIALIAS)
if pil_image_rgb.size[0] > self.image_base_size and pil_image_rgb.size[1] > self.image_base_size:
if pil_image_rgb.size[0] > pil_image_rgb.size[1]:
image_height = int(pil_image_rgb.size[1] * (self.image_base_size / pil_image_rgb.size[0]))
pil_resized = pil_image_rgb.resize((self.image_base_size, image_height), Image.ANTIALIAS)
else:
image_width = int(pil_image_rgb.size[0] * (self.image_base_size / pil_image_rgb.size[1]))
pil_resized = pil_image_rgb.resize((image_width, self.image_base_size), Image.ANTIALIAS)
else:
pil_resized = pil_image_rgb
#image_height = int(pil_image_rgb.size[1] * (self.image_base_size / pil_image_rgb.size[0]))
#pil_resized = pil_image_rgb.resize((self.image_base_size, image_height), Image.ANTIALIAS)
except:
print(f"Warning: Failed to convert and resize image {key}")
......@@ -155,6 +164,31 @@ def sort_images_into_data_folders(source_csv_path, input_path, output_path, vali
print(f"Skipping {image_id}: File not in {input_path}!")
def create_cleaned_class_subset(clean_csv_path, train_csv_path, out_csv_path, num_classes):
clean_csv_path = Path(clean_csv_path)
train_csv_path = Path(train_csv_path)
out_csv_path = Path(out_csv_path)
clean_data = pd.read_csv(clean_csv_path)
clean_data = clean_data.apply(lambda x: pd.Series([x[0], x[1].split(' '), len(x[1].split(' '))],
index=['landmark_id', 'images', 'image_count']), axis=1)
clean_data = clean_data.sort_values('image_count', ascending=False)
clean_data = clean_data[0:num_classes]
ids = []
for row in clean_data.iterrows():
ids.extend(row[1][1])
train_data = pd.read_csv(train_csv_path)
selection = []
for row in tqdm(train_data.iterrows()):
if row[1][0] in ids:
selection += [row[1]]
selection = pd.DataFrame(selection)
selection = selection.replace(to_replace='None', value=np.nan).dropna()
selection.to_csv(out_csv_path, index=False)
#create_cleaned_class_subset("train_clean.csv", "train.csv", "cleaned_subset.csv", 40)
if __name__ == '__main__':
fire.Fire({'define_subset': create_class_subset,
'download_data': DatasetDownloader,
......
......@@ -156,8 +156,10 @@ class Delf(torch.nn.Module):
if self.target_layer == "layer4":
feature = self.module_dict['layer4'](feature)
if self.use_l2_normalization:
feature = torch.nn.functional.normalize(feature, p=2, dim=1)
attention_score = self.module_dict['attention'](feature)
attention_feature = torch.nn.functional.normalize(feature, p=2, dim=1)
else:
attention_feature = feature
attention_score = self.module_dict['attention'](attention_feature)
return feature, attention_score
def extract_features(self, image_input):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment