def __init__(self, hr_dir, upscale_factor, lr_dir=None, crop_size_val=None, **kwargs): super(ValDataset, self).__init__() self.hr_files = [join(hr_dir, x) for x in listdir(hr_dir) if utils.is_image_file(x)] self.upscale_factor = upscale_factor self.crop_size = crop_size_val if lr_dir is None: self.lr_files = None else: self.lr_files = [join(lr_dir, x) for x in listdir(lr_dir) if utils.is_image_file(x)]
def evaluate(args): # device = torch.device('cuda' if args.cuda and torch.cuda.is_available() else 'cpu') model = TransformerNet() state_dict = torch.load(args.model) if args.gpus is not None: model = nn.DataParallel(model, device_ids=args.gpus) else: model = nn.DataParallel(model) model.load_state_dict(state_dict) if args.cuda: model.cuda() with torch.no_grad(): for root, dirs, filenames in os.walk(args.input_dir): for filename in filenames: if utils.is_image_file(filename): impath = osp.join(root, filename) img = utils.load_image(impath) img = img.unsqueeze(0) if args.cuda: img.cuda() rec_img = model(img) if args.cuda: rec_img = rec_img.cpu() img = img.cpu() save_path = osp.join(args.output_dir, filename) # utils.save_image(rec_img[0], save_path) utils.save_image_preserv_length(rec_img[0], img[0], save_path)
def __init__(self, image_dir): super(DatasetFromFolder, self).__init__() self.a_path = join(image_dir, "low") self.b_path = join(image_dir, "high") self.image_filenames = [ x for x in listdir(self.a_path) if is_image_file(x) ]
def __init__(self, image_dir, input_transforms=None): super(ImageDataset, self).__init__() self.image_filenames = [ os.path.join(image_dir, x) for x in os.listdir(image_dir) if is_image_file(x) ] self.input_transforms = input_transforms
def __init__(self, image_dir, direction): super(DatasetFromFolder, self).__init__() self.direction = direction self.a_path = join(image_dir, "a") self.b_path = join(image_dir, "b") self.image_filenames = [ x for x in listdir(self.a_path) if is_image_file(x) ] self.transformA = transforms.Compose([ transforms.Resize((int(256 * 1.1), int(256 * 1.1))), transforms.RandomRotation(3), transforms.RandomCrop(256), transforms.ColorJitter(hue=.05, saturation=.05), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) self.transformB = transforms.Compose([ transforms.Resize((256, 256)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])
def handle_chat_message(room_id, message_input): trimmed_body = message_input["body"] if trimmed_body: trimmed_body = utils.trim_spaces(trimmed_body) file = message_input["file"] if not trimmed_body and not file: return if file: is_image = utils.is_image_file(file) if not is_image: return current_user = session["current_user"] message = { "id": generate(), "author": current_user, "body": trimmed_body, "file": file, "timestamp": time() * 1000 } emit("chat message", message, room=room_id, include_self=False) return message
def __init__(self, noisy_dir, crop_size, upscale_factor=4, cropped=False, flips=False, rotations=False, **kwargs): super(TrainDataset, self).__init__() # get all directories used for training if isinstance(noisy_dir, str): noisy_dir = [noisy_dir] self.files = [] for n_dir in noisy_dir: self.files += [ join(n_dir, x) for x in listdir(n_dir) if utils.is_image_file(x) ] # intitialize image transformations and variables self.input_transform = T.Compose([ T.RandomVerticalFlip(0.5 if flips else 0.0), T.RandomHorizontalFlip(0.5 if flips else 0.0), T.RandomCrop(crop_size) ]) self.crop_transform = T.RandomCrop(crop_size // upscale_factor) self.upscale_factor = upscale_factor self.cropped = cropped self.rotations = rotations
def __init__(self, root_dir, split, data_splits=None, im_dim=None, transform=None): super(SplitTCIA3DDataset, self).__init__() if data_splits == None: data_splits = {'train': [], 'test': []} all_splits = ['split_' + str(i + 1) for i in range(6)] for i in range(6): data_splits['test'] = [all_splits[i]] data_splits['train'] = all_splits[:i] + all_splits[i + 1:] self.im_dim = im_dim # list_dir = [] self.image_filenames = [] for i in data_splits: # list_dir.append(join(root_dir, i)) image_dir = join(root_dir, self.im_dim, i, 'image') # print("\n\n\n", image_dir,"\n\n\n") target_dir = join(root_dir, self.im_dim, i, 'label') self.image_filenames += [ join(image_dir, x) for x in listdir(image_dir) if is_image_file(x) ] self.target_filenames += [ join(target_dir, x) for x in listdir(target_dir) if is_image_file(x) ] self.image_filenames = sorted(self.image_filenames) self.target_filenames = sorted(self.target_filenames) assert len(self.image_filenames) == len(self.target_filenames) # report the number of images in the dataset print('Number of {0} images: {1} NIFTIs'.format(split, self.__len__())) # data augmentation self.transform = transform
def __init__(self, dataPath='/media/data1/JY/neuron/ISBI/test/imgs/', loadSize=512): super(ISBI_TEST, self).__init__() # list all images into a list self.image_list = [x for x in listdir(dataPath) if is_image_file(x)] self.dataPath = dataPath self.loadSize = loadSize
def __init__(self, dataPath='', loadSize=72, fineSize=64, flip=1): super(DATASET, self).__init__() # list all images into a list self.list = [x for x in listdir(dataPath) if is_image_file(x)] self.dataPath = dataPath self.loadSize = loadSize self.fineSize = fineSize self.flip = flip
def __init__(self, dataPath='/media/data1/zs/BSDS500/images/test/data', transform=None): super(BSDS500_TEST, self).__init__() # list all images into a list self.image_list = [x for x in listdir(dataPath) if is_image_file(x)] self.dataPath = dataPath self.transform = transform
def __init__(self, dataset_dir, crop_size, upscale_factor=4, flips=False, rotations=False, **kwargs): super(DiscDataset, self).__init__() self.files = [join(dataset_dir, x) for x in listdir(dataset_dir) if utils.is_image_file(x)] self.input_transform = T.Compose([ T.RandomVerticalFlip(0.5 if flips else 0.0), T.RandomHorizontalFlip(0.5 if flips else 0.0), T.RandomCrop(crop_size // upscale_factor) ]) self.rotations = rotations
def __init__( self, dataPath='/media/data1/JY/neuron/ISBI/ISBI_train/train_with_def_20', loadSize=512): super(ISBI, self).__init__() # list all images into a list self.image_list = [ x for x in listdir(dataPath + '/img/') if is_image_file(x) ] self.dataPath = dataPath self.loadSize = loadSize
def __init__(self, image_dir, direction): super(DatasetFromFolder, self).__init__() self.direction = direction self.a_path = join(image_dir, "a") self.b_path = join(image_dir, "b") self.image_filenames = [x for x in listdir(self.a_path) if is_image_file(x)] transform_list = [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))] self.transform = transforms.Compose(transform_list)
def __init__(self, dataPath='/media/data1/zs/BSDS500/images/train', transform=None, target_transform=None): super(BSDS500, self).__init__() # list all images into a list self.image_list = [ x for x in listdir(dataPath + '/data_aug/') if is_image_file(x) ] self.dataPath = dataPath self.transform = transform self.target_transform = target_transform
def __init__(self, dataPath='facades/train', loadSize=286, fineSize=256, flip=1): super(Facades, self).__init__() # list all images into a list self.image_list = [x for x in listdir(dataPath) if is_image_file(x)] self.dataPath = dataPath self.loadSize = loadSize self.fineSize = fineSize self.flip = flip
def __init__(self, path): self.data_path = path self.only_images = [ f for f in os.listdir(self.data_path) if is_image_file( os.path.join( self.data_path, f ) ) ]
def __init__(self, image_dir): super(DatasetFromFolder_in_test_mode, self).__init__() self.img_path = image_dir self.image_filenames = [ x for x in listdir(self.img_path) if is_image_file(x) ] transform_list = [ transforms.ToTensor(), transforms.Normalize([0.5], [0.5]) ] self.transform = transforms.Compose(transform_list)
def __init__(self, image_dir): super(DatasetFromFolder_simplified, self).__init__() self.source_path = join(image_dir, "source") self.target_path = join(image_dir, "target") self.image_filenames = [ x for x in listdir(self.source_path) if is_image_file(x) ] transform_list = [ transforms.ToTensor(), transforms.Normalize([0.5], [0.5]) ] self.transform = transforms.Compose(transform_list)
def __init__(self, image_dir): super(DatasetFromFolder, self).__init__() self.images_path = join(image_dir, "images") self.masks_path = join(image_dir, "masks") self.image_filenames = [ x for x in listdir(self.images_path) if is_image_file(x) ] transform_list = [ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ] self.transform = transforms.Compose(transform_list)
def __init__(self, img_dir, input_transform, logger=None): self.img_dir = img_dir self.img_file_names = [ file_name for file_name in listdir(img_dir) if is_image_file(file_name) ] if logger is not None: logger.info( f'Finished creating an instance of {self.__class__.__name__} with {len(self.img_file_names)} examples' ) else: print( f'Finished creating an instance of {self.__class__.__name__} with {len(self.img_file_names)} examples' ) self.input_transform = input_transform
def __init__(self, path): self.path = path if isinstance(path, Path) else Path(path) self.P1 = None self.P2 = None self.P3 = None self.P4 = None self.dealer_idx = None self.players = [None] * 4 self.saved_roi = [] self.saved_fig = [] # paths are assumed to be given by os.listdir in a non deterministic way self._paths = order_files_by_name( [p for p in self.path.iterdir() if is_image_file(p)]) self.images = [] self._roi_table = [None] * len(self._paths) self._current_step = -1 self._processed_steps = [] self.max_step = len(self._paths) self.is_done = False
def stream_train_images(dir_path, true_rectangles_dict, window_size=(128, 128), window_step=32, visualize=False): # define the window width and height winW, winH = window_size for image_path in list_images(dir_path): if not is_image_file(image_path): continue # Read the image image = scipy.misc.imread(image_path) parent_dir_path, image_name = os.path.split(image_path) parent_dir_name = os.path.split(parent_dir_path)[-1] image_name_key = os.path.join(parent_dir_name, image_name) true_rectangles = true_rectangles_dict[image_name_key] if visualize: clone = image.copy() for rect in true_rectangles: cv2.rectangle(clone, (rect[0], rect[1]), (rect[2], rect[3]), RED, thickness=2) for (x, y, window) in sliding_window(image, step_size=window_step, window_size=(winW, winH)): # if the window does not meet our desired window size, ignore it if window.shape[0] != winH or window.shape[1] != winW: continue if visualize: copy = clone.copy() cv2.rectangle(copy, (x, y), (x + winW, y + winH), BLUE, thickness=2) cv2.imshow(image_path, copy) cv2.waitKey(1) if all(bb_intersection_over_union((x, y, x + winW, y + winH), rect) == 0 for rect in true_rectangles): if visualize: cv2.rectangle(clone, (x, y), (x + winW, y + winH), GREEN, thickness=2) cv2.imshow(image_path, clone) cv2.waitKey(1) yield image_name, window if visualize: cv2.destroyAllWindows()
def __init__(self, img_dir, mask_dir, input_transform, target_transform, logger=None): self.img_dir = img_dir self.mask_dir = mask_dir mask_file_names = [ file_name for file_name in listdir(mask_dir) if not file_name.startswith('.') ] name2filename = { splitext(file_name)[0]: file_name for file_name in mask_file_names } self.img_file_names = [ file_name for file_name in listdir(img_dir) if is_image_file(file_name) ] self.mask_file_names = [ name2filename[splitext(file_name)[0]] for file_name in self.img_file_names ] if logger is not None: logger.info( f'Finished creating an instance of {self.__class__.__name__} with {len(self.img_file_names)} examples' ) else: print( f'Finished creating an instance of {self.__class__.__name__} with {len(self.img_file_names)} examples' ) self.input_transform = input_transform self.target_transform = target_transform
def index(): """adopt from flask documentation, a request handler to process image uploads """ if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return 'no file', 400 file = request.files['file'] # file is an image file with png,jpg suffix if file and is_image_file(file.filename): # avoid malicious filename filename = secure_filename(file.filename) # get file path on system upload_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) # save file to disk file.save(upload_path) # get md5 of file md5 = get_md5(upload_path) # check if image exists res = query_db(md5) if res: return jsonify(res) # otherwise extract letters from image try: letters = letterify(upload_path) except Exception as e: app.logger.error(e) abort(400) # build query url query_url = url_for('result', key=md5) # save result to db data = save_to_db(md5, letters, query_url) return jsonify(data) else: return 'file type not supported', 400 return "Letterify is service to extract letters from images."
choices=[4], help='super resolution upscale factor') opt = parser.parse_args() # define input and target directories with open('./paths.yml', 'r') as stream: PATHS = yaml.load(stream) if opt.dataset == 'df2k': path_sdsr = PATHS['datasets']['df2k'] + '/generated/sdsr/' path_tdsr = PATHS['datasets']['df2k'] + '/generated/tdsr/' input_source_dir = PATHS['df2k']['tdsr']['source'] input_target_dir = PATHS['df2k']['tdsr']['target'] source_files = [ os.path.join(input_source_dir, x) for x in os.listdir(input_source_dir) if utils.is_image_file(x) ] target_files = [ os.path.join(input_target_dir, x) for x in os.listdir(input_target_dir) if utils.is_image_file(x) ] else: path_sdsr = PATHS['datasets'][ opt. dataset] + '/generated/' + opt.artifacts + '/' + opt.track + opt.name + '_sdsr/' path_tdsr = PATHS['datasets'][ opt. dataset] + '/generated/' + opt.artifacts + '/' + opt.track + opt.name + '_tdsr/' input_source_dir = PATHS[opt.dataset][opt.artifacts]['hr'][opt.track] input_target_dir = None source_files = [
model_path3 = "checkpoint/{}/netG3_model_epoch_{}.pth".format( opt.dataset, opt.nepochs) model_path4 = "checkpoint/{}/netG4_model_epoch_{}.pth".format( opt.dataset, opt.nepochs) net_g1 = torch.load(model_path).to(device) net_g2 = torch.load(model_path2).to(device) net_g3 = torch.load(model_path3).to(device) net_g4 = torch.load(model_path4).to(device) if opt.direction == "a2b": image_dir = "dataset/{}/test/a/".format(opt.dataset) else: image_dir = "dataset/{}/test/b/".format(opt.dataset) image_filenames = [x for x in os.listdir(image_dir) if is_image_file(x)] transform_list = [ transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, )) ] transform = transforms.Compose(transform_list) for image_name in image_filenames: img = load_img(image_dir + image_name) r, g, b = img.split() limg = transform(r) input = limg.unsqueeze(0).to(device) out = net_g1(input) out = net_g2(out)
from sklearn.manifold import TSNE import cv2, os import numpy as np from utils import is_image_file from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import axes3d, Axes3D D = 3 SIZE = 256 images = [] Y = [] for root, _, fnames in sorted(os.walk('r0000')): for fname in fnames: if is_image_file(fname): inputimg = cv2.imread(os.path.join(root, fname), -1) input_image = cv2.resize(np.float32(inputimg), (SIZE, SIZE), cv2.INTER_CUBIC) / 255.0 Y.append(0) images.append(input_image) for root, _, fnames in sorted(os.walk('r0150')): for fname in fnames: if is_image_file(fname): inputimg = cv2.imread(os.path.join(root, fname), -1) input_image = cv2.resize(np.float32(inputimg), (SIZE, SIZE), cv2.INTER_CUBIC) / 255.0 Y.append(1) images.append(input_image) for root, _, fnames in sorted(os.walk('root_training_real_data/blended')):
parser.add_argument('--num_res_blocks', default=8, type=int, help='number of ResNet blocks') parser.add_argument('--cleanup_factor', default=2, type=int, help='downscaling factor for image cleanup') parser.add_argument('--upscale_factor', default=2, type=int, choices=[4,2], help='super resolution upscale factor') opt = parser.parse_args() # define input and target directories with open('paths.yml', 'r') as stream: PATHS = yaml.load(stream) if opt.dataset == 'aim2019': path_sdsr = PATHS['datasets']['aim2019'] + '/generated/sdsr/' path_tdsr = PATHS['datasets']['aim2019'] + '/generated/tdsr/' input_source_dir = PATHS['aim2019']['tdsr']['source'] # input_target_dir = PATHS['aim2019']['tdsr']['target'] input_target_dir = "/media/4T/Dizzy/AIM/AIM_datasets/DIV2K_trainval_weights/HR" source_files = [os.path.join(input_source_dir, x) for x in os.listdir(input_source_dir) if utils.is_image_file(x)] target_files = [os.path.join(input_target_dir, x) for x in os.listdir(input_target_dir) if utils.is_image_file(x)] else: path_sdsr = PATHS['datasets'][opt.dataset] + '/generated/' + opt.artifacts + '/' + opt.track + opt.name + '_sdsr/' path_tdsr = PATHS['datasets'][opt.dataset] + '/generated/' + opt.artifacts + '/' + opt.track + opt.name + '_tdsr/' input_source_dir = PATHS[opt.dataset][opt.artifacts]['hr'][opt.track] input_target_dir = None source_files = [os.path.join(input_source_dir, x) for x in os.listdir(input_source_dir) if utils.is_image_file(x)] target_files = [] sdsr_hr_dir = path_sdsr + 'HR' sdsr_lr_dir = path_sdsr + 'LR' tdsr_hr_dir = path_tdsr + 'HR' tdsr_lr_dir = path_tdsr + 'LR' if not os.path.exists(sdsr_hr_dir):
def main(): parser = argparse.ArgumentParser(description="RAISR") parser.add_argument("--rate", type=int, default=3, help="upscale scale rate") parser.add_argument("--patch", type=int, default=11, help="image patch size") parser.add_argument("--Qangle", type=int, default=24, help="Training Qangle size") parser.add_argument("--Qstrength", type=int, default=3, help="Training Qstrength size") parser.add_argument("--Qcoherence", type=int, default=3, help="Training Qcoherence size") parser.add_argument('--datasets', default='./datasets/291/', type=str, help='path save the train dataset') opt = parser.parse_args() print(opt) rate = int(opt.rate) patch_size = int(opt.patch) images_path = [ os.path.join(opt.datasets, x) for x in os.listdir(opt.datasets) if is_image_file(x) ] print("Load dataset ", len(images_path)) # Implement of Algorithm 1: Computing the hash-table keys. Qangle = opt.Qangle Qstrength = opt.Qstrength Qcoherence = opt.Qcoherence Q = np.zeros((Qangle, Qstrength, Qcoherence, rate * rate, patch_size * patch_size, patch_size * patch_size)) V = np.zeros((Qangle, Qstrength, Qcoherence, rate * rate, patch_size * patch_size, 1)) H = np.zeros( (Qangle, Qstrength, Qcoherence, rate * rate, patch_size * patch_size)) for image_path in tqdm(images_path): print("HashMap of %s" % image_path) im = misc.imread(image_path, mode='YCbCr') im_y = mod_crop(im[:, :, 0], rate) h, w = im_y.shape label = im_y data = misc.imresize(label, (h // rate, w // rate), interp='bicubic') for xP in range(0, data.shape[0] - data.shape[0] % patch_size, patch_size): for yP in range(0, data.shape[1] - data.shape[1] % patch_size, patch_size): im_patch = data[xP:xP + patch_size, yP:yP + patch_size] [angle, strength, coherence] = hashTable(im_patch, Qangle, Qstrength, Qcoherence) t = xP % rate * rate + yP % rate # Pixel type X = label[xP, yP] # GT pixle A = im_patch.reshape(1, -1) Q[angle, strength, coherence, t] += A * A.T V[angle, strength, coherence, t] += A.T * X operationcount = 0 totaloperations = rate * rate * Qangle * Qstrength * Qcoherence for t in range(0, rate * rate): for angle in range(0, Qangle): for strength in range(0, Qstrength): for coherence in range(0, Qcoherence): if round(operationcount * 100 / totaloperations) != round( (operationcount + 1) * 100 / totaloperations): operationcount += 1 H[angle, strength, coherence, pixeltype] = cgls( Q[angle, strength, coherence, pixeltype], V[angle, strength, coherence, pixeltype]) np.save("./filters", H)