def __getitem__(self, index): # data basename base_name, _ = self.data_list[index].split('.') # read image file img = Image.open( os.path.join(self.data_dir, "bg_imgs/" + base_name + ".jpg")) img = img.convert(self.image_mode) # get face bounding box pt2d = get_label_from_txt( os.path.join(self.data_dir, "bbox/" + base_name + ".txt")) x_min, y_min, x_max, y_max = pt2d # crop face loosely:k=0to 0.2 k = np.random.random_sample() * 0.1 x_min -= 0.6 * k * abs(x_max - x_min) y_min -= k * abs(y_max - y_min) x_max += 0.6 * k * abs(x_max - x_min) y_max += 0.6 * k * abs(y_max - y_min) img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) # Augmentation:Blur? if np.random.random_sample() < 0.05: img = img.filter(ImageFilter.BLUR) # Augmentation:Gray? if np.random.random_sample() < 0.5 and base_name.find('ID') < 0: img = img.convert('L').convert("RGB") # transform if self.transform: img = self.transform(img) # RGB2BGR img = img[np.array([2, 1, 0]), :, :] # get pose quat quat = get_label_from_txt( os.path.join(self.data_dir, "info/" + base_name + '.txt')) # face orientation vector vector_label = get_attention_vector(quat) vector_label = torch.FloatTensor(vector_label) # classification label classify_label = torch.LongTensor(np.digitize(vector_label, self.bins)) classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return img, soft_label, vector_label, os.path.join( self.data_dir, "bg_imgs/" + base_name + ".jpg")
def __getitem__(self, index): base_name = self.data_list[index][:-4] img = Image.open( os.path.join(self.data_dir, 'bg_imgs/' + base_name + '.jpg')) img = img.convert(self.image_mode) # get face bbox bbox_path = os.path.join(self.data_dir, 'bbox/' + base_name + '.txt') pt2d = get_label_from_txt(bbox_path) x_min = pt2d[0] y_min = pt2d[1] x_max = pt2d[2] y_max = pt2d[3] # Crop the face loosely k = 0.1 x_min -= k * abs(x_max - x_min) y_min -= k * abs(y_max - y_min) x_max += k * abs(x_max - x_min) y_max += 0.3 * k * abs(y_max - y_min) img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) # get pose angle pitch,yaw,roll(degrees) angle_path = os.path.join(self.data_dir, 'angles/' + base_name + '.txt') angle = get_label_from_txt(angle_path) angle = torch.FloatTensor(angle) # get pose quat quat_path = os.path.join(self.data_dir, 'info/' + base_name + '.txt') quat = get_label_from_txt(quat_path) # Attention vector attention_vector = get_attention_vector(quat) vector_label = torch.FloatTensor(attention_vector) # classification label bins = np.array(range(-99, 100, self.bin_size)) / 99 classify_label = torch.LongTensor(np.digitize(attention_vector, bins)) # 1-num_classes classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label = torch.stack([soft_label_x, soft_label_y, soft_label_z]) if self.transform is not None: img = self.transform(img) # RGB2BGR img = img[np.array([2, 1, 0]), :, :] return img, soft_label, vector_label, angle, torch.FloatTensor( pt2d), os.path.join(self.data_dir, 'bg_imgs/' + base_name + '.jpg')
def __getitem__(self, index): img = cv2.imread(self.data_list[index]) if self.crop: # get face bbox bbox = bbox_300W(self.data_list[index].replace("imgs/","labels/").replace(".jpg",".txt")) x_min = bbox[0] y_min = bbox[1] x_max = bbox[2] y_max = bbox[3] img = img[y_min:y_max, x_min:x_max] img = cv2.resize(img, (self.input_size, self.input_size)) img = img.swapaxes(1,2).swapaxes(0,1) img = np.ascontiguousarray(img) #get left vector, down vector and front vector left_vector, down_vector, front_vector = Vector300W(self.data_list[index].replace("imgs/","labels/").replace(".jpg",".txt")) vector_label_l = torch.FloatTensor(left_vector) vector_label_d = torch.FloatTensor(down_vector) vector_label_f = torch.FloatTensor(front_vector) #----------------left vector------------------------- # classification label classify_label = torch.LongTensor(np.digitize(left_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_l = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #-------------------down vector-------------- classify_label = torch.LongTensor(np.digitize(down_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_d = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #------------------front vector------------------- classify_label = torch.LongTensor(np.digitize(front_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_f = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return torch.from_numpy(img), soft_label_l, soft_label_d, soft_label_f, vector_label_l, vector_label_d, vector_label_f, self.data_list[index], vector_label_l, vector_label_d, vector_label_f
def __getitem__(self, index): #base_name, _ = os.path.basename(self.data_list[index]).split('.') # read image file img = cv2.imread(self.data_list[index]) #crop head part if self.crop: # get face bounding box bbox = bbox_300W(self.data_list[index].replace('imgs/','labels/').replace('.jpg','.txt')) x_min, y_min, x_max, y_max = bbox img = img[y_min:y_max, x_min:x_max] img = cv2.resize(img, (self.input_size, self.input_size)) img = augment_data(img) #C,H,W to H,W,C img = img.swapaxes(1,2).swapaxes(0,1) img = np.ascontiguousarray(img) #get left vector, down vector, front vector left_vector, down_vector, front_vector = Vector300W(self.data_list[index].replace('imgs/','labels/').replace('.jpg','.txt')) vector_label_l = torch.FloatTensor(left_vector) vector_label_d = torch.FloatTensor(down_vector) vector_label_f = torch.FloatTensor(front_vector) #----------------left vector------------------------- # classification label classify_label = torch.LongTensor(np.digitize(left_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_l = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #-------------------down vector-------------- classify_label = torch.LongTensor(np.digitize(down_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_d = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #------------------front vector------------------- classify_label = torch.LongTensor(np.digitize(front_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_f = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return torch.from_numpy(img), soft_label_l, soft_label_d, soft_label_f, vector_label_l, vector_label_d, vector_label_f, self.data_list[index], vector_label_l, vector_label_d, vector_label_f
def __getitem__(self, index, crop=True): # data basename base_name, _ = self.data_list[index].split('.') # read image file img = Image.open( os.path.join(self.data_dir, "imgs/" + base_name + ".jpg")) img = img.convert(self.image_mode) if crop: # get face bounding box bbox = Bbox300W( os.path.join(self.data_dir, "labels/" + base_name + ".txt")) x_min, y_min, x_max, y_max = bbox img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) # Augmentation:Blur? if np.random.random_sample() < 0.05: img = img.filter(ImageFilter.BLUR) # Augmentation:Gray? if np.random.random_sample(): img = img.convert('L').convert("RGB") # transform if self.transform: img = self.transform(img) # RGB2BGR img = img[np.array([2, 1, 0]), :, :] #get left vector, down vector, front vector left_vector, down_vector, front_vector = Vector300W( os.path.join(self.data_dir, "labels/" + base_name + '.txt')) vector_label_l = torch.FloatTensor(left_vector) vector_label_d = torch.FloatTensor(down_vector) vector_label_f = torch.FloatTensor(front_vector) #----------------left vector------------------------- # classification label classify_label = torch.LongTensor(np.digitize( left_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_l = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #-------------------down vector-------------- classify_label = torch.LongTensor(np.digitize( down_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_d = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #------------------front vector------------------- classify_label = torch.LongTensor(np.digitize( front_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_f = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return img, soft_label_l, soft_label_d, soft_label_f, vector_label_l, vector_label_d, vector_label_f, os.path.join( self.data_dir, "imgs/" + base_name + ".jpg")
def __getitem__(self, index, crop=True): base_name, _ = self.data_list[index].split('.') img = Image.open( os.path.join(self.data_dir, 'imgs/' + base_name + '.jpg')) img = img.convert(self.image_mode) if crop: # get face bbox bbox = Bbox300W( os.path.join(self.data_dir, 'labels/' + base_name + '.txt')) x_min = bbox[0] y_min = bbox[1] x_max = bbox[2] y_max = bbox[3] img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) if self.transform is not None: img = self.transform(img) # RGB2BGR img = img[np.array([2, 1, 0]), :, :] #get left vector, down vector and front vector left_vector, down_vector, front_vector = Vector300W( os.path.join(self.data_dir, "labels/" + base_name + '.txt')) vector_label_l = torch.FloatTensor(left_vector) vector_label_d = torch.FloatTensor(down_vector) vector_label_f = torch.FloatTensor(front_vector) bins = np.linspace(-1, 1, self.num_classes) #----------------left vector------------------------- # classification label classify_label = torch.LongTensor(np.digitize( left_vector, bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_l = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #-------------------down vector-------------- classify_label = torch.LongTensor(np.digitize( down_vector, bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_d = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #------------------front vector------------------- classify_label = torch.LongTensor(np.digitize( front_vector, bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_f = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return img, soft_label_l, soft_label_d, soft_label_f, vector_label_l, vector_label_d, vector_label_f, os.path.join( self.data_dir, "imgs/" + base_name + ".jpg")
def __getitem__(self, index, crop=False): # data basename base_name, _ = self.data_list[index].split('.') # read image file img = Image.open( os.path.join(self.data_dir, "dataset/bg_imgs/" + base_name + ".jpg")) img = img.convert(self.image_mode) if crop: # get face bounding box pt2d = get_label_from_txt( os.path.join(self.data_dir, "bbox/" + base_name + ".txt")) x_min, y_min, x_max, y_max = pt2d # crop face loosely:k=0to 0.2 k = np.random.random_sample() * 0.1 x_min -= 0.6 * k * abs(x_max - x_min) y_min -= k * abs(y_max - y_min) x_max += 0.6 * k * abs(x_max - x_min) y_max += 0.6 * k * abs(y_max - y_min) img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) # Augmentation:Blur? if np.random.random_sample() < 0.05: img = img.filter(ImageFilter.BLUR) # Augmentation:Gray? if np.random.random_sample() < 0.5 and base_name.find('ID') < 0: img = img.convert('L').convert("RGB") # transform if self.transform: img = self.transform(img) # RGB2BGR img = img[np.array([2, 1, 0]), :, :] # get pose quat #quat = get_label_from_txt(os.path.join(self.data_dir, "info/" + base_name + '.txt')) info = get_info_from_txt( os.path.join(self.data_dir, "info_all/" + base_name + '.txt')) # face orientation vector #vector_label = get_attention_vector(quat) #get one front vector #vector_label = angle2vector(os.path.join(self.data_dir, "info/" + base_name + '.txt')) #vector_label = torch.FloatTensor(vector_label) #get front vector and right vector front_vector, right_vector, up_vector = get_vectors(info) #print(np.dot(np.array(front_vector),np.array(right_vector))) #print(np.dot(np.array(front_vector),np.array(up_vector))) #print(np.dot(np.array(right_vector),np.array(up_vector))) vector_label_f = torch.FloatTensor(front_vector) vector_label_r = torch.FloatTensor(right_vector) vector_label_u = torch.FloatTensor(up_vector) #----------------front vector------------------------- # classification label classify_label = torch.LongTensor(np.digitize( front_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_f = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #-------------------right vector-------------- classify_label = torch.LongTensor(np.digitize( right_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_r = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #------------------up vector------------------- classify_label = torch.LongTensor(np.digitize( up_vector, self.bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_u = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return img, soft_label_f, soft_label_r, soft_label_u, vector_label_f, vector_label_r, vector_label_u, os.path.join( self.data_dir, "dataset/bg_imgs/" + base_name + ".jpg")
def __getitem__(self, index, crop=False): base_name, _ = self.data_list[index].split('.') img = Image.open( os.path.join(self.data_dir, 'dataset/bg_imgs/' + base_name + '.jpg')) img = img.convert(self.image_mode) if crop: # get face bbox bbox_path = os.path.join(self.data_dir, 'bbox/' + base_name + '.txt') pt2d = get_label_from_txt(bbox_path) x_min = pt2d[0] y_min = pt2d[1] x_max = pt2d[2] y_max = pt2d[3] # Crop the face loosely k = 0.1 x_min -= k * abs(x_max - x_min) y_min -= k * abs(y_max - y_min) x_max += k * abs(x_max - x_min) y_max += 0.3 * k * abs(y_max - y_min) img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) # get pose angle pitch,yaw,roll(degrees) ANGLES NO NEEDED!!! #angle_path = os.path.join(self.data_dir, 'angles/' + base_name + '.txt') #angle = get_label_from_txt(angle_path) #angle = torch.FloatTensor(angle) if self.transform is not None: img = self.transform(img) # RGB2BGR img = img[np.array([2, 1, 0]), :, :] # get pose quat #quat = get_label_from_txt(os.path.join(self.data_dir, "info/" + base_name + '.txt')) info = get_info_from_txt( os.path.join(self.data_dir, "info_all/" + base_name + '.txt')) # face orientation vector #vector_label = get_attention_vector(quat) #get one front vector #vector_label = angle2vector(os.path.join(self.data_dir, "info/" + base_name + '.txt')) #vector_label = torch.FloatTensor(vector_label) #get front vector and right vector front_vector, right_vector, up_vector = get_vectors(info) #print(np.dot(np.array(front_vector),np.array(right_vector))) #print(np.dot(np.array(front_vector),np.array(up_vector))) #print(np.dot(np.array(right_vector),np.array(up_vector))) vector_label_f = torch.FloatTensor(front_vector) vector_label_r = torch.FloatTensor(right_vector) vector_label_u = torch.FloatTensor(up_vector) bins = np.array(range(-99, 100, self.bin_size)) / 99 #----------------front vector------------------------- # classification label classify_label = torch.LongTensor(np.digitize( front_vector, bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_f = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #-------------------right vector-------------- classify_label = torch.LongTensor(np.digitize( right_vector, bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_r = torch.stack([soft_label_x, soft_label_y, soft_label_z]) #------------------up vector------------------- classify_label = torch.LongTensor(np.digitize( up_vector, bins)) # return the index classify_label = np.where(classify_label > self.num_classes, self.num_classes, classify_label) classify_label = np.where(classify_label < 1, 1, classify_label) # soft label soft_label_x = get_soft_label(classify_label[0], self.num_classes) soft_label_y = get_soft_label(classify_label[1], self.num_classes) soft_label_z = get_soft_label(classify_label[2], self.num_classes) soft_label_u = torch.stack([soft_label_x, soft_label_y, soft_label_z]) return img, soft_label_f, soft_label_r, soft_label_u, vector_label_f, vector_label_r, vector_label_u, os.path.join( self.data_dir, "dataset/bg_imgs/" + base_name + ".jpg")