text stringlengths 0 4.99k |
|---|
def on_epoch_end(self): |
self.indexes = np.arange(len(self.image_keys)) |
if self.train: |
np.random.shuffle(self.indexes) |
def __getitem__(self, index): |
indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size] |
image_keys_temp = [self.image_keys[k] for k in indexes] |
(images, keypoints) = self.__data_generation(image_keys_temp) |
return (images, keypoints) |
def __data_generation(self, image_keys_temp): |
batch_images = np.empty((self.batch_size, IMG_SIZE, IMG_SIZE, 3), dtype=\"int\") |
batch_keypoints = np.empty( |
(self.batch_size, 1, 1, NUM_KEYPOINTS), dtype=\"float32\" |
) |
for i, key in enumerate(image_keys_temp): |
data = get_dog(key) |
current_keypoint = np.array(data[\"joints\"])[:, :2] |
kps = [] |
# To apply our data augmentation pipeline, we first need to |
# form Keypoint objects with the original coordinates. |
for j in range(0, len(current_keypoint)): |
kps.append(Keypoint(x=current_keypoint[j][0], y=current_keypoint[j][1])) |
# We then project the original image and its keypoint coordinates. |
current_image = data[\"img_data\"] |
kps_obj = KeypointsOnImage(kps, shape=current_image.shape) |
# Apply the augmentation pipeline. |
(new_image, new_kps_obj) = self.aug(image=current_image, keypoints=kps_obj) |
batch_images[i,] = new_image |
# Parse the coordinates from the new keypoint object. |
kp_temp = [] |
for keypoint in new_kps_obj: |
kp_temp.append(np.nan_to_num(keypoint.x)) |
kp_temp.append(np.nan_to_num(keypoint.y)) |
# More on why this reshaping later. |
batch_keypoints[i,] = np.array(kp_temp).reshape(1, 1, 24 * 2) |
# Scale the coordinates to [0, 1] range. |
batch_keypoints = batch_keypoints / IMG_SIZE |
return (batch_images, batch_keypoints) |
To know more about how to operate with keypoints in imgaug check out this document. |
Define augmentation transforms |
train_aug = iaa.Sequential( |
[ |
iaa.Resize(IMG_SIZE, interpolation=\"linear\"), |
iaa.Fliplr(0.3), |
# `Sometimes()` applies a function randomly to the inputs with |
# a given probability (0.3, in this case). |
iaa.Sometimes(0.3, iaa.Affine(rotate=10, scale=(0.5, 0.7))), |
] |
) |
test_aug = iaa.Sequential([iaa.Resize(IMG_SIZE, interpolation=\"linear\")]) |
Create training and validation splits |
np.random.shuffle(samples) |
train_keys, validation_keys = ( |
samples[int(len(samples) * 0.15) :], |
samples[: int(len(samples) * 0.15)], |
) |
Data generator investigation |
train_dataset = KeyPointsDataset(train_keys, train_aug) |
validation_dataset = KeyPointsDataset(validation_keys, test_aug, train=False) |
print(f\"Total batches in training set: {len(train_dataset)}\") |
print(f\"Total batches in validation set: {len(validation_dataset)}\") |
sample_images, sample_keypoints = next(iter(train_dataset)) |
assert sample_keypoints.max() == 1.0 |
assert sample_keypoints.min() == 0.0 |
sample_keypoints = sample_keypoints[:4].reshape(-1, 24, 2) * IMG_SIZE |
visualize_keypoints(sample_images[:4], sample_keypoints) |
Total batches in training set: 166 |
Total batches in validation set: 29 |
png |
Model building |
The Stanford dogs dataset (on which the StanfordExtra dataset is based) was built using the ImageNet-1k dataset. So, it is likely that the models pretrained on the ImageNet-1k dataset would be useful for this task. We will use a MobileNetV2 pre-trained on this dataset as a backbone to extract meaningful features from t... |
def get_model(): |
# Load the pre-trained weights of MobileNetV2 and freeze the weights |
backbone = keras.applications.MobileNetV2( |
weights=\"imagenet\", include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3) |
) |
backbone.trainable = False |
inputs = layers.Input((IMG_SIZE, IMG_SIZE, 3)) |
x = keras.applications.mobilenet_v2.preprocess_input(inputs) |
x = backbone(x) |
x = layers.Dropout(0.3)(x) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.