tensorflow TypeError:在cnn模型中无法呼叫'bool'对象[已关闭]

tf7tbtn2  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(129)

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
5小时前关门了。
Improve this question
我正在尝试对视网膜OCT图像(光学相干断层扫描)进行分类
1.正常
1.德鲁森

  1. CNV值
  2. DME数据集链接:https://www.kaggle.com/datasets/paultimothymooney/kermany2018
model = Sequential();
model.add((Conv2D(32,kernel_size = 5,input_shape=(img_height,img_width,3),activation="relu"))) model.add((Conv2D(32,kernel_size = 5,activation="relu")))
model.add((Conv2D(64,kernel_size = 4,activation="relu")))
model.add((Conv2D(64,kernel_size = 4,activation="relu")))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add((Conv2D(32,kernel_size = 5,activation="relu")))
model.add((Conv2D(64,kernel_size = 4,activation="relu")))
model.add((Conv2D(64,kernel_size = 4,activation="relu")))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add((Conv2D(32,kernel_size = 4,activation="relu")))
model.add(MaxPooling2D(pool_size=2))
model.add((Conv2D(64,kernel_size = 3,activation="relu")))
model.add((Conv2D(64,kernel_size = 3,activation="relu")))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(1024,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(512,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(4,activation="softmax"))

model.compile(optimizer = SGD(lr=0.001,momentum = 0.9),loss="categorical_crossentropy",metrics=['accuracy'])
model.built(input_shape = (img_height,img_width,3))
model.summary()

以上是我的模型误差是

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_23/2596533629.py in <module>
     28 
     29 model.compile(optimizer = SGD(lr=0.001,momentum = 0.9),loss="categorical_crossentropy",metrics=['accuracy'])
---> 30 model.built(input_shape = (img_height,img_width,3))
     31 model.summary()

TypeError: 'bool' object is not callable

它显示

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_23/3973631267.py in <module>
     29 model.compile(optimizer = SGD(lr=0.001,momentum = 0.9),loss="categorical_crossentropy",metrics=['accuracy'])
     30 # model.built(input_shape = (img_height,img_width,3))
---> 31 model.summary()

/opt/conda/lib/python3.7/site-packages/keras/engine/training.py in summary(self, line_length, positions, print_fn)
   2519     """
   2520     if not self.built:
-> 2521       raise ValueError('This model has not yet been built. '
   2522                        'Build the model first by calling `build()` or calling '
   2523                        '`fit()` with some data, or specify '

ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

在添加模型之前。built(input_shape =(img_height,img_width,3))行

qv7cva1a

qv7cva1a1#

the type of bool returned when no type matches and it is string operations that are programming logics but for your question answer is the correct function model.build(input_shape = (img_height,img_width,3)).
The model.build() inherits from class benefits when you have multiple of model variables and reuse it.
Sample: From the question, the method name is model.build() and input is enough for its dimension.

import tensorflow as tf

img_height = 256
img_width = 256

model = tf.keras.models.Sequential();
model.add((tf.keras.layers.Conv2D(32,kernel_size = 5,input_shape=(img_height,img_width,3),activation="relu"))) 
model.add((tf.keras.layers.Conv2D(32,kernel_size = 5,activation="relu")))
model.add((tf.keras.layers.Conv2D(64,kernel_size = 4,activation="relu")))
model.add((tf.keras.layers.Conv2D(64,kernel_size = 4,activation="relu")))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.2))

model.add((tf.keras.layers.Conv2D(32,kernel_size = 5,activation="relu")))
model.add((tf.keras.layers.Conv2D(64,kernel_size = 4,activation="relu")))
model.add((tf.keras.layers.Conv2D(64,kernel_size = 4,activation="relu")))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.2))

model.add((tf.keras.layers.Conv2D(32,kernel_size = 4,activation="relu")))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add((tf.keras.layers.Conv2D(64,kernel_size = 3,activation="relu")))
model.add((tf.keras.layers.Conv2D(64,kernel_size = 3,activation="relu")))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.2))

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(1024,activation="relu"))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(512,activation="relu"))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(4,activation="softmax"))

model.build(input_shape = (img_height,img_width,3))
model.compile(optimizer = tf.keras.optimizers.SGD(lr=0.001,momentum = 0.9),loss="categorical_crossentropy",metrics=['accuracy'])
model.summary()

示例:执行继承类的测试,函数build()内部包含初始权重,执行时必须正确预期。

import tensorflow as tf

class MyDenseLayer_1(tf.keras.layers.Layer):
    def __init__(self, num_outputs):
        super(MyDenseLayer_1, self).__init__()
        self.num_outputs = num_outputs
        
    def build(self, input_shape):
        min_size_init = tf.keras.initializers.RandomUniform(minval=1, maxval=5, seed=None)
        self.kernel = self.add_weight(shape=[int(input_shape[-1]), self.num_outputs],
        initializer = min_size_init, trainable=True)

    def call(self, inputs):
        temp = tf.matmul(inputs, self.kernel)       # , shape=(10, 10), dtype=float32)
        return temp
        
start = 3
limit = 33
delta = 3
sample = tf.range(start, limit, delta)
sample = tf.cast( sample, dtype=tf.float32 )
sample = tf.constant( sample, shape=( 10, 1 ) )
layer_1 = MyDenseLayer_1(10)

print( layer_1( sample ) )
layer_1.build( [1] )
print( layer_1( sample ) )

Output: The results after calling the function build are different we are expecting the same behavior from the model.build(). The initial value is important because in some problem equations critical points can have more than one and we do not want to skip some small steps. Recalling the build() function that is you start from the same alignment as find a and b from ax + b = y.

tf.Tensor(
[[  9.630259    3.4856472  11.3084545  10.482325    8.795776   12.19894
    6.0301466   4.6374693  10.00268    12.748121 ]
 [ 19.260517    6.9712944  22.616909   20.96465    17.591553   24.39788
   12.060293    9.274939   20.00536    25.496243 ]
 [ 28.890778   10.456942   33.925365   31.446974   26.387327   36.59682
   18.090439   13.912408   30.00804    38.244366 ]
 [ 38.521034   13.942589   45.233818   41.9293     35.183105   48.79576
   24.120586   18.549877   40.01072    50.992485 ]
 [ 48.151295   17.428236   56.542274   52.41162    43.978878   60.994698
   30.150734   23.187347   50.013397   63.74061  ]
 [ 57.781555   20.913883   67.85073    62.893948   52.774654   73.19364
   36.180878   27.824816   60.01608    76.48873  ]
 [ 67.41181    24.39953    79.15919    73.376274   61.57043    85.39258
   42.211025   32.462284   70.01876    89.236855 ]
 [ 77.04207    27.885178   90.467636   83.8586     70.36621    97.59152
   48.241173   37.099754   80.02144   101.98497  ]
 [ 86.67233    31.370825  101.77609    94.34092    79.16198   109.79046
   54.27132    41.737225   90.02412   114.73309  ]
 [ 96.30259    34.856472  113.08455   104.82324    87.957756  121.989395
   60.301468   46.374695  100.026794  127.48122  ]], shape=(10, 10), dtype=float32)
tf.Tensor(
[[  7.649829    9.33099     9.594252   10.802372    5.9865932   6.833057
    7.6979485   5.853832   12.8820095  13.096819 ]
 [ 15.299658   18.66198    19.188503   21.604744   11.9731865  13.666114
   15.395897   11.707664   25.764019   26.193638 ]
 [ 22.949486   27.992971   28.782755   32.407116   17.95978    20.49917
   23.093845   17.561495   38.646027   39.29046  ]
 [ 30.599316   37.32396    38.377007   43.209488   23.946373   27.332228
   30.791794   23.415327   51.528038   52.387276 ]
 [ 38.249146   46.654953   47.97126    54.011856   29.932966   34.165283
   38.489742   29.26916    64.41004    65.48409  ]
 [ 45.89897    55.985943   57.56551    64.81423    35.91956    40.99834
   46.18769    35.12299    77.29205    78.58092  ]
 [ 53.5488     65.31693    67.15976    75.6166     41.90615    47.831398
   53.88564    40.97682    90.174065   91.677734 ]
 [ 61.19863    74.64792    76.75401    86.418976   47.892746   54.664455
   61.583588   46.830654  103.056076  104.77455  ]
 [ 68.84846    83.97891    86.34827    97.221344   53.87934    61.497513
   69.28154    52.684486  115.93808   117.87137  ]
 [ 76.49829    93.309906   95.94252   108.02371    59.865932   68.33057
   76.979485   58.53832   128.82008   130.96819  ]], shape=(10, 10), dtype=float32)

相关问题