def __getitem__(self, idx): # --- GET IMAGE ---# # torch > 1.7 try: image = read_image( os.path.join(self.root, str(self.photo_id[idx]) + '.jpg')) except: return self.__getitem__(idx) # --- GET LABEL ---# label = self.get_signal(idx) seq_label = self.get_seqence(idx) if not self.inf: return image, label, seq_label elif self.inf: return image, label, seq_label, str(self.photo_id[idx])
def overlay_img_with_density_padding(img_origin, density_map_path, output_path): """ :param img_path: :param density_map_path: output .torch of density map :param output_path: :return: """ # # img_origin = Image.open(img_path).convert('RGB') # # transformer = transforms.Compose([ # transforms.ToTensor() # ]) img_tensor = read_image(img_origin) print("img tensor shape", img_tensor.shape) density_map_tensor = torch.load(density_map_path) print(img_tensor.shape) print(density_map_tensor.shape) print(density_map_tensor.sum()) density_map_tensor = torch.from_numpy(density_map_tensor).unsqueeze( dim=0).unsqueeze(dim=0) print("density_map_tensor.shape", density_map_tensor.shape) # torch.Size([1, 1, 46, 82]) upsampling_density_map_tensor = nn.functional.interpolate( density_map_tensor, scale_factor=8) / 64 pad_density_map_tensor = torch.zeros( (1, 3, img_tensor.shape[1], img_tensor.shape[2])) pad_density_map_tensor[:, 0, :upsampling_density_map_tensor. shape[2], :upsampling_density_map_tensor. shape[3]] = upsampling_density_map_tensor overlay_density_map = img_tensor.detach().clone() pad_density_map_tensor = (pad_density_map_tensor.squeeze(dim=0) / pad_density_map_tensor.max() * 255) overlay_density_map[0] = torch.clamp_max(img_tensor[0] + pad_density_map_tensor[0] * 2, max=255) write_jpeg(overlay_density_map.type(torch.uint8), output_path, quality=100)
def reconstruct_from_file(generator, file_name, target_size, size=1): image = io.read_image(file_name) image = T.Resize(target_size)(image) / 127.5 - 1.0 image = image[None, ...] recons = tensor_to_images(generator(image)) image = tensor_to_images(image) plt.figure(figsize=(4 * size, 4 * size)) plt.subplot(1, 2, 1) plt.imshow(image[0]) plt.title('original', fontsize=6 + 2 * size) plt.axis('off') plt.subplot(1, 2, 2) plt.imshow(recons[0]) plt.title('restored', fontsize=6 + 2 * size) plt.axis('off') plt.show()
def _get_image(self, idx): if torch.is_tensor(idx): idx = idx.tolist() image = self.index[idx] img_path = image['path'] img_arr = io.read_image(os.path.join(self.data_directory, img_path)) # Repeat channels for gray images if (img_arr.shape[0] == 1): img_arr = torch.repeat_interleave(img_arr, 3, axis=0) # Remove channels for RGBA if (img_arr.shape[0] == 4): img_arr = img_arr[:3, :, :] img_arr = self.normalizer(img_arr) return img_arr
def __getitem__(self, idx): """ 从给定索引idx的数据集中加载并返回一个样本 根据这个索引,它标识图像在磁盘上的位置。 使用read_image()将其转化为张量 从self.img_labels()中的CSV数据中获取相应的标签,对其调用transform转化函数(如果适用), 并在一个Python Dict中返回张量图像和相应的标签 :param idx: :return: """ img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0]) image = read_image(img_path) label = self.img_labels.iloc[idx, 1] if self.transform: image = self.transform(image) if self.target_transform: label = self.target_transform(label) sample = {"image": image, "label": label} return sample
def _get_image(self, idx): if torch.is_tensor(idx): idx = idx.tolist() image = self.index[idx] img_path = image['path'] img_arr = io.read_image(img_path) # Repeat channels for gray images if (img_arr.shape[0] == 1): img_arr = torch.repeat_interleave(img_arr, 3, axis=0) # Remove channels for RGBA if (img_arr.shape[0] == 4): img_arr = img_arr[:3, :, :] # FIXME: when using multiprocessing on jupyter notebook, # the computation stucks with the following call. img_arr = self.normalizer(img_arr) return img_arr
def __getitem__(self, idx): """ An instance in this dataset is just an image. This returns that image with the desired transforms + the filename """ # Load image image = read_image(f"{self.fp_root}/{self.filenames[idx]}") # Apply transforms if self.transform: image = self.transform(image) # Return as a dict instance = { 'image': image, 'filename': self.filenames[idx], } # TODO HDF5 / bmp efficiency return instance
def __getitem__(self, idx): """ An instance in this dataset is the image, label, and associated info """ # Load image image = read_image(f"{self.fp_root}/{self.filenames[idx]}") # Apply transforms if self.transform: image = self.transform(image) # Return as a dict instance = { 'image': image, 'filename': self.filenames[idx], 'label': self.labels[idx], 'label_name': self.label_name_map[self.labels[idx]] } # TODO HDF5 / bmp efficiency return instance
def prepare_flower_dataset(img_folder, output_folder, x_size=32, y_size=128): y_transforms = T.Compose([ T.Resize(y_size), T.RandomHorizontalFlip(), T.RandomCrop(y_size), ]) x_transforms = T.Compose([ T.Resize(x_size), ]) os.makedirs(output_folder + 'y/', exist_ok=True) os.makedirs(output_folder + 'x/', exist_ok=True) for name in os.listdir(img_folder): output_name = os.path.splitext(name)[0] if name.endswith('.jpg'): image = io.read_image(img_folder + name) y_image = y_transforms(image) x_image = x_transforms(y_image) save_image(y_image, output_folder + 'y/' + output_name + '.png') save_image(x_image, output_folder + 'x/' + output_name + '.png')
def save_as_grid(paths, data_root, save_to, indices, projects, strategies, ncentroids, img_per_grid=50, nrow=5): save_to = Path(save_to) / "grids" if not save_to.exists(): save_to.mkdir() indices = indices.ravel() cwd = Path(hydra.utils.get_original_cwd()) slides = defaultdict(set) for proj in projects: for strategy in strategies: manifest = f"tcga_manifests/{proj}_{strategy}.txt" df = pd.read_csv(f"{cwd}/{manifest}", delimiter="\t") slides[proj] |= set([Path(i).stem for i in df.filename]) for grp in tqdm(range(ncentroids)): indice = (indices == grp) if indice.sum() == 0: continue paths_grp = paths[indice] random.shuffle(paths_grp) for proj in projects: paths_proj = pathsOfProj(paths_grp, slides[proj]) paths_grid = paths_proj[:img_per_grid] if len(paths_grid) == 0: continue imgs_grid = [read_image(i) / 255 for i in paths_grid] save_image(imgs_grid, f"{save_to}/cluster_{grp}_{proj}.jpg", nrow=nrow, padding=1)
def __getitem__(self, idx): annotations = self.annotation_dict["annotations"] annotation = annotations[idx] img_name = annotation["file_name"] img_path = os.path.join(self.img_dir, img_name) img = read_image(img_path, torchvision.io.image.ImageReadMode.RGB) labels = [] imgh_ori = annotation["height"] imgw_ori = annotation["width"] scalex = 224/imgw_ori scaley = 224/imgh_ori for obj_coordinate in annotation["coordinates"]: center = obj_coordinate["center"] polar_coordinate = obj_coordinate["polar_coordinate"] label = [] label.extend(center) for pc in polar_coordinate: theta = pc[0] d = pc[1] dx = math.cos(theta)*d dy = math.sin(theta)*d dx_scaled = dx*scalex dy_scaled = dy*scaley d_scaled = math.sqrt(dx_scaled**2+dy_scaled**2) label.append(d_scaled) labels.append(label) labels = torch.tensor(labels) if self.transform: img = self.transform(img) if self.target_transform: labels = self.target_transform(labels) return img, labels
def __getitem__(self, item): img = read_image(self.images[item]) tgt = self.parse_voc_xml(ET.parse(self.annotations[item]).getroot()) bbox = [] label = [] difficult = [] objs = tgt['annotation']['object'] if not isinstance(objs, list): objs = [objs] for obj in objs: bbox.append([ int(obj['bndbox'][k]) for k in ('xmin', 'ymin', 'xmax', 'ymax') ]) label.append(self.voc_bbox_label_names.index(obj['name'])) difficult.append(int(obj['difficult'])) tgt = dict(boxes=torch.tensor(bbox, dtype=torch.long), labels=torch.tensor(label, dtype=torch.long), difficult=torch.tensor(difficult, dtype=torch.long)) if self.transforms is not None: bbox = tgt['boxes'].float() img, bbox = self.transforms(img, bbox) tgt['boxes'] = bbox.long() return img, tgt
from tqdm import tqdm from matplotlib import pyplot as plt import torch from torch import nn from torch.utils.data import DataLoader from torchvision.io import read_image from torchvision.io.image import ImageReadMode from model import * from gradient_utils import * from utils import * img_path = pathlib.Path.cwd() / 'imgs' / 'shaderball.png' img_ = read_image(str(img_path), ImageReadMode.GRAY) / 255. img = 2 * (img_ - .5) img = torch.squeeze(img) downsampling_factor = 2 img = img[::downsampling_factor, ::downsampling_factor] dataset = pixel_dataset(torch.squeeze(img)) #Hyperparameters epochs = 15000 batch_size = img.size()[1]**2 log_freq = 3 hidden_features = 256 hidden_layers = 3 siren_model = image_siren(hidden_layers=hidden_layers,
def get_input(self, index): from torchvision.io import read_image item_folder = self.folder_path +"/"+ self.ids[index] return read_image(item_folder+"/data.jpg")
def __init__(self, loader: Optional[Callable] = None): if loader is None: loader = lambda x: read_image(x) / 255. self._loader = loader
axs[0, i].imshow(np.asarray(img)) axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) #################################### # Visualizing a grid of images # ---------------------------- # The :func:`~torchvision.utils.make_grid` function can be used to create a # tensor that represents multiple images in a grid. This util requires a single # image of dtype ``uint8`` as input. from torchvision.utils import make_grid from torchvision.io import read_image from pathlib import Path dog1_int = read_image(str(Path('assets') / 'dog1.jpg')) dog2_int = read_image(str(Path('assets') / 'dog2.jpg')) grid = make_grid([dog1_int, dog2_int, dog1_int, dog2_int]) show(grid) #################################### # Visualizing bounding boxes # -------------------------- # We can use :func:`~torchvision.utils.draw_bounding_boxes` to draw boxes on an # image. We can set the colors, labels, width as well as font and font size. # The boxes are in ``(xmin, ymin, xmax, ymax)`` format. from torchvision.utils import draw_bounding_boxes
dataset_test[3][0].shape # pick one image from the test set img, _ = dataset_test[0] # put the model in evaluation mode model.eval() with torch.no_grad(): prediction = model([img.to(device)]) # This tests the model with completely new images I created by manually # manipulating the images in the original set. from torchvision.io import read_image from torchvision.transforms.functional import convert_image_dtype dog1_int = read_image('/content/drive/MyDrive/APHIS Farm Bill (2020Milestones)/Protocols/For John/images/New set for John/collection/four_chambers/newtestimgs/set100m_vh_4.png') dog2_int = read_image('/content/drive/MyDrive/APHIS Farm Bill (2020Milestones)/Protocols/For John/images/New set for John/collection/four_chambers/newtestimgs/set100m_vh_5.png') # The data need to be converted to GPU, or cuda, compatible. # An example of loading model and data: #device = torch.device("cuda") #model = TheModelClass(*args, **kwargs) #model.load_state_dict(torch.load(PATH)) #model.to(device) # Make sure to call input = input.to(device) on any input tensors that you feed to the model dog1 = dog1_int.to(device) dog2 = dog2_int.to(device) batch_int = torch.stack([dog1, dog2])
def _load_image(self, idx: int): path = self.image_paths[idx] return read_image(path, mode=ImageReadMode.RGB)
def __getitem__(self, idx): image = read_image(os.path.join(self.img_dir, self.images[idx])) label = int("dog" in os.path.basename(self.images[idx])) if self.transform: image = self.transform(image) return image, torch.tensor(label)
def file_loader(self, path): image = io.read_image(path) # Convert uint8 to float32 and rescale image = transforms.functional.convert_image_dtype(image) img_tensor = self.preprocess(image) return img_tensor
"Error: odd number of images. Is there a 0 min. image and a 61 min. image for each assay?" ) print("The following images will be analyzed:{}\n".format(waiting_images)) img_count = 0 # Need a way to check if image have been processed or not. Have to set up a # tracking table. im_start_lis = list() im_perm_lis = list() for i in range(len(waiting_images)): # "read_image" is the Torchvision version of image ingestion. im_start_lis.append(read_image(w_im_dir_read[i]).to(device)) # Change Torch-based image read to shape that plt can render. (4,1600,1600) # to (1600,1600,4) im_perm_lis.append(im_start_lis[i].permute(1, 2, 0)) batch_int = list() for i, j in zip(range(0, len(im_start_lis), 2), range(1, len(im_start_lis), 2)): # Gather inputs for batches. The model is run on each batch for the # comparison. batch_int.append(torch.stack([im_start_lis[i], im_start_lis[j]])) #batch_lis.append(batch(convert_image_dtype(batch_int[i], dtype=torch.float))) batch = list()
train_loss = 0 dset = ImageCaptions(data, batchsize=batchSize) # initialize iterator for i, c in dset: # loop through dataset by minibatch text = torch.LongTensor(c) # a minibatch of text (numerical tokens) images = torch.zeros(len(i), 3, 256, 256) # placeholder for images text = text.to(device) #print(text) # fetch images into tensor based on paths given in minibatch ix = 0 for imgfn in i: # iterate through image paths in minibatch # note: images are expected to be in ./imagefolder/0/ img_t = read_image(opt.dataPath + "/" + imgfn).float( ) / 255. # read image and scale into float 0..1 img_t = tf(img_t) # normalize images[ix, :, :, :] = img_t ix += 1 images = images.to(device) mask = torch.ones_like(text).bool().to(device) # train and optimize a single minibatch optimizer.zero_grad() loss = dalle(text, images, mask=mask, return_loss=True) train_loss += loss.item() loss.backward() optimizer.step()
def transform_images(examples): images = [read_image(image_file, mode=ImageReadMode.RGB) for image_file in examples[image_column]] examples["pixel_values"] = [image_transformations(image) for image in images] return examples
def _get_attr_batch(self) -> Tuple[torch.ByteTensor, torch.LongTensor]: """Get pairs of image patches, and the EXIF values predictions Returns ------- Tuple[torch.ByteTensor, torch.LongTensor] [2, batch_size, C, H, W], [batch_size, n_exif_attr] Range [0, 255], One of {0, 1} """ # FIXME Disable automatic batching? Or define a sampler? # TODO Include post-processing consistency pipeline n_rows, n_cols = self.exif_data.shape # Randomly choose an EXIF value exif_idx = np.random.randint(n_cols) # FIXME Cache EXIF values? exif_col = self.exif_data.iloc[:, exif_idx] while True: exif_value = np.random.choice(exif_col.unique()) if exif_value is not np.nan: break # Get all images with / w/o that `exif_value` is_exif_value = exif_col == exif_value imgs_with_value = self.img_paths[is_exif_value] imgs_wo_value = self.img_paths[~is_exif_value] # [2, B, C, H, W] img_batch = torch.zeros(2, self.batch_size, 3, self.patch_size, self.patch_size, dtype=torch.uint8) # [2, B, n_exif_attr] attrs_batch = np.empty((2, self.batch_size, self.n_exif_attr), dtype=object) # FIXME Possible to vectorize this? # Create batch for batch_idx in range(self.batch_size): for pair_idx in (0, 1): # Create negative pairs for second half of the batch if batch_idx >= int(self.batch_size / 2): imgs_to_sample = imgs_wo_value if pair_idx else imgs_with_value else: imgs_to_sample = imgs_with_value img_sample = imgs_to_sample.sample() img_idx = img_sample.index.values[0] # Get attributes attrs = self.exif_data.loc[img_idx].values attrs_batch[pair_idx, batch_idx] = attrs # Get image img_path = img_sample.values[0] img = read_image(img_path) # Resize image if smaller than patch size img = self._resize_img(img) img_patch = self._get_random_patch(img) img_batch[pair_idx, batch_idx] = img_patch # Compute labels; by comparing the attrs of each pair labels_batch = attrs_batch[0] == attrs_batch[1] labels_batch = torch.tensor(labels_batch, dtype=torch.int64) return img_batch, labels_batch
import torch # Blog: https://blog.csdn.net/fengbingchun/article/details/121191194 # 下载MNIST数据集: torchvision.datasets包 test = datasets.MNIST("../../data", train=False, download=True) train = datasets.MNIST("../../data", train=True, download=False) print(f"raw_folder: test: {test.raw_folder}, train: {train.raw_folder}") print( f"processed_folder: test: {test.processed_folder}, train: {train.processed_folder}" ) print(f"extra_repr:\ntest: {test.extra_repr}\ntrain: {train.extra_repr}") print(f"class to index: {test.class_to_idx}") # 读写图像: torchvision.io包 tensor = io.read_image("../../data/image/1.jpg") print("tensor shape:", tensor.shape) io.write_png(tensor, "../../data/image/result.png") tensor = io.read_image("../../data/image/lena.png") print("tensor shape:", tensor.shape) io.write_jpeg(tensor, "../../data/image/result.jpg") # 下载pre-trained AlexNet模型: torchvision.models包 net = models.alexnet(pretrained=True) # 计算机视觉操作: torchvision.ops包 boxes = torch.tensor([[1, 1, 101, 101], [3, 5, 13, 15], [2, 4, 22, 44]]) area = ops.box_area(boxes) print(f"area: {area}")
axs[0, i].imshow(np.asarray(img)) axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) #################################### # Visualizing a grid of images # ---------------------------- # The :func:`~torchvision.utils.make_grid` function can be used to create a # tensor that represents multiple images in a grid. This util requires a single # image of dtype ``uint8`` as input. from torchvision.utils import make_grid from torchvision.io import read_image from pathlib import Path dog1_int = read_image(str(Path('assets') / 'dog1.jpg')) dog2_int = read_image(str(Path('assets') / 'dog2.jpg')) grid = make_grid([dog1_int, dog2_int, dog1_int, dog2_int]) show(grid) #################################### # Visualizing bounding boxes # -------------------------- # We can use :func:`~torchvision.utils.draw_bounding_boxes` to draw boxes on an # image. We can set the colors, labels, width as well as font and font size. # The boxes are in ``(xmin, ymin, xmax, ymax)`` format. from torchvision.utils import draw_bounding_boxes boxes = torch.tensor([[50, 50, 100, 200], [210, 150, 350, 430]],
def read_image(image_path: str, read_mode=ImageReadMode.RGB): return io.read_image(image_path, read_mode)
@functools.lru_cache(maxsize=32) def read_image_from_str(img: str, num_channels: Optional[int] = None) -> torch.Tensor: try: from torchvision.io import ImageReadMode, read_image except ImportError: logger.error( " torchvision is not installed. " "In order to install all image feature dependencies run " "pip install ludwig[image]" ) sys.exit(-1) try: if num_channels == 1: return read_image(img, mode=ImageReadMode.GRAY) elif num_channels == 2: return read_image(img, mode=ImageReadMode.GRAY_ALPHA) elif num_channels == 3: return read_image(img, mode=ImageReadMode.RGB) elif num_channels == 4: return read_image(img, mode=ImageReadMode.RGB_ALPHA) else: return read_image(img) except HTTPError as e: upgraded = upgrade_http(img) if upgraded: logger.info(f"reading image url {img} failed due to {e}. upgrading to https and retrying") return read_image(upgraded) logger.info(f"reading image url {img} failed due to {e} and cannot be upgraded to https") return None
torch.manual_seed(1) def show(imgs): fix, axs = plt.subplots(ncols=len(imgs), squeeze=False) for i, img in enumerate(imgs): img = T.ToPILImage()(img.to('cpu')) axs[0, i].imshow(np.asarray(img)) axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) #################################### # The :func:`~torchvision.io.read_image` function allows to read an image and # directly load it as a tensor dog1 = read_image(str(Path('assets') / 'dog1.jpg')) dog2 = read_image(str(Path('assets') / 'dog2.jpg')) show([dog1, dog2]) #################################### # Transforming images on GPU # -------------------------- # Most transforms natively support tensors on top of PIL images (to visualize # the effect of the transforms, you may refer to see # :ref:`sphx_glr_auto_examples_plot_transforms.py`). # Using tensor images, we can run the transforms on GPUs if cuda is available! import torch.nn as nn transforms = torch.nn.Sequential( T.RandomCrop(224),
def _make_img_gt_point_pair(self, index): _img = read_image(self.dataset_dir+os.sep+self.filenames[index]) _target = self.labels[index,:] return _img, _target