#!pip install tensorflow-addons
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow_addons as tfa我的数据集只是一个父文件夹,名称为“dataset”,其中包含测试集图像的“test”文件夹和包含培训图像的“培训”文件夹。除了测试和培训文件夹之外,数据集文件夹中也有代码文件。单击此处查看文件数据集目录结构。
from pathlib import Path
from PIL import Image
import os, shutil
from os import listdir
## Image Resizing
from PIL import Image
# load and display an image with Matplotlib
from matplotlib import image
from matplotlib import pyplot
# Image folder
images_dir = Path('Test/Images').expanduser()
images_dir
input_shape = (72, 72)
# Resizing all the images to same dimension
X_image_test = []
for fname in listdir(images_dir):
fpath = os.path.join(images_dir, fname)
im = Image.open(fpath)
im_resized = im.resize(input_shape)
X_image_test.append(im_resized)
## Converting the image to numpy array
X_image_test_array=[]
for x in range(len(X_image_test)):
X_image=np.array(X_image_test[x],dtype='uint8')
X_image_test_array.append(X_image)
# Checking the size of a single image
X_image_test_array[0].shape
#np.stack(X_image_array)
# Checking the size of a single image
#X_image_array[15].shape我所拥有的图像只是原始图像,没有任何标签,只是训练和测试图像。
# Image folder
images_dir = Path('Training/Images').expanduser()
images_dir
input_shape = (72, 72)
# Resizing all the images to same dimension
X_image_train = []
for fname in listdir(images_dir):
fpath = os.path.join(images_dir, fname)
im = Image.open(fpath)
im_resized = im.resize(input_shape)
X_image_train.append(im_resized)
## Converting the image to numpy array
X_image_train_array=[]
for x in range(len(X_image_train)):
X_image=np.array(X_image_train[x],dtype='uint8')
X_image_train_array.append(X_image)
# Checking the size of a single image
print(X_image_train_array[0].shape)
#print(np.stack(X_image_train_array))
# Checking the size of a single image
print(X_image_train_array[15].shape)import tensorflow as tf
X_image_train_array=tf.convert_to_tensor(X_image_train_array
, dtype=None, dtype_hint=None, name=None
)input_size = (72, 72)
batch_size = 32
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
"Training",
#validation_split=0.2,
#subset="training",
seed=1337,
image_size=input_size,
batch_size=batch_size,
)
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
"Test",
#validation_split=0.2,
#subset="validation",
seed=1337,
image_size=input_size,
batch_size=batch_size,
)learning_rate = 0.001
weight_decay = 0.0001
batch_size = 256
num_epochs = 15
image_size = 72 # We'll resize input images to this size
patch_size = 6 # Size of the patches to be extract from the input images
num_patches = (image_size // patch_size) ** 2
projection_dim = 64
num_heads = 4
transformer_units = [
projection_dim * 2,
projection_dim,
] # Size of the transformer layers
transformer_layers = 8
mlp_head_units = [2048, 1024] # Size of the dense layers of the final classifierdata_augmentation = keras.Sequential(
[
layers.Normalization(),
layers.Resizing(image_size, image_size),
layers.RandomFlip("horizontal"),
layers.RandomRotation(factor=0.02),
layers.RandomZoom(
height_factor=0.2, width_factor=0.2
),
],
name="data_augmentation",
)
# Compute the mean and the variance of the training data for normalization.
data_augmentation.layers[0].adapt(X_image_train_array)def mlp(x, hidden_units, dropout_rate):
for units in hidden_units:
x = layers.Dense(units, activation=tf.nn.gelu)(x)
x = layers.Dropout(dropout_rate)(x)
return xclass Patches(layers.Layer):
def __init__(self, patch_size):
super(Patches, self).__init__()
self.patch_size = patch_size
def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, self.patch_size, 1],
strides=[1, self.patch_size, self.patch_size, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patchesimport matplotlib.pyplot as plt
from tensorflow.python.ops.numpy_ops import np_config
np_config.enable_numpy_behavior()
plt.figure(figsize=(4, 4))
image = X_image_train_array[np.random.choice(range(X_image_train_array.shape[0]))]
plt.imshow(image.astype("uint8"))
plt.axis("off")
resized_image = tf.image.resize(
tf.convert_to_tensor([image]), size=(image_size, image_size)
)
patches = Patches(patch_size)(resized_image)
print(f"Image size: {image_size} X {image_size}")
print(f"Patch size: {patch_size} X {patch_size}")
print(f"Patches per image: {patches.shape[1]}")
print(f"Elements per patch: {patches.shape[-1]}")
n = int(np.sqrt(patches.shape[1]))
plt.figure(figsize=(4, 4))
for i, patch in enumerate(patches[0]):
ax = plt.subplot(n, n, i + 1)
patch_img = tf.reshape(patch, (patch_size, patch_size, 3))
plt.imshow(patch_img.numpy().astype("uint8"))
plt.axis("off")>>>Image size: 72 X 72
Patch size: 6 X 6
Patches per image: 144
Elements per patch: 108class PatchEncoder(layers.Layer):
def __init__(self, num_patches, projection_dim):
super(PatchEncoder, self).__init__()
self.num_patches = num_patches
self.projection = layers.Dense(units=projection_dim)
self.position_embedding = layers.Embedding(
input_dim=num_patches, output_dim=projection_dim
)
def call(self, patch):
positions = tf.range(start=0, limit=self.num_patches, delta=1)
encoded = self.projection(patch) + self.position_embedding(positions)
return encodeddef create_vit_classifier():
inputs = layers.Input(shape=input_shape)
# Augment data.
augmented = data_augmentation(inputs)
# Create patches.
patches = Patches(patch_size)(augmented)
# Encode patches.
encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)
# Create multiple layers of the Transformer block.
for _ in range(transformer_layers):
# Layer normalization 1.
x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
# Create a multi-head attention layer.
attention_output = layers.MultiHeadAttention(
num_heads=num_heads, key_dim=projection_dim, dropout=0.1
)(x1, x1)
# Skip connection 1.
x2 = layers.Add()([attention_output, encoded_patches])
# Layer normalization 2.
x3 = layers.LayerNormalization(epsilon=1e-6)(x2)
# MLP.
x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)
# Skip connection 2.
encoded_patches = layers.Add()([x3, x2])
# Create a [batch_size, projection_dim] tensor.
representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
representation = layers.Flatten()(representation)
representation = layers.Dropout(0.5)(representation)
# Add MLP.
features = mlp(representation, hidden_units=mlp_head_units, dropout_rate=0.5)
# Classify outputs.
logits = layers.Dense(num_classes)(features)
# Create the Keras model.
model = keras.Model(inputs=inputs, outputs=logits)
return modeldef run_experiment(model):
optimizer = tfa.optimizers.AdamW(
learning_rate=learning_rate, weight_decay=weight_decay
)
model.compile(
optimizer=optimizer,
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[
keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
],
)
checkpoint_filepath = "/tmp/checkpoint"
checkpoint_callback = keras.callbacks.ModelCheckpoint(
checkpoint_filepath,
monitor="val_accuracy",
save_best_only=True,
save_weights_only=True,
)
history = model.fit(
x=x_train,
y=y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
callbacks=[checkpoint_callback],
)
model.load_weights(checkpoint_filepath)
_, accuracy, top_5_accuracy = model.evaluate(x_test, y_test)
print(f"Test accuracy: {round(accuracy * 100, 2)}%")
print(f"Test top 5 accuracy: {round(top_5_accuracy * 100, 2)}%")
return history这是我在运行最后一个单元格时遇到的错误。
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1879 try:
-> 1880 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1881 except errors.InvalidArgumentError as e:
InvalidArgumentError: Dimensions must be equal, but are 72 and 3 for '{{node data_augmentation/normalization/sub/sub}} = Sub[T=DT_FLOAT](Placeholder, data_augmentation/normalization/sub/y)' with input shapes: [?,72,72], [1,1,1,3].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\math_ops.py in binary_op_wrapper(x, y)
1377 r_op = getattr(y, "__r%s__" % op_name)
-> 1378 out = r_op(x)
1379 if out is NotImplemented:
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\math_ops.py in r_binary_op_wrapper(y, x)
1399 y, x = maybe_promote_tensors(y, x)
-> 1400 return func(x, y, name=name)
1401
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\math_ops.py in subtract(x, y, name)
547 def subtract(x, y, name=None):
--> 548 return gen_math_ops.sub(x, y, name)
549
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\gen_math_ops.py in sub(x, y, name)
10651 # Add nodes to the TensorFlow graph.
> 10652 _, _, _op, _outputs = _op_def_library._apply_op_helper(
10653 "Sub", x=x, y=y, name=name)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
747 # pylint: disable=protected-access
--> 748 op = g._create_op_internal(op_type_name, inputs, dtypes=None,
749 name=scope, input_types=input_types,
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\func_graph.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
598 captured_inputs.append(inp)
--> 599 return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
600 op_type, captured_inputs, dtypes, input_types, name, attrs, op_def,
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
3560 with self._mutation_lock():
-> 3561 ret = Operation(
3562 node_def,
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
2040 op_def = self._graph._get_op_def(node_def.op)
-> 2041 self._c_op = _create_c_op(self._graph, node_def, inputs,
2042 control_input_ops, op_def)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1882 # Convert to ValueError for backwards compatibility.
-> 1883 raise ValueError(str(e))
1884
ValueError: Dimensions must be equal, but are 72 and 3 for '{{node data_augmentation/normalization/sub/sub}} = Sub[T=DT_FLOAT](Placeholder, data_augmentation/normalization/sub/y)' with input shapes: [?,72,72], [1,1,1,3].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-14-6ee34a4aff67> in <module>
38
39
---> 40 vit_classifier = create_vit_classifier()
41 history = run_experiment(vit_classifier)
<ipython-input-13-a790bd39a000> in create_vit_classifier()
2 inputs = layers.Input(shape=input_shape)
3 # Augment data.
----> 4 augmented = data_augmentation(inputs)
5 # Create patches.
6 patches = Patches(patch_size)(augmented)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
974 # >> model = tf.keras.Model(inputs, outputs)
975 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 976 return self._functional_construction_call(inputs, args, kwargs,
977 input_list)
978
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1112 layer=self, inputs=inputs, build_graph=True, training=training_value):
1113 # Check input assumptions set after layer building, e.g. input shape.
-> 1114 outputs = self._keras_tensor_symbolic_call(
1115 inputs, input_masks, args, kwargs)
1116
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
846 return tf.nest.map_structure(keras_tensor.KerasTensor, output_signature)
847 else:
--> 848 return self._infer_output_signature(inputs, args, kwargs, input_masks)
849
850 def _infer_output_signature(self, inputs, args, kwargs, input_masks):
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
886 self._maybe_build(inputs)
887 inputs = self._maybe_cast_inputs(inputs)
--> 888 outputs = call_fn(inputs, *args, **kwargs)
889
890 self._handle_activity_regularization(inputs, outputs)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\sequential.py in call(self, inputs, training, mask)
381 kwargs['training'] = training
382
--> 383 outputs = layer(inputs, **kwargs)
384
385 if len(tf.nest.flatten(outputs)) != 1:
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
1035 with autocast_variable.enable_auto_cast_variables(
1036 self._compute_dtype_object):
-> 1037 outputs = call_fn(inputs, *args, **kwargs)
1038
1039 if self._activity_regularizer:
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\layers\preprocessing\normalization.py in call(self, inputs)
250 # explicitly cast here to also allow integer inputs to be passed
251 inputs = tf.cast(inputs, self.compute_dtype)
--> 252 return ((inputs - self.mean) /
253 tf.maximum(tf.sqrt(self.variance), backend.epsilon()))
254
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\math_ops.py in binary_op_wrapper(x, y)
1381 return out
1382 except (TypeError, ValueError):
-> 1383 raise e
1384 else:
1385 raise
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\math_ops.py in binary_op_wrapper(x, y)
1365 # r_binary_op_wrapper use different force_same_dtype values.
1366 x, y = maybe_promote_tensors(x, y, force_same_dtype=False)
-> 1367 return func(x, y, name=name)
1368 except (TypeError, ValueError) as e:
1369 # Even if dispatching the op failed, the RHS may be a tensor aware
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\util\dispatch.py in 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
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\math_ops.py in subtract(x, y, name)
546 @dispatch.add_dispatch_support
547 def subtract(x, y, name=None):
--> 548 return gen_math_ops.sub(x, y, name)
549
550
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\ops\gen_math_ops.py in sub(x, y, name)
10650 pass # Add nodes to the TensorFlow graph.
10651 # Add nodes to the TensorFlow graph.
> 10652 _, _, _op, _outputs = _op_def_library._apply_op_helper(
10653 "Sub", x=x, y=y, name=name)
10654 _result = _outputs[:]
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
746 # Add Op to graph
747 # pylint: disable=protected-access
--> 748 op = g._create_op_internal(op_type_name, inputs, dtypes=None,
749 name=scope, input_types=input_types,
750 attrs=attr_protos, op_def=op_def)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\func_graph.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
597 inp = self.capture(inp)
598 captured_inputs.append(inp)
--> 599 return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
600 op_type, captured_inputs, dtypes, input_types, name, attrs, op_def,
601 compute_device)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
3559 # Session.run call cannot occur between creating and mutating the op.
3560 with self._mutation_lock():
-> 3561 ret = Operation(
3562 node_def,
3563 self,
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
2039 if op_def is None:
2040 op_def = self._graph._get_op_def(node_def.op)
-> 2041 self._c_op = _create_c_op(self._graph, node_def, inputs,
2042 control_input_ops, op_def)
2043 name = compat.as_str(node_def.name)
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1881 except errors.InvalidArgumentError as e:
1882 # Convert to ValueError for backwards compatibility.
-> 1883 raise ValueError(str(e))
1884
1885 return c_op
ValueError: Dimensions must be equal, but are 72 and 3 for '{{node data_augmentation/normalization/sub}} = Sub[T=DT_FLOAT](Placeholder, data_augmentation/normalization/sub/y)' with input shapes: [?,72,72], [1,1,1,3].
发布于 2022-01-16 22:21:58
在create_vit_classifier()中,inputs = layers.Input(shape=input_shape)的输出是KerasTensor(type_spec=TensorSpec(shape=(None, 72, 72), dtype=tf.float32, name='input_12'), name='input_12', description="created by layer 'input_12'")
当它到达augmented = data_augmentation(inputs)时,它会给出以下错误:
ValueError: Exception encountered when calling layer "normalization_2" (type Normalization).
Dimensions must be equal, but are 72 and 4 for '{{node data_augmentation/normalization_2/sub}} = Sub[T=DT_FLOAT](Placeholder, data_augmentation/normalization_2/sub/y)' with input shapes: [?,72,72], [1,1,1,4].
Call arguments received:
• inputs=tf.Tensor(shape=(None, 72, 72), dtype=float32)现在,在前面的一行中:patch_img = tf.reshape(patch, (patch_size, patch_size, 3)),我必须将3改为4,否则我会得到错误:
InvalidArgumentError: Input to reshape is a tensor with 144 values, but the requested shape has 108 [Op:Reshape]这提供了以下产出:
Image size: 72 X 72
Patch size: 6 X 6
Patches per image: 144
Elements per patch: 144为了那个特殊的细胞。
回到create_vit_classifier()..。在这一行中添加72条:augmented = data_augmentation(72, inputs)将让我一路走到logits。
我知道错误:
33 # Classify outputs.
---> 34 logits = layers.Dense(num_classes)(features)
35 # Create the Keras model.
36 model = keras.Model(inputs=inputs, outputs=logits)
NameError: name 'num_classes' is not defined因此,您的num_classes没有定义,问题在create_vit_classifier()中
编辑:
我将num_classes初始化为14 (这是前面的输出),我可以使用create_vit_classifier()创建模型,但由于run_experiment()中的NameError: name 'x_train' is not defined失败了
https://stackoverflow.com/questions/70731155
复制相似问题