TensorFlow: Label has to be same size as logits - python

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:
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!

Related

torch.tensor object has no attribute 'dim' with a basic network

I m having some issues with pytorch after I train my model, it seems that the type i pass into my network changes, i.e when i try to do.a single prediction my forward function has an error as it has no ".dim" function even though I pass a torch.tensor object as input. this is my basic network architecture
class Net(nn.Module):
def __init__(self,input_shape):
super(Net,self).__init__()
self.fc1 = nn.Linear(input_shape,2)
self.fc2 = nn.Dropout(p=0.2)
self.fc3 = nn.Linear(2,2)
def forward(self,x):
x = torch.relu(self.fc1(x))
x = torch.softmax(self.fc3(x), 1)
return x
it trains and works as expected on the training batch but when i pass a single tensor
torch.Size([1, 100])
it gives me this error
x = torch.relu(self.fc1(x)) AttributeError: 'builtin_function_or_method' object has no attribute 'dim'
The network was trained with batches of
torch.Size([64, 100])
And it worked during training on the batches of 64 and output the results I expected when when I perform a single prediction using
prediction = model(X[0])
I encounter this error. I have tried a few things such as printing the type to verify that it was a valid object being passed in but it seems that somehow during the forward pass the types I have passed become invalid and I cannot understand why if my input is a singular element of the original training data . I also tried wrapping the X[0] inside a Variable() as Im unsure exactly what pytorch seems to expect as input given this error. I have also read other answers but they seem to address changes mostly in other layers I am not using as returning different values and I am unsure how to verify if the output of one of my layers is changing the type passed into the next layer.
Any help would be appreciated.
EDITED------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-602-1f45fd5f7727> in <module>
---> 12 y_pred = model(X[0])
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
<ipython-input-595-74840ffbc280> in forward(self, x)
13 self.fc3 = nn.Linear(2,2)
14 def forward(self,x):
---> 15 x = torch.relu(self.fc1(x))
16 x = torch.softmax(self.fc3(x), 1)
17 return x
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/modules/linear.py in forward(self, input)
65 #weak_script_method
66 def forward(self, input):
---> 67 return F.linear(input, self.weight, self.bias)
68
69 def extra_repr(self):
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1348 - Output: :math:`(N, *, out\_features)`
1349 """
-> 1350 if input.dim() == 2 and bias is not None:
1351 # fused op is marginally faster
1352 ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
AttributeError: 'builtin_function_or_method' object has no attribute 'dim'
Here is my training batching also incase this is useful.
from torch.utils.data import Dataset, DataLoader #Define a dataset and trainer for pytorch
class dataset(Dataset):
def __init__(self,x,y):
self.x = x.clone().detach().requires_grad_(True)
self.y = y.clone().detach().requires_grad_(False)
self.length = self.x.shape[0]
def __getitem__(self,idx):
return self.x[idx],self.y[idx]
def __len__(self):
return self.length
trainset = dataset(X,y)
trainloader = DataLoader(trainset,batch_size=64,shuffle=False)
EDITED------------------------------------------------------------------------
To add more details:
If i print(X)
tensor([[ 1.0430, -0.8477, 0.9932, ..., -0.8662, 0.6748, -0.4958],
[ 2.0728, -1.3987, 2.0863, ..., -1.2584, 0.9423, -1.1652],
[ 1.1594, -0.2600, 1.1863, ..., -0.2268, -0.0596, -1.2434],
...,
[ 1.2905, -2.1808, 1.2301, ..., -2.4004, 2.1426, 0.8747],
[-0.4722, -1.2714, -0.4748, ..., -1.3785, 1.6691, 1.8886],
[ 0.6362, -1.1497, 0.6117, ..., -1.1174, 1.0634, 0.2901]],
dtype=torch.float64)
and to actually train the network im using:
output = model(x_train)
loss = loss_fn(output,y_train)
predicted = model(X.clone().detach().requiresgrad(True).float())
optimizer.zero_grad()
loss.backward()
optimizer.step()
EDITED------------------------------------------------------------------------
If i add the type passed into forward i get this during training
def forward(self,x):
print(type(X))
x = torch.relu(self.fc1(x))
x = torch.softmax(self.fc3(x), 1)
return x
<class 'torch.Tensor'>
<class 'torch.Tensor'>

Error converting shape to a TensorShape: Dimension value must be integer or None or have an __index__ method, got value '

I am coding a simple Convolutional Network for a regression using RGB images but i am having a problem with the input shape.
The shape is (8,96,96,3) but the error tells me that it's not an integer which is normal because it's a tuple.
I have a relatively small dataset.
This is the code and the bug.
The Code:
import kerastuner
from keras.models import load_model
from kerastuner.tuners import RandomSearch
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Activation
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from imutils import paths
import numpy as np
import argparse
import cv2
import os
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from tensorflow.keras.models import Sequential
from keras.utils.vis_utils import plot_model
from keras.layers import Input
from keras.models import Model
from tensorflow import keras
from google.colab.patches import cv2_imshow
from google.colab import drive
drive.mount('/content/drive')
LABELS = set(["56", "76", "72", "110"])
print("[INFO] loading images...")
imagePaths = list(paths.list_images('/content/drive/My Drive/train'))
data = []
labels = []
for imagePath in imagePaths:
label = imagePath.split(os.path.sep)[-2]
if label not in LABELS:
continue
# load the image and resize it to be a fixed 96x96 pixels,
# ignoring aspect ratio
image = cv2.imread(imagePath)
image = cv2.resize(image, (96, 96))
# update the data and labels lists, respectively
data.append(image)
labels.append(label)
labels=list(set(labels))
data = np.array(data, dtype="float") / 250
labels = np.array(labels, dtype="int")
(X, Y) = (data, labels)
LABELS = set(["59"])
imagePaths = list(paths.list_images('/content/drive/My Drive/test'))
data = []
labels = []
for imagePath in imagePaths:
label = imagePath.split(os.path.sep)[-2]
if label not in LABELS:
continue
image = cv2.imread(imagePath)
image = cv2.resize(image, (96, 96))
data.append(image)
labels.append(label)
labels=list(set(labels))
data = np.array(data, dtype="float") / 250
labels = np.array(labels, dtype="int")
(Z,T) =(data,labels)
import numpy as np
from sklearn.preprocessing import StandardScaler
aug = ImageDataGenerator(rotation_range=20, zoom_range=0.15,
width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
horizontal_flip=True, fill_mode="nearest")
import tensorflow as tf
X.reshape(8,96,96,3)
input_shape =Input(batch_shape=[8,96,96,3])
Z.reshape(2,96,96,3)
X=np.array(X, dtype="int")
Z=np.array(Z, dtype="int")
def create_model(hp):
model = Sequential()
model.add(keras.layers.Conv2D(hp.Int('a',32,256,step=1), (3,3), input_shape=input_shape,activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Conv2D(hp.Int('c',32,256,step=1), (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Conv2D(hp.Int('w',32,256,step=1), (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)
model.fit(x=aug.flow(X, Y, batch_size=5),validation_data=(Z, T), steps_per_epoch=5, epochs=hp.Int('n',30,31,step=1))
return model
np.set_printoptions(precision=2,suppress=True)
my_dir ='/content/drive/My Drive/'
tuner= RandomSearch(create_model,objective= 'val_loss', max_trials =1, executions_per_trial = 1, directory = 'my_dir' )
tuner.search(x=X, y=Y,epochs=30,batch_size=5,validation_data=(Z, T))
create_model(tuner.get_best_hyperparameters()[0]).save('model1.h5')
model1=keras.models.load_model('model1.h5')
for i in range(len(Z)):
print("Prediction: " + str(model1.predict(Z[i] )) + " | True: " + str(T[i]))
scores=model1.evaluate(Z,T,batch_size=5)
print(scores)
tf.keras.utils.plot_model(model1, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
print(tuner.get_best_hyperparameters()[0].values)
The Error:
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py", line 213, in make_shape
raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: Dimension value must be integer or None or have an __index__ method, got value '<tf.Tensor 'strided_slice_40:0' shape=(96, 96, 3) dtype=float32>' with type '<class 'tensorflow.python.framework.ops.Tensor'>'.
[Warning] Invalid model 5/5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in make_shape(v, arg_name)
210 try:
--> 211 shape = tensor_shape.as_shape(v)
212 except TypeError as e:
22 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in as_shape(shape)
1234 else:
-> 1235 return TensorShape(shape)
1236
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, dims)
755 if isinstance(dims, (tuple, list)): # Most common case.
--> 756 self._dims = [Dimension(d) for d in dims]
757 elif dims is None:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in <listcomp>(.0)
755 if isinstance(dims, (tuple, list)): # Most common case.
--> 756 self._dims = [Dimension(d) for d in dims]
757 elif dims is None:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, value)
203 "an __index__ method, got value '{0!r}' with type '{1!r}'"
--> 204 .format(value, type(value))), None)
205 if self._value < 0:
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
TypeError: Dimension value must be integer or None or have an __index__ method, got value '<tf.Tensor 'strided_slice_40:0' shape=(96, 96, 3) dtype=float32>' with type '<class 'tensorflow.python.framework.ops.Tensor'>'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
104 with maybe_distribute(self.distribution_strategy):
--> 105 model = self.hypermodel.build(hp)
106 except:
<ipython-input-16-7d8b77f7488e> in create_model(hp)
114
--> 115 model.add(keras.layers.Conv2D(hp.Int('a',32,256,step=1), (3,3), input_shape=input_shape,activation='relu'))
116
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
201 x = input_layer.Input(
--> 202 batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')
203 # This will build the current layer
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_layer.py in Input(shape, batch_size, name, dtype, sparse, tensor, ragged, **kwargs)
310 {'batch_size': batch_size, 'input_shape': shape})
--> 311 input_layer = InputLayer(**input_layer_config)
312
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_layer.py in __init__(self, input_shape, batch_size, dtype, input_tensor, sparse, name, ragged, **kwargs)
159 sparse=sparse,
--> 160 ragged=ragged)
161
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in placeholder(shape, ndim, dtype, sparse, name, ragged)
1222 else:
-> 1223 x = array_ops.placeholder(dtype, shape=shape, name=name)
1224
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py in placeholder(dtype, shape, name)
3099
-> 3100 return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
3101
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py in placeholder(dtype, shape, name)
6806 shape = None
-> 6807 shape = _execute.make_shape(shape, "shape")
6808 _, _, _op, _outputs = _op_def_library._apply_op_helper(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in make_shape(v, arg_name)
212 except TypeError as e:
--> 213 raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
214 except ValueError as e:
TypeError: Error converting shape to a TensorShape: Dimension value must be integer or None or have an __index__ method, got value '<tf.Tensor 'strided_slice_40:0' shape=(96, 96, 3) dtype=float32>' with type '<class 'tensorflow.python.framework.ops.Tensor'>'.
During handling of the above exception, another exception occurred:
RuntimeError Traceback (most recent call last)
<ipython-input-16-7d8b77f7488e> in <module>()
133 np.set_printoptions(precision=2,suppress=True)
134 my_dir ='/content/drive/My Drive/'
--> 135 tuner= RandomSearch(create_model,objective= 'val_loss', max_trials =1, executions_per_trial = 1, directory = 'my_dir' )
136
137 tuner.search(x=X, y=Y,epochs=30,batch_size=5,validation_data=(Z, T))
/usr/local/lib/python3.6/dist-packages/kerastuner/tuners/randomsearch.py in __init__(self, hypermodel, objective, max_trials, seed, hyperparameters, tune_new_entries, allow_new_entries, **kwargs)
173 oracle,
174 hypermodel,
--> 175 **kwargs)
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/multi_execution_tuner.py in __init__(self, oracle, hypermodel, executions_per_trial, **kwargs)
56 **kwargs):
57 super(MultiExecutionTuner, self).__init__(
---> 58 oracle, hypermodel, **kwargs)
59 if isinstance(oracle.objective, list):
60 raise ValueError(
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/tuner.py in __init__(self, oracle, hypermodel, max_model_size, optimizer, loss, metrics, distribution_strategy, directory, project_name, logger, tuner_id, overwrite)
101 project_name=project_name,
102 logger=logger,
--> 103 overwrite=overwrite)
104
105 self.distribution_strategy = distribution_strategy
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/base_tuner.py in __init__(self, oracle, hypermodel, directory, project_name, logger, overwrite)
89 self._display = tuner_utils.Display()
90
---> 91 self._populate_initial_space()
92
93 if not overwrite and tf.io.gfile.exists(self._get_tuner_fname()):
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/base_tuner.py in _populate_initial_space(self)
104 """
105 hp = self.oracle.get_space()
--> 106 self.hypermodel.build(hp)
107 self.oracle.update_space(hp)
108
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in _build_wrapper(self, hp, *args, **kwargs)
63 # to the search space.
64 hp = hp.copy()
---> 65 return self._build(hp, *args, **kwargs)
66
67
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
113 if i == self._max_fail_streak:
114 raise RuntimeError(
--> 115 'Too many failed attempts to build model.')
116 continue
117
RuntimeError: Too many failed attempts to build model.
You assign to input shape a tf.layer.Input() object, not a tuple.
input_shape = Input(batch_shape=[8,96,96,3])
You can fix this by instead doing the following, which gets the shape of the first input of the Input layer:
input_shape = Input(batch_shape=[8, 96, 96, 3]).input_shape()[0]
Of course, since you know your shape I think you could specify input_shape=(96, 96, 3). You omit the first dimension because batch size is inferred. See this TF tutorial for an example.

How to use my own picture to generate adversarial example using FGSM?

I am trying to generate adversarial example using FGSM, and the code frame i am using is from Google Colab code(https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/generative/adversarial_fgsm.ipynb#scrollTo=wpYrQ4OQSYWk). And the kernal information of my Jupyter Book is Python3.7. However, when I tried to use my own picture to generate the adversarial example, the compilation failed all the time. Actually, the only part that I changed is "image_path = 'cat.jpg'". Well, I searched the errors on google, but it seems like there are no similar situations like this. Therefore, could you please give a hand,thanks a lot!
Here are my codes:
import tensorflow as tf
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['figure.figsize'] = (8, 8)
mpl.rcParams['axes.grid'] = False
pretrained_model = tf.keras.applications.MobileNetV2(include_top=True,
weights='imagenet')
pretrained_model.trainable = False
# ImageNet labels
decode_predictions = tf.keras.applications.mobilenet_v2.decode_predictions
# Helper function to preprocess the image so that it can be inputted in MobileNetV2
def preprocess(image):
image = tf.cast(image, tf.float32)
image = tf.image.resize(image, (224, 224))
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
image = image[None, ...]
return image
# Helper function to extract labels from probability vector
def get_imagenet_label(probs):
return decode_predictions(probs, top=1)[0][0]
image_path = 'cat.jpg'
image_raw = tf.io.read_file(image_path)
image = tf.image.decode_image(image_raw)
image = preprocess(image)
image_probs = pretrained_model.predict(image)
plt.figure()
plt.imshow(image[0]*0.5+0.5) # To change [-1, 1] to [0,1]
_, image_class, class_confidence = get_imagenet_label(image_probs)
plt.title('{} : {:.2f}% Confidence'.format(image_class, class_confidence*100))
plt.show()
loss_object = tf.keras.losses.CategoricalCrossentropy()
def create_adversarial_pattern(input_image, input_label):
with tf.GradientTape() as tape:
tape.watch(input_image)
prediction = pretrained_model(input_image)
loss = loss_object(input_label, prediction)
# Get the gradients of the loss w.r.t to the input image.
gradient = tape.gradient(loss, input_image)
# Get the sign of the gradients to create the perturbation
signed_grad = tf.sign(gradient)
return signed_grad
# Get the input label of the image.
labrador_retriever_index = 208
label = tf.one_hot(labrador_retriever_index, image_probs.shape[-1])
label = tf.reshape(label, (1, image_probs.shape[-1]))
perturbations = create_adversarial_pattern(image, label)
plt.imshow(perturbations[0]*0.5+0.5); # To change [-1, 1] to [0,1]
def display_images(image, description):
_, label, confidence = get_imagenet_label(pretrained_model.predict(image))
plt.figure()
plt.imshow(image[0]*0.5+0.5)
plt.title('{} \n {} : {:.2f}% Confidence'.format(description,
label, confidence*100))
plt.show()
epsilons = [0, 0.02, 0.2, 0.4, 0.8, 0.9, 1.0]
descriptions = [('Epsilon = {:0.3f}'.format(eps) if eps else 'Input')
for eps in epsilons]
for i, eps in enumerate(epsilons):
adv_x = image + eps*perturbations
adv_x = tf.clip_by_value(adv_x, -1, 1)
display_images(adv_x, descriptions[i])
And the erros are:
> ---------------------------------------------------------------------------
_FallbackException Traceback (most recent call last)
D:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\gen_io_ops.py in read_file(filename, name)
601 _ctx._context_handle, _ctx._thread_local_data.device_name, "ReadFile",
--> 602 name, _ctx._post_execution_callbacks, filename)
603 return _result
_FallbackException: This function does not handle the case of the path where all inputs are not already EagerTensors.
During handling of the above exception, another exception occurred:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-18-4edeac21c176> in <module>
27
28 image_path = 'cat.jpg'
---> 29 image_raw = tf.io.read_file(image_path)
30 image = tf.image.decode_image(image_raw)
31
D:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\gen_io_ops.py in read_file(filename, name)
605 try:
606 return read_file_eager_fallback(
--> 607 filename, name=name, ctx=_ctx)
608 except _core._SymbolicException:
609 pass # Add nodes to the TensorFlow graph.
D:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\gen_io_ops.py in read_file_eager_fallback(filename, name, ctx)
654 _attrs = None
655 _result = _execute.execute(b"ReadFile", 1, inputs=_inputs_flat,
--> 656 attrs=_attrs, ctx=_ctx, name=name)
657 _execute.record_gradient(
658 "ReadFile", _inputs_flat, _attrs, _result, name)
D:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
59 tensors = pywrap_tensorflow.TFE_Py_Execute(ctx._handle, device_name,
60 op_name, inputs, attrs,
---> 61 num_outputs)
62 except core._NotOkStatusException as e:
63 if name is not None:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 57: invalid continuation byte
I tested your code with some of my images and everything is ok.
it's just about your image encode.
run again with some other images or change the image encoding.

tensorflow dataset api input for training tuple object has no ndims attribute

so I'm trying to train a GAN to color images using a the new TensorFlow data set API
and I cant get it to work
I'm trying to use the simple one shot iterator for my data set and I think it might be causing the problem but I can't figure out why
so what I'm asking is
can someone tell me whats wrong with the code
code:
creating the data set
def get_next():
#where gray_ls is just a list of image paths
gray_ds = tf.data.Dataset.from_tensor_slices(gray_ls).shuffle(50).map(in_parser).batch(30).repeat()
print(f"output types = {gray_ds.output_types}") # --> output types = <dtype: 'float32'>
print(f"output shapes = {gray_ds.output_shapes}") # --> output shapes = (?, ?, ?, ?)
gray_iter = gray_ds.make_one_shot_iterator()
next_gray = gray_iter.get_next()
# next_color is the same as next gray just different images
return next_color, next_gray
# mapping function
def in_parser(img_path):
img_file = tf.read_file(img_path)
img = tf.image.decode_image(img_file,channels=3)
img = tf.image.random_flip_left_right(img)
img = tf.image.random_brightness(img, max_delta = 0.1)
img = tf.image.random_contrast(img, lower = 0.9, upper = 1.1)
img = tf.cast(img, tf.float32)
img = img/255.0
print(img)
return img
#some global vars
stddev = 0.02
decay = 0.9
epsilon = 1e-4
k_size = [5,5]
strides = [2,2]
def gen(input, is_train):
#chanel number
c1 , c2 ,c3 ,c4 = 64, 128, 256, 512
with tf.variable_scope("gen",reuse=tf.AUTO_REUSE):
#this is where it crashes
conv1 = tf.layers.conv2d(input,c1,k_size,strides,'SAME',
kernel_initializer=tf.truncated_normal_initializer(stddev=stddev),
name='conv1')
bn1 = tf.contrib.layers.batch_norm(conv1,is_training=is_train, updates_collections=None,
decay=decay,epsilon=epsilon,scope='bn1')
ac1 = lrelu(bn1,'ac1')
#there is more code after this
trying to run it:
next_color, next_gray = get_next()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
foo = sess.run(next_gray)
print(f"foo ndims : {foo.ndim}") # --> foo ndims : 4
gen_image = gen(foo, True)
# some more code after this
now this rasises an error:
AttributeError: 'tuple' object has no attribute 'ndims'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-701a9276e633> in <module>()
94
95
---> 96 train()
<ipython-input-1-701a9276e633> in train()
41 # print(foo.shape)
42 print("==========================+==============")
---> 43 gen_image = gen(foo, True)
44 # gen_image = gen(next_gray, True)
45 print("==========================+==============")
~\Desktop\code\python\image_processing\Untitled Folder\Untitled Folder\testing1_2\my_gen.py in gen(input, is_train)
30 conv1 = tf.layers.conv2d(input,c1,k_size,strides,'SAME',
31 kernel_initializer=tf.truncated_normal_initializer(stddev=stddev),
---> 32 name='conv1')
33
34 bn1 = tf.contrib.layers.batch_norm(conv1,is_training=is_train, updates_collections=None,
~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\layers\convolutional.py in conv2d(inputs, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, trainable, name, reuse)
423 _reuse=reuse,
424 _scope=name)
--> 425 return layer.apply(inputs)
426
427
~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in apply(self, inputs, *args, **kwargs)
803 Output tensor(s).
804 """
--> 805 return self.__call__(inputs, *args, **kwargs)
806
807 def _set_learning_phase_metadata(self, inputs, outputs):
~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\layers\base.py in __call__(self, inputs, *args, **kwargs)
360
361 # Actually call layer
--> 362 outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
363
364 if not context.executing_eagerly():
~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
718
719 # Check input assumptions set before layer building, e.g. input rank.
--> 720 self._assert_input_compatibility(inputs)
721 if input_list and self._dtype is None:
722 try:
~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _assert_input_compatibility(self, inputs)
1408 spec.min_ndim is not None or
1409 spec.max_ndim is not None):
-> 1410 if x.shape.ndims is None:
1411 raise ValueError('Input ' + str(input_index) + ' of layer ' +
1412 self.name + ' is incompatible with the layer: '
AttributeError: 'tuple' object has no attribute 'ndims'
thanks in advance
so apparently casting the out put to a tf.float32 solves the problem
next_color, next_gray = get_next()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
foo = sess.run(next_gray)
gray_batch = tf.cast(foo, dtype = tf.float32)
gen_image = gen(gray_batch, True)

How to avoid shape error when I change Keras' backend from Theano to Tensorflow?

I tried to use Deep Semantic Similarity Model(DSSM): https://github.com/airalcorn2/Deep-Semantic-Similarity-Model/blob/master/deep_semantic_similarity_keras.py on Keras using Tensorflow backend. The code is for Keras using Theano backend and works without any errors on it.
To make it run on Keras using Tensorflow backend correctly, I modified some Tensor attributes as:
-: backend.dot(x, backend.transpose(y)) / (x.norm(2) * y.norm(2))
+: backend.dot(backend.l2_normalize(x, 0), backend.transpose(backend.l2_normalize(y, 0)))
and
-: x.max(axis = 1)
+: backend.max(x, 1)
When I compiled the modified model, ValueError: logits and targets must have the same shape (() vs (?, ?)) occurred. How can I avoid the error?
Here is the modified code
import keras
import numpy as np
from keras import backend
from keras.layers import Input, merge
from keras.layers.core import Dense, Lambda, Reshape
from keras.layers.convolutional import Convolution1D
from keras.models import Model
def R(vects):
(x, y) = vects
return backend.dot(backend.l2_normalize(x, 0), backend.transpose(backend.l2_normalize(y, 0)))
LETTER_GRAM_SIZE = 3
WINDOW_SIZE = 3
TOTAL_LETTER_GRAMS = int(3 * 1e4)
WORD_DEPTH = WINDOW_SIZE * TOTAL_LETTER_GRAMS
K = 300
L = 128
J = 4
FILTER_LENGTH = 1
query = Input(shape = (None, WORD_DEPTH))
pos_doc = Input(shape = (None, WORD_DEPTH))
neg_docs = [Input(shape = (None, WORD_DEPTH)) for j in range(J)]
query_conv = Convolution1D(K, FILTER_LENGTH, border_mode = "same", input_shape = (None, WORD_DEPTH), activation = "tanh")(query) # See equation (2).
query_max = Lambda(lambda x: backend.max(x, 1), output_shape = (K, ))(query_conv)
query_sem = Dense(L, activation = "tanh", input_dim = K)(query_max)
doc_conv = Convolution1D(K, FILTER_LENGTH, border_mode = "same", input_shape = (None, WORD_DEPTH), activation = "tanh")
doc_max = Lambda(lambda x: backend.max(x, 1), output_shape = (K, ))
doc_sem = Dense(L, activation = "tanh", input_dim = K)
pos_doc_conv = doc_conv(pos_doc)
neg_doc_convs = [doc_conv(neg_doc) for neg_doc in neg_docs]
pos_doc_max = doc_max(pos_doc_conv)
neg_doc_maxes = [doc_max(neg_doc_conv) for neg_doc_conv in neg_doc_convs]
pos_doc_sem = doc_sem(pos_doc_max)
neg_doc_sems = [doc_sem(neg_doc_max) for neg_doc_max in neg_doc_maxes]
R_layer = Lambda(R, output_shape = (1, ))
R_Q_D_p = R_layer([query_sem, pos_doc_sem])
R_Q_D_ns = [R_layer([query_sem, neg_doc_sem]) for neg_doc_sem in neg_doc_sems]
concat_Rs = merge([R_Q_D_p] + R_Q_D_ns, mode = "concat")
concat_Rs = Reshape((J + 1, 1))(concat_Rs)
weight = np.array([1]).reshape(1, 1, 1, 1)
with_gamma = Convolution1D(1, 1, border_mode = "same", input_shape = (J + 1, 1), activation = "linear", bias = False, weights = [weight])(concat_Rs)
exponentiated = Lambda(lambda x: backend.exp(x), output_shape = (J + 1, ))(with_gamma) # See equation (5).
exponentiated = Reshape((J + 1, ))(exponentiated)
prob = Lambda(lambda x: x[0][0] / backend.sum(x[0]), output_shape = (1, ))(exponentiated)
model = Model(input = [query, pos_doc] + neg_docs, output = prob)
model.compile(optimizer = "adadelta", loss = "binary_crossentropy")
and error message.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Users/jun/anaconda/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
571 try:
--> 572 self.assert_same_rank(other)
573 new_dims = []
/Users/jun/anaconda/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py in assert_same_rank(self, other)
617 raise ValueError(
--> 618 "Shapes %s and %s must have the same rank" % (self, other))
619
ValueError: Shapes (?, ?) and () must have the same rank
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/Users/jun/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/nn.py in sigmoid_cross_entropy_with_logits(logits, targets, name)
430 try:
--> 431 targets.get_shape().merge_with(logits.get_shape())
432 except ValueError:
/Users/jun/anaconda/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
578 raise ValueError("Shapes %s and %s are not compatible" %
--> 579 (self, other))
580
ValueError: Shapes (?, ?) and () are not compatible
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-6-a4001289be07> in <module>()
61
62 model = Model(input = [query, pos_doc] + neg_docs, output = prob)
---> 63 model.compile(optimizer = "adadelta", loss = "binary_crossentropy")
/Users/jun/anaconda/lib/python3.5/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, **kwargs)
628 loss_weight = loss_weights_list[i]
629 output_loss = weighted_loss(y_true, y_pred,
--> 630 sample_weight, mask)
631 if len(self.outputs) > 1:
632 self.metrics_tensors.append(output_loss)
/Users/jun/anaconda/lib/python3.5/site-packages/keras/engine/training.py in weighted(y_true, y_pred, weights, mask)
330 def weighted(y_true, y_pred, weights, mask=None):
331 # score_array has ndim >= 2
--> 332 score_array = fn(y_true, y_pred)
333 if mask is not None:
334 # Cast the mask to floatX to avoid float64 upcasting in theano
/Users/jun/anaconda/lib/python3.5/site-packages/keras/objectives.py in binary_crossentropy(y_true, y_pred)
46
47 def binary_crossentropy(y_true, y_pred):
---> 48 return K.mean(K.binary_crossentropy(y_pred, y_true), axis=-1)
49
50
/Users/jun/anaconda/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in binary_crossentropy(output, target, from_logits)
1464 output = tf.clip_by_value(output, epsilon, 1 - epsilon)
1465 output = tf.log(output / (1 - output))
-> 1466 return tf.nn.sigmoid_cross_entropy_with_logits(output, target)
1467
1468
/Users/jun/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/nn.py in sigmoid_cross_entropy_with_logits(logits, targets, name)
432 except ValueError:
433 raise ValueError("logits and targets must have the same shape (%s vs %s)"
--> 434 % (logits.get_shape(), targets.get_shape()))
435
436 # The logistic loss formula from above is
ValueError: logits and targets must have the same shape (() vs (?, ?))
(Converted from a comment to an answer at #dga's request)
I asked our resident Keras expert, and his response was: Looking at the error message and code, one can infer that the final output prob is a scalar, whereas it should be a 2D array (one scalar probability per batch entry). The problem is likely to be at line
prob = Lambda(lambda x: x[0][0] / backend.sum(x[0]), output_shape = (1, ))(exponentiated)

Categories