Skip to content

Commit dfe30b2

Browse files
committed
edit image classifier tutorial to use tensorflow_datasets API
1 parent d14109e commit dfe30b2

File tree

3 files changed

+70
-47
lines changed

3 files changed

+70
-47
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
keras
21
matplotlib
32
numpy
4-
tensorflow==1.15.2
3+
tensorflow==2.0.0
4+
tensorflow_datasets

machine-learning/image-classifier/test.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from train import load_data
2-
from keras.models import load_model
1+
from train import load_data, batch_size
2+
from tensorflow.keras.models import load_model
33
import matplotlib.pyplot as plt
44
import numpy as np
55

@@ -18,20 +18,23 @@
1818
}
1919

2020
# load the testing set
21-
(_, _), (X_test, y_test) = load_data()
22-
# load the model with optimal weights
23-
model = load_model("results/cifar10-loss-0.58-acc-0.81.h5")
21+
# (_, _), (X_test, y_test) = load_data()
22+
ds_train, ds_test, info = load_data()
23+
# load the model with final model weights
24+
model = load_model("results/cifar10-model-v1.h5")
2425
# evaluation
25-
loss, accuracy = model.evaluate(X_test, y_test)
26+
loss, accuracy = model.evaluate(ds_test, steps=info.splits["test"].num_examples // batch_size)
2627
print("Test accuracy:", accuracy*100, "%")
2728

2829
# get prediction for this image
29-
sample_image = X_test[7500]
30+
data_sample = next(iter(ds_test))
31+
sample_image = data_sample[0].numpy()[0]
32+
sample_label = categories[data_sample[1].numpy()[0]]
3033
prediction = np.argmax(model.predict(sample_image.reshape(-1, *sample_image.shape))[0])
31-
print(categories[prediction])
34+
print("Predicted label:", categories[prediction])
35+
print("True label:", sample_label)
3236

3337
# show the first image
3438
plt.axis('off')
3539
plt.imshow(sample_image)
36-
plt.savefig("frog.png")
3740
plt.show()

machine-learning/image-classifier/train.py

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from keras.datasets import cifar10 # importing the dataset from keras
2-
from keras.models import Sequential
3-
from keras.layers import Dense, Dropout, Activation, Flatten
4-
from keras.layers import Conv2D, MaxPooling2D
5-
from keras.callbacks import ModelCheckpoint, TensorBoard
6-
from keras.utils import to_categorical
1+
from tensorflow.keras.models import Sequential
2+
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
3+
from tensorflow.keras.layers import Conv2D, MaxPooling2D
4+
from tensorflow.keras.callbacks import TensorBoard
5+
import tensorflow as tf
6+
import tensorflow_datasets as tfds
77
import os
88

99
# hyper-parameters
@@ -79,56 +79,76 @@ def create_model(input_shape):
7979
# print the summary of the model architecture
8080
model.summary()
8181

82-
# training the model using rmsprop optimizer
83-
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
82+
# training the model using adam optimizer
83+
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
8484
return model
8585

8686

8787
def load_data():
8888
"""
89-
This function loads CIFAR-10 dataset, normalized, and labels one-hot encoded
89+
This function loads CIFAR-10 dataset, and preprocess it
9090
"""
91+
# Loading data using Keras
9192
# loading the CIFAR-10 dataset, splitted between train and test sets
92-
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
93-
print("Training samples:", X_train.shape[0])
94-
print("Testing samples:", X_test.shape[0])
95-
print(f"Images shape: {X_train.shape[1:]}")
96-
97-
# converting image labels to binary class matrices
98-
y_train = to_categorical(y_train, num_classes)
99-
y_test = to_categorical(y_test, num_classes)
100-
101-
# convert to floats instead of int, so we can divide by 255
102-
X_train = X_train.astype("float32")
103-
X_test = X_test.astype("float32")
104-
X_train /= 255
105-
X_test /= 255
93+
# (X_train, y_train), (X_test, y_test) = cifar10.load_data()
94+
# print("Training samples:", X_train.shape[0])
95+
# print("Testing samples:", X_test.shape[0])
96+
# print(f"Images shape: {X_train.shape[1:]}")
97+
98+
# # converting image labels to binary class matrices
99+
# y_train = to_categorical(y_train, num_classes)
100+
# y_test = to_categorical(y_test, num_classes)
101+
102+
# # convert to floats instead of int, so we can divide by 255
103+
# X_train = X_train.astype("float32")
104+
# X_test = X_test.astype("float32")
105+
# X_train /= 255
106+
# X_test /= 255
107+
# return (X_train, y_train), (X_test, y_test)
108+
# Loading data using Tensorflow Datasets
109+
def preprocess_image(image, label):
110+
# convert [0, 255] range integers to [0, 1] range floats
111+
image = tf.image.convert_image_dtype(image, tf.float32)
112+
return image, label
113+
# loading the CIFAR-10 dataset, splitted between train and test sets
114+
ds_train, info = tfds.load("cifar10", with_info=True, split="train", as_supervised=True)
115+
ds_test = tfds.load("cifar10", split="test", as_supervised=True)
116+
# repeat dataset forever, shuffle, preprocess, split by batch
117+
ds_train = ds_train.repeat().shuffle(1024).map(preprocess_image).batch(batch_size)
118+
ds_test = ds_test.repeat().shuffle(1024).map(preprocess_image).batch(batch_size)
119+
return ds_train, ds_test, info
106120

107-
return (X_train, y_train), (X_test, y_test)
108121

109122

110123
if __name__ == "__main__":
111124

112125
# load the data
113-
(X_train, y_train), (X_test, y_test) = load_data()
126+
ds_train, ds_test, info = load_data()
127+
# (X_train, y_train), (X_test, y_test) = load_data()
114128

115129
# constructs the model
116-
model = create_model(input_shape=X_train.shape[1:])
130+
# model = create_model(input_shape=X_train.shape[1:])
131+
model = create_model(input_shape=info.features["image"].shape)
117132

118133
# some nice callbacks
119-
tensorboard = TensorBoard(log_dir="logs/cifar10-model-v1")
120-
checkpoint = ModelCheckpoint("results/cifar10-loss-{val_loss:.2f}-acc-{val_acc:.2f}.h5",
121-
save_best_only=True,
122-
verbose=1)
134+
logdir = os.path.join("logs", "cifar10-model-v1")
135+
tensorboard = TensorBoard(log_dir=logdir)
123136

124137
# make sure results folder exist
125138
if not os.path.isdir("results"):
126139
os.mkdir("results")
127140

128141
# train
129-
model.fit(X_train, y_train,
130-
batch_size=batch_size,
131-
epochs=epochs,
132-
validation_data=(X_test, y_test),
133-
callbacks=[tensorboard, checkpoint],
134-
shuffle=True)
142+
# model.fit(X_train, y_train,
143+
# batch_size=batch_size,
144+
# epochs=epochs,
145+
# validation_data=(X_test, y_test),
146+
# callbacks=[tensorboard, checkpoint],
147+
# shuffle=True)
148+
model.fit(ds_train, epochs=epochs, validation_data=ds_test, verbose=1,
149+
steps_per_epoch=info.splits["train"].num_examples // batch_size,
150+
validation_steps=info.splits["test"].num_examples // batch_size,
151+
callbacks=[tensorboard])
152+
153+
# save the model to disk
154+
model.save("results/cifar10-model-v1.h5")

0 commit comments

Comments
 (0)