def _transform2(data, mean, train=True, mean_flag=False): img, label = data img = img.copy() size316 = (316, 316) size = (224, 224) img_o = transforms.scale(img, 316) img_o = transforms.center_crop(img_o, size316) # 学習のときだけ実行 if train: img_o = transforms.random_flip(img_o, y_random=True) img_o = transforms.random_rotate(img_o) # img = random_erase(img) img_o = transforms.resize(img_o, size) # 画像から平均を引く if mean_flag: img_o -= mean img_o *= (1.0 / 255.0) r = random.randint(316, 1500) img_st = transforms.scale(img, r) img_st = transforms.center_crop(img_st, (224, 224)) # 画像から平均を引く if mean_flag: img_st -= mean img_st *= (1.0 / 255.0) return img_o, label, img_st
def transform(data, mean, train=True): img, lable = data img = img.copy() img -= mean size = (224, 224) if train: h, w = img.shape[1:] angles = [i for i in range(0, 360, 10)] angle = np.random.choice(angles) img = rotate(img, angle) rad = angle * np.pi / 180 new_length = int(h / (np.abs(np.cos(rad)) + np.abs(np.sin(rad)))) img = transforms.center_crop(img, (new_length, new_length)) # img = transforms.random_rotate(img, return_param=False) img = transforms.random_flip(img, x_random=True) img = transforms.resize(img, size, interpolation=2) img *= (1.0 / 255.0) return img, lable
def get_example(self, i): if self.imgtype == "npy": img = np.load(self.get_img_path(i)) img = 2 * (np.clip(img, self.base, self.base + self.range) - self.base) / self.range - 1.0 if len(img.shape) == 2: img = img[np.newaxis, ] else: img = self.img2var( read_image(self.get_img_path(i), color=self.color)) # img = resize(img, (self.resize_to, self.resize_to)) if self.crop: H, W = self.crop else: H, W = (16 * ((img.shape[1] - 2 * self.random) // 16), 16 * ((img.shape[2] - 2 * self.random) // 16)) if img.shape[1] < H + 2 * self.random or img.shape[ 2] < W + 2 * self.random: p = max(H + 2 * self.random - img.shape[1], W + 2 * self.random - img.shape[2]) img = np.pad(img, ((0, 0), (p, p), (p, p)), 'edge') img = random_crop( center_crop(img, (H + 2 * self.random, W + 2 * self.random)), (H, W)) if self.random: img = random_flip(img, x_random=True) return img.astype(self.dtype)
def export_onnx(input_image_path, output_path, gpu, only_output=True): """Export ResNet50 model to ONNX graph 'model.onnx' file will be exported under ``output_path``. """ model = C.ResNet50(pretrained_model='imagenet', arch='fb') input_image = read_image(input_image_path) input_image = scale(input_image, 256) input_image = center_crop(input_image, (224, 224)) input_image -= model.mean input_image = input_image[None, :] if gpu >= 0: model.to_gpu() input_image = chainer.cuda.to_gpu(input_image) if only_output: os.makedirs(output_path, exist_ok=True) name = os.path.join(output_path, 'model.onnx') export(model, input_image, filename=name) else: # an input and output given by Chainer will be also emitted # for using as test dataset export_testcase(model, input_image, output_path)
def __call__(self, img): img = scale(img=img, size=self.resize_value) img = center_crop(img, self.input_image_size) img /= 255.0 img -= self.mean img /= self.std return img
def _prepare(self, img): """Prepare an image for feeding it to a model. This is a standard preprocessing scheme used by feature extraction models. First, the image is scaled or resized according to :math:`scale_size`. Note that this step is optional. Next, the image is cropped to :math:`crop_size`. Last, the image is mean subtracted by an array :obj:`mean`. Args: img (~numpy.ndarray): An image. This is in CHW format. The range of its value is :math:`[0, 255]`. Returns: ~numpy.ndarray: A preprocessed image. This is 4D array whose batch size is the number of crops. """ if self.scale_size is not None: if isinstance(self.scale_size, int): img = scale(img, size=self.scale_size) else: img = resize(img, size=self.scale_size) if self.crop == '10': imgs = ten_crop(img, self.crop_size) elif self.crop == 'center': imgs = center_crop(img, self.crop_size)[np.newaxis] imgs -= self.mean[np.newaxis] return imgs
def _transform(data, mean, train=True, mean_flag=False): img, label = data img = img.copy() size316 = (316, 316) size = (224, 224) img = transforms.scale(img, 316) img = transforms.center_crop(img, size316) # 学習のときだけ実行 if train: img = transforms.random_rotate(img) img = transforms.random_flip(img, x_random=True, y_random=True) # imgのリサイズ img = img.transpose(1, 2, 0) img = resize(img, size) # 画像から平均を引く if mean_flag: img -= mean print("mean use") img *= (1.0 / 255.0) img = img.transpose(2, 0, 1) return img, label
def _preprocess(self, img): img = scale(img=img, size=self.scale_size) img = center_crop(img, self.crop_size) img /= 255.0 img -= self.mean img /= self.std return img
def get_example(self, i): if self.imgtype == "npy": img = np.load(self.get_img_path(i)) img = 2 * (np.clip(img, self.base, self.base + self.range) - self.base) / self.range - 1.0 if len(img.shape) == 2: img = img[np.newaxis, ] else: ref_dicom = dicom.read_file(self.get_img_path(i), force=True) # print(ref_dicom) # ref_dicom.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian img = ref_dicom.pixel_array + ref_dicom.RescaleIntercept img = self.img2var(img) img = img[np.newaxis, :, :] if self.scale_to > 0: img = resize(img, (self.scale_to, self.scale_to)) H, W = self.crop # print(img.shape) if img.shape[1] < H + 2 * self.random or img.shape[ 2] < W + 2 * self.random: p = max(H + 2 * self.random - img.shape[1], W + 2 * self.random - img.shape[2]) img = np.pad(img, ((0, 0), (p, p), (p, p)), 'edge') if H + self.random < img.shape[1] and W + self.random < img.shape[2]: img = center_crop(img, (H + self.random, W + self.random)) img = random_crop(img, self.crop) return img
def val_transform(sample): img, label = sample img = np.transpose(img, (2, 0, 1)) img = transforms.resize(img, resize_size) img = transforms.center_crop(img, patchsize) img = img - mean img = img.astype(dtype) if soft: label = hard_to_soft(label, dtype=dtype) return img, label
def test_transform_office(in_data): mean = np.load('imagenet_mean.npy').reshape(3, 256, 256).astype('f') crop_size = 227 img, label = in_data # subtract the mean file img = img - mean # center crop image to 227x227 img = center_crop(img, (crop_size, crop_size)) return img, label
def test_transform(sample): img = sample if len(img.shape) == 2: # Grayscale img = np.stack([img, img, img], 2) img = np.transpose(img, (2, 0, 1)) img = transforms.resize(img, resize_size) img = transforms.center_crop(img, patchsize) img = img - mean img = img.astype(dtype) return img
def test_center_crop(self): img = np.random.uniform(size=(3, 48, 32)) out, param = center_crop(img, (16, 24), return_param=True) x_slice = param['x_slice'] y_slice = param['y_slice'] np.testing.assert_equal(out, img[:, y_slice, x_slice]) self.assertEqual(x_slice, slice(8, 24)) self.assertEqual(y_slice, slice(12, 36))
def __call__(self, img): img = random_crop(img=img, size=self.resize_value) img = random_flip(img=img, x_random=True) img = pca_lighting(img=img, sigma=25.5) img = scale(img=img, size=self.resize_value) img = center_crop(img, self.input_image_size) img /= 255.0 img -= self.mean img /= self.std return img
def test_center_crop(self): img = np.random.uniform(size=(3, 48, 32)) out, param = center_crop(img, (24, 16), return_param=True) y_slice = param['y_slice'] x_slice = param['x_slice'] np.testing.assert_equal(out, img[:, y_slice, x_slice]) self.assertEqual(y_slice, slice(12, 36)) self.assertEqual(x_slice, slice(8, 24))
def get_example(self, i): img = read_image(self.get_img_path(i), color=self.color) img = img * 2 / 255.0 - 1.0 # [-1, 1) # img = resize(img, (self.resize_to, self.resize_to)) img = random_crop( center_crop( img, (self.crop[0] + self.random, self.crop[1] + self.random)), self.crop) if self.random: img = random_flip(img, x_random=True) return img.astype(self.dtype)
def load_dicom(path,base,rng,h,scale): import pydicom as dicom from chainercv.transforms import center_crop from skimage.transform import rescale ref_dicom = dicom.read_file(path, force=True) ref_dicom.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian img = ref_dicom.pixel_array.astype(np.float32)+ref_dicom.RescaleIntercept if scale != 1.0: img = rescale(img,scale,mode="reflect",preserve_range=True) img = (np.clip(img,base,base+rng)-base)/rng * 255 - np.array([103.939, 116.779, 123.68]) img = center_crop(img[np.newaxis,:],(h,h)) return(img.astype(np.float32))
def transform_test(in_data): img, label = in_data if img.shape[0] == 1: img = np.array([img[0], img[0], img[0]]) elif img.shape[0] == 4: img = np.array([img[0], img[1], img[2]]) img /= 255 img = T.scale(img, 256, interpolation=PIL.Image.BICUBIC) img = T.center_crop(img, (224, 224)) img = (img - mean[:, None, None]) / std[:, None, None] return img, label
def augment_data(image, resize_width, resize_height, use_random_x_flip, use_random_y_flip, use_random_rotate, use_pca_lighting, crop_edit, crop_width, crop_height): image = transforms.random_flip(image, use_random_x_flip, use_random_y_flip) if use_random_rotate: image = transforms.random_rotate(image) image = transforms.pca_lighting(image, sigma=use_pca_lighting) if crop_edit == 'Center Crop': image = transforms.center_crop(image, (crop_width, crop_height)) elif crop_edit == 'Random Crop': image = transforms.random_crop(image, (crop_width, crop_height)) image = transforms.resize(image, (resize_width, resize_height)) return image
def get_example(self, i): if self.time_series: offset = random.randrange(self.random) if self.random > 0 else 0 img = np.loadtxt(self.get_img_path(i), delimiter=',', skiprows=self.skiprows, dtype=np.float32) * self.amp img = img[offset:(offset + 3 * self.cw * self.ch), self.cols[0]].reshape(3, self.ch, self.cw) else: img = read_image(self.get_img_path(i), color=self.color) img = L.model.vision.resnet.prepare(img, size=None) img = center_crop(img, (self.ch + self.random, self.cw + self.random)) img = random_crop(img, (self.ch, self.cw)) # img = img * 2 / 255.0 - 1.0 # [-1, 1) if self.random > 0: img = random_flip(img, x_random=True) return (img, self.dat[i])
def __call__(self, chw): chw = chw.astype(np.uint8) # if False: # TODO: fix this # chw = random_blur(chw, self.p_blur, self.blur_max_ksize) # if False: # TODO: fix this # chw = add_random_lines(chw, self.p_add_lines, self.max_num_lines) outputs = [] chw_original = chw for log_ar in self.log_ars: chw = chw_original.copy() if self.preprocess == 'edge': chw = edge(chw) elif self.preprocess == 'blur': chw = blur(chw) chw = stretch(chw, log_ar) chw = dataset_transform.inscribed_center_crop(chw) chw = transforms.scale(chw, self.scaled_size) chw = transforms.center_crop(chw, (self.crop_size, self.crop_size)) chw = chw.astype(np.float32) / 256.0 outputs.append(chw) return outputs
def __call__(self, in_data): img, label = in_data img = scale(img, 256) img = center_crop(img, (224, 224)) img -= self.mean return img, label
model = L.VGG16Layers() # Load image img = np.asarray(Image.open('images/cat.jpg')) # Convert RGB to BGR img = img[:, :, ::-1] # Subtract the mean value of ImageNet train dataset (BGR) img = img - np.array([[[103.939, 116.779, 123.68]]]) # Transpose the image array from (H, W, C) to (C, H, W) img = img.transpose(2, 0, 1) # Crop the center region img = transforms.center_crop(img, (224, 224)) # Create a minibatch whose batchsize=1 x = np.asarray([img], dtype=np.float32) # Export to ONNX onnx_model = onnx_chainer.export(model, x) # Create a ONNXRuntime session sess = rt.InferenceSession(onnx_model.SerializeToString()) # Create a prediction result with ONNXRuntime input_names = [i.name for i in sess.get_inputs()] rt_out = sess.run( None, {name: array for name, array in zip(input_names, (x,))})
def __call__(self, in_data): img, label = in_data img = transforms.scale(img, 256) img = transforms.center_crop(img, (224, 224)) img -= self.mean return img.astype(chainer.get_dtype()), label
def __init__(self, path, args, random=0, forceSpacing=0): self.path = path self.base = args.HU_base self.range = args.HU_range self.random = random self.crop = (args.crop_height, args.crop_width) self.ch = args.num_slices self.forceSpacing = forceSpacing self.dtype = dtypes[args.dtype] self.imgtype = args.imgtype self.dcms = [] self.names = [] self.idx = [] if not args.crop_height: self.crop = (384, 480) ## default for the CBCT dataset print("Load Dataset from disk: {}".format(path)) dirlist = [path] for f in os.listdir(path): if os.path.isdir(os.path.join(path, f)): dirlist.append(os.path.join(path, f)) skipcount = 0 j = 0 # dir index for dirname in sorted(dirlist): files = [ os.path.join(dirname, fname) for fname in sorted(os.listdir(dirname)) if fname.endswith(args.imgtype) ] slices = [] filenames = [] loc = [] for f in files: ds = dicom.dcmread(f, force=True) ds.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian # sort slices according to SliceLocation header if hasattr(ds, 'ImagePositionPatient'): loc.append(float(ds.ImagePositionPatient[2])) slices.append(ds) filenames.append(f) else: skipcount = skipcount + 1 s = sorted(range(len(slices)), key=lambda k: loc[k]) # if the current dir contains at least one slice if len(s) > 0: volume = self.img2var( np.stack([ slices[i].pixel_array.astype(self.dtype) + slices[i].RescaleIntercept for i in s ])) if self.forceSpacing > 0: scaling = float( slices[0].PixelSpacing[0]) / self.forceSpacing volume = rescale(volume, scaling, mode="reflect", preserve_range=True) # img = imresize(img,(int(img.shape[0]*self.scale), int(img.shape[1]**self.scale)), interp='bicubic') volume = center_crop( volume, (self.crop[0] + self.random, self.crop[1] + self.random)) self.dcms.append(volume) self.names.append([filenames[i] for i in s]) self.idx.extend([(j, k) for k in range((self.ch - 1) // 2, len(slices) - self.ch // 2)]) j = j + 1 print("#dir {}, #file {}, #skipped {}".format(len(dirlist), len(self.idx), skipcount))
def get_example(self, i): cutmix_alpha = self.cutmix_alpha class_num = self.class_num h = 224 w = 224 img_1, label_1 = self.base[i] while True: img_2, label_2 = random.choice(self.base) if label_1 != label_2: break img_1 = transforms.scale(img_1, 316) img_2 = transforms.scale(img_2, 316) img_1 = transforms.center_crop(img_1, (316, 316)) img_2 = transforms.center_crop(img_2, (316, 316)) img_1 = transforms.random_flip(img_1, x_random=True, y_random=True) img_2 = transforms.random_flip(img_2, x_random=True, y_random=True) img_1 = img_1.transpose(1, 2, 0) img_2 = img_2.transpose(1, 2, 0) #img_1 = random_rotate(img_1) #img_2 = random_rotate(img_2) img_1 = img_1.transpose(2, 0, 1) img_2 = img_2.transpose(2, 0, 1) img_1 = transforms.resize(img_1, (224, 224)) img_2 = transforms.resize(img_2, (224, 224)) img_1 = transforms.random_rotate(img_1) img_2 = transforms.random_rotate(img_2) ##################################################### # cutmix実装 ##################################################### # sample the bounding box coordinates while True: # the combination ratio λ between two data points is # sampled from the beta distribution Beta(α, α) l = np.random.beta(cutmix_alpha, cutmix_alpha) rx = random.randint(0, w) rw = w * math.sqrt(1-l) ry = random.randint(0, h) rh = h * math.sqrt(1-l) if ((ry + round(rh)) < h)and((rx + round(rw)) < w): break # denotes a binary mask indicating where to drop out # and fill in from two images M_1 = np.zeros((h, w)) M_1[ry:(ry + round(rh)),rx:(rx + round(rw))] = 1 M = np.ones((h, w)) M = M - M_1 # Define the combining operation. img = (img_1 * M + img_2 * M_1).astype(np.float32) # eye関数は単位行列を生成する eye = np.eye(class_num) label = (eye[label_1] * l + eye[label_2] * (1 - l)).astype(np.float32) # 画像255で割る(輝度値をすべて0~1の間に収める) img = img.transpose(1, 2, 0) # 画像から平均を引く if self.mean_flag: img -= self.mean img = img / 255.0 img = img.transpose(2, 0, 1) return img, label
def __init__(self, path, args, base, rang, random=0, mask_value=None): self.path = path self.base = base self.range = rang self.random = random self.ch = args.num_slices self.forceSpacing = args.forceSpacing self.dtype = dtypes[args.dtype] self.imgtype = args.imgtype self.dcms = [] self.names = [] self.idx = [] self.crop = (args.crop_height, args.crop_width) print("Loading Dataset from: {}".format(path)) dirlist = [path] for f in os.listdir(path): if os.path.isdir(os.path.join(path, f)): dirlist.append(os.path.join(path, f)) j = 0 # dir index for dirname in sorted(dirlist): files = [ os.path.join(dirname, fname) for fname in sorted(os.listdir(dirname)) if fname.endswith(args.imgtype) ] slices = [] filenames = [] loc = [] for f in files: ds = dicom.dcmread(f, force=True) #ds.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian # sort slices according to SliceLocation header if hasattr(ds, 'ImagePositionPatient') and ( args.slice_range is not None ): # Thanks to johnrickman for letting me know to use this DICOM entry # if hasattr(ds, 'SliceLocation'): z = float(ds.ImagePositionPatient[2]) if (args.slice_range[0] < z < args.slice_range[1]): slices.append(ds) filenames.append(f) loc.append(z) # sort by z-coord else: slices.append(ds) filenames.append(f) loc.append(f) # sort by filename s = sorted(range(len(slices)), key=lambda k: loc[k]) # if the current dir contains at least one slice if len(s) > 0: vollist = [] for i in s: sl = slices[i].pixel_array.astype( self.dtype) + slices[i].RescaleIntercept if self.forceSpacing > 0: scaling = self.forceSpacing / float( slices[i].PixelSpacing[0]) sl = resize(sl[np.newaxis, ], (int(scaling * sl.shape[0]), int(scaling * sl.shape[1])))[0] # volume = rescale(sl,scaling,mode="reflect",preserve_range=True) vollist.append(sl) volume = self.img2var(np.stack(vollist)) # shape = (z,x,y) print("Loaded volume {} of size {}".format( dirname, volume.shape)) if volume.shape[ 1] < self.crop[0] + 2 * self.random or volume.shape[ 2] < self.crop[1] + 2 * self.random: p = max(self.crop[0] + 2 * self.random - volume.shape[1], self.crop[1] + 2 * self.random - volume.shape[2]) volume = np.pad(volume, ((0, 0), (p, p), (p, p)), 'edge') volume = center_crop(volume, (self.crop[0] + 2 * self.random, self.crop[1] + 2 * self.random)) self.dcms.append(volume) self.names.append([filenames[i] for i in s]) self.idx.extend([(j, k) for k in range((self.ch - 1) // 2, len(slices) - self.ch // 2)]) j = j + 1 print("#dir {}, #file {}, #slices {}".format( len(dirlist), len(self.idx), sum([len(fd) for fd in filenames])))