reshape ( l, ( batch_size, 1, 1, 1 )) y_l = tf. shape ( images_one ) # Sample lambda and reshape it to do the mixup l = sample_beta_distribution ( batch_size, alpha, alpha ) x_l = tf. gamma ( shape =, alpha = concentration_0 ) return gamma_1_sample / ( gamma_1_sample + gamma_2_sample ) def mix_up ( ds_one, ds_two, alpha = 0.2 ): # Unpack two datasets images_one, labels_one = ds_one images_two, labels_two = ds_two batch_size = tf. gamma ( shape =, alpha = concentration_1 ) gamma_2_sample = tf. X1 and x2 are images) and the same equation is applied to the labels as well.ĭef sample_beta_distribution ( size, concentration_0 = 0.2, concentration_1 = 0.2 ): gamma_1_sample = tf. such that, for example, new_x = lambda * x1 + (1 - lambda) * x2 (where The same dataset, and apply a lambda value within the range sampled from a Beta distribution To perform the mixup routine, we create new virtual datasets using the training data from from_tensor_slices (( x_test, y_test )). zip (( train_ds_one, train_ds_two )) val_ds = tf. batch ( BATCH_SIZE ) ) # Because we will be mixing up the images and their corresponding labels, we will be # combining two shuffled datasets from the same training data. from_tensor_slices (( new_x_train, new_y_train )). batch ( BATCH_SIZE ) ) train_ds_two = ( tf. # Put aside a few samples to create our validation set val_samples = 2000 x_val, y_val = x_train, y_train new_x_train, new_y_train = x_train, y_train train_ds_one = ( tf. This example requires TensorFlow 2.4 or higher. mixup can beĮxtended to a variety of data modalities such as computer vision, naturallanguage Transforms for a given dataset, medical imaging datasets, for example. Mixup is specifically useful when we are not sure about selecting a set of augmentation mixup relaxes this byĬombining different features with one another (same happens for the labels too) so thatĪ network does not get overconfident about the relationship between the features and The technique is quite systematically named - we are literally mixing up the features and (Note that the lambda values are values with the range and are sampled from the It's implemented with the following formulas: Mixup is a domain-agnostic data augmentation technique proposed in mixup: Beyond Empirical Risk Minimizationīy Zhang et al. MixUp augmentation for image classificationĭescription: Data augmentation using the mixup technique for image classification.