CIFAR-10 Image Classification with CNN in TensorFlow
To load the CIFAR-10 dataset, you can use the tf.keras.datasets.cifar10.load_data() function from TensorFlow. Then, you can build a CNN network with convolution layers using the Keras API. Here's an example code to accomplish this:
import tensorflow as tf
from tensorflow import keras
# Load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
x_train = x_train / 255.0
x_test = x_test / 255.0
# Build the CNN model
model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.Flatten(),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('
Test accuracy:', test_acc)
You can tune the hyperparameters such as the number of filters, filter sizes, and number of layers to improve the accuracy. You can also try changing the optimizer or adding regularization techniques like dropout or batch normalization layers. Experimenting with different hyperparameter settings and architectures can help you find the best accuracy for your classification task.
原文地址: https://www.cveoy.top/t/topic/h471 著作权归作者所有。请勿转载和采集!