def background_adjust_model(settings, bkg, seed=0): offset = settings['detector'].get('train_offset', 0) limit = settings['detector'].get('train_limit') files = sorted(glob.glob(settings['detector']['train_dir']))[ offset:limit] # * settings['detector'].get('duplicate', 1) try: detector = gv.Detector.load(settings['detector']['file']) except KeyError: raise Exception("Need to train the model first") # We need the descriptor to generate and manipulate images descriptor = gv.load_descriptor(settings) sh = (28, 88) # Create accumulates for each mixture component # TODO: Temporary until multicomp #counts = np.zeros_like(detector.kernel_templates) counts = np.zeros((1, sh[0], sh[1], descriptor.num_parts)) num_files = len(files) num_duplicates = settings['detector'].get('duplicate', 1) # Create several random states, so it's easier to measure # the influence of certain features prnds = [np.random.RandomState(seed + i) for i in xrange(10)] # Setup unspread bedges settings bsettings = settings['edges'].copy() radius = bsettings['radius'] bsettings['radius'] = 0 locations0 = xrange(sh[0]) locations1 = xrange(sh[1]) padded_theta = descriptor.unspread_parts_padded #pad = 10 pad = 5 X_pad_size = (9 + pad * 2, ) * 2 #X_pad_size = padded_theta.shape[1:3] neg_filenames = sorted( glob.glob( os.path.join(os.environ['UIUC_DIR'], 'TrainImages', 'neg-*.pgm'))) gen_raw = generate_random_patches(neg_filenames, X_pad_size, seed) # Pre-generate a bunch of background patches and loop them. bkgs = [gen_raw.next() for i in xrange(2000)] def new_gen(): i = 0 while True: yield bkgs[i] i += 1 if i == 2000: i = 0 gen = new_gen() for seed, fn in enumerate(files): ag.info("Processing file", fn) # Which mixture component does this image belong to? # TODO: Temporary until multicomp mixcomp = 0 #np.argmax(detector.affinities # Binarize support and Extract alpha color_img, alpha = gv.img.load_image_binarized_alpha(fn) img = gv.img.asgray(color_img) img_pad = ag.util.zeropad(img, pad) alpha_pad = ag.util.zeropad(alpha, pad) inv_alpha_pad_expanded = np.expand_dims(~alpha_pad, -1) # Iterate every duplicate #ag.info("Iteration {0}/{1}".format(loop+1, num_duplicates)) #ag.info("Iteration") for i, j in product(locations0, locations1): selection = [ slice(i, i + X_pad_size[0]), slice(j, j + X_pad_size[1]) ] #X_pad = edges_pad[selection].copy() patch = img_pad[selection] alpha_patch = alpha_pad[selection] #ag.info("Position {0} {1}".format(i, j)) for loop in xrange(num_duplicates): bkgmap = gen.next() # Composite img_with_bkg = composite(patch, bkgmap, alpha_patch) # Retrieve unspread edges (with a given background gray level) edges_pad = ag.features.bedges(img_with_bkg, **bsettings) # Pad the edges #edges_pad = ag.util.zeropad(edges, (pad, pad, 0)) # Do spreading X_pad_spread = ag.features.bspread(edges_pad, spread=bsettings['spread'], radius=radius) # De-pad padding = pad - 2 X_spread = X_pad_spread[padding:-padding, padding:-padding] # Code parts parts = descriptor.extract_parts(X_spread.astype(np.uint8)) # Accumulate and return counts[mixcomp, i, j] += parts[0, 0] """ if 0: from multiprocessing import Pool p = Pool(7) mapf = p.map else: mapf = map def _process_file(fn): return _process_file_full(fn, sh, descriptor, detector) # Iterate images all_counts = mapf(_process_file, files) for counti in all_counts: counts += counti """ # Divide accmulate to get new distribution counts /= num_files * num_duplicates # Create a new model, with this distribution new_detector = detector.copy() new_detector.kernel_templates = counts new_detector.support = None new_detector.use_alpha = False # Return model return new_detector
def get_bkg_stack(settings, X_pad_size, M=20): descriptor = gv.load_descriptor(settings) bsettings = settings['edges'].copy() radius = bsettings['radius'] bsettings['radius'] = 0 descriptor_name = settings['detector']['descriptor'] #neg_filenames= sorted(glob.glob(os.path.join(os.environ['UIUC_DIR'], 'TrainImages', 'neg-*.pgm'))) neg_filenames = sorted( glob.glob(os.path.expandvars( settings[descriptor_name]['image_dir']))) * 100 gen_raw = generate_random_patches(neg_filenames, X_pad_size, 0, per_image=25) print descriptor.num_parts bkg_stack_num = np.zeros(descriptor.num_parts + 1) bkg_stack = np.zeros(( descriptor.num_parts + 1, M, ) + X_pad_size) psize = settings['detector']['subsample_size'] radii = settings['detector']['spread_radii'] sett = dict(subsample_size=psize, spread_radii=radii) i = 0 import matplotlib.pylab as plt N = 100000 for patch in gen_raw: #edges = ag.features.bedges(patch, **bsettings) #plt.imshow(patch, interpolation='nearest', cmap=plt.cm.gray) #plt.show() #X_pad_spread = ag.features.bspread(edges, spread=bsettings['spread'], radius=radius) padding = pad - 2 #X_spread = X_pad_spread[padding:-padding,padding:-padding] # Code parts #parts = descriptor.extract_parts(X_spread.astype(np.uint8), edges, settings=sett) parts = descriptor.extract_features(patch, settings=sett) # Accumulate and return if parts[0, 0].sum() == 0: f = 0 else: f = np.argmax(parts[0, 0]) + 1 #cc[f] += 1 # The i%10 is to avoid all background images for f=0 to be from the same image (and thus # likely overlapping patches) if bkg_stack_num[f] < M and (f != 0 or i % 10 == 0): bkg_stack[f, bkg_stack_num[f]] = patch bkg_stack_num[f] += 1 if i % 10000 == 0: print i, bkg_stack_num if bkg_stack_num.min() == M: break i += 1 if i == N: break #print 'i', i #print 'min', sorted(cc)[:10] #cc /= N #print cc[:10] #print bkg[:10] assert i != 0, "No images found" #print cc.sum() #print bkg.sum() return bkg_stack, bkg_stack_num
def background_adjust_model(settings, bkg, seed=0): offset = settings["detector"].get("train_offset", 0) limit = settings["detector"].get("train_limit") files = sorted(glob.glob(settings["detector"]["train_dir"]))[ offset:limit ] # * settings['detector'].get('duplicate', 1) try: detector = gv.Detector.load(settings["detector"]["file"]) except KeyError: raise Exception("Need to train the model first") # We need the descriptor to generate and manipulate images descriptor = gv.load_descriptor(settings) sh = (28, 88) # Create accumulates for each mixture component # TODO: Temporary until multicomp # counts = np.zeros_like(detector.kernel_templates) counts = np.zeros((1, sh[0], sh[1], descriptor.num_parts)) num_files = len(files) num_duplicates = settings["detector"].get("duplicate", 1) # Create several random states, so it's easier to measure # the influence of certain features prnds = [np.random.RandomState(seed + i) for i in xrange(10)] # Setup unspread bedges settings bsettings = settings["edges"].copy() radius = bsettings["radius"] bsettings["radius"] = 0 locations0 = xrange(sh[0]) locations1 = xrange(sh[1]) padded_theta = descriptor.unspread_parts_padded # pad = 10 pad = 5 X_pad_size = (9 + pad * 2,) * 2 # X_pad_size = padded_theta.shape[1:3] neg_filenames = sorted(glob.glob(os.path.join(os.environ["UIUC_DIR"], "TrainImages", "neg-*.pgm"))) gen_raw = generate_random_patches(neg_filenames, X_pad_size, seed) # Pre-generate a bunch of background patches and loop them. bkgs = [gen_raw.next() for i in xrange(2000)] def new_gen(): i = 0 while True: yield bkgs[i] i += 1 if i == 2000: i = 0 gen = new_gen() for seed, fn in enumerate(files): ag.info("Processing file", fn) # Which mixture component does this image belong to? # TODO: Temporary until multicomp mixcomp = 0 # np.argmax(detector.affinities # Binarize support and Extract alpha color_img, alpha = gv.img.load_image_binarized_alpha(fn) img = gv.img.asgray(color_img) img_pad = ag.util.zeropad(img, pad) alpha_pad = ag.util.zeropad(alpha, pad) inv_alpha_pad_expanded = np.expand_dims(~alpha_pad, -1) # Iterate every duplicate # ag.info("Iteration {0}/{1}".format(loop+1, num_duplicates)) # ag.info("Iteration") for i, j in product(locations0, locations1): selection = [slice(i, i + X_pad_size[0]), slice(j, j + X_pad_size[1])] # X_pad = edges_pad[selection].copy() patch = img_pad[selection] alpha_patch = alpha_pad[selection] # ag.info("Position {0} {1}".format(i, j)) for loop in xrange(num_duplicates): bkgmap = gen.next() # Composite img_with_bkg = composite(patch, bkgmap, alpha_patch) # Retrieve unspread edges (with a given background gray level) edges_pad = ag.features.bedges(img_with_bkg, **bsettings) # Pad the edges # edges_pad = ag.util.zeropad(edges, (pad, pad, 0)) # Do spreading X_pad_spread = ag.features.bspread(edges_pad, spread=bsettings["spread"], radius=radius) # De-pad padding = pad - 2 X_spread = X_pad_spread[padding:-padding, padding:-padding] # Code parts parts = descriptor.extract_parts(X_spread.astype(np.uint8)) # Accumulate and return counts[mixcomp, i, j] += parts[0, 0] """ if 0: from multiprocessing import Pool p = Pool(7) mapf = p.map else: mapf = map def _process_file(fn): return _process_file_full(fn, sh, descriptor, detector) # Iterate images all_counts = mapf(_process_file, files) for counti in all_counts: counts += counti """ # Divide accmulate to get new distribution counts /= num_files * num_duplicates # Create a new model, with this distribution new_detector = detector.copy() new_detector.kernel_templates = counts new_detector.support = None new_detector.use_alpha = False # Return model return new_detector
def _create_kernel_for_mixcomp(mixcomp, settings, bb, indices, files, neg_files): im_size = settings['detector']['image_size'] size = gv.bb.size(bb) orig_size = size gen = generate_random_patches(neg_files, size, seed=0) descriptor = gv.load_descriptor(settings) radii = settings['detector']['spread_radii'] psize = settings['detector']['subsample_size'] rotspread = settings['detector'].get('rotation_spreading_radius', 0) duplicates = settings['detector'].get('duplicates', 1) cb = settings['detector'].get('crop_border') crop_image = settings['detector'].get('crop_image') totals = 0 bkg = None kern = None alpha_cum = None setts = dict(spread_radii=radii, subsample_size=psize, rotation_spreading_radius=rotspread, crop_border=cb) counts = 0 all_b = [] all_X = [] all_s = [] for index in indices: ag.info("Processing image of index {0} and mixture component {1}".format(index, mixcomp)) gray_im, alpha = _load_cad_image(files[index], im_size, bb, crop=crop_image) bin_alpha = (alpha > 0.05).astype(np.uint32) if alpha_cum is None: alpha_cum = bin_alpha else: alpha_cum += bin_alpha for dup in xrange(duplicates): neg_im = gen.next() neg_feats = descriptor.extract_features(neg_im, settings=setts) superimposed_im = neg_im * (1 - alpha) + gray_im * alpha feats = descriptor.extract_features(superimposed_im, settings=setts) counts += 1 #bkg_feats = gv.sub.subsample(bkg_feats, psize) if bkg is None: bkg = neg_feats.astype(np.uint32) else: bkg += neg_feats #feats = gv.sub.subsample(feats, psize) if kern is None: kern = feats.astype(np.uint32) else: kern += feats # NEW TODO: This throws out low-activity negatives #if abs(neg_feats.mean() - 0.2) < 0.05: #if neg_feats.mean() < 0.05: if True: all_b.append(neg_feats) all_X.append(feats) all_s.append(bin_alpha) totals += 1 ag.info('COUNTS', counts) np.seterr(divide='raise') kern = kern.astype(np.float64) / totals bkg = bkg.astype(np.float64) / totals #kern = kern.astype(np.float64) / total #kern = np.clip(kern, eps, 1-eps) #bkg = bkg.astype(np.float64) / total support = alpha_cum.astype(np.float64) / len(indices) return kern, bkg, orig_size, support
def __process_one(index, mixcomp, files, im_size, bb, duplicates, neg_files, descriptor, sett, extra): size = gv.bb.size(bb) psize = sett['subsample_size'] crop_image = sett.get('crop_image') ADAPTIVE = extra.get('selective') if ADAPTIVE: gen = generate_feature_patches_dense(neg_files, size, lambda im: descriptor.extract_features(im, settings=sett), psize, seed=index) cons = extra['concentrations'] obj = cons['pos_avg'] obj_std = cons['pos_std'] s = '(adaptive)' running_avg = None C = 0 from scipy.stats import norm #{{{ if 0: dd = gv.Detector.load('uiuc-np3b.npy') support = dd.extra['sturf'][0]['support'] pos = dd.extra['sturf'][0]['pos'].astype(bool) neg = dd.extra['sturf'][0]['neg'].astype(bool) S = support[...,np.newaxis] appeared = pos & ~neg A = appeared.mean(0) / (0.00001+((1-neg).mean(0))) obj = ((np.apply_over_axes(np.mean, (A*S), [0, 1])) / S.mean()).ravel() #obj = dd.extra['sturf'][0]['pavg'] obj_clipped = gv.bclip(obj, 0.001) obj_std = np.sqrt(obj_clipped * (1 - obj_clipped)) if 0: kern = dd.kernel_templates[mixcomp] obj = np.apply_over_axes(np.mean, kern, [0, 1]).squeeze() obj_clipped = np.clip(obj, 0.01, 1-0.01) obj_std = np.sqrt(obj_clipped * (1 - obj_clipped)) #obj_std = np.ones(obj.size) from scipy.stats import norm running_avg = None C = 0 #}}} else: gen = generate_random_patches(neg_files, size, seed=index) s = '' ag.info("Fetching positives from image of index {0} and mixture component {1} {2}".format(index, mixcomp, s)) gray_im, alpha = _load_cad_image(files[index], im_size, bb, crop=crop_image) all_pos_feats = [] all_neg_feats = [] for dup in xrange(duplicates): neg_feats = None if ADAPTIVE: best = (np.inf,) for i in xrange(2500): neg_im, neg_feats, x, y, im = gen.next() avg = np.apply_over_axes(np.mean, neg_feats, [0, 1]).squeeze() if C == 0: mean_avg = avg else: mean_avg = running_avg * (C - 1) / C + avg * 1 / C #dist = np.sum((avg - obj)**2) dist = -norm.logpdf((mean_avg - obj) / obj_std).sum() #if neg_feats.mean() > 0.02: #break if dist < best[0]: best = (dist, neg_im, neg_feats, mean_avg) running_avg = best[3] C += 1 ag.info('dist', best[0]) neg_im, neg_feats = best[1:3] else: neg_im = gen.next() neg_feats = descriptor.extract_features(neg_im, settings=sett) # Check which component this one is superimposed_im = neg_im * (1 - alpha) + gray_im * alpha pos_feats = descriptor.extract_features(superimposed_im, settings=sett) all_pos_feats.append(pos_feats) all_neg_feats.append(neg_feats) return all_pos_feats, all_neg_feats, alpha
def get_bkg_stack(settings, X_pad_size, M=20): descriptor = gv.load_descriptor(settings) bsettings = settings['edges'].copy() radius = bsettings['radius'] bsettings['radius'] = 0 descriptor_name = settings['detector']['descriptor'] #neg_filenames= sorted(glob.glob(os.path.join(os.environ['UIUC_DIR'], 'TrainImages', 'neg-*.pgm'))) neg_filenames = sorted(glob.glob(os.path.expandvars(settings[descriptor_name]['image_dir']))) * 100 gen_raw = generate_random_patches(neg_filenames, X_pad_size, 0, per_image=25) print descriptor.num_parts bkg_stack_num = np.zeros(descriptor.num_parts + 1) bkg_stack = np.zeros((descriptor.num_parts + 1, M,) + X_pad_size) psize = settings['detector']['subsample_size'] radii = settings['detector']['spread_radii'] sett = dict(subsample_size=psize, spread_radii=radii) i = 0 import matplotlib.pylab as plt N = 100000 for patch in gen_raw: #edges = ag.features.bedges(patch, **bsettings) #plt.imshow(patch, interpolation='nearest', cmap=plt.cm.gray) #plt.show() #X_pad_spread = ag.features.bspread(edges, spread=bsettings['spread'], radius=radius) padding = pad - 2 #X_spread = X_pad_spread[padding:-padding,padding:-padding] # Code parts #parts = descriptor.extract_parts(X_spread.astype(np.uint8), edges, settings=sett) parts = descriptor.extract_features(patch, settings=sett) # Accumulate and return if parts[0,0].sum() == 0: f = 0 else: f = np.argmax(parts[0,0]) + 1 #cc[f] += 1 # The i%10 is to avoid all background images for f=0 to be from the same image (and thus # likely overlapping patches) if bkg_stack_num[f] < M and (f != 0 or i%10 == 0): bkg_stack[f,bkg_stack_num[f]] = patch bkg_stack_num[f] += 1 if i % 10000 == 0: print i, bkg_stack_num if bkg_stack_num.min() == M: break i += 1 if i == N: break #print 'i', i #print 'min', sorted(cc)[:10] #cc /= N #print cc[:10] #print bkg[:10] assert i != 0, "No images found" #print cc.sum() #print bkg.sum() return bkg_stack, bkg_stack_num