| | |
| | """Cat&Dogs.ipynb |
| | |
| | Automatically generated by Colaboratory. |
| | |
| | Original file is located at |
| | https://colab.research.google.com/drive/17AFfKN67SFvxF7FdjjugeJGIa000SGU8 |
| | """ |
| |
|
| | import tensorflow as tf |
| |
|
| | |
| | (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() |
| |
|
| | |
| | x_train = tf.keras.utils.normalize(x_train, axis=1) |
| | x_test = tf.keras.utils.normalize(x_test, axis=1) |
| |
|
| | |
| | model = tf.keras.models.Sequential() |
| | model.add(tf.keras.layers.Flatten()) |
| | model.add(tf.keras.layers.Dense(128, activation=tf.nn.leaky_relu)) |
| | model.add(tf.keras.layers.Dense(128, activation=tf.nn.leaky_relu)) |
| | model.add(tf.keras.layers.Dense(10, activation=tf.nn.sigmoid)) |
| |
|
| | |
| | model.compile(optimizer='adam', |
| | loss='sparse_categorical_crossentropy', |
| | metrics=['accuracy']) |
| |
|
| | |
| | model.fit(x_train, y_train, epochs=100) |
| |
|
| | |
| | val_loss, val_acc = model.evaluate(x_test, y_test) |
| | print(val_loss) |
| | print(val_acc) |
| |
|
| | |
| | predictions = model.predict(x_test) |
| |
|
| | import matplotlib.pyplot as plt |
| | import numpy as np |
| |
|
| | |
| | indices = np.random.randint(0, len(x_test), size=1) |
| | images = x_test[indices] |
| |
|
| | |
| | predictions = model.predict(images) |
| |
|
| | |
| | for i, (image, prediction) in enumerate(zip(images, predictions)): |
| | |
| | image = np.uint8(image * 255).reshape(32, 32, 3) |
| |
|
| | |
| | label = np.argmax(prediction) |
| | probability = prediction[label] |
| |
|
| | |
| | plt.subplot(1, 5, i + 1) |
| | plt.imshow(image) |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | plt.title("Prediction: {} ({:.2f})".format(label, probability)) |
| |
|
| | plt.show() |
| |
|
| |
|