TensorFlow: Label has to be same size as logits

Viewed 29

I have a dataset where there are 500 images of dimension (32, 32, 3) and its label which is a single integer. Thus making my label a shape (). However when I run my code, it gives error where:

Shapes () and (1, 32, 32, 3) are incompatible

I'm assuming my model is requiring a (32x32x3) dimension data, so how should I alter my label (integer of 0 to 7) into a (1,32,32,3) shape to fit?

Summary of model:

enter image description here

Code I'm running:

for i in range(20):
    n = random.randint(0, len(data_manager.X_test))
    label = data_manager.y_test[n]
    #label_samples = np.zeros((1,32,32,3), dtype=label.dtype)
    img = data_manager.X_test[n]
    img = tf.expand_dims(img, axis=0)
    img = np.array(img)
    img = img.astype('float32')

    
    true_pred = our_network_skip.predict(img)

    pa = pgd_attack(our_network_skip, img, label,
                  epsilon=0.0313, 
                  num_steps=20, 
                  step_size=0.002, 
                  clip_value_min=0., 
                  clip_value_max=1.0, 
                  soft_label=False,
                  from_logits= False)  #error from this function where label has to be (1,32,32,3)

Function for the error:

def pgd_attack(model, input_image, input_label= None, 
              epsilon=0.0313, 
              num_steps=20, 
              step_size=0.002, 
              clip_value_min=0., 
              clip_value_max=1.0, 
              soft_label=False,
              from_logits= False): 
    
    loss_fn = tf.keras.losses.categorical_crossentropy  #compute CE loss from logits or prediction probabilities
    
    if type(input_image) is np.ndarray: 
        input_image = tf.convert_to_tensor(input_image)
    
    if type(input_label) is np.ndarray: 
        input_label = tf.convert_to_tensor(input_label)
        
    # random initialization around input_image 
    random_noise = tf.random.uniform(shape=input_image.shape, minval=-epsilon, maxval=epsilon)
    adv_image = input_image + random_noise

    for _ in range(num_steps): 
        with tf.GradientTape(watch_accessed_variables=False) as tape: 
            tape.watch(adv_image)
            
            if not soft_label:
                loss = loss_fn(input_label, adv_image, from_logits= from_logits) # use ground-truth label to attack
            else: 
                pred_label = tf.math.argmax(adv_image, axis=1)
                loss = loss_fn(pred_label, adv_image, from_logits= from_logits) # use predicted label to attack

        gradient = tape.gradient(loss, adv_image) # get the gradient of the loss w.r.t. the current point 
        adv_image = adv_image + step_size * tf.sign(gradient) # move current adverarial example along the gradient direction with step size is eta 
        adv_image = tf.clip_by_value(adv_image, input_image-epsilon, input_image+epsilon) # clip to a valid boundary  
        adv_image = tf.clip_by_value(adv_image, clip_value_min, clip_value_max)  # clip to a valid range
        adv_image = tf.stop_gradient(adv_image) # stop the gradient to make the adversarial image as a constant input 
    return adv_image

Full error:

ValueError                                Traceback (most recent call last)
Input In [57], in <cell line: 48>()
     57 img = img.astype('float32')
     60 true_pred = our_network_skip.predict(img)
---> 62 pa = pgd_attack(our_network_skip, img, label,
     63               epsilon=0.0313, 
     64               num_steps=20, 
     65               step_size=0.002, 
     66               clip_value_min=0., 
     67               clip_value_max=1.0, 
     68               soft_label=False,
     69               from_logits= False)
     71 pgd_pred = our_network_skip.predict(pa)
     72 print("True label: {}, adversarial label: {}".format(true_pred, pgd_pred))

Input In [57], in pgd_attack(model, input_image, input_label, epsilon, num_steps, step_size, clip_value_min, clip_value_max, soft_label, from_logits)
     32 tape.watch(adv_image)
     34 if not soft_label:
---> 35     loss = loss_fn(input_label, adv_image, from_logits= from_logits) # use ground-truth label to attack
     36 else: 
     37     pred_label = tf.math.argmax(adv_image, axis=1)

File ~\anaconda3\envs\tf2_cpu\lib\site-packages\tensorflow\python\util\dispatch.py:206, in add_dispatch_support.<locals>.wrapper(*args, **kwargs)
    204 """Call target, and fall back on dispatchers if there is a TypeError."""
    205 try:
--> 206   return target(*args, **kwargs)
    207 except (TypeError, ValueError):
    208   # Note: convert_to_eager_tensor currently raises a ValueError, not a
    209   # TypeError, when given unexpected types.  So we need to catch both.
    210   result = dispatch(wrapper, args, kwargs)

File ~\anaconda3\envs\tf2_cpu\lib\site-packages\keras\losses.py:1665, in categorical_crossentropy(y_true, y_pred, from_logits, label_smoothing, axis)
   1660   return y_true * (1.0 - label_smoothing) + (label_smoothing / num_classes)
   1662 y_true = tf.__internal__.smart_cond.smart_cond(label_smoothing, _smooth_labels,
   1663                                lambda: y_true)
-> 1665 return backend.categorical_crossentropy(
   1666     y_true, y_pred, from_logits=from_logits, axis=axis)

File ~\anaconda3\envs\tf2_cpu\lib\site-packages\tensorflow\python\util\dispatch.py:206, in add_dispatch_support.<locals>.wrapper(*args, **kwargs)
    204 """Call target, and fall back on dispatchers if there is a TypeError."""
    205 try:
--> 206   return target(*args, **kwargs)
    207 except (TypeError, ValueError):
    208   # Note: convert_to_eager_tensor currently raises a ValueError, not a
    209   # TypeError, when given unexpected types.  So we need to catch both.
    210   result = dispatch(wrapper, args, kwargs)

File ~\anaconda3\envs\tf2_cpu\lib\site-packages\keras\backend.py:4839, in categorical_crossentropy(target, output, from_logits, axis)
   4837 target = tf.convert_to_tensor(target)
   4838 output = tf.convert_to_tensor(output)
-> 4839 target.shape.assert_is_compatible_with(output.shape)
   4841 # Use logits whenever they are available. `softmax` and `sigmoid`
   4842 # activations cache logits on the `output` Tensor.
   4843 if hasattr(output, '_keras_logits'):

File ~\anaconda3\envs\tf2_cpu\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1161, in TensorShape.assert_is_compatible_with(self, other)
   1149 """Raises exception if `self` and `other` do not represent the same shape.
   1150 
   1151 This method can be used to assert that there exists a shape that both
   (...)
   1158   ValueError: If `self` and `other` do not represent the same shape.
   1159 """
   1160 if not self.is_compatible_with(other):
-> 1161   raise ValueError("Shapes %s and %s are incompatible" % (self, other))

ValueError: Shapes () and (1, 32, 32, 3) are incompatible

Thank you!

0 Answers
Related