def __init__(self, fidelity: Fidelity, space: Union[Space, Dict], seed=0, **kwargs): self.identity = 'uid' for k, v in kwargs.items(): debug(f'used parameter ({k}: {v})') if isinstance(space, dict): space = Space.from_dict(space) if space is not None: space.identity(self.identity) if isinstance(fidelity, dict): fidelity = Fidelity.from_dict(fidelity) self.fidelity = fidelity self.space = space self.seed = seed self.seed_time = 0 self.manual_insert = 0 self.manual_samples = [] self.manual_fidelity = [] self.trials = OrderedDict()
def observe(self, hpo): debug('observe') new_results = 0 m = self.pop_result() while m is not None: actioned = True if m.mtype == RESULT_ITEM: info(f'HPO {self.experiment} observed {m.message[0]["uid"]}') try: hpo.observe(m.message[0], m.message[1]) new_results += 1 except TrialDoesNotExist as e: warning(f'Could not observe trial: {e}') actioned = False elif m.mtype == WORKER_JOIN: self.worker_count += 1 elif m.mtype == WORKER_LEFT: self.worker_count -= 1 else: debug(f'Received: {m}') if actioned: self.future_client.mark_actioned(RESULT_QUEUE, m) m = self.pop_result() return new_results
def set_attribute(data): debug(f'set {name} to {data}') if data == 0: data = None setattr(self, name, data) self.make_graph()
def generate(self, image, min_confidence, max_iter=10, round_trip=False): from torch.autograd import Variable self.model.eval() target = torch.as_tensor([self.target_class]) image = self.preprocessor(image) # Save the original to compute the final noise original_image = image for i in range(max_iter): image = Variable(image, requires_grad=True) image.grad = None # Compute the current confidence output = self.model(image) self.model.zero_grad() with torch.no_grad(): probabilities = F.softmax(output, dim=1) prediction = torch.argmax(probabilities) confidence = probabilities[0, prediction] target_confidence = probabilities[0, self.target_class] self.stats.update(prediction, confidence, target_confidence, probabilities) debug( f'{i:4d} Predicted {prediction} with {confidence:.4f},' f'our target: {self.target_class} has {target_confidence.item():.4f}' ) if prediction == self.target_class and confidence > min_confidence: break # Tampering some more loss = F.cross_entropy(output, target) loss.backward() adversarial_noise = self.alpha * torch.sign(image.grad.detach()) image = image + adversarial_noise if round_trip: image = self.preprocessor(self.postprocessor(image)) noises = self.get_noise(image, original_image) return self.postprocessor(image), noises
def suggest(self, hpo): debug('suggest') trials = self._maybe_suggest(hpo, **self.work['kwargs']) if trials is None: return 0 for trial in trials: new_work = copy.deepcopy(self.work) new_work['kwargs'] = trial info(f'HPO {self.experiment} suggested {trial["uid"]}') self.future_client.push(WORK_QUEUE, self.experiment, new_work, mtype=WORK_ITEM) return len(trials)
def make_graph(self): debug('new graph') get_element_size('graph_container', self.set_size) if self.xlabel is None or self.ylabel is None or self.mark is None: return mark = self.mark_options[self.mark][0] xlabel = self.columns[self.xlabel] ylabel = self.columns[self.ylabel] kwargs = { 'x': alt.X(xlabel, type=self.guess_datatype(xlabel, mark)), 'y': alt.Y(ylabel, type=self.guess_datatype(ylabel, mark)) } if self.y2label is not None: y2label = self.columns[self.y2label] kwargs['y2'] = alt.Y2(y2label) if self.shape is not None: shape = self.columns[self.shape] kwargs['shape'] = alt.Shape(shape, type=self.guess_datatype(shape, mark)) if self.colors is not None: colors = self.columns[self.colors] kwargs['color'] = alt.Color(colors, type=self.guess_datatype(colors, mark)) mark_method = self.mark_options[self.mark][1] chart = mark_method(alt.Chart(self.data)).encode( **kwargs ).interactive().properties( width=self.width, height=self.height ) set_attribute(self.graph_id, 'srcdoc', html.altair_plot(chart, with_iframe=False))
def fetcher(): flag = stop_thread() last_time = mtime monitor = client if isinstance(client, dict): monitor = new_monitor(**client) while flag.running: # Only fetch new messages messages = monitor.messages(queue, namespace, time=last_time) if len(messages) > 0: debug('messages') last_time = messages[-1].time yield list(preprocessor(messages)) else: debug('nothing') # just let the browser breath time.sleep(1) debug('stopping')
def worker(remote, parent_remote, env_factory, stat: WorkerStat, unique_set): parent_remote.close() cmd, data = remote.recv() assert cmd == 'init', 'first message should be about environment init' env = env_factory(*data) def check_for_duplicates(state): """Check if the state is a duplicate""" key = hash(state.tostring()) if key not in unique_set: unique_set[key] = 1 else: stat.duplicates += 1 while True: cmd, data = remote.recv() if cmd == 'step': stat.step += 1 s = time.time() ob, reward, done, info = env.step(data) stat.step_time += time.time() - s if done: stat.done += 1 stat.reset += 1 s = time.time() ob = env.reset() stat.reset_time += time.time() - s check_for_duplicates(ob) remote.send((ob, reward, int(done), info)) elif cmd == 'reset': stat.reset += 1 s = time.time() ob = env.reset() stat.reset_time += time.time() - s check_for_duplicates(ob) remote.send(ob) elif cmd == 'reset_task': stat.reset += 1 s = time.time() ob = env.reset_task() stat.reset_time += time.time() - s check_for_duplicates(ob) remote.send(ob) elif cmd == 'close': stat.close += 1 debug('closing env') break elif cmd == 'get_spaces': stat.get_spaces += 1 remote.send((env.observation_space, env.action_space)) else: raise NotImplementedError debug('cleaning up') env.close() remote.close()
def shutdown(self): debug('sending shutdown signals') for _ in range(self.worker_count): self.future_client.push(WORK_QUEUE, self.experiment, {}, mtype=SHUTDOWN)
def generate(self, image, min_confidence, lr=0.7, max_iter=500, round_trip=False): """Generate an adversarial example that should be misclassified as target_class Parameters ---------- image: Union[Tensor, List[Image]] list of images to tamper with min_confidence: float Confidence we need to reach before stopping lr: float learning rate for the optimizer max_iter: int Maximal number of iteration round_trip: bool when enabled the tensor is periodically converted to image and back to tensor """ self.model.eval() target_confidence = 0 target = self.target_class original_image, batch = self.to_batch(image) for i in range(max_iter): if target_confidence > min_confidence: break batch.requires_grad = True optimizer = Optimizer('sgd', params=[batch], lr=lr, momentum=0, weight_decay=0) probabilities = F.softmax(self.model(batch), dim=1) class_predicted = torch.argmax(probabilities) prediction_confidence = probabilities[0, class_predicted] target_confidence = probabilities[0, target] self.stats.update(class_predicted, prediction_confidence, target_confidence, probabilities) debug( f'{i:4d} Predicted {class_predicted} with {prediction_confidence:.4f},' f'our target: {target} has {target_confidence.item():.4f}') self.model.zero_grad() optimizer.backward(-1 * target_confidence) optimizer.step() if round_trip: batch = self.preprocessor(self.postprocessor(batch)) noises = self.get_noise(batch, original_image) return batch, noises