def sample_fn_csgm_kde(present, num_samples, compute_labels=True): ''' present = which segments to choose from... ''' data = np.zeros((num_samples, n_features)) data[:, present] = 1 print(num_samples) # now generate some images _, mask = image_utils.create_mask(None, segments, {'feature': present}, rgb=True) target = self.get_target(image, mask) if self.present != present: self.sampler = csgm.CSGM(target, mask, self.G, num_samples, bandwidth=0.5) self.present = present BS = 64 #raw_data = np.zeros((num_samples,28,28)) raw_data = data _, _, backgrounds = self.sampler.sample(num_samples) current_batch = backgrounds + target current_preds = classifier_fn(current_batch) current_preds_max = np.argmax(current_preds, axis=1) labels = (current_preds_max == true_label).astype(int) return raw_data, data, labels
def sample_fn_csgm(present, num_samples, compute_labels=True): ''' present = which segments to choose from... ''' if not compute_labels: data = np.random.randint(0, 2, num_samples * n_features).reshape( (num_samples, n_features)) data[:, present] = 1 return [], data, [] data = np.zeros((num_samples, n_features)) data[:, present] = 1 # now generate some images _, mask = image_utils.create_mask(None, segments, {'feature': present}) target = self.get_target(image, mask) #plt.imshow(target) BS = 64 #raw_data = np.zeros((num_samples,28,28)) raw_data = data labels = np.zeros((num_samples)).astype(int) for j in range(0, num_samples, BS): n_s = min(num_samples, j + BS) - j if self.true_sampling: _, _, backgrounds = csgm.reconstruct_batch_threshold( target, mask, np.sum(mask), self.G, n_s, self.sample_p(n_s), lr=1e-1 if self.batch_norm else 1e-2) else: _, _, backgrounds = csgm.reconstruct_batch( target, mask, np.sum(mask), self.G, n_s, lr=1e-1 if self.batch_norm else 1e-2, threshold=self.threshold) current_batch = np.zeros((n_s, 28, 28)) for i in range(len(backgrounds)): temp = copy.deepcopy(target) curr = backgrounds[i] ## should be (28,28) temp += curr current_batch[i] = np.expand_dims(temp, 0) current_preds = classifier_fn(current_batch) current_preds_max = np.argmax(current_preds, axis=1) current_labels = (current_preds_max == true_label).astype(int) labels[j:j + n_s] = current_labels return raw_data, data, labels
def sample_fn_stein(present, num_samples, compute_labels=True): ''' present = which segments to choose from... ''' data = np.zeros((num_samples, n_features)) data[:, present] = 1 print(num_samples) # now generate some images _, mask = image_utils.create_mask(None, segments, {'feature': present}, rgb=True) target = self.get_target(image, mask) # first time we called it if self.stein is None: # setup for stein's G = dcgan.ProbGenerator(self.G, mask, target) K = svgd.RBF() X = torch.randn(num_samples, 100).cuda() adam = torch.optim.Adam([X], lr=1e-1) self.stein = svgd.SVGD(G, K, adam) # train... print("Starting Stein Training") X = self.stein.train(X, num_iter=1000) print("Trained!") BS = 64 collected = 0 labels = np.zeros((num_samples)).astype(int) raw_data = data print("Predicting...") while collected < num_samples: X = self.stein.sample(BS) if X.shape[0] == 0: # print("Bad samples") continue #print("KDE found", X.shape[0], "good samples.") backgrounds = X.view(-1, 28, 28).data.cpu().numpy() * (1 - target) end = min(backgrounds.shape[0], num_samples - collected) backgrounds = backgrounds[:end] current_batch = backgrounds + target current_preds = classifier_fn(current_batch) current_preds_max = np.argmax(current_preds, axis=1) current_labels = (current_preds_max == true_label).astype(int) labels[collected:collected + end] = current_labels collected += end print("Collected", num_samples, "!") return raw_data, data, labels