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

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

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

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

346閲覧

tensorflowにおけるモデル構築Lambda文について

退会済みユーザー

退会済みユーザー

総合スコア0

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/10/30 08:11

質問

tensorflowのimage内の関数random_cropを使うと以下のエラーが出ます。
tf.random_cropでは次元の数は変わらないと思うのですが。
どのようにすれば解消できますか。

エラーメッセージ

以下はモデルを初期化する際に発生するメッセージです。

python

1Dimensions must be equal, but are 4 and 3 for 'lambda_20/random_crop/GreaterEqual' (op: 'GreaterEqual') with input shapes: [4], [3].

### ソースコード

python

1def multiresolution_model(): 2 inputs = Input(shape=(entire_x, entire_y, 3)) 3 4 high = Lambda(lambda image: tf.image.resize_images(image, (img_width, img_height)))(inputs) #こちらは問題なく通る 5 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(high) 6 x = BatchNormalization()(x) 7 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) 8 x = BatchNormalization()(x) 9 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block1_pool')(x) 10 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) 11 x = BatchNormalization()(x) 12 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) 13 x = BatchNormalization()(x) 14 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block2_pool')(x) 15 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) 16 x = BatchNormalization()(x) 17 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) 18 x = BatchNormalization()(x) 19 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) 20 x = BatchNormalization()(x) 21 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block3_pool')(x) 22 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) 23 x = BatchNormalization()(x) 24 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) 25 x = BatchNormalization()(x) 26 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) 27 x = BatchNormalization()(x) 28 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block4_pool')(x) 29 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) 30 x = BatchNormalization()(x) 31 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) 32 x = BatchNormalization()(x) 33 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) 34 x = BatchNormalization()(x) 35 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block5_pool')(x) 36 flattened_high = Flatten(name='flatten')(x) 37 38 #ここが問題の文 39 low = Lambda(lambda image: tf.random_crop(image, [img_height, img_width, 3]))(inputs)#次元が違うとして止まる 40 ###### 41 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1-2')(low) 42 x = BatchNormalization()(x) 43 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2-2')(x) 44 x = BatchNormalization()(x) 45 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block1_pool-2')(x) 46 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1-2')(x) 47 x = BatchNormalization()(x) 48 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2-2')(x) 49 x = BatchNormalization()(x) 50 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block2_pool-2')(x) 51 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1-2')(x) 52 x = BatchNormalization()(x) 53 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2-2')(x) 54 x = BatchNormalization()(x) 55 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3-2')(x) 56 x = BatchNormalization()(x) 57 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block3_pool-2')(x) 58 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1-2')(x) 59 x = BatchNormalization()(x) 60 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2-2')(x) 61 x = BatchNormalization()(x) 62 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3-2')(x) 63 x = BatchNormalization()(x) 64 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block4_pool-2')(x) 65 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1-2')(x) 66 x = BatchNormalization()(x) 67 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2-2')(x) 68 x = BatchNormalization()(x) 69 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3-2')(x) 70 x = BatchNormalization()(x) 71 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block5_pool-2')(x) 72 flattened_low = Flatten(name='flatten-2')(x) 73 74 merge = concatenate([flattened_low, flattened_high]) 75 x = Dense(4096, activation='relu', name='fc1')(merge) 76 x = Dropout(0.5, name='dropout1')(x) 77 x = Dense(4096, activation='relu', name='fc2')(x) 78 x = Dropout(0.5, name='dropout2')(x) 79 predictions = Dense(nb_classes, activation='softmax', name='predictions')(x) 80 model = Model(inputs=inputs, outputs=predictions) 81 82 return model

補足

入力層でクロップ画像とリサイズ画像の2パターンを並列に扱うモデルを組み立てる際にcropがモデル中で定義できなかったので質問させていただきました。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Tensorflow の画像処理系の関数は画像1枚だけを想定しているので、ミニバッチに対して、まとめて処理する場合は、tf.map_fn() を使いましょう。

map on the list of tensors unpacked from elems on dimension 0.

Python の map() 関数同様に以下のように使います。

python

1tf.map_fn(lambda img: tf.random_crop(img, [500, 500, 3]), input_tensor)

サンプルコード

python

1import matplotlib.pyplot as plt 2import numpy as np 3import tensorflow as tf 4from PIL import Image 5 6# 画像を読み込む。 7img = np.array(Image.open('test.jpg')) 8 9# 画像を複製する。 10imgs = np.tile(img, (4, 1, 1, 1)) 11print(imgs.shape) # (4, 1960, 1960, 3) 12 13# 計算グラフを作成する。 14h, w = img.shape[:2] 15input_tensor = tf.placeholder(tf.uint8, [None, h, w, 3]) 16output = tf.map_fn(lambda img: tf.random_crop(img, [500, 500, 3]), input_tensor) 17 18# 実行する。 19with tf.Session() as sess: 20 cropped_imgs = sess.run(output, feed_dict={input_tensor: imgs}) 21print(cropped_imgs.shape) # (4, 500, 500, 3) 22 23# 描画する。 24fig, ax_list = plt.subplots(2, 2, figsize=(8, 8)) 25for cropped, ax in zip(cropped_imgs, ax_list.ravel()): 26 ax.set_axis_off() 27 ax.imshow(cropped) 28plt.show()

イメージ説明
入力

イメージ説明
出力

追記

次元は変わらないですが、crop しているので、形状は変わります。

python

1import tensorflow as tf 2 3h, w = 1000, 1000 4input_tensor = tf.placeholder(tf.uint8, [None, h, w, 3]) 5output = tf.map_fn(lambda img: tf.random_crop(img, [500, 500, 3]), input_tensor) 6 7print(input_tensor.shape) 8print(output.shape)
(?, 1000, 1000, 3) (?, 500, 500, 3)

投稿2018/10/30 08:48

編集2018/11/06 05:19
tiitoi

総合スコア21956

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

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

退会済みユーザー

退会済みユーザー

2018/11/01 16:52

回答ありがとうございます。multiresolution_model内で、ここに書かれたコード通りに関数を定義し実行したところ次元のエラーは回避できましたが以下のエラーが出ました。 "setting an array element with a sequence." shapeの値がずれているのはなぜでしょうか。
tiitoi

2018/11/02 03:57

> shapeの値がずれているのはなぜでしょうか。 どこのことでしょうか?
退会済みユーザー

退会済みユーザー

2018/11/06 04:38

おそらくこのエラーコードがデータの次元が変更されてしまっていることから来ているものだと思ったのです。tf.mat_fnを通す前後で軸が変更されてしまうことはありますか?
tiitoi

2018/11/06 05:19

次元や軸の位置は変わりません。Crop しているので、形状は変わります。
退会済みユーザー

退会済みユーザー

2018/11/06 06:21

ありがとうございました。解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問