Upload 4 files
Browse files- generate_occluded_imagenet.py +207 -0
- generate_occluded_imagenet_faster.py +139 -0
- generate_occluded_imagenet_single_occluder.py +156 -0
- occlusion_info.csv +0 -0
generate_occluded_imagenet.py
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import datasets, transforms, io
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_random_occluder(dataset_occluder):
|
| 10 |
+
index = random.randint(0, len(dataset_occluder) - 1)
|
| 11 |
+
texture_path, texture_class_index = dataset_occluder.imgs[index]
|
| 12 |
+
texture_class = dataset_occluder.classes[texture_class_index]
|
| 13 |
+
|
| 14 |
+
# load with the alpha channel
|
| 15 |
+
texture = io.read_image(texture_path, mode=io.image.ImageReadMode.RGB_ALPHA)
|
| 16 |
+
|
| 17 |
+
return texture, texture_class
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def resize_occluder(occluder_pil, target_area):
|
| 21 |
+
alpha = np.array(occluder_pil.getchannel('A'))
|
| 22 |
+
non_transparent_area = np.count_nonzero(alpha > 0)
|
| 23 |
+
|
| 24 |
+
area_scale_factor = target_area / non_transparent_area
|
| 25 |
+
|
| 26 |
+
width_scale_factor = np.sqrt(area_scale_factor * (occluder_pil.width / occluder_pil.height))
|
| 27 |
+
height_scale_factor = np.sqrt(area_scale_factor * (occluder_pil.height / occluder_pil.width))
|
| 28 |
+
|
| 29 |
+
new_width = occluder_pil.width * width_scale_factor
|
| 30 |
+
new_height = occluder_pil.height * height_scale_factor
|
| 31 |
+
|
| 32 |
+
resized_occluder = occluder_pil.resize((int(new_width), int(new_height)), Image.LANCZOS)
|
| 33 |
+
|
| 34 |
+
return resized_occluder
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def randomly_rotate_occluder(occluder_pil):
|
| 38 |
+
angle = random.uniform(-180, 180)
|
| 39 |
+
return occluder_pil.rotate(angle, resample=Image.BICUBIC, expand=True)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def calculate_distance(point1, point2):
|
| 43 |
+
x1, y1 = point1
|
| 44 |
+
x2, y2 = point2
|
| 45 |
+
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def try_rotations(occluder_pil, image_pil, target_area, pos1=None):
|
| 49 |
+
best_occluder = None
|
| 50 |
+
best_area = 0
|
| 51 |
+
best_pos = None
|
| 52 |
+
min_distance = 130 # min distance if two occluders
|
| 53 |
+
for _ in range(75): # increase number of attempts to find better position
|
| 54 |
+
rotated = randomly_rotate_occluder(occluder_pil)
|
| 55 |
+
resized = resize_occluder(rotated, target_area)
|
| 56 |
+
if resized.width > image_pil.width or resized.height > image_pil.height:
|
| 57 |
+
pos = (image_pil.width // 2 - resized.width // 2,
|
| 58 |
+
image_pil.height // 2 - resized.height // 2)
|
| 59 |
+
else:
|
| 60 |
+
max_x = max(0, image_pil.width - resized.width)
|
| 61 |
+
max_y = max(0, image_pil.height - resized.height)
|
| 62 |
+
pos = (random.randint(0, max_x), random.randint(0, max_y))
|
| 63 |
+
|
| 64 |
+
if pos1 is not None and calculate_distance(pos1, pos) < min_distance:
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
mask = Image.new('1', image_pil.size)
|
| 68 |
+
mask.paste(resized.getchannel('A'), pos, resized.getchannel('A'))
|
| 69 |
+
area = np.count_nonzero(np.array(mask))
|
| 70 |
+
|
| 71 |
+
if area > best_area:
|
| 72 |
+
best_area = area
|
| 73 |
+
best_occluder = resized
|
| 74 |
+
best_pos = pos
|
| 75 |
+
|
| 76 |
+
return best_occluder, best_pos
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def occlude_image(image, occluder_tensor, percentage_occlusion, occluded_dir, img_name):
|
| 80 |
+
occluder_pil = transforms.ToPILImage(mode='RGBA')(occluder_tensor)
|
| 81 |
+
image_pil = transforms.ToPILImage()(image)
|
| 82 |
+
|
| 83 |
+
binary_mask = Image.new('1', image_pil.size)
|
| 84 |
+
|
| 85 |
+
rn = random.random()
|
| 86 |
+
|
| 87 |
+
if rn < 0.5: # randomly use two occluders
|
| 88 |
+
occluder_resizing_factor = 0.5
|
| 89 |
+
else:
|
| 90 |
+
occluder_resizing_factor = 1.0
|
| 91 |
+
|
| 92 |
+
target_area = image_pil.width * image_pil.height * percentage_occlusion * occluder_resizing_factor
|
| 93 |
+
|
| 94 |
+
if rn < 0.5: # randomly use two occluders (can make this k occluders)
|
| 95 |
+
occluder_pil1, pos1 = try_rotations(occluder_pil, image_pil, target_area / 2)
|
| 96 |
+
image_pil.paste(occluder_pil1, pos1, occluder_pil1)
|
| 97 |
+
occluder_alpha1 = occluder_pil1.getchannel('A')
|
| 98 |
+
binary_mask.paste(occluder_alpha1, pos1, occluder_alpha1)
|
| 99 |
+
|
| 100 |
+
occluder_pil2, pos2 = try_rotations(occluder_pil, image_pil, target_area / 2, pos1)
|
| 101 |
+
if occluder_pil2 is not None and pos2 is not None:
|
| 102 |
+
image_pil.paste(occluder_pil2, pos2, occluder_pil2)
|
| 103 |
+
occluder_alpha2 = occluder_pil2.getchannel('A')
|
| 104 |
+
binary_mask.paste(occluder_alpha2, pos2, occluder_alpha2)
|
| 105 |
+
|
| 106 |
+
if pos2 is None:
|
| 107 |
+
pos = [pos1]
|
| 108 |
+
else:
|
| 109 |
+
pos = [pos1, pos2]
|
| 110 |
+
else:
|
| 111 |
+
occluder_pil, pos = try_rotations(occluder_pil, image_pil, target_area)
|
| 112 |
+
image_pil.paste(occluder_pil, pos, occluder_pil)
|
| 113 |
+
occluder_alpha = occluder_pil.getchannel('A')
|
| 114 |
+
binary_mask.paste(occluder_alpha, pos, occluder_alpha)
|
| 115 |
+
|
| 116 |
+
pos = [pos]
|
| 117 |
+
|
| 118 |
+
image_with_occluder_tensor = transforms.ToTensor()(image_pil)
|
| 119 |
+
|
| 120 |
+
mask_array = np.array(binary_mask)
|
| 121 |
+
mask_path = os.path.join(occluded_dir, f"{img_name}_mask.npy")
|
| 122 |
+
np.save(mask_path, mask_array)
|
| 123 |
+
|
| 124 |
+
return image_with_occluder_tensor, mask_path, pos
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def rebuild_display_mask(image_path, mask_path):
|
| 128 |
+
image_pil = Image.open(image_path)
|
| 129 |
+
binary_mask = Image.new('1', image_pil.size)
|
| 130 |
+
|
| 131 |
+
mask_array = np.load(mask_path)
|
| 132 |
+
mask_indices = np.transpose(np.nonzero(mask_array))
|
| 133 |
+
|
| 134 |
+
for i, j in mask_indices:
|
| 135 |
+
binary_mask.putpixel((j, i), 1)
|
| 136 |
+
|
| 137 |
+
binary_mask.show()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def build_dataset(data_path, transform):
|
| 141 |
+
dataset = datasets.ImageFolder(data_path, transform=transform)
|
| 142 |
+
nb_classes = len(dataset.classes)
|
| 143 |
+
return dataset, nb_classes
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def build_transform():
|
| 147 |
+
t = []
|
| 148 |
+
t.append(transforms.ToTensor())
|
| 149 |
+
return transforms.Compose(t)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def main():
|
| 153 |
+
data_dir = 'imagenet'
|
| 154 |
+
texture_dir = 'occluders_segmented'
|
| 155 |
+
occluded_data_dir = 'imagenet_occluded'
|
| 156 |
+
|
| 157 |
+
transform = build_transform()
|
| 158 |
+
dataset, nb_classes = build_dataset(data_dir, transform)
|
| 159 |
+
dataset_occluder, _ = build_dataset(texture_dir, transform)
|
| 160 |
+
percentage_occlusion = 0.3 # ~30%
|
| 161 |
+
|
| 162 |
+
occlusion_info = pd.DataFrame(columns=["image_name", "class_name", "occluder_class",
|
| 163 |
+
"percentage_occlusion", "mask", "pos"])
|
| 164 |
+
|
| 165 |
+
count = 0
|
| 166 |
+
|
| 167 |
+
for idx in range(len(dataset)):
|
| 168 |
+
image, label = dataset[idx]
|
| 169 |
+
category = dataset.classes[label]
|
| 170 |
+
|
| 171 |
+
in_dir = os.path.join(data_dir, category)
|
| 172 |
+
occluded_dir = os.path.join(occluded_data_dir, category)
|
| 173 |
+
|
| 174 |
+
os.makedirs(occluded_dir, exist_ok=True)
|
| 175 |
+
|
| 176 |
+
img_name = dataset.imgs[idx][0].split('/')[-1].split('.')[0]
|
| 177 |
+
|
| 178 |
+
occluder_tensor, occluder_class = get_random_occluder(dataset_occluder)
|
| 179 |
+
occluded_image, mask_path, pos = occlude_image(image, occluder_tensor,
|
| 180 |
+
percentage_occlusion,
|
| 181 |
+
occluded_dir,
|
| 182 |
+
img_name)
|
| 183 |
+
|
| 184 |
+
mask_array = np.load(mask_path)
|
| 185 |
+
actual_percentage_occlusion = np.count_nonzero(mask_array) / (image.shape[1] * image.shape[2])
|
| 186 |
+
|
| 187 |
+
occluded_image_path = os.path.join(occluded_dir, f"{img_name}_occluded.png")
|
| 188 |
+
transforms.ToPILImage()(occluded_image).save(occluded_image_path)
|
| 189 |
+
|
| 190 |
+
new_row = pd.DataFrame({
|
| 191 |
+
"image_name": [f"{img_name}_occluded.png"],
|
| 192 |
+
"class_name": [category],
|
| 193 |
+
"occluder_class": [occluder_class],
|
| 194 |
+
"percentage_occlusion": [actual_percentage_occlusion],
|
| 195 |
+
"mask": [mask_path],
|
| 196 |
+
"pos": [pos]
|
| 197 |
+
})
|
| 198 |
+
|
| 199 |
+
occlusion_info = pd.concat([occlusion_info, new_row], ignore_index=True)
|
| 200 |
+
if count % 50 == 0:
|
| 201 |
+
print("Folder: {}/1000 ({})".format(count / 50, category))
|
| 202 |
+
count+=1
|
| 203 |
+
|
| 204 |
+
occlusion_info.to_csv(os.path.join(occluded_data_dir, "occlusion_info.csv"), index=False)
|
| 205 |
+
|
| 206 |
+
if __name__ == "__main__":
|
| 207 |
+
main()
|
generate_occluded_imagenet_faster.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import datasets, transforms, io
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_random_texture(dataset_occluder):
|
| 11 |
+
index = random.randint(0, len(dataset_occluder) - 1)
|
| 12 |
+
texture_path, texture_class_index = dataset_occluder.imgs[index]
|
| 13 |
+
texture_class = dataset_occluder.classes[texture_class_index]
|
| 14 |
+
|
| 15 |
+
# Load the texture with the alpha channel
|
| 16 |
+
texture = io.read_image(texture_path, mode=io.image.ImageReadMode.RGB_ALPHA)
|
| 17 |
+
|
| 18 |
+
return texture, texture_class
|
| 19 |
+
|
| 20 |
+
def resize_occluder(occluder_pil, target_area, image_width, image_height):
|
| 21 |
+
alpha = np.array(occluder_pil.getchannel('A'))
|
| 22 |
+
non_transparent_area = np.count_nonzero(alpha > 0)
|
| 23 |
+
|
| 24 |
+
area_scale_factor = target_area / non_transparent_area
|
| 25 |
+
|
| 26 |
+
width_scale_factor = np.sqrt(area_scale_factor * (occluder_pil.width / occluder_pil.height))
|
| 27 |
+
height_scale_factor = np.sqrt(area_scale_factor * (occluder_pil.height / occluder_pil.width))
|
| 28 |
+
|
| 29 |
+
new_width = occluder_pil.width * width_scale_factor
|
| 30 |
+
new_height = occluder_pil.height * height_scale_factor
|
| 31 |
+
|
| 32 |
+
resized_occluder = occluder_pil.resize((int(new_width), int(new_height)), Image.LANCZOS)
|
| 33 |
+
|
| 34 |
+
return resized_occluder
|
| 35 |
+
|
| 36 |
+
def randomly_rotate_occluder(occluder_pil):
|
| 37 |
+
angle = random.uniform(-180, 180)
|
| 38 |
+
return occluder_pil.rotate(angle, resample=Image.BICUBIC, expand=True)
|
| 39 |
+
|
| 40 |
+
def occlude_image(image, occluder_tensor, percentage_occlusion, occluded_dir, img_name):
|
| 41 |
+
occluder_pil = transforms.ToPILImage(mode='RGBA')(occluder_tensor)
|
| 42 |
+
occluder_pil = randomly_rotate_occluder(occluder_pil)
|
| 43 |
+
image_pil = transforms.ToPILImage()(image)
|
| 44 |
+
|
| 45 |
+
target_area = image_pil.width * image_pil.height * percentage_occlusion
|
| 46 |
+
occluder_pil = resize_occluder(occluder_pil, target_area, image_pil.width, image_pil.height)
|
| 47 |
+
|
| 48 |
+
if occluder_pil.width > image_pil.width or occluder_pil.height > image_pil.height:
|
| 49 |
+
pos = (image_pil.width // 2 - occluder_pil.width // 2,
|
| 50 |
+
image_pil.height // 2 - occluder_pil.height // 2)
|
| 51 |
+
else:
|
| 52 |
+
max_x = max(0, image_pil.width - occluder_pil.width)
|
| 53 |
+
max_y = max(0, image_pil.height - occluder_pil.height)
|
| 54 |
+
pos = (random.randint(0, max_x), random.randint(0, max_y))
|
| 55 |
+
|
| 56 |
+
image_pil.paste(occluder_pil, pos, occluder_pil)
|
| 57 |
+
image_with_occluder_tensor = transforms.ToTensor()(image_pil)
|
| 58 |
+
|
| 59 |
+
occluder_alpha = occluder_pil.getchannel('A')
|
| 60 |
+
binary_mask = Image.new('1', image_pil.size)
|
| 61 |
+
binary_mask.paste(occluder_alpha, pos, occluder_alpha)
|
| 62 |
+
|
| 63 |
+
mask_array = np.array(binary_mask)
|
| 64 |
+
mask_path = os.path.join(occluded_dir, f"{img_name}_mask.npy")
|
| 65 |
+
np.save(mask_path, mask_array)
|
| 66 |
+
|
| 67 |
+
return image_with_occluder_tensor, mask_path, pos
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def rebuild_display_mask(image_path, mask_path):
|
| 71 |
+
image_pil = Image.open(image_path)
|
| 72 |
+
binary_mask = Image.new('1', image_pil.size)
|
| 73 |
+
|
| 74 |
+
mask_array = np.load(mask_path)
|
| 75 |
+
mask_indices = np.transpose(np.nonzero(mask_array))
|
| 76 |
+
|
| 77 |
+
for i, j in mask_indices:
|
| 78 |
+
binary_mask.putpixel((j, i), 1)
|
| 79 |
+
|
| 80 |
+
binary_mask.show()
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def build_dataset(data_path, transform):
|
| 84 |
+
dataset = datasets.ImageFolder(data_path, transform=transform)
|
| 85 |
+
nb_classes = len(dataset.classes)
|
| 86 |
+
return dataset, nb_classes
|
| 87 |
+
|
| 88 |
+
def build_transform():
|
| 89 |
+
t = []
|
| 90 |
+
t.append(transforms.ToTensor())
|
| 91 |
+
return transforms.Compose(t)
|
| 92 |
+
|
| 93 |
+
def main():
|
| 94 |
+
data_dir = 'imagenet1'
|
| 95 |
+
texture_dir = 'occluders_segmented'
|
| 96 |
+
occluded_data_dir = 'imagenet_occluded'
|
| 97 |
+
|
| 98 |
+
transform = build_transform()
|
| 99 |
+
dataset, nb_classes = build_dataset(data_dir, transform)
|
| 100 |
+
dataset_occluder, _ = build_dataset(texture_dir, transform)
|
| 101 |
+
|
| 102 |
+
occlusion_info = pd.DataFrame(columns=["image_name", "class_name", "occluder_class",
|
| 103 |
+
"percentage_occlusion", "mask", "pos"])
|
| 104 |
+
|
| 105 |
+
for idx in range(len(dataset)):
|
| 106 |
+
image, label = dataset[idx]
|
| 107 |
+
category = dataset.classes[label]
|
| 108 |
+
|
| 109 |
+
in_dir = os.path.join(data_dir, category)
|
| 110 |
+
occluded_dir = os.path.join(occluded_data_dir, category)
|
| 111 |
+
|
| 112 |
+
os.makedirs(occluded_dir, exist_ok=True)
|
| 113 |
+
|
| 114 |
+
img_name = dataset.imgs[idx][0].split('/')[-1].split('.')[0]
|
| 115 |
+
|
| 116 |
+
occluder_tensor, occluder_class = get_random_texture(dataset_occluder)
|
| 117 |
+
occluded_image, mask_path, pos = occlude_image(image, occluder_tensor, 0.5, occluded_dir, img_name)
|
| 118 |
+
|
| 119 |
+
mask_array = np.load(mask_path)
|
| 120 |
+
actual_percentage_occlusion = np.count_nonzero(mask_array) / (image.shape[1] * image.shape[2])
|
| 121 |
+
|
| 122 |
+
occluded_image_path = os.path.join(occluded_dir, f"{img_name}_occluded.png")
|
| 123 |
+
transforms.ToPILImage()(occluded_image).save(occluded_image_path)
|
| 124 |
+
|
| 125 |
+
new_row = pd.DataFrame({
|
| 126 |
+
"image_name": [f"{img_name}_occluded.png"],
|
| 127 |
+
"class_name": [category],
|
| 128 |
+
"occluder_class": [occluder_class],
|
| 129 |
+
"percentage_occlusion": [actual_percentage_occlusion],
|
| 130 |
+
"mask": [mask_path],
|
| 131 |
+
"pos": [pos]
|
| 132 |
+
})
|
| 133 |
+
|
| 134 |
+
occlusion_info = pd.concat([occlusion_info, new_row], ignore_index=True)
|
| 135 |
+
|
| 136 |
+
occlusion_info.to_csv(os.path.join(occluded_data_dir, "occlusion_info.csv"), index=False)
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
main()
|
generate_occluded_imagenet_single_occluder.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import datasets, transforms, io
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_random_texture(dataset_occluder):
|
| 11 |
+
index = random.randint(0, len(dataset_occluder) - 1)
|
| 12 |
+
texture_path, texture_class_index = dataset_occluder.imgs[index]
|
| 13 |
+
texture_class = dataset_occluder.classes[texture_class_index]
|
| 14 |
+
|
| 15 |
+
# Load the texture with the alpha channel
|
| 16 |
+
texture = io.read_image(texture_path, mode=io.image.ImageReadMode.RGB_ALPHA)
|
| 17 |
+
|
| 18 |
+
return texture, texture_class
|
| 19 |
+
|
| 20 |
+
def resize_occluder(occluder_pil, target_area, image_width, image_height):
|
| 21 |
+
alpha = np.array(occluder_pil.getchannel('A'))
|
| 22 |
+
non_transparent_area = np.count_nonzero(alpha > 0)
|
| 23 |
+
|
| 24 |
+
area_scale_factor = target_area / non_transparent_area
|
| 25 |
+
|
| 26 |
+
width_scale_factor = np.sqrt(area_scale_factor * (occluder_pil.width / occluder_pil.height))
|
| 27 |
+
height_scale_factor = np.sqrt(area_scale_factor * (occluder_pil.height / occluder_pil.width))
|
| 28 |
+
|
| 29 |
+
new_width = occluder_pil.width * width_scale_factor
|
| 30 |
+
new_height = occluder_pil.height * height_scale_factor
|
| 31 |
+
|
| 32 |
+
resized_occluder = occluder_pil.resize((int(new_width), int(new_height)), Image.LANCZOS)
|
| 33 |
+
|
| 34 |
+
return resized_occluder
|
| 35 |
+
|
| 36 |
+
def randomly_rotate_occluder(occluder_pil):
|
| 37 |
+
angle = random.uniform(-180, 180)
|
| 38 |
+
return occluder_pil.rotate(angle, resample=Image.BICUBIC, expand=True)
|
| 39 |
+
|
| 40 |
+
def try_rotations(occluder_pil, image_pil, target_area):
|
| 41 |
+
best_occluder = None
|
| 42 |
+
best_area = 0
|
| 43 |
+
best_pos = None
|
| 44 |
+
for _ in range(10):
|
| 45 |
+
rotated = randomly_rotate_occluder(occluder_pil)
|
| 46 |
+
resized = resize_occluder(rotated, target_area, image_pil.width, image_pil.height)
|
| 47 |
+
if resized.width > image_pil.width or resized.height > image_pil.height:
|
| 48 |
+
pos = (image_pil.width // 2 - resized.width // 2,
|
| 49 |
+
image_pil.height // 2 - resized.height // 2)
|
| 50 |
+
else:
|
| 51 |
+
max_x = max(0, image_pil.width - resized.width)
|
| 52 |
+
max_y = max(0, image_pil.height - resized.height)
|
| 53 |
+
pos = (random.randint(0, max_x), random.randint(0, max_y))
|
| 54 |
+
|
| 55 |
+
mask = Image.new('1', image_pil.size)
|
| 56 |
+
mask.paste(resized.getchannel('A'), pos, resized.getchannel('A'))
|
| 57 |
+
area = np.count_nonzero(np.array(mask))
|
| 58 |
+
|
| 59 |
+
if area > best_area:
|
| 60 |
+
best_area = area
|
| 61 |
+
best_occluder = resized
|
| 62 |
+
best_pos = pos
|
| 63 |
+
return best_occluder, best_pos
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def occlude_image(image, occluder_tensor, percentage_occlusion, occluded_dir, img_name):
|
| 67 |
+
occluder_pil = transforms.ToPILImage(mode='RGBA')(occluder_tensor)
|
| 68 |
+
image_pil = transforms.ToPILImage()(image)
|
| 69 |
+
|
| 70 |
+
target_area = image_pil.width * image_pil.height * percentage_occlusion
|
| 71 |
+
occluder_pil, pos = try_rotations(occluder_pil, image_pil, target_area)
|
| 72 |
+
|
| 73 |
+
image_pil.paste(occluder_pil, pos, occluder_pil)
|
| 74 |
+
image_with_occluder_tensor = transforms.ToTensor()(image_pil)
|
| 75 |
+
|
| 76 |
+
occluder_alpha = occluder_pil.getchannel('A')
|
| 77 |
+
binary_mask = Image.new('1', image_pil.size)
|
| 78 |
+
binary_mask.paste(occluder_alpha, pos, occluder_alpha)
|
| 79 |
+
|
| 80 |
+
mask_array = np.array(binary_mask)
|
| 81 |
+
mask_path = os.path.join(occluded_dir, f"{img_name}_mask.npy")
|
| 82 |
+
np.save(mask_path, mask_array)
|
| 83 |
+
|
| 84 |
+
return image_with_occluder_tensor, mask_path, pos
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def rebuild_display_mask(image_path, mask_path):
|
| 88 |
+
image_pil = Image.open(image_path)
|
| 89 |
+
binary_mask = Image.new('1', image_pil.size)
|
| 90 |
+
|
| 91 |
+
mask_array = np.load(mask_path)
|
| 92 |
+
mask_indices = np.transpose(np.nonzero(mask_array))
|
| 93 |
+
|
| 94 |
+
for i, j in mask_indices:
|
| 95 |
+
binary_mask.putpixel((j, i), 1)
|
| 96 |
+
|
| 97 |
+
binary_mask.show()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def build_dataset(data_path, transform):
|
| 101 |
+
dataset = datasets.ImageFolder(data_path, transform=transform)
|
| 102 |
+
nb_classes = len(dataset.classes)
|
| 103 |
+
return dataset, nb_classes
|
| 104 |
+
|
| 105 |
+
def build_transform():
|
| 106 |
+
t = []
|
| 107 |
+
t.append(transforms.ToTensor())
|
| 108 |
+
return transforms.Compose(t)
|
| 109 |
+
|
| 110 |
+
def main():
|
| 111 |
+
data_dir = 'imagenet1'
|
| 112 |
+
texture_dir = 'occluders_segmented'
|
| 113 |
+
occluded_data_dir = 'imagenet_occluded'
|
| 114 |
+
|
| 115 |
+
transform = build_transform()
|
| 116 |
+
dataset, nb_classes = build_dataset(data_dir, transform)
|
| 117 |
+
dataset_occluder, _ = build_dataset(texture_dir, transform)
|
| 118 |
+
|
| 119 |
+
occlusion_info = pd.DataFrame(columns=["image_name", "class_name", "occluder_class",
|
| 120 |
+
"percentage_occlusion", "mask", "pos"])
|
| 121 |
+
|
| 122 |
+
for idx in range(len(dataset)):
|
| 123 |
+
image, label = dataset[idx]
|
| 124 |
+
category = dataset.classes[label]
|
| 125 |
+
|
| 126 |
+
in_dir = os.path.join(data_dir, category)
|
| 127 |
+
occluded_dir = os.path.join(occluded_data_dir, category)
|
| 128 |
+
|
| 129 |
+
os.makedirs(occluded_dir, exist_ok=True)
|
| 130 |
+
|
| 131 |
+
img_name = dataset.imgs[idx][0].split('/')[-1].split('.')[0]
|
| 132 |
+
|
| 133 |
+
occluder_tensor, occluder_class = get_random_texture(dataset_occluder)
|
| 134 |
+
occluded_image, mask_path, pos = occlude_image(image, occluder_tensor, 0.3, occluded_dir, img_name)
|
| 135 |
+
|
| 136 |
+
mask_array = np.load(mask_path)
|
| 137 |
+
actual_percentage_occlusion = np.count_nonzero(mask_array) / (image.shape[1] * image.shape[2])
|
| 138 |
+
|
| 139 |
+
occluded_image_path = os.path.join(occluded_dir, f"{img_name}_occluded.png")
|
| 140 |
+
transforms.ToPILImage()(occluded_image).save(occluded_image_path)
|
| 141 |
+
|
| 142 |
+
new_row = pd.DataFrame({
|
| 143 |
+
"image_name": [f"{img_name}_occluded.png"],
|
| 144 |
+
"class_name": [category],
|
| 145 |
+
"occluder_class": [occluder_class],
|
| 146 |
+
"percentage_occlusion": [actual_percentage_occlusion],
|
| 147 |
+
"mask": [mask_path],
|
| 148 |
+
"pos": [pos]
|
| 149 |
+
})
|
| 150 |
+
|
| 151 |
+
occlusion_info = pd.concat([occlusion_info, new_row], ignore_index=True)
|
| 152 |
+
|
| 153 |
+
occlusion_info.to_csv(os.path.join(occluded_data_dir, "occlusion_info.csv"), index=False)
|
| 154 |
+
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
main()
|
occlusion_info.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|