def flow(self, mode='train'): while True: if mode == 'train': print self.train_keys[0] # shuffle(self.train_keys) keys = self.train_keys elif mode == 'val' or mode == 'demo': shuffle(self.validation_keys) keys = self.validation_keys else: raise Exception('invalid mode: %s' % mode) inputs = [] targets = [] for key in keys: image_path = self.path_prefix + key if not os.path.exists(image_path): continue image_array = imread(image_path) image_array = imresize(image_array, self.image_size) num_image_channels = len(image_array.shape) if num_image_channels != 3: continue ground_truth = self.ground_truth_data[key] if self.do_random_crop: image_array = self._do_random_crop(image_array) image_array = image_array.astype('float32') if mode == 'train' or mode == 'demo': if self.ground_truth_transformer != None: image_array, ground_truth = self.transform( image_array, ground_truth) ground_truth = (self.ground_truth_transformer. assign_boxes(ground_truth)) else: image_array = self.transform(image_array)[0] if self.grayscale: image_array = cv2.cvtColor( image_array.astype('uint8'), cv2.COLOR_RGB2GRAY).astype('float32') image_array = np.expand_dims(image_array, -1) inputs.append(image_array) targets.append(ground_truth) if len(inputs) == self.batch_size: #print len(inputs) inputs = np.asarray(inputs) targets = np.asarray(targets) #print targets targets = to_categorical(targets, self.classes) if mode == 'train' or mode == 'val': inputs = self.preprocess_images(inputs) #print inputs.shape #print targets.shape image_data = {'input': inputs} targets = {'label': targets} yield [image_data, targets] inputs = [] targets = []
def preprocess_image(image, input_shape): image = imresize(image, input_shape[:2]) image = image.astype('float32') image = image / 255.0 image = image - 0.5 image = image * 2.0 image = np.expand_dims(image, axis=0) return image
def flow(self, mode='train'): while True: if mode == 'train': shuffle(self.train_keys) keys = self.train_keys elif mode == 'val' or mode == 'demo': shuffle(self.validation_keys) keys = self.validation_keys else: raise Exception('invalid mode: %s' % mode) inputs = [] targets = [] for key in keys: image_path = self.path_prefix + key image_array = imread(image_path) image_array = imresize(image_array, self.image_size) num_image_channels = len(image_array.shape) if num_image_channels != 3: continue ground_truth = self.ground_truth_data[key] image_array = image_array.astype('float32') if mode == 'train' or mode == 'demo': image_array = self.transform(image_array)[0] if self.grayscale: image_array = cv2.cvtColor( image_array.astype('uint8'), cv2.COLOR_RGB2GRAY).astype('float32') image_array = np.expand_dims(image_array, -1) inputs.append(image_array) targets.append(ground_truth) if len(targets) == self.batch_size: inputs = np.asarray(inputs) targets = np.asarray(targets) gender = np_utils.to_categorical(targets[:, 0], 2) # Quantizing the age age_bins = np.linspace(0, 100, 21) age_step = np.digitize(targets[:, 1], age_bins) age_quantized = np_utils.to_categorical(age_step, 21) if mode == 'train' or mode == 'val': inputs = self.preprocess_images(inputs) yield self._wrap_in_dictionary(inputs, gender, age_quantized) if mode == 'demo': yield self._wrap_in_dictionary(inputs, gender, age_quantized) inputs = [] targets = []
def flow(self, mode='train'): while True: if mode == 'train': shuffle(self.train_keys) keys = self.train_keys elif mode == 'val': keys = self.validation_keys else: raise Exception('invalid mode: %s' % mode) inputs = [] targets = [] for key in keys: image_path = os.path.join(self.path_prefix, key) image_array = imread(image_path) image_array = imresize(image_array, self.image_size) num_image_channels = len(image_array.shape) if num_image_channels != 3: continue ground_truth = self.ground_truth_data[key] image_array = image_array.astype('float32') if mode == 'train': image_array = self.transform(image_array)[0] if self.grayscale: image_array = cv2.cvtColor( image_array.astype('uint8'), cv2.COLOR_RGB2GRAY).astype('float32') image_array = np.expand_dims(image_array, -1) inputs.append(image_array) targets.append(ground_truth) if len(targets) == self.batch_size: inputs = np.asarray(inputs) targets = np.asarray(targets) # gender = np_utils.to_categorical(targets[:, 0], 2) # softmax gender = targets[:, 0] # sigmoid age = np_utils.to_categorical(targets[:, 1], self.bins) emotion = np_utils.to_categorical(targets[:, 2], 7) if mode == 'train' or mode == 'val': inputs = self.preprocess_images(inputs) yield self._wrap_in_dictionary(inputs, gender, age, emotion) inputs = [] targets = []