質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

1681閲覧

Google Colaboratoryでのエラー表示

退会済みユーザー

退会済みユーザー

総合スコア0

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/11/22 13:54

編集2021/11/22 14:21
!pip install foolbox==2.4.0 import numpy as np from tensorflow.keras.applications.resnet50 import ResNet50 from foolbox.criteria import Misclassification, ConfidentMisclassification from tensorflow.keras.preprocessing import image as img from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions import matplotlib.pyplot as plt import foolbox import pprint as pp import tensorflow.keras %matplotlib inline --------------------------------------------------- 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') -------------------------------------------------------- tensorflow.keras.backend.set_learning_phase(0) kmodel = ResNet50(weights='imagenet') preprocessing = (np.array([104, 116, 123]), 1) fmodel = foolbox.models.KerasModel(kmodel, bounds=(0, 255), preprocessing=preprocessing) to_classify = np.expand_dims(image, axis=0) preds = kmodel.predict(to_classify) print('Predicted:', pp.pprint(decode_predictions(preds, top=20)[0])) label = np.argmax(preds) image = image[:, :, ::-1] attack = foolbox.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]))

を実行しようと思っているのですが、

TypeError Traceback (most recent call last) <ipython-input-16-df2f00679ec1> in <module>() 4 5 fmodel = foolbox.models.KerasModel(kmodel, bounds=(0, 255), ----> 6 preprocessing=preprocessing) 7 8 to_classify = np.expand_dims(image, axis=0) 1 frames /usr/local/lib/python3.7/dist-packages/keras/engine/keras_tensor.py in op(self) 231 @property 232 def op(self): --> 233 raise TypeError('Keras symbolic inputs/outputs do not ' 234 'implement `op`. You may be ' 235 'trying to pass Keras symbolic inputs/outputs ' TypeError: Keras symbolic inputs/outputs do not implement `op`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

というエラーが表示されてしまいます。
どのように改善すればよいでしょうか?
教えていただきたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/11/23 00:34 編集

根拠はありませんが、TFのデータをNumpyで捌こうとしてエラーが出ているような気がします。 というのも、 from tensorflow.keras.preprocessing import image as img とあって x = img.img_to_array(image)ですので、xはTF関連の中身と思います。 そこにnp.expand_dims(image, axis=0) ※ここでimageの中身は上記xと思います。 としていますのでnp.○○(TFのもの,axis=0)という構図になっているのがエラーの原因のような気がします。nparrayじゃなくてTFのTensorなりなんなりをNumpyに捌かせようとしているというのでしょうか。 試しにtf.expand_dimsに切り替えても動かした結果を教えてもらえますか?
guest

回答1

0

foolbox==2.4.0リリース時点で既に存在してたバージョンのtensorflowとkerasを入れて、
Examples
の書き方(「tensorflow.keras」を使わない)に変えて、さらにちょっと直して、適当な画像ファイルを使って実行したら、「adversarial = attack(image, label)」の行までは実行できました

その次の「new_preds =...」の行でエラーが出ますが、それはこの質問とは別のエラーなので、それに付いてさらに聞きたければ、別の質問にしてください
(質問する前に、まず質問者さんご自身が調べたり考えたりしてください)

以下で、行頭に「#」が付いてるあたりが、変更したところです

python

1# 追加 2!pip install tensorflow==2.0.0 3!pip install keras==2.3.1 4# 5!pip install foolbox==2.4.0 6import numpy as np 7#from tensorflow.keras.applications.resnet50 import ResNet50 8from keras.applications.resnet50 import ResNet50 9from foolbox.criteria import Misclassification, ConfidentMisclassification 10#from tensorflow.keras.preprocessing import image as img 11#from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions 12from keras.preprocessing import image as img 13from keras.applications.resnet50 import preprocess_input, decode_predictions 14import matplotlib.pyplot as plt 15import foolbox 16import pprint as pp 17#import tensorflow.keras 18import keras 19%matplotlib inline 20#--------------------------------------------------- 21def load_image(img_path: str): 22 image = img.load_img(img_path, target_size=(224, 224)) 23 plt.imshow(image) 24 x = img.img_to_array(image) 25 return x 26 27image = load_image('DSC_0897.jpg') 28#-------------------------------------------------------- 29#tensorflow.keras.backend.set_learning_phase(0) 30keras.backend.set_learning_phase(0) 31kmodel = ResNet50(weights='imagenet') 32preprocessing = (np.array([104, 116, 123]), 1) 33 34fmodel = foolbox.models.KerasModel(kmodel, bounds=(0, 255), 35 preprocessing=preprocessing) 36 37to_classify = np.expand_dims(image, axis=0) 38preds = kmodel.predict(to_classify) 39print('Predicted:', 40 pp.pprint(decode_predictions(preds, top=20)[0])) 41label = np.argmax(preds) 42 43image = image[:, :, ::-1] 44#attack = foolbox.attacks.FGSM(fmodel, threshold=.9, 45attack = foolbox.v1.attacks.FGSM(fmodel, threshold=.9, 46 criterion=ConfidentMisclassification(.9)) 47adversarial = attack(image, label) 48 49new_preds = kmodel.predict(np.expand_dims(adversarial, axis=0)) 50print('Predicted:', 51 pp.pprint(decode_predictions(new_preds, top=20)[0]))

投稿2021/11/23 02:32

編集2021/11/23 02:50
jbpb0

総合スコア7651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jbpb0

2021/11/23 07:13

「new_preds =...」の行でエラーが出るのは、もしかしたら当方で使った画像が悪かったのかもしれませんので、質問者さんがお使いの画像で実行したらエラー出ないかもしれません コードの意図を理解してないので、もしかしたら適切ではない画像を使ったかもしれません あと、上記のコードは、 https://foolbox.readthedocs.io/en/v2.4.0/user/examples.html のコード例の書き方を真似て、「adversarial = attack(image, label)」の行まではエラーが出なくなった状態のを書いたのですが、コードの意図を理解してないので、適切な修正かどうかは分かりません
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問