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

Question:

I am working on skin lesion detection and indentification using Densenet pretrained network. I however am stuck on this problem where it says that the weights can not be broadcasted to values.

```
%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)

```

I keep getting this error:

#############################################
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=(?,).

I haven’t had practice in using pretrained network so this is new to me. Any help would be appreciated. Thank you

Asked By: Shikhar Ghimire

||

Answers:

I think you might be trying to attach a dense layer directly on top of a convolutional layer.
Try flattening the output of the pretrained model:

######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) 
Answered By: Teo Cherici

I had a similar issue but the reason was in incompatible tf version (<2.2.0rc2) like in https://github.com/faustomorales/keras-ocr/issues/58#issuecomment-610486094
Updating fixed the issue.

Answered By: MosQuan