def augmentation_preprocess(path): datasets = load_data(path) Disgust = datasets[datasets.emotion == 1].iloc[:, 1].values disgust_data = str2arr(Disgust) aug_images = data_augmentation(disgust_data) aug_remove_err = [ im for im in aug_images if im.shape[0] == 48 and im.shape[1] == 48 ] cvt_np_arr = np.asarray(aug_remove_err) labels = np.ones(len(cvt_np_arr)) #visulize data aug fig, axis = plt.subplots(6, 6, figsize=(12, 12)) for i in range(6): for j in range(6): axis[i, j].imshow(cvt_np_arr[i], cmap="Greys") axis[i, j].axis("off") plt.show() # split train test validation aug_xtrain, aug_xtest, aug_ytrain, aug_ytest = train_test_split( cvt_np_arr, labels, test_size=0.2, random_state=0) xtrain, x_val, ytrain, y_val = train_test_split(aug_xtrain, aug_ytrain, test_size=0.2, random_state=0) return xtrain, x_val, ytrain, y_val, aug_xtest, aug_ytest
def next_gen_batch(self): batch_size = self.batch_size imgs, labels = self.generator.generate_images(batch_size) labels = [encode_label(label, CHARS_DICT) for label in labels] images = np.zeros( [batch_size, self.img_h, self.img_w, self.channel_num]) for i, img in enumerate(imgs): img = data_augmentation(img) img = cv2.resize(img, (self.img_w, self.img_h)) images[i, ...] = img sparse_labels = sparse_tuple_from(labels) return images, sparse_labels, labels
def read_data(img_list, annotation, batch_size, aug): num_batch = len(img_list) / batch_size count = 0 while (True): image_data = [] annotation_data = [] for i in range(batch_size): temp_index = i + count * batch_size temp_index %= len(img_list) image = cv2.imread(img_list[temp_index]) if aug: image = data_augmentation(image) image = image[:, :, ::-1] image = image.astype(np.float32) # image = cv2.resize(image,(FLAGS.image_size,FLAGS.image_size)) image = image / 255 image_data.append(image) annotation_data.append(annotation[temp_index]) count += 1 # image_data = np.array(image_data) yield image_data, annotation_data
def fill_queue(self, batch_size=0): load_index = self.load_index self.load_index += batch_size if self.load_index >= self.dataset_size: if not self.is_testset: # test set just end if self.require_shuffle: self.shuffle_dataset() load_index = 0 self.load_index = load_index + batch_size else: self.work_exit.value = True if self.progress < 0.2: max_voxel_points = cfg.VOXEL_POINT_COUNT elif self.progress < 0.4: max_voxel_points = cfg.VOXEL_POINT_COUNT - 2 + np.random.randint(3) elif self.progress < 0.6: max_voxel_points = cfg.VOXEL_POINT_COUNT - 4 + np.random.randint(5) elif self.progress < 0.8: max_voxel_points = cfg.VOXEL_POINT_COUNT - 6 + np.random.randint(7) else: max_voxel_points = cfg.VOXEL_POINT_COUNT - 8 + np.random.randint(9) labels, tag, voxel, doubled_voxel, rgb, raw_lidar, calib = [], [], [], [], [], [], [] voxel_size = np.array( [cfg.VOXEL_Z_SIZE, cfg.VOXEL_Y_SIZE, cfg.VOXEL_X_SIZE], dtype=np.float32) double_voxel_size = 2 * voxel_size # warn("fill : {}".format(batch_size)) for _ in range(batch_size): try: t0 = time.time() ##### AFTER DATA AUGMENTATION STABILIZED #### rgb_img = cv2.imread(self.f_rgb[load_index]) img_height, img_width, _ = rgb_img.shape # warn("height: {} width: {}".format(height, width)) rgb.append( cv2.resize(rgb_img, (cfg.IMAGE_WIDTH, cfg.IMAGE_HEIGHT))) calib_mat = read_calib_mat(self.f_calib[load_index]) calib.append(calib_mat.copy()) lidar, label = data_augmentation( f_lidar=self.f_lidar[load_index], f_label=self.f_label[load_index], calib_mat=calib_mat, img_width=img_width, img_height=img_height) raw_lidar.append(lidar) labels.append(label) tag.append(self.data_tag[load_index]) voxel.append( voxelize(file=self.f_lidar[load_index], lidar=lidar, voxel_size=voxel_size, T=max_voxel_points)) doubled_voxel.append( voxelize(file=self.f_lidar[load_index], lidar=lidar, voxel_size=double_voxel_size, T=max_voxel_points)) t1 = time.time() load_index += 1 # only for voxel -> [gpu, k_single_batch, ...] vox_feature, vox_number, vox_coordinate = [], [], [] single_batch_size = int(self.batch_size / self.multi_gpu_sum) for idx in range(self.multi_gpu_sum): # warn("normal voxel") _, per_vox_feature, per_vox_number, per_vox_coordinate = build_input( voxel[idx * single_batch_size:(idx + 1) * single_batch_size]) vox_feature.append(per_vox_feature) vox_number.append(per_vox_number) vox_coordinate.append(per_vox_coordinate) doubled_vox_feature, doubled_vox_number, doubled_vox_coordinate = [], [], [] for idx in range(self.multi_gpu_sum): # warn("doubled voxel") _, per_vox_feature, per_vox_number, per_vox_coordinate = build_input( doubled_voxel[idx * single_batch_size:(idx + 1) * single_batch_size]) doubled_vox_feature.append(per_vox_feature) doubled_vox_number.append(per_vox_number) doubled_vox_coordinate.append(per_vox_coordinate) self.dataset_queue.put_nowait( (labels, (vox_feature, vox_number, vox_coordinate), (doubled_vox_feature, doubled_vox_number, doubled_vox_coordinate), rgb, raw_lidar, calib, tag)) except: # warn("exception occured") if not self.is_testset: # test set just end self.load_index = 0 if self.require_shuffle: self.shuffle_dataset() else: self.work_exit.value = True
default="./train") parser.add_argument("-n", "--num", help="number of images", type=int, default=1000) args = parser.parse_args() save_dir = args.save_dir def labelToSaveFilename(label): rand_tail = random.randint(10000, 99999) name = '{}_{}.jpg'.format(label, rand_tail) return name FONT_HEIGHT = 32 ttfCharGen = ImageGenerator('./fonts/', char_set=model.CHARS, char_height=FONT_HEIGHT) plates, labels = ttfCharGen.generate_images(args.num) #for plate in plates: #cv2.imshow('', plate) #cv2.waitKey(0) if not os.path.isdir(save_dir): os.mkdir(save_dir) for img, label in zip(plates, labels): full_path = os.path.join(save_dir, labelToSaveFilename(label)) img = data_augmentation(img) cv2.imwrite(full_path, img)