def _aug_image(self, instance, net_h, net_w): image_name = instance['filename'] image_name = image_name.replace('.raw', '.mhd') image = raw_reader(image_name, instance['img_num']) if image.shape[0] != 512: return None, None if image is None: print('Cannot find ', image_name) image_h, image_w, _ = image.shape # determine the amount of scaling and cropping dw = self.jitter * image_w dh = self.jitter * image_h # dw = 0 # dh = 0 new_ar = (image_w + np.random.normal(0, dw)) / ( image_h + np.random.normal(0, dh)) # scale = np.random.uniform(0.25, 2) scale = np.random.normal(1, 0.15) if new_ar < 1: new_h = int(scale * net_h) new_w = int(net_h * new_ar) else: new_w = int(scale * net_w) new_h = int(net_w / new_ar) dx = int(np.random.uniform(0, net_w - new_w)) dy = int(np.random.uniform(0, net_h - new_h)) # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space # im_sized = random_distort_image(im_sized) # randomly flip flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) # correct the size and pos of bounding boxes all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) im_sized = self.gauss_noise(im_sized, 0, 0.001) return im_sized, all_objs
def _aug_image(self, instance, net_h, net_w): # Read image in BGR format filename = instance['filename'] image = cv2.imread(filename) if image is None: raise RuntimeError("Unable to load image file: %s" % filename) # Convert to RGB image = image[:,:,::-1] # Apply jitter and scaling image_h, image_w, _ = image.shape dw = self.aug_jitter * image_w dh = self.aug_jitter * image_h new_ar = (image_w + np.random.uniform(-dw, dw)) / (image_h + np.random.uniform(-dh, dh)) scale = np.random.uniform(self.aug_scale[0], self.aug_scale[1]) if (new_ar < 1): new_h = int(scale * net_h) new_w = int(new_h * new_ar) else: new_w = int(scale * net_w) new_h = int(new_w / new_ar) # Apply padding to place the image in a random position within the net if not self.aug_pad: dx = 0 dy = 0 else: dx = int(np.random.uniform(0, net_w - new_w)) dy = int(np.random.uniform(0, net_h - new_h)) # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space im_sized = random_distort_image(im_sized, self.aug_hue, self.aug_saturation, self.aug_exposure) # randomly flip if self.aug_flip: flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) else: flip = False # Make into gray image if self.aug_gray: im_sized = cv2.cvtColor(im_sized, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis] # correct the size and pos of bounding boxes all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) return im_sized, all_objs
def _aug_image(self, instance, net_h, net_w): image_name = instance['filename'] image_name = image_name.replace( 'yolo_v3', '') # hack for changed folder structure image = cv2.imread(image_name) # RGB image if image is None: print('Cannot find ', image_name) image = image[:, :, ::-1] # RGB image image_h, image_w, _ = image.shape # determine the amount of scaling and cropping dw = self.jitter * image_w dh = self.jitter * image_h new_ar = (image_w + np.random.uniform(-dw, dw)) / ( image_h + np.random.uniform(-dh, dh)) scale = np.random.uniform(0.95, 1.05) if (new_ar < 1): new_h = int(scale * net_h) new_w = int(net_h * new_ar) else: new_w = int(scale * net_w) new_h = int(net_w / new_ar) dx = int(np.random.uniform(0, net_w - new_w)) dy = int(np.random.uniform(0, net_h - new_h)) # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space # im_sized = random_distort_image(im_sized) # randomly flip flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) #im_sized = random_flip(image, flip) flip2 = np.random.randint(2) im_sized = random_flip2(im_sized, flip2) # correct the size and pos of bounding boxes #all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) all_objs = correct_bounding_boxes2(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, flip2, image_w, image_h) return im_sized, all_objs
def _aug_image(self, instance, net_h, net_w): #image_name = "." + instance['filename'] image_name = instance['filename'] pil_image = PIL.Image.open(image_name).convert('RGB') image = np.array(pil_image) #image = cv2.imread(image_name) # RGB image if image is None: print('Cannot find ', image_name) #'image = image[:, :, ::-1] # RGB image image_h, image_w, _ = image.shape # determine the amount of scaling and cropping dw = self.jitter * image_w dh = self.jitter * image_h new_ar = (image_w + np.random.uniform(-dw, dw)) / ( image_h + np.random.uniform(-dh, dh)) scale = np.random.uniform(0.25, 2) if (new_ar < 1): new_h = int(scale * net_h) new_w = int(net_h * new_ar) else: new_w = int(scale * net_w) new_h = int(net_w / new_ar) dx = int(np.random.uniform(0, net_w - new_w)) dy = int(np.random.uniform(0, net_h - new_h)) # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space im_sized = random_distort_image(im_sized) # randomly flip flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) # correct the size and pos of bounding boxes all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) return im_sized, all_objs
def _aug_image(self, instance, net_h, net_w): _, image_name = os.path.split(instance['filename']) full_image_name = self.im_dir + image_name image = cv2.imread(full_image_name) # RGB image if image is None: print('Cannot find ', full_image_name) image = image[:,:,::-1] # RGB image image_h, image_w, _ = image.shape # determine the amount of scaling and cropping dw = self.jitter * image_w; dh = self.jitter * image_h; new_ar = (image_w + np.random.uniform(-dw, dw)) / (image_h + np.random.uniform(-dh, dh)); scale = np.random.uniform(0.95, 1.05); if (new_ar < 1): new_h = int(scale * net_h); new_w = int(net_h * new_ar); else: new_w = int(scale * net_w); new_h = int(net_w / new_ar); dx = int(np.random.uniform(0, net_w - new_w)); dy = int(np.random.uniform(0, net_h - new_h)); # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space # im_sized = random_distort_image(im_sized) # randomly flip flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) #im_sized = random_flip(image, flip) #flip2 = np.random.randint(2) #im_sized = random_flip2(im_sized, flip2) # correct the size and pos of bounding boxes all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) #all_objs = correct_bounding_boxes2(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, flip2, image_w, image_h) return im_sized, all_objs
def _aug_image(self, instance, net_h, net_w): _, image_name = os.path.split(instance['filename']) full_image_name = self.im_dir + image_name image = cv2.imread(full_image_name) # RGB image if image is None: print('Cannot find ', full_image_name) image = image[:,:,::-1] # RGB image image_h, image_w, _ = image.shape # determine the amount of scaling and cropping dw = self.jitter * image_w; dh = self.jitter * image_h; new_ar = (image_w + np.random.uniform(-dw, dw)) / (image_h + np.random.uniform(-dh, dh)); scale = np.random.uniform(0.95, 1.05); if (new_ar < 1): new_h = int(scale * net_h); new_w = int(net_h * new_ar); else: new_w = int(scale * net_w); new_h = int(net_w / new_ar); dx = int(np.random.uniform(0, net_w - new_w)); dy = int(np.random.uniform(0, net_h - new_h)); # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space # im_sized = random_distort_image(im_sized) # randomly flip flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) #im_sized = random_flip(image, flip) flip2 = np.random.randint(2) im_sized = random_flip2(im_sized, flip2) # correct the size and pos of bounding boxes #all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) all_objs = correct_bounding_boxes2(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, flip2, image_w, image_h) return im_sized, all_objs
def _aug_image(self, instance, net_h, net_w): image_name = instance['filename'] image = cv2.imread(image_name, ) # RGB image image = cv2.imdecode(np.fromfile(image_name, dtype=np.uint8), cv2.IMREAD_UNCHANGED) # 打开含有中文路径的图片 if image is None: print('Cannot find ', image_name) image = image[:,:,::-1] # RGB image image_h, image_w, _ = image.shape # determine the amount of scaling and cropping dw = self.jitter * image_w; dh = self.jitter * image_h; new_ar = (image_w + np.random.uniform(-dw, dw)) / (image_h + np.random.uniform(-dh, dh)); scale = np.random.uniform(0.25, 2); if (new_ar < 1): new_h = int(scale * net_h); new_w = int(net_h * new_ar); else: new_w = int(scale * net_w); new_h = int(net_w / new_ar); dx = int(np.random.uniform(0, net_w - new_w)); dy = int(np.random.uniform(0, net_h - new_h)); # apply scaling and cropping im_sized = apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy) # randomly distort hsv space im_sized = random_distort_image(im_sized) # randomly flip flip = np.random.randint(2) im_sized = random_flip(im_sized, flip) # correct the size and pos of bounding boxes all_objs = correct_bounding_boxes(instance['object'], new_w, new_h, net_w, net_h, dx, dy, flip, image_w, image_h) return im_sized, all_objs