def __init__(self, interface, z_dim, image_size, channels, dataset, split, save_subdir, **kwargs): super(SampleCheckpoint, self).__init__(path=None, **kwargs) self.interface = interface self.image_size = image_size self.channels = channels self.save_subdir = save_subdir self.iteration = 0 self.epoch_src = "{0}/sample.png".format(save_subdir) self.rows=7 self.cols=10 self.spacing = 3 self.z_dim = z_dim numanchors = 10 + 10 #putting this import here allows serialized models to be loaded without chips from chips.fuel_helper import get_anchor_images self.anchor_images = get_anchor_images(dataset, split, numanchors=numanchors, image_size=image_size, include_targets=False) if not os.path.exists(self.save_subdir): os.makedirs(self.save_subdir)
def __init__(self, interface, z_dim, image_size, channels, dataset, split, save_subdir, **kwargs): super(SampleCheckpoint, self).__init__(path=None, **kwargs) self.interface = interface self.image_size = image_size self.channels = channels self.save_subdir = save_subdir self.iteration = 0 self.epoch_src = "{0}/sample.png".format(save_subdir) self.rows = 7 self.cols = 10 self.spacing = 3 self.z_dim = z_dim numanchors = 10 + 10 #putting this import here allows serialized models to be loaded without chips from chips.fuel_helper import get_anchor_images self.anchor_images = get_anchor_images(dataset, split, numanchors=numanchors, image_size=image_size, include_targets=False) if not os.path.exists(self.save_subdir): os.makedirs(self.save_subdir)
def run_with_args(args, dmodel, cur_anchor_image, cur_save_path, cur_z_step): if args.seed is not None: np.random.seed(args.seed) random.seed(args.seed) anchor_images = None if args.anchors: _, get_anchor_images = lazy_init_fuel_dependencies() allowed = None prohibited = None include_targets = False if(args.allowed): include_targets = True allowed = map(int, args.allowed.split(",")) if(args.prohibited): include_targets = True prohibited = map(int, args.prohibited.split(",")) anchor_images = get_anchor_images(args.dataset, args.split, args.offset, args.stepsize, args.numanchors, allowed, prohibited, args.image_size, args.color_convert, include_targets=include_targets) if cur_anchor_image is not None: _, _, anchor_images = anchors_from_image(cur_anchor_image, image_size=(args.image_size, args.image_size)) if args.offset > 0: anchor_images = anchor_images[args.offset:] # untested if args.numanchors is not None: anchor_images = anchor_images[:args.numanchors] if args.passthrough: print('Preparing image grid...') img = grid2img(anchor_images, args.rows, args.cols, not args.tight) img.save(cur_save_path) sys.exit(0) if dmodel is None: model_class_parts = args.model_class.split(".") model_class_name = model_class_parts[-1] model_module_name = ".".join(model_class_parts[:-1]) print("Loading {} interface from {}".format(model_class_name, model_module_name)) ModelClass = getattr(importlib.import_module(model_module_name), model_class_name) print("Loading model from {}".format(args.model)) dmodel = ModelClass(filename=args.model) if anchor_images is not None: x_queue = anchor_images[:] anchors = None # print("========> ENCODING {} at a time".format(args.batch_size)) while(len(x_queue) > 0): cur_x = x_queue[:args.batch_size] x_queue = x_queue[args.batch_size:] encoded = dmodel.encode_images(cur_x) if anchors is None: anchors = encoded else: anchors = np.concatenate((anchors, encoded), axis=0) # anchors = dmodel.encode_images(anchor_images) elif args.anchor_vectors is not None: anchors = get_json_vectors(args.anchor_vectors) else: anchors = None if args.invert_anchors: anchors = -1 * anchors if args.encoder: if anchors is not None: output_vectors(anchors) else: stream_output_vectors(dmodel, args.dataset, args.split, batch_size=args.batch_size) sys.exit(0) global_offset = None if args.anchor_offset is not None: # compute anchors as offsets from existing anchor offsets = get_json_vectors(args.anchor_offset) if args.anchor_noise: anchors = anchors_noise_offsets(anchors, offsets, args.rows, args.cols, args.spacing, cur_z_step, args.anchor_offset_x, args.anchor_offset_y, args.anchor_offset_x_minscale, args.anchor_offset_y_minscale, args.anchor_offset_x_maxscale, args.anchor_offset_y_maxscale) else: anchors = anchors_from_offsets(anchors[0], offsets, args.anchor_offset_x, args.anchor_offset_y, args.anchor_offset_x_minscale, args.anchor_offset_y_minscale, args.anchor_offset_x_maxscale, args.anchor_offset_y_maxscale) if args.global_offset is not None: offsets = get_json_vectors(args.global_offset) if args.global_ramp: offsets = cur_z_step * offsets global_offset = get_global_offset(offsets, args.global_indices, args.global_scale) z_dim = dmodel.get_zdim() # I don't remember what partway/encircle do so they are not handling the chain layout # this handles the case (at least) of mines with random anchors if (args.partway is not None) or args.encircle or (args.mine and anchors is None): srows=((args.rows // args.spacing) + 1) scols=((args.cols // args.spacing) + 1) rand_anchors = generate_latent_grid(z_dim, rows=srows, cols=scols, fan=False, gradient=False, spherical=False, gaussian=False, anchors=None, anchor_images=None, mine=False, chain=False, spacing=args.spacing, analogy=False, rand_uniform=args.uniform) if args.partway is not None: l = len(rand_anchors) clipped_anchors = anchors[:l] anchors = (1.0 - args.partway) * rand_anchors + args.partway * clipped_anchors elif args.encircle: anchors = surround_anchors(srows, scols, anchors, rand_anchors) else: anchors = rand_anchors z = generate_latent_grid(z_dim, args.rows, args.cols, args.fan, args.gradient, not args.linear, args.gaussian, anchors, anchor_images, args.mine, args.chain, args.spacing, args.analogy) if global_offset is not None: z = z + global_offset grid_from_latents(z, dmodel, args.rows, args.cols, anchor_images, args.tight, args.shoulders, cur_save_path, args.batch_size) return dmodel
def run_with_args(args, dmodel, cur_anchor_image, cur_save_path, cur_z_step): if args.seed is not None: np.random.seed(args.seed) random.seed(args.seed) anchor_images = None if args.anchors: _, get_anchor_images = lazy_init_fuel_dependencies() allowed = None prohibited = None include_targets = False if (args.allowed): include_targets = True allowed = map(int, args.allowed.split(",")) if (args.prohibited): include_targets = True prohibited = map(int, args.prohibited.split(",")) anchor_images = get_anchor_images(args.dataset, args.split, args.offset, args.stepsize, args.numanchors, allowed, prohibited, args.image_size, args.color_convert, include_targets=include_targets) if cur_anchor_image is not None: _, _, anchor_images = anchors_from_image(cur_anchor_image, image_size=(args.image_size, args.image_size)) if args.offset > 0: anchor_images = anchor_images[args.offset:] # untested if args.numanchors is not None: anchor_images = anchor_images[:args.numanchors] if args.passthrough: print('Preparing image grid...') img = grid2img(anchor_images, args.rows, args.cols, not args.tight) img.save(cur_save_path) sys.exit(0) if dmodel is None: model_class_parts = args.model_class.split(".") model_class_name = model_class_parts[-1] model_module_name = ".".join(model_class_parts[:-1]) print("Loading {} interface from {}".format(model_class_name, model_module_name)) ModelClass = getattr(importlib.import_module(model_module_name), model_class_name) print("Loading model from {}".format(args.model)) dmodel = ModelClass(filename=args.model) if anchor_images is not None: x_queue = anchor_images[:] anchors = None # print("========> ENCODING {} at a time".format(args.batch_size)) while (len(x_queue) > 0): cur_x = x_queue[:args.batch_size] x_queue = x_queue[args.batch_size:] encoded = dmodel.encode_images(cur_x) if anchors is None: anchors = encoded else: anchors = np.concatenate((anchors, encoded), axis=0) # anchors = dmodel.encode_images(anchor_images) elif args.anchor_vectors is not None: anchors = get_json_vectors(args.anchor_vectors) else: anchors = None if args.invert_anchors: anchors = -1 * anchors if args.encoder: if anchors is not None: output_vectors(anchors) else: stream_output_vectors(dmodel, args.dataset, args.split, batch_size=args.batch_size) sys.exit(0) global_offset = None if args.anchor_offset is not None: # compute anchors as offsets from existing anchor offsets = get_json_vectors(args.anchor_offset) if args.anchor_noise: anchors = anchors_noise_offsets( anchors, offsets, args.rows, args.cols, args.spacing, cur_z_step, args.anchor_offset_x, args.anchor_offset_y, args.anchor_offset_x_minscale, args.anchor_offset_y_minscale, args.anchor_offset_x_maxscale, args.anchor_offset_y_maxscale) else: anchors = anchors_from_offsets( anchors[0], offsets, args.anchor_offset_x, args.anchor_offset_y, args.anchor_offset_x_minscale, args.anchor_offset_y_minscale, args.anchor_offset_x_maxscale, args.anchor_offset_y_maxscale) if args.global_offset is not None: offsets = get_json_vectors(args.global_offset) if args.global_ramp: offsets = cur_z_step * offsets global_offset = get_global_offset(offsets, args.global_indices, args.global_scale) z_dim = dmodel.get_zdim() # I don't remember what partway/encircle do so they are not handling the chain layout # this handles the case (at least) of mines with random anchors if (args.partway is not None) or args.encircle or (args.mine and anchors is None): srows = ((args.rows // args.spacing) + 1) scols = ((args.cols // args.spacing) + 1) rand_anchors = generate_latent_grid(z_dim, rows=srows, cols=scols, fan=False, gradient=False, spherical=False, gaussian=False, anchors=None, anchor_images=None, mine=False, chain=False, spacing=args.spacing, analogy=False, rand_uniform=args.uniform) if args.partway is not None: l = len(rand_anchors) clipped_anchors = anchors[:l] anchors = (1.0 - args.partway ) * rand_anchors + args.partway * clipped_anchors elif args.encircle: anchors = surround_anchors(srows, scols, anchors, rand_anchors) else: anchors = rand_anchors z = generate_latent_grid(z_dim, args.rows, args.cols, args.fan, args.gradient, not args.linear, args.gaussian, anchors, anchor_images, args.mine, args.chain, args.spacing, args.analogy) if global_offset is not None: z = z + global_offset grid_from_latents(z, dmodel, args.rows, args.cols, anchor_images, args.tight, args.shoulders, cur_save_path, args.batch_size) return dmodel