def getValidation(self, params): if self.validation_dict == None: images = [] labels = [] # Read files for sample_filepath in self.validation_list: sample_fullpath = self.corpus_dirpath + '/' + sample_filepath + '/' + sample_filepath # IMAGE sample_img = cv2.imread( sample_fullpath + '.png', cv2.IMREAD_GRAYSCALE) # Grayscale is assumed! height = params['img_height'] sample_img = ctc_utils.resize(sample_img, height) images.append(ctc_utils.normalize(sample_img)) # GROUND TRUTH if self.semantic: sample_full_filepath = sample_fullpath + '.semantic' else: sample_full_filepath = sample_fullpath + '.agnostic' sample_gt_file = open(sample_full_filepath, 'r') sample_gt_plain = sample_gt_file.readline().rstrip().split( ctc_utils.word_separator()) sample_gt_file.close() labels.append([self.word2int[lab] for lab in sample_gt_plain]) # Transform to batch image_widths = [img.shape[1] for img in images] max_image_width = max(image_widths) batch_images = np.ones(shape=[ len(self.validation_list), params['img_height'], max_image_width, params['img_channels'] ], dtype=np.float32) * self.PAD_COLUMN for i, img in enumerate(images): batch_images[i, 0:img.shape[0], 0:img.shape[1], 0] = img # LENGTH width_reduction = 1 for i in range(params['conv_blocks']): width_reduction = width_reduction * params[ 'conv_pooling_size'][i][1] lengths = [batch_images.shape[2] / width_reduction ] * batch_images.shape[0] self.validation_dict = { 'inputs': batch_images, 'seq_lengths': np.asarray(lengths), 'targets': labels, } return self.validation_dict, len(self.validation_list)
def nextBatch(self, params): images = [] labels = [] for i in range(16): temp_filepath = self.training_data[self.curr_idx] full_path = self.data_dirpath + '/' + temp_filepath + '/' + temp_filepath sample_img = cv2.imread(full_path + '.png', False) height = 128 sample_img = ctc_utils.resize(sample_img,height) images.append(ctc_utils.normalize(sample_img)) sample_full_filepath = full_path + '.semantic' gt_file = open(sample_full_filepath, 'r') gt_list = gt_file.readline().rstrip().split(ctc_utils.word_separator()) gt_file.close() labels.append([self.word2int[lab] for lab in gt_list]) self.curr_idx = (self.curr_idx + 1) % len( self.training_data ) image_widths = [img.shape[1] for img in images] max_width = max(image_widths) batch_images = np.ones(shape=[16, 128, max_width, 1], dtype=np.float32)*self.PAD_COLUMN for i, img in enumerate(images): batch_images[i, 0:img.shape[0], 0:img.shape[1], 0] = img width_reduction = 1 for i in range(4): width_reduction = width_reduction * conv_pool[i][1] lengths = [ batch_images.shape[2] / width_reduction ] * batch_images.shape[0] return { 'inputs': batch_images, 'seq_lengths': np.asarray(lengths), 'targets': labels, }
def nextBatch(self, params, mode = 'Train'): images = [] labels = [] # Read files for _ in range(params['batch_size']): if mode == 'Train': sample_filepath = self.training_list[self.current_idx] sample_fullpath = self.corpus_dirpath + '/' + sample_filepath + '/' + sample_filepath elif mode == 'Validation': sample_filepath = self.validation_list[self.current_val_idx] sample_fullpath = self.corpus_dirpath + '/' + sample_filepath + '/' + sample_filepath # IMAGE if self.distortions: sample_img = cv2.imread(sample_fullpath + '_distorted.jpg', False) # Grayscale is assumed else: sample_img = cv2.imread(sample_fullpath + '.png', False) # Grayscale is assumed! height = params['img_height'] sample_img = ctc_utils.resize(sample_img,height) images.append(ctc_utils.normalize(sample_img)) # GROUND TRUTH if self.semantic: sample_full_filepath = sample_fullpath + '.semantic' else: sample_full_filepath = sample_fullpath + '.agnostic' sample_gt_file = open(sample_full_filepath, 'r') sample_gt_plain = sample_gt_file.readline().rstrip().split(ctc_utils.word_separator()) sample_gt_file.close() labels.append([self.word2int[lab] for lab in sample_gt_plain]) if mode == 'Train': self.current_idx = (self.current_idx + 1) % len( self.training_list ) elif mode == 'Validation': self.current_val_idx = (self.current_val_idx + 1) % len( self.validation_list ) # Transform to batch image_widths = [img.shape[1] for img in images] max_image_width = max(image_widths) batch_images = np.ones(shape=[params['batch_size'], params['img_height'], max_image_width, params['img_channels']], dtype=np.float32)*self.PAD_COLUMN for i, img in enumerate(images): batch_images[i, 0:img.shape[0], 0:img.shape[1], 0] = img # LENGTH width_reduction = 1 for i in range(params['conv_blocks']): width_reduction = width_reduction * params['conv_pooling_size'][i][1] lengths = [ batch_images.shape[2] / width_reduction ] * batch_images.shape[0] return { 'inputs': batch_images, 'seq_lengths': np.asarray(lengths), 'targets': labels, }