text stringlengths 0 4.99k |
|---|
[0, 0, 0], |
[0, 0, 0], |
[0, 0, 0], |
[0, 0, 0]], |
'seg': ...} |
In this example, the keys we are interested in are: |
img_path |
joints |
There are a total of 24 entries present inside joints. Each entry has 3 values: |
x-coordinate |
y-coordinate |
visibility flag of the keypoints (1 indicates visibility and 0 indicates non-visibility) |
As we can see joints contain multiple [0, 0, 0] entries which denote that those keypoints were not labeled. In this example, we will consider both non-visible as well as unlabeled keypoints in order to allow mini-batch learning. |
# Load the metdata definition file and preview it. |
keypoint_def = pd.read_csv(KEYPOINT_DEF) |
keypoint_def.head() |
# Extract the colours and labels. |
colours = keypoint_def[\"Hex colour\"].values.tolist() |
colours = [\"#\" + colour for colour in colours] |
labels = keypoint_def[\"Name\"].values.tolist() |
# Utility for reading an image and for getting its annotations. |
def get_dog(name): |
data = json_dict[name] |
img_data = plt.imread(os.path.join(IMG_DIR, data[\"img_path\"])) |
# If the image is RGBA convert it to RGB. |
if img_data.shape[-1] == 4: |
img_data = img_data.astype(np.uint8) |
img_data = Image.fromarray(img_data) |
img_data = np.array(img_data.convert(\"RGB\")) |
data[\"img_data\"] = img_data |
return data |
Visualize data |
Now, we write a utility function to visualize the images and their keypoints. |
# Parts of this code come from here: |
# https://github.com/benjiebob/StanfordExtra/blob/master/demo.ipynb |
def visualize_keypoints(images, keypoints): |
fig, axes = plt.subplots(nrows=len(images), ncols=2, figsize=(16, 12)) |
[ax.axis(\"off\") for ax in np.ravel(axes)] |
for (ax_orig, ax_all), image, current_keypoint in zip(axes, images, keypoints): |
ax_orig.imshow(image) |
ax_all.imshow(image) |
# If the keypoints were formed by `imgaug` then the coordinates need |
# to be iterated differently. |
if isinstance(current_keypoint, KeypointsOnImage): |
for idx, kp in enumerate(current_keypoint.keypoints): |
ax_all.scatter( |
[kp.x], [kp.y], c=colours[idx], marker=\"x\", s=50, linewidths=5 |
) |
else: |
current_keypoint = np.array(current_keypoint) |
# Since the last entry is the visibility flag, we discard it. |
current_keypoint = current_keypoint[:, :2] |
for idx, (x, y) in enumerate(current_keypoint): |
ax_all.scatter([x], [y], c=colours[idx], marker=\"x\", s=50, linewidths=5) |
plt.tight_layout(pad=2.0) |
plt.show() |
# Select four samples randomly for visualization. |
samples = list(json_dict.keys()) |
num_samples = 4 |
selected_samples = np.random.choice(samples, num_samples, replace=False) |
images, keypoints = [], [] |
for sample in selected_samples: |
data = get_dog(sample) |
image = data[\"img_data\"] |
keypoint = data[\"joints\"] |
images.append(image) |
keypoints.append(keypoint) |
visualize_keypoints(images, keypoints) |
png |
The plots show that we have images of non-uniform sizes, which is expected in most real-world scenarios. However, if we resize these images to have a uniform shape (for instance (224 x 224)) their ground-truth annotations will also be affected. The same applies if we apply any geometric transformation (horizontal flip,... |
Prepare data generator |
class KeyPointsDataset(keras.utils.Sequence): |
def __init__(self, image_keys, aug, batch_size=BATCH_SIZE, train=True): |
self.image_keys = image_keys |
self.aug = aug |
self.batch_size = batch_size |
self.train = train |
self.on_epoch_end() |
def __len__(self): |
return len(self.image_keys) // self.batch_size |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.