前提・実現したいこと
cnnの構築を行う際、大きいサイズの画像をブロックに分割して複数入力を取り扱うモデルを考えました。
以下のソースコードを実行した時に出るエラーを解消したいです。
発生している問題・エラーメッセージ
File "hoge.py", line 76, in parallel_VGGs branch = mid_VGG(block, num)(block) TypeError: 'Tensor' object is not callable
該当のソースコード
Python
1import os 2from keras.applications.vgg16 import VGG16 3from keras.preprocessing.image import ImageDataGenerator 4from keras.models import Sequential, Model 5from keras.layers import merge, Input, Dropout, Conv2D, MaxPooling2D, Lambda, Dense, Flatten, BatchNormalization, concatenate 6from keras import optimizers 7from keras.utils.np_utils import to_categorical 8from sklearn import preprocessing 9from PIL import Image 10import tensorflow as tf 11import matplotlib.pyplot as plt 12import numpy as np 13import time 14import random 15import sys 16 17img_width, img_height = 224, 224 18entire_y, entire_x = 2178, 2178 19block_y, block_x = 2, 2 20nb_classes = 2 21 22def mid_VGG(inputs, number): #分割後のCNN一つ一つの構造 23 #inputs = Input(shape=(224, 224, 3)) 24 #inputs = Input(shape=(entire_x, entire_y, 3)) 25 # Due to memory limitation, images will resized on-the-fly. 26 #x = Lambda(lambda image: tf.image.resize_images(image, (img_width*8, img_height*8)))(inputs) 27 #x = Lambda(lambda image: block_transform(image, mode="channel"))(x) 28 29 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1'+ "_vol_" +str(number))(inputs) 30 x = BatchNormalization()(x) 31 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2'+ "_vol_" +str(number))(x) 32 x = BatchNormalization()(x) 33 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block1_pool'+ "_vol_" +str(number))(x) 34 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1'+ "_vol_" +str(number))(x) 35 x = BatchNormalization()(x) 36 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2'+ "_vol_" +str(number))(x) 37 x = BatchNormalization()(x) 38 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block2_pool'+ "_vol_" +str(number))(x) 39 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1'+ "_vol_" +str(number))(x) 40 x = BatchNormalization()(x) 41 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2'+ "_vol_" +str(number))(x) 42 x = BatchNormalization()(x) 43 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3'+ "_vol_" +str(number))(x) 44 x = BatchNormalization()(x) 45 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block3_pool'+ "_vol_" +str(number))(x) 46 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1'+ "_vol_" +str(number))(x) 47 x = BatchNormalization()(x) 48 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2'+ "_vol_" +str(number))(x) 49 x = BatchNormalization()(x) 50 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3'+ "_vol_" +str(number))(x) 51 x = BatchNormalization()(x) 52 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block4_pool'+ "_vol_" +str(number))(x) 53 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1'+ "_vol_" +str(number))(x) 54 x = BatchNormalization()(x) 55 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2'+ "_vol_" +str(number))(x) 56 x = BatchNormalization()(x) 57 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3'+ "_vol_" +str(number))(x) 58 x = BatchNormalization()(x) 59 x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block5_pool'+ "_vol_" +str(number))(x) 60 """flattened = Flatten(name='flatten')(x) 61 x = Dense(4096, activation='relu', name='fc1')(flattened) 62 x = Dropout(0.5, name='dropout1')(x) 63 x = Dense(4096, activation='relu', name='fc2')(x) 64 x = Dropout(0.5, name='dropout2')(x) 65 predictions = Dense(nb_classes, activation='softmax', name='predictions')(x)""" 66 return x 67 68def parallel_VGGs(): #mid_VGGsを並列に並べてブロック分割による複数入力に対応する 69 inputs = Input(shape=(entire_y, entire_x, 3)) 70 in_ = Lambda(lambda image: tf.image.resize_images(image, (img_width*block_x, img_height*block_y)))(inputs) 71 VGGs = [] 72 num = 1 73 for by in range(block_y): 74 for bx in range(block_x): 75 block = Lambda(lambda image: image[:, img_width*bx: img_width*(bx+1), img_height*by: img_height*(by+1), :], output_shape=(img_width, img_height, 3))(in_) #block separation 76 branch = mid_VGG(block, num)(block) 77 num += 1 78 VGGs.append(branch) 79 80 x = concatenate(VGGs) 81 flattened = Flatten(name='flatten')(x) 82 x = Dense(4096, activation='relu', name='fc1')(flattened) 83 x = Dropout(0.5, name='dropout1')(x) 84 x = Dense(4096, activation='relu', name='fc2')(x) 85 x = Dropout(0.5, name='dropout2')(x) 86 predictions = Dense(nb_classes, activation='softmax', name='predictions')(x) 87 return Model(input = inputs, output = predictions) 88 89if __name__ == "__main__": 90 model=parallel_VGGs() 91 from keras.utils import plot_model 92 plot_model(model, to_file='model.png', 93 show_shapes=True, show_layer_names=True) 94
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/11/27 04:53