def shapelet_transform(run_tag): # Shapelet transformation st = ShapeletTransform(n_shapelets=5, window_sizes=[0.1, 0.2, 0.3, 0.4 ], sort=True, verbose=1, n_jobs=1) path = 'data/' + run_tag + '/' + run_tag + '_eval.txt' data = load_ucr(path) X = data[:, 1:] y = data[:, 0] X_new = st.fit_transform(X, y) file = open('shapelet_pos/' + run_tag + '_shapelet_pos.txt', 'w+') for i, index in enumerate(st.indices_): idx, start, end = index file.write(run_tag + ' ') file.write(str(idx) + ' ') file.write(str(start) + ' ') file.write(str(end) + '\n') file.close() # Visualize the most discriminative shapelets plt.figure(figsize=(6, 4)) for i, index in enumerate(st.indices_): idx, start, end = index plt.plot(X[idx], color='C{}'.format(i), label='Sample {}'.format(idx)) plt.plot(np.arange(start, end), X[idx, start:end], lw=5, color='C{}'.format(i)) plt.xlabel('Time', fontsize=12) plt.title('The five more discriminative shapelets', fontsize=14) plt.legend(loc='best', fontsize=8) plt.savefig('shapelet_fig/' + run_tag + '_shapelet.pdf')
def __init__(self, txt_file, channel_last, normalize): ''' :param txt_file: path of file :param channel_last ''' # self.data = np.loadtxt(txt_file) self.data = load_ucr(txt_file, normalize) self.channel_last = channel_last if self.channel_last: self.data = np.reshape(self.data, [self.data.shape[0], self.data.shape[1], 1]) else: self.data = np.reshape(self.data, [self.data.shape[0], 1, self.data.shape[1]])
def get_magnitude(run_tag, factor, normalize): ''' :param run_tag: :param factor: :return: Perturbed Magnitude ''' data = load_ucr('data/' + run_tag + '/' + run_tag + '_unseen.txt', normalize=normalize) X = data[:, 1:] max_magnitude = X.max(1) min_magnitude = X.min(1) mean_magnitude = np.mean(max_magnitude - min_magnitude) perturbed_mag = mean_magnitude * factor print('Perturbed Magnitude:', perturbed_mag) return perturbed_mag
def plot_pdf(run_tag): path = 'data/' + run_tag + '/' + run_tag + '_eval.txt' data = load_ucr(path) X = data[:, 1:] y = data[:, 0] pos_path = 'shapelet_pos/' + run_tag + '_shapelet_pos.txt' shapelet_pos = np.loadtxt(pos_path, usecols=(1, 2, 3)) plt.figure(figsize=(6, 4)) for i in range(3): idx, start, end = int(shapelet_pos[i, 0]), int(shapelet_pos[i, 1]), int(shapelet_pos[i, 2]) plt.plot(X[idx], color='C{}'.format(i), label='Sample {}'.format(idx)) plt.plot(np.arange(start, end), X[idx, start:end], lw=5, color='C{}'.format(i)) plt.xlabel('%s'%run_tag, fontsize=12) plt.title('The top 3 shapelets', fontsize=14) plt.legend(loc='best', fontsize=8) plt.savefig('shapelet_fig/' + run_tag + '.pdf',pad_inches=0.0, bbox_inches='tight') plt.show()
def attack(self, sample_idx, target_class=-1, factor=0.04, max_iteration=60, popsize=200, verbose=True): test = load_ucr('data/' + self.run_tag + '/' + self.run_tag + '_unseen.txt', normalize=self.normalize) ori_ts = test[sample_idx][1:] attacked_probs, attacked_vec, prior_probs, prior_vec, real_label = query_one( self.run_tag, idx=sample_idx, attack_ts=ori_ts, target_class=target_class, normalize=self.normalize, verbose=False, cuda=self.cuda, e=self.e, model_type=self.model_type) prior_class = torch.argmax(prior_vec) if prior_class != real_label: print( 'The %d sample cannot be classified correctly, no need to attack' % sample_idx) return ori_ts, [ prior_probs, attacked_probs, 0, 0, 0, 0, 0, 'WrongSample' ] steps_count = 0 # count the number of coordinates # Get the maximum perturbed magnitude perturbed_magnitude = get_magnitude(self.run_tag, factor, normalize=self.normalize) bounds = [] for interval in self.intervals: steps_count += int(interval[1]) - int(interval[0]) for i in range(int(interval[0]), int(interval[1])): bounds.append((-1 * perturbed_magnitude, perturbed_magnitude)) print('The length of shapelet interval', steps_count) popmul = max(1, popsize // len(bounds)) # Record of the number of iterations iterations = [0] queries = [0] def fitness_fn(perturbations): return self.fitness(perturbations=perturbations, ts=ori_ts, queries=queries, sample_idx=sample_idx, target_class=target_class) def callback_fn(x, convergence): return self.attack_success(perturbations=x, ts=ori_ts, sample_idx=sample_idx, iterations=iterations, target_class=target_class, verbose=verbose) attack_result = differential_evolution(func=fitness_fn, bounds=bounds, maxiter=max_iteration, popsize=popmul, recombination=0.7, callback=callback_fn, atol=-1, polish=False) attack_ts = self.perturb_ts(attack_result.x, ori_ts) mse = mean_squared_error(ori_ts, attack_ts) attacked_probs, attacked_vec, prior_probs, prior_vec, real_label = query_one( self.run_tag, idx=sample_idx, attack_ts=attack_ts, target_class=target_class, normalize=self.normalize, verbose=False, cuda=self.cuda, e=self.e, model_type=self.model_type) predicted_class = torch.argmax(attacked_vec) prior_class = torch.argmax(prior_vec) if prior_class != real_label: success = 'WrongSample' elif prior_class == target_class: success = 'NoNeedAttack' else: if (predicted_class.item() != prior_class.item() and target_class == -1) \ or (predicted_class.item() == target_class and target_class != -1): success = 'Success' else: success = 'Fail' if success == 'Success': self.plot_per(perturbations=attack_result.x, ts=ori_ts, target_class=target_class, sample_idx=sample_idx, prior_probs=prior_probs, attack_probs=attacked_probs, factor=factor) return attack_ts, [ prior_probs, attacked_probs, prior_class.item(), predicted_class.item(), queries[0], mse, iterations[0], success ]