keras ValueError:无法将权重广播到values. values.rank=3. weights.rank=1. values.shape=(?,7,7). weights.shape=(?,)

wgeznvg7  于 2023-03-30  发布在  其他
关注(0)|答案(2)|浏览(213)

我正在使用Densenet预训练网络进行皮肤病变检测和识别。然而,我被这个问题卡住了,它说权值不能广播到值。

%tensorflow_version 1.x
import tensorflow as tf
from tensorflow.keras.applications import densenet
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential,Model,load_model
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten,Dense,Activation,Dropout
from tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau,ModelCheckpoint,Callback
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model

image_width,image_height=224,224 #Assigning height and width ---Densenet is trained in 224*224 dimensions
training_samples = 33068 #No of training sample
testing_samples = 1103 #Number of validation sample
epochs = 100
batch_size = 10 #Taking the batch_size of 10
n_classes = 7 #Number of classes is 7. We have 7 categories of skin disease

training_folder = '/content/gdrive/My Drive/Deep Learning Projects/skincancer/skin_cancer_classified/training_directory/'
testing_folder = '/content/gdrive/My Drive/Deep Learning Projects/skincancer/skin_cancer_classified/testing_directory/'

datagen = ImageDataGenerator(
preprocessing_function=
tf.keras.applications.densenet.preprocess_input)
#Using the same pre processing technique that was applied to the original rgb images for densenet architecture

training_batches = datagen.flow_from_directory(training_folder,
target_size=(image_height,image_width),
batch_size = batch_size)

testing_batches = datagen.flow_from_directory(testing_folder,
target_size=(image_height,image_width),
batch_size = batch_size)

network = tf.keras.applications.densenet.DenseNet121()

######Creating the model architecture by removing the last 5 layers from the network
engineered_network = network.layers[-5].output

engineered_network = Dropout(0.25)(engineered_network)

predictions = Dense(7,activation='softmax')(engineered_network)

final_model = Model(inputs = network.input, outputs = predictions)

for layer in final_model.layers[:-30]:
layer.trainable = False

final_model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=["accuracy"])

class_weights = {
0: 1.0,
1: 1.0,
2: 1.0,
3: 1.0,
4: 3.2, #making melanoma more sensitive
5: 1.0,
6: 1.0,
}

network_training = final_model.fit_generator(training_batches,
steps_per_epoch=36,
class_weight=class_weights,
validation_data = testing_batches,
validation_steps = 110,
epochs=epochs,
verbose = 1)

我一直收到这个错误:

#############################################
Epoch 1/100
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-6a6e3a644f6a> in <module>()
      5                                              validation_steps = 110,
      6                                              epochs=epochs,
----> 7                                              verbose = 1)

11 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/weights_broadcast_ops.py in assert_broadcastable(weights, values)
    101             " values.shape=%s. weights.shape=%s." % (
    102                 _ASSERT_BROADCASTABLE_ERROR_PREFIX, values_rank_static,
--> 103                 weights_rank_static, values.shape, weights.shape))
    104       weights_shape_static = tensor_util.constant_value(weights_shape)
    105       values_shape_static = tensor_util.constant_value(values_shape)

ValueError: weights can not be broadcast to values. values.rank=3. weights.rank=1. values.shape=(?, 7, 7). weights.shape=(?,).

我还没有使用预训练网络的实践,所以这对我来说是新的。任何帮助都将不胜感激。谢谢

jljoyd4f

jljoyd4f1#

我认为你可能试图在卷积层上直接附加一个密集层。尝试将预训练模型的输出平坦化:

######Creating the model architecture by removing the last 5 layers from the network
engineered_network = network.layers[-5].output

engineered_network = Dropout(0.25)(engineered_network)

engineered_network = Flatten()(engineered_network)

predictions = Dense(7,activation='softmax')(engineered_network)
8oomwypt

8oomwypt2#

我有一个类似的问题,但原因是在不兼容的tf版本(〈2.2.0rc2)一样,在https://github.com/faustomorales/keras-ocr/issues/58#issuecomment-610486094更新修复了这个问题。

相关问题