def load_data(self): ''' Loads data to use in training. ''' # Load data if self.dataset == FaceRecData.Yalefaces_A: self.instances = ImageIO.loadYalefacesImages(self.data_directory, loud=False) elif self.dataset == FaceRecData.Yalefaces_B: self.instances = ImageIO.loadExtendedCroppedYalefaces( self.data_directory, loud=False) else: raise RuntimeError('FaceRecTest not assigned a valid dataset') # Sample from each class to create train/dev/test partitions self.trn_data = list() self.dev_data = list() self.tst_data = list() # Sort instances by label classes = dict() for instance in self.instances: label = instance[0] if label not in classes.keys(): classes[label] = list() classes[label].append(instance) num_labels = max(classes.keys()) + 1 # Calculate how many samples should be in each partition num_trn = int((self.trn_part / 100.0) * len(self.instances)) num_dev = int((self.dev_part / 100.0) * len(self.instances)) num_tst = int((self.tst_part / 100.0) * len(self.instances)) # If any leftovers, give more to training partition num_trn += len(self.instances) - sum((num_trn, num_dev, num_tst)) # Partition trn/dev/tst def partition(src, dest, num): num_labels = max(src.keys()) + 1 count = 0 i = 0 while i < num: instances = src[count % num_labels] if len(instances) > 0: idx = random.randint(0, len(instances) - 1) dest.append(instances.pop(idx)) i += 1 count += 1 partition(classes, self.trn_data, num_trn) partition(classes, self.dev_data, num_dev) partition(classes, self.tst_data, num_tst)
def load_data(self): ''' Loads data to use in training. ''' # Load data if self.dataset == FaceRecData.Yalefaces_A: self.instances = ImageIO.loadYalefacesImages( self.data_directory, loud=False) elif self.dataset == FaceRecData.Yalefaces_B: self.instances = ImageIO.loadExtendedCroppedYalefaces( self.data_directory, loud=False) else: raise RuntimeError('FaceRecTest not assigned a valid dataset') # Sample from each class to create train/dev/test partitions self.trn_data = list() self.dev_data = list() self.tst_data = list() # Sort instances by label classes = dict() for instance in self.instances: label = instance[0] if label not in classes.keys(): classes[label] = list() classes[label].append(instance) num_labels = max(classes.keys())+1 # Calculate how many samples should be in each partition num_trn = int( (self.trn_part/100.0) * len(self.instances) ) num_dev = int( (self.dev_part/100.0) * len(self.instances) ) num_tst = int( (self.tst_part/100.0) * len(self.instances) ) # If any leftovers, give more to training partition num_trn += len(self.instances) - sum((num_trn, num_dev, num_tst)) # Partition trn/dev/tst def partition(src, dest, num): num_labels = max(src.keys())+1 count = 0 i = 0 while i < num: instances = src[count % num_labels] if len(instances) > 0: idx = random.randint(0, len(instances)-1) dest.append( instances.pop(idx) ) i += 1 count += 1 partition(classes, self.trn_data, num_trn) partition(classes, self.dev_data, num_dev) partition(classes, self.tst_data, num_tst)
def load_data(self): ''' Loads data to use in training. ''' self.face_instances = list() self.non_face_instances = list() # Load data if self.dataset == FaceDetData.MIT_Face_Data: self.face_instances.extend( ImageIO.loadFaceDetectionImages( self.data_directory + '/train/face', 1)) self.face_instances.extend( ImageIO.loadFaceDetectionImages( self.data_directory + '/test/face', 1)) self.non_face_instances.extend( ImageIO.loadFaceDetectionImages( self.data_directory + '/train/non-face', 0)) self.non_face_instances.extend( ImageIO.loadFaceDetectionImages( self.data_directory + '/test/non-face', 0)) else: raise RuntimeError('FaceDetTest not assigned a valid dataset') img_size = numpy.shape(self.face_instances[0][0]) self.face_detector = FaceDetector(self.iterations, img_size) self.total_face_instances = len(self.face_instances) self.total_non_face_instances = len(self.non_face_instances) # Sample from each class to create train/dev/test partitions self.face_trn_data = list() self.non_face_trn_data = list() self.face_dev_data = list() self.non_face_dev_data = list() self.face_tst_data = list() self.non_face_tst_data = list() # Calculate how many samples should be in each partition num_face_trn = int((self.trn_part / 100.0) * len(self.face_instances)) num_non_face_trn = int( (self.trn_part / 100.0) * len(self.non_face_instances)) num_face_dev = int((self.dev_part / 100.0) * len(self.face_instances)) num_non_face_dev = int( (self.dev_part / 100.0) * len(self.non_face_instances)) num_face_tst = int((self.tst_part / 100.0) * len(self.face_instances)) num_non_face_tst = int( (self.tst_part / 100.0) * len(self.non_face_instances)) # If any leftovers, give more to training partition num_face_trn += len(self.face_instances) - sum( (num_face_trn, num_face_dev, num_face_tst)) num_non_face_trn += len(self.non_face_instances) - sum( (num_non_face_trn, num_non_face_dev, num_non_face_tst)) # Partition trn/dev/tst def partition(src, dest, num): i = 0 while i < num: if len(src) > 0: idx = random.randint(0, len(src) - 1) dest.append(src.pop(idx)) i += 1 partition(self.face_instances, self.face_trn_data, num_face_trn) partition(self.non_face_instances, self.non_face_trn_data, num_non_face_trn) partition(self.face_instances, self.face_dev_data, num_face_dev) partition(self.non_face_instances, self.non_face_dev_data, num_non_face_dev) partition(self.face_instances, self.face_tst_data, num_face_tst) partition(self.non_face_instances, self.non_face_tst_data, num_non_face_tst)
def load_data(self): ''' Loads data to use in training. ''' self.face_instances = list() self.non_face_instances = list() # Load data if self.dataset == FaceDetData.MIT_Face_Data: self.face_instances.extend(ImageIO.loadFaceDetectionImages(self.data_directory + '/train/face', 1)) self.face_instances.extend(ImageIO.loadFaceDetectionImages(self.data_directory + '/test/face', 1)) self.non_face_instances.extend(ImageIO.loadFaceDetectionImages(self.data_directory + '/train/non-face', 0)) self.non_face_instances.extend(ImageIO.loadFaceDetectionImages(self.data_directory + '/test/non-face', 0)) else: raise RuntimeError('FaceDetTest not assigned a valid dataset') img_size = numpy.shape(self.face_instances[0][0]) self.face_detector = FaceDetector(self.iterations, img_size) self.total_face_instances = len(self.face_instances) self.total_non_face_instances = len(self.non_face_instances) # Sample from each class to create train/dev/test partitions self.face_trn_data = list() self.non_face_trn_data = list() self.face_dev_data = list() self.non_face_dev_data = list() self.face_tst_data = list() self.non_face_tst_data = list() # Calculate how many samples should be in each partition num_face_trn = int( (self.trn_part/100.0) * len(self.face_instances) ) num_non_face_trn = int( (self.trn_part/100.0) * len(self.non_face_instances) ) num_face_dev = int( (self.dev_part/100.0) * len(self.face_instances) ) num_non_face_dev = int( (self.dev_part/100.0) * len(self.non_face_instances) ) num_face_tst = int( (self.tst_part/100.0) * len(self.face_instances) ) num_non_face_tst = int( (self.tst_part/100.0) * len(self.non_face_instances) ) # If any leftovers, give more to training partition num_face_trn += len(self.face_instances) - sum((num_face_trn, num_face_dev, num_face_tst)) num_non_face_trn += len(self.non_face_instances) - sum((num_non_face_trn, num_non_face_dev, num_non_face_tst)) # Partition trn/dev/tst def partition(src, dest, num): i = 0 while i < num: if len(src) > 0: idx = random.randint(0, len(src)-1) dest.append( src.pop(idx) ) i += 1 partition(self.face_instances, self.face_trn_data, num_face_trn) partition(self.non_face_instances, self.non_face_trn_data, num_non_face_trn) partition(self.face_instances, self.face_dev_data, num_face_dev) partition(self.non_face_instances, self.non_face_dev_data, num_non_face_dev) partition(self.face_instances, self.face_tst_data, num_face_tst) partition(self.non_face_instances, self.non_face_tst_data, num_non_face_tst)