This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.fit(train_dataset, epochs=5, validation_data=val_dataset) | |
model.evaluate(test_dataset, verbose=2) | |
# Output | |
# loss: 1.0985 - accuracy: 0.6060 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.keras import layers, models, losses | |
def create_model(): | |
model = models.Sequential([ | |
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), | |
layers.MaxPooling2D(2, 2), | |
layers.Conv2D(64, (3, 3), activation='relu'), | |
layers.MaxPooling2D(2, 2), | |
layers.Conv2D(64, (3, 3), activation='relu'), | |
layers.Flatten(), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract(example): | |
data = tf.io.parse_example( | |
example, | |
# Schema of the example. | |
{ | |
'image': tf.io.FixedLenFeature(shape=(32, 32, 3), dtype=tf.float32), | |
'label': tf.io.FixedLenFeature(shape=(), dtype=tf.int64) | |
} | |
) | |
return data['image'], data['label'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] | |
dataset = tf.data.TFRecordDataset([GCS_PATH_FOR_DATA + 'train.tfrecord']) | |
plt.figure(figsize=(10, 10)) | |
for i, example in enumerate(dataset.take(16)): | |
data = tf.train.Example() | |
data.ParseFromString(example.numpy()) | |
image = tf.constant(data.features.feature['image'].float_list.value, shape=[32, 32, 3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
(train_images, train_labels), (test_images, test_labels) = tf.keras.dataset.cifar10.load_data() | |
def preprocess(filename, images, labels): | |
with tf.io.TFRecordWriter(filename) as writer: | |
for image, label in zip(images, labels): | |
# Encode the image and label in tf.train.Example. | |
feature = { | |
# Normalize the image to range [0, 1]. |
NewerOlder