def __init__(self, models, data, class_names, dimensions=(32, 32)): # Load data and model self.models = models self.x_test, self.y_test = data self.class_names = class_names self.dimensions = dimensions network_stats, correct_imgs = helper.evaluate_models(self.models, self.x_test, self.y_test) self.correct_imgs = pd.DataFrame(correct_imgs, columns=['name', 'img', 'label', 'confidence', 'pred']) self.network_stats = pd.DataFrame(network_stats, columns=['name', 'accuracy', 'param_count'])
def __init__(self, models, data, class_names, dimensions=(28, 28)): # Load data and model self.models = models self.x_test, self.y_test = data self.class_names = class_names self.dimensions = dimensions network_stats, correct_imgs = helper.evaluate_models(self.models, self.x_test, self.y_test) self.correct_imgs = pd.DataFrame(correct_imgs, columns=['name', 'img', 'label', 'confidence', 'pred']) self.network_stats = pd.DataFrame(network_stats, columns=['name', 'accuracy', 'param_count'])
# processed_images = mobilenet_preprocess_input(images.copy()) processed_images = images.copy() print("Processed Images shape {}, min {}, max {}".format( processed_images.shape, np.min(processed_images), np.max(processed_images))) # # ----------------------------------------------------------------------------------- # # Model Accuracy # # ----------------------------------------------------------------------------------- print("Evaluating Model Accuracy - Regular Images") models = [model] network_stats, correct_imgs = helper.evaluate_models( models, processed_images, labels, preprocessing_cb=mobilenet_preprocess_input) correct_imgs = pd.DataFrame( correct_imgs, columns=['name', 'img', 'label', 'confidence', 'pred']) network_stats = pd.DataFrame(network_stats, columns=['name', 'accuracy', 'param_count']) print(network_stats) print("Number of correctly Classifiered images {}".format( len(correct_imgs))) # ----------------------------------------------------------------------------------- # Adverserial Attacks # -----------------------------------------------------------------------------------
batchsize=10) images = np.array([img_to_array(original) for original in images ]) # Convert the PIL image to a numpy array #print(np.shape(y_test)) print(np.shape(labels.reshape(10, 1))) labels = labels.reshape(10, 1) #自定义网络 # lenet = LeNet() resnet = ResNet() # model_filename = 'networks/models/resnet.h5' # resnet=load_model(model_filename) models = [resnet] network_stats, correct_imgs = helper.evaluate_models(models, images, labels) correct_imgs = pd.DataFrame( correct_imgs, columns=['name', 'img', 'label', 'confidence', 'pred']) network_stats = pd.DataFrame(network_stats, columns=['name', 'accuracy', 'param_count']) print(network_stats) pixel = np.array([16, 20, 0, 255, 255]) model = resnet image_id = 1 true_class = labels[image_id, 0] prior_confidence = model.predict_one(images[image_id])[true_class] confidence = helper.predict_classes(pixel, images[image_id], true_class, model)[0]
print('Attacking with target', class_names[target]) if (target == y_test[img, 0]): continue result = attack(img, model, target, pixel_count, maxiter=args.maxiter, popsize=args.popsize, verbose=verbose) model_results.append(result) results += model_results return results network_stats, correct_imgs = evaluate_models(models, x_test, y_test) correct_imgs = pd.DataFrame( correct_imgs, columns=['name', 'img', 'label', 'confidence', 'pred']) network_stats = pd.DataFrame(network_stats, columns=['name', 'accuracy', 'param_count']) print('Starting attack') results = attack_all(models, samples=args.samples, targeted=args.targeted, pixels=args.pixels, verbose=args.verbose) print('Saving to', args.savedir) with open(args.savedir, 'wb') as file: