Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
Give me some code examples for image classification with TensorFlow
- User
Give me some code examples for image classification with TensorFlow
- Assistant
Sure! Here is some sample code for image classification using TensorFlow 2.x:
code# Import TensorFlow import tensorflow as tf # Load the dataset (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # Normalize the inputs x_train = x_train / 255.0 x_test = x_test / 255.0 # Reshape the inputs x_train = x_train.reshape(-1, 28, 28, 1) x_test = x_test.reshape(-1, 28, 28, 1) # Build the model model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation='softmax') ]) # Compile the model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train the model model.fit(x_train, y_train, epochs=10) # Evaluate the model model.evaluate(x_test, y_test)This code trains a convolutional neural network on the MNIST dataset for handwritten digit classification. The model consists of a series of convolutional and pooling layers, followed by a fully-connected layer and a softmax output layer. The model is trained using the Adam optimizer and the Sparse Categorical Crossentropy loss function.