Advertisement
CodeCrusader

AI Image Classifier

Jun 4th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | Software | 0 0
  1. import tensorflow as tf
  2. from tensorflow.keras import layers
  3.  
  4. # Load the dataset
  5. (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
  6.  
  7. # Preprocess the data
  8. train_images = train_images.reshape((60000, 28, 28, 1))
  9. train_images = train_images.astype('float32') / 255
  10.  
  11. test_images = test_images.reshape((10000, 28, 28, 1))
  12. test_images = test_images.astype('float32') / 255
  13.  
  14. # Define the model
  15. model = tf.keras.Sequential([
  16.     layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
  17.     layers.MaxPooling2D((2, 2)),
  18.     layers.Conv2D(64, (3, 3), activation='relu'),
  19.     layers.MaxPooling2D((2, 2)),
  20.     layers.Conv2D(64, (3, 3), activation='relu'),
  21.     layers.Flatten(),
  22.     layers.Dense(64, activation='relu'),
  23.     layers.Dense(10)
  24. ])
  25.  
  26. # Compile the model
  27. model.compile(optimizer='adam',
  28.               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
  29.               metrics=['accuracy'])
  30.  
  31. # Train the model
  32. history = model.fit(train_images, train_labels, epochs=10,
  33.                     validation_data=(test_images, test_labels))
  34.  
  35. # Evaluate the model
  36. test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
  37. print('Test accuracy:', test_acc)
  38.  
  39.  
  40. # THIS CODE TAKES AN IMAGE - GUESSES WHAT IT IS FROM A DATASET OF OVER 60,000+ LABELED IMAGES LIKE AIRPLANE, BIRD, HOUSE etc. AND CLASSIFIES YOUR IMAGE.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement