Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
train_path = "./Project_data/train"
def generator(source_path,
folder_list,
batch_size=32,
index_size = 'best',
img_dim = 120,
use_imgnet_means = True):
if type(index_size) == list:
img_idx = index_size
elif index_size == 'best':
img_idx = list(range(7,26,2))#[4,5,6,10,15,27,28,29] #
elif (index_size > 0) & (type(index_size) == int):
img_idx = sorted(random.sample(range(30),index_size))
num_indices = len(img_idx)
y = img_dim
z = img_dim
while True:
t = np.random.permutation(folder_list)
num_batches = int(len(t)/batch_size)
for batch in range(num_batches):
batch_data = np.zeros((batch_size,num_indices,y,z,3))
batch_labels = np.zeros((batch_size,5))
for folder in range(batch_size):
imgs = os.listdir(source_path+'/'+ t[folder + (batch*batch_size)].split(';')[0])
for idx,item in enumerate(img_idx):
image = imread(source_path+'/'+ t[folder + (batch*batch_size)].strip().split(';')[0]+'/'+imgs[item]).astype(np.float32)
# 1400 images are 160 * 120 , to fix aspect ratio maintain squaring
if image.shape[1] == 160:
resized_image = imresize(image[:,20:140,:],(img_dim,img_dim)).astype(np.float32)
else:
resized_image = imresize(image,(img_dim,img_dim)).astype(np.float32)
if use_imgnet_means:
# Using Imagenet Means for Normalization
batch_data[folder,idx,:,:,0] = resized_image[:,:,0] - 104
batch_data[folder,idx,:,:,1] = resized_image[:,:,1] - 117
batch_data[folder,idx,:,:,2] = resized_image[:,:,2] - 123
else:
#Getting mean and std for normalization
mean = resized_image.mean()
std = resized_image.std()
batch_data[folder,idx,:,:,0] = (resized_image[:,:,0] - mean)/std
batch_data[folder,idx,:,:,1] = (resized_image[:,:,1] - mean)/std
batch_data[folder,idx,:,:,2] = (resized_image[:,:,2] - mean)/std
batch_labels[folder, int(t[folder + (batch*batch_size)].strip().split(';')[2])] = 1
yield batch_data, batch_labels
Whats the best way to create augmented data to improve model performance and reduce overfitting? I am using keras as the deep learning library. Feel free to use any addiitonal libraries like albumentations or augmentor. Some Augmentations relevant to the use case.
Here is one way to add data augmentation to your generator using Albumentations:
import albumentations as A
transform = A.Compose([
A.HorizontalFlip(p=0.5),
A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=15, p=0.5),
A.GaussianBlur(blur_limit=(3,5), p=0.3),
A.RGBShift(r_shift_limit=10, g_shift_limit=10, b_shift_limit=10, p=0.25),
A.RandomBrightnessContrast(p=0.3),
A.CLAHE(clip_limit=2.0, p=0.4),
A.HueSaturationValue(p=0.3)
])
def generator(source_path,
folder_list,
batch_size=32,
index_size = 'best',
img_dim = 120,
use_imgnet_means = True):
# same as before upto batch_data initialization
for folder in range(batch_size):
imgs = os.listdir(source_path+'/'+ t[folder + (batch*batch_size)].split(';')[0])
for idx,item in enumerate(img_idx):
image = imread(source_path+'/'+ t[folder + (batch*batch_size)].strip().split(';')[0]+'/'+imgs[item]).astype(np.float32)
augmented = transform(image=image)['image']
# resize, normalize and assign to batch_data
# same as before
yield batch_data, batch_labels
Some key points:
Let me know if you have any other questions!
does this replace the images or adds more training images? I have chosen only specific frames of a video for each gesture . Will that be an issue
Good question! The Albumentations augmentations are applied "on the fly" to each image as it is loaded in the generator. So it does not actually add more training images to disk. It just augments each image differently each time it is seen in training.
This allows you to keep your chosen frames, but see augmented versions of them during training. This helps the model generalize better.
The only caveat is that the augmentations are random, so if you chose certain frames because of some specific property, the augmentation could alter that property.
For example, if you chose frame 10 because it had the hand in a certain position, a rotation augmentation could move the hand.
So in summary:
You'll likely still see good improvement from augs like flip, brightness, blur etc. But disable something like rotation if it ruins your chosen frames.
Let me know if this helps explain the behavior!