!pip install tensorflow==2.0.0 !pip install keras==2.3.1 !pip install foolbox==2.4.0 !pip3 install h5py==2.10.0 import h5py import numpy as np from keras.applications.resnet50 import ResNet50 from foolbox.criteria import Misclassification, ConfidentMisclassification from keras.preprocessing import image as img from keras.applications.resnet50 import preprocess_input, decode_predictions import matplotlib.pyplot as plt import foolbox import pprint as pp import keras import pandas as pd %matplotlib inline #---------------------------- !wget https://github.com/GANs-in-Action/gans-in-action/raw/Chapter-10/chapter-10/DSC_0897.jpg !wget https://github.com/GANs-in-Action/gans-in-action/raw/Chapter-10/chapter-10/DSC_0896.jpg !wget https://raw.githubusercontent.com/GANs-in-Action/gans-in-action/master/chapter-10/initialization_vals_for_noise.csv #------------------------------------------------------ def load_image(img_path: str): image = img.load_img(img_path, target_size=(224, 224)) plt.imshow(image) x = img.img_to_array(image) return x image = load_image('DSC_0897.jpg') #------------------------------------------------------- keras.backend.set_learning_phase(0) kmodel = ResNet50(weights='imagenet') preprocessing = (np.array([104, 116, 123]), 1) #Kerasからfoolboxモデルのオブジェクトを生成する fmodel = foolbox.models.KerasModel(kmodel, bounds=(0, 255), preprocessing=preprocessing) #画像を(1,224,224,3)にしてResNet-50に合わせ、 #推定に使う画像がバッチに収まるようにする to_classify = np.expand_dims(image, axis=0) #predictを呼び出し、結果を表示する preds = kmodel.predict(to_classify) print('Predicted:', pp.pprint(decode_predictions(preds, top=20)[0])) #あとでラベルとして使うため、一番大きな数のインデックスを得る label = np.argmax(preds) #Keras ReNet-50はRGBではなくBGRのため、::-1で色チャンネルを逆転させる image = image[:, :, ::-1] #高い分類間違い基準を持った、攻撃のためのオブジェクトを作る attack = foolbox.v1.attacks.FGSM(fmodel, threshold=.9, criterion=ConfidentMisclassification(.9)) #ソース画像に攻撃を加える adversarial = attack(image, label) #敵対的サンプルのラベルを新たに推定する new_preds = kmodel.predict(np.expand_dims(adversarial, axis=0)) print('Predicted:', pp.pprint(decode_predictions(new_preds, top=20)[0])) # ------------------------------------------------------------ plt.figure(figsize=(12,12)) plt.subplot(1, 3, 1) plt.title('Original') plt.imshow(image[:,:,::-1] / 255) # division by 255 to convert [0, 255] to [0, 1] plt.axis('off') plt.subplot(1, 3, 2) plt.title('Adversarial') plt.imshow(adversarial[:, :, ::-1] / 255) # ::-1 to convert BGR to RGB plt.axis('off') plt.subplot(1, 3, 3) plt.title('Difference') difference = adversarial[:, :, ::-1] - image plt.imshow(difference / abs(difference).max() * 0.2 + 0.5) plt.axis('off') plt.show() # -------------------------------------------- max_vals = pd.read_csv('initialization_vals_for_noise.csv') #----------------------------------------- fig = plt.figure(figsize=(20,20)) # 平均と分散をfloatのリストにする sigma_list = list(max_vals.sigma) mu_list = list(max_vals.mu) conf_list = [] # 図10.8を表示するためのコア関数 def make_subplot(x, y, z, new_row=False): # それぞれの平均と分散を持ったのノイズをサンプリング rand_noise = np.random.normal(loc=mu, scale=sigma, size=(224,224, 3)) # 0-255の画素値のみを許す rand_noise = np.clip(rand_noise, 0, 255.) # 最初の推定を得る noise_preds = kmodel.predict(np.expand_dims(rand_noise, axis=0)) # 推定したクラスと確信度を得る prediction, num = decode_predictions(noise_preds, top=20)[0][0][1:3] num = round(num * 100, 2) conf_list.append(num) # 図10.8のための注釈用コードをセットアップしたのちに、注釈文を足す ax = fig.add_subplot(x,y,z) ax.annotate(prediction, xy=(0.1, 0.6), xycoords=ax.transAxes, fontsize=16, color='yellow') ax.annotate(f'{num}%' , xy=(0.1, 0.4), xycoords=ax.transAxes, fontsize=20, color='orange') if new_row: ax.annotate(f'$\mu$:{mu}, $\sigma$:{sigma}' , xy=(-.2, 0.8), xycoords=ax.transAxes, rotation=90, fontsize=16, color='black') # [0, 255]を[0, 1]に変換するため、255で割る ax.imshow(rand_noise / 255) ax.axis('off') # 画像内に文字を挿入するためのループ本体 for i in range(1,101): if (i-1) % 10==0: mu = mu_list.pop(0) sigma = sigma_list.pop(0) make_subplot(10,10, i, new_row=True) else: make_subplot(10,10, i) plt.show() ##---------------------------------------------------- fig = plt.figure(figsize=(20,20)) sigma_list = list(max_vals.sigma) mu_list = list(max_vals.mu) sum_pred = [] def make_subplot(x, y, z, new_row=False): rand_noise = np.random.normal(loc=mu, scale=sigma, size=(224,224, 3)) rand_noise = np.clip(rand_noise, 0, 255.) first_pred = kmodel.predict(np.expand_dims(rand_noise, axis=0)) label = np.argmax(first_pred) attack = foolbox.attacks.ProjectedGradientDescentAttack(fmodel, threshold=.999, distance=foolbox.distances.Linfinity, criterion=ConfidentMisclassification(.999)) adversarial = attack(rand_noise[:, :, ::-1], label) ##ここです## noise_preds = kmodel.predict(np.expand_dims(adversarial, axis=0)) prediction, num = decode_predictions(noise_preds, top=20)[0][0][1:3] num = round(num * 100, 2) sum_pred.append(num) ax = fig.add_subplot(x,y,z) ax.annotate(prediction, xy=(0.1, 0.6), xycoords=ax.transAxes, fontsize=16, color='yellow') ax.annotate(f'{num}%', xy=(0.1, 0.4), xycoords=ax.transAxes, fontsize=20, color='orange') if new_row: ax.annotate(f'$\mu$:{mu}, $\sigma$:{sigma}' , xy=(-.2, 0.8), xycoords=ax.transAxes, rotation=90, fontsize=16, color='black') ax.imshow(adversarial / 255) # division by 255 to convert [0, 255] to [0, 1] ax.axis('off') for i in range(1,101): if (i-1) % 10==0: mu = mu_list.pop(0) sigma = sigma_list.pop(0) make_subplot(10,10, i, new_row=True) else: make_subplot(10,10, i) plt.show() #--------------------------------------------------
を実行しようとすると、最後の段落の##ここです##のところで
エラーメッセージ AssertionError Traceback (most recent call last) <ipython-input-3-9446ca790563> in <module>() 164 mu = mu_list.pop(0) 165 sigma = sigma_list.pop(0) --> 166 make_subplot(10,10, i, new_row=True) 167 else: 168 make_subplot(10,10, i) 1 frames /usr/local/lib/python3.7/dist-packages/foolbox/attacks/base.py in __call__(self, inputs, labels, unpack, individual_kwargs, **kwargs) 23 def __call__(self, inputs, labels, unpack=True, individual_kwargs=None, **kwargs): 24 assert isinstance(inputs, np.ndarray) ---> 25 assert isinstance(labels, np.ndarray) 26 27 if len(inputs) != len(labels): AssertionError:
というエラーが表示されてしまします。いろいろ調べてもわかりません。教えてほしいです。お願いします。
引用は、主にhttps://colab.research.google.com/drive/1bsmaB23bSEuu1zWyPSajDJ_DO3QqlovQ
こちらです。
回答1件
あなたの回答
tips
プレビュー