Skip to content

Commit e6fb277

Browse files
committed
Training a CNN with CIFAR-10 dataset
1 parent 365f3d7 commit e6fb277

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Learner: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
#
4+
# Topic: Deep Learning: Convolutional Neural Networks (CNNs)
5+
# A CNN that trains on Keras's CIFAR-10 images dataset
6+
7+
import numpy as np
8+
9+
from keras.datasets import cifar10
10+
from keras.models import Sequential
11+
from keras.layers import BatchNormalization, Conv2D, Dense, Dropout, Flatten
12+
from keras.optimizers import Adam
13+
from keras.utils import to_categorical
14+
15+
from ml.deep_learning.neural_network_keras.decision_boundaries import decision_boundary_2dimensional as boundary
16+
17+
# Hyperparameters we can adjust
18+
DROPOUT_PROBABILITY = 0.5
19+
20+
(X_train_raw, Y_train_raw), (X_test_raw, Y_test_raw) = cifar10.load_data()
21+
22+
X_train = X_train_raw / 255
23+
X_test_all = X_test_raw / 255
24+
X_validation, X_test = np.split(X_test_all, 2)
25+
Y_train_encoded = to_categorical(Y_train_raw)
26+
Y_validation_encoded, Y_test = np.split(to_categorical(Y_test_raw), 2)
27+
28+
# Create a sequential model
29+
model = Sequential()
30+
31+
# Convolutional layers
32+
model.add(Conv2D(16, (3, 3), activation='relu'))
33+
model.add(BatchNormalization()) # To improve accuracy
34+
model.add(Dropout(DROPOUT_PROBABILITY)) # To reduce over-fitting
35+
36+
model.add(Conv2D(32, (3, 3), activation='relu'))
37+
model.add(BatchNormalization())
38+
model.add(Dropout(DROPOUT_PROBABILITY))
39+
40+
# Four-dimensional tensor => bi-dimensional matrix of flat data.
41+
model.add(Flatten())
42+
43+
# Fully connected layers - Dense
44+
model.add(Dense(1000, activation='relu'))
45+
model.add(BatchNormalization())
46+
model.add(Dropout(DROPOUT_PROBABILITY))
47+
48+
model.add(Dense(512, activation='relu'))
49+
model.add(BatchNormalization())
50+
model.add(Dropout(DROPOUT_PROBABILITY))
51+
52+
model.add(Dense(10, activation='softmax'))
53+
54+
# Compile a model
55+
model.compile(
56+
loss='categorical_crossentropy',
57+
optimizer=Adam(),
58+
metrics=['accuracy']
59+
)
60+
61+
# Train a network
62+
history = model.fit(
63+
X_train,
64+
Y_train_encoded,
65+
validation_data=(X_validation, Y_validation_encoded),
66+
epochs=20,
67+
batch_size=32
68+
)
69+
70+
# Draw a decision boundary
71+
boundary.plot(history)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Learner: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
#
4+
# Topic: Deep Learning: Convolutional Neural Networks (CNNs)
5+
# The Keras's CIFAR-10 images dataset
6+
# Prints random sample images this dataset.
7+
8+
from keras.datasets import cifar10
9+
import matplotlib.pyplot as plt
10+
import random
11+
12+
# Load CIFAR-10
13+
(X, Y), (_, _) = cifar10.load_data()
14+
15+
# Print a 4x10 grid of images
16+
ROWS = 4
17+
COLUMNS = 10
18+
for i in range(ROWS * COLUMNS):
19+
ax = plt.subplot(ROWS, COLUMNS, i + 1) # Get the next cell in the grid
20+
ax.axis('off') # Remove ticks on axes
21+
idx = random.randint(0, X.shape[0]) # Select a random image
22+
ax.set_title(Y[idx][0], fontsize=15) # Print the image's label
23+
ax.imshow(X[idx]) # Show the image
24+
plt.show()
217 KB
Loading

ml/deep_learning/keras_functional_api/converting_keras_inceptionv4_model_coreml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from ml.deep_learning.keras_functional_api.inception_v4_network import model
1111

12+
# https://drive.google.com/file/d/1NqMs2js-uOyOLL9MB0nzX0AoHx-hy8ty/view?usp=share_link
1213
weights_path = 'inceptionv4_dogscats_weights.h5'
1314
model.load_weights(weights_path)
1415

0 commit comments

Comments
 (0)