Skip to content

Commit 1989dd2

Browse files
author
Lazy Programmer
committed
cnn2
1 parent 53c89dd commit 1989dd2

24 files changed

+2164
-0
lines changed

cnn_class2/content/elephant.jpg

21.6 KB
Loading

cnn_class2/content/sydney.jpg

79.3 KB
Loading

cnn_class2/extra_reading.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
A Neural Algorithm of Artistic Style
2+
https://arxiv.org/abs/1508.06576
3+
4+
SSD: Single Shot MultiBox Detector
5+
https://arxiv.org/abs/1512.02325
6+
7+
Very Deep Convolutional Networks for Large-Scale Image Recognition (VGG)
8+
https://arxiv.org/abs/1409.1556
9+
10+
Deep Residual Learning for Image Recognition
11+
https://arxiv.org/abs/1512.03385
12+
13+
Going Deeper with Convolutions (Inception)
14+
https://arxiv.org/abs/1409.4842

cnn_class2/fashion.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# https://deeplearningcourses.com/advanced-computer-vision
2+
# https://www.udemy.com/advanced-computer-vision
3+
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
from keras.models import Sequential
10+
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
11+
12+
import matplotlib.pyplot as plt
13+
import pandas as pd
14+
import numpy as np
15+
16+
17+
# helper
18+
def y2indicator(Y):
19+
N = len(Y)
20+
K = len(set(Y))
21+
I = np.zeros((N, K))
22+
I[np.arange(N), Y] = 1
23+
return I
24+
25+
26+
# get the data
27+
# https://www.kaggle.com/zalando-research/fashionmnist
28+
data = pd.read_csv('../large_files/fashionmnist/fashion-mnist_train.csv')
29+
data = data.as_matrix()
30+
np.random.shuffle(data)
31+
32+
X = data[:, 1:].reshape(-1, 28, 28, 1) / 255.0
33+
Y = data[:, 0].astype(np.int32)
34+
35+
# get shapes
36+
# N = len(Y)
37+
K = len(set(Y))
38+
39+
# by default Keras wants one-hot encoded labels
40+
# there's another cost function we can use
41+
# where we can just pass in the integer labels directly
42+
# just like Tensorflow / Theano
43+
Y = y2indicator(Y)
44+
45+
46+
# the model will be a sequence of layers
47+
model = Sequential()
48+
49+
50+
# make the CNN
51+
# model.add(Input(shape=(28, 28, 1)))
52+
model.add(Conv2D(input_shape=(28, 28, 1), filters=32, kernel_size=(3, 3)))
53+
model.add(BatchNormalization())
54+
model.add(Activation('relu'))
55+
model.add(MaxPooling2D())
56+
57+
model.add(Conv2D(filters=64, kernel_size=(3, 3)))
58+
model.add(BatchNormalization())
59+
model.add(Activation('relu'))
60+
model.add(MaxPooling2D())
61+
62+
model.add(Conv2D(filters=128, kernel_size=(3, 3)))
63+
model.add(BatchNormalization())
64+
model.add(Activation('relu'))
65+
model.add(MaxPooling2D())
66+
67+
model.add(Flatten())
68+
model.add(Dense(units=300))
69+
model.add(Activation('relu'))
70+
model.add(Dropout(0.2))
71+
model.add(Dense(units=K))
72+
model.add(Activation('softmax'))
73+
74+
75+
# list of losses: https://keras.io/losses/
76+
# list of optimizers: https://keras.io/optimizers/
77+
# list of metrics: https://keras.io/metrics/
78+
model.compile(
79+
loss='categorical_crossentropy',
80+
optimizer='adam',
81+
metrics=['accuracy']
82+
)
83+
84+
# note: multiple ways to choose a backend
85+
# either theano, tensorflow, or cntk
86+
# https://keras.io/backend/
87+
88+
89+
# gives us back a <keras.callbacks.History object at 0x112e61a90>
90+
r = model.fit(X, Y, validation_split=0.33, epochs=15, batch_size=32)
91+
print("Returned:", r)
92+
93+
# print the available keys
94+
# should see: dict_keys(['val_loss', 'acc', 'loss', 'val_acc'])
95+
print(r.history.keys())
96+
97+
# plot some data
98+
plt.plot(r.history['loss'], label='loss')
99+
plt.plot(r.history['val_loss'], label='val_loss')
100+
plt.legend()
101+
plt.show()
102+
103+
# accuracies
104+
plt.plot(r.history['acc'], label='acc')
105+
plt.plot(r.history['val_acc'], label='val_acc')
106+
plt.legend()
107+
plt.show()
108+
109+

cnn_class2/fashion2.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# https://deeplearningcourses.com/advanced-computer-vision
2+
# https://www.udemy.com/advanced-computer-vision
3+
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
from keras.models import Sequential, Model
10+
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization, Input
11+
12+
import matplotlib.pyplot as plt
13+
import pandas as pd
14+
import numpy as np
15+
16+
17+
# helper
18+
def y2indicator(Y):
19+
N = len(Y)
20+
K = len(set(Y))
21+
I = np.zeros((N, K))
22+
I[np.arange(N), Y] = 1
23+
return I
24+
25+
26+
# get the data
27+
# https://www.kaggle.com/zalando-research/fashionmnist
28+
data = pd.read_csv('../large_files/fashionmnist/fashion-mnist_train.csv')
29+
data = data.as_matrix()
30+
np.random.shuffle(data)
31+
32+
X = data[:, 1:].reshape(-1, 28, 28, 1) / 255.0
33+
Y = data[:, 0].astype(np.int32)
34+
35+
# get shapes
36+
# N = len(Y)
37+
K = len(set(Y))
38+
39+
# by default Keras wants one-hot encoded labels
40+
# there's another cost function we can use
41+
# where we can just pass in the integer labels directly
42+
# just like Tensorflow / Theano
43+
Y = y2indicator(Y)
44+
45+
46+
47+
48+
# make the CNN
49+
i = Input(shape=(28, 28, 1))
50+
x = Conv2D(filters=32, kernel_size=(3, 3))(i)
51+
x = BatchNormalization()(x)
52+
x = Activation('relu')(x)
53+
x = MaxPooling2D()(x)
54+
55+
x = Conv2D(filters=64, kernel_size=(3, 3))(x)
56+
x = BatchNormalization()(x)
57+
x = Activation('relu')(x)
58+
x = MaxPooling2D()(x)
59+
60+
x = Flatten()(x)
61+
x = Dense(units=100)(x)
62+
x = Activation('relu')(x)
63+
x = Dropout(0.3)(x)
64+
x = Dense(units=K)(x)
65+
x = Activation('softmax')(x)
66+
67+
model = Model(inputs=i, outputs=x)
68+
69+
70+
# list of losses: https://keras.io/losses/
71+
# list of optimizers: https://keras.io/optimizers/
72+
# list of metrics: https://keras.io/metrics/
73+
model.compile(
74+
loss='categorical_crossentropy',
75+
optimizer='adam',
76+
metrics=['accuracy']
77+
)
78+
79+
# note: multiple ways to choose a backend
80+
# either theano, tensorflow, or cntk
81+
# https://keras.io/backend/
82+
83+
84+
# gives us back a <keras.callbacks.History object at 0x112e61a90>
85+
r = model.fit(X, Y, validation_split=0.33, epochs=15, batch_size=32)
86+
print("Returned:", r)
87+
88+
# print the available keys
89+
# should see: dict_keys(['val_loss', 'acc', 'loss', 'val_acc'])
90+
print(r.history.keys())
91+
92+
# plot some data
93+
plt.plot(r.history['loss'], label='loss')
94+
plt.plot(r.history['val_loss'], label='val_loss')
95+
plt.legend()
96+
plt.show()
97+
98+
# accuracies
99+
plt.plot(r.history['acc'], label='acc')
100+
plt.plot(r.history['val_acc'], label='val_acc')
101+
plt.legend()
102+
plt.show()
103+
104+

cnn_class2/make_limited_datasets.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# https://deeplearningcourses.com/advanced-computer-vision
2+
# https://www.udemy.com/advanced-computer-vision
3+
import os
4+
5+
def mkdir(p):
6+
if not os.path.exists(p):
7+
os.mkdir(p)
8+
9+
def link(src, dst):
10+
if not os.path.exists(dst):
11+
os.symlink(src, dst, target_is_directory=True)
12+
13+
mkdir('../large_files/fruits-360-small')
14+
15+
16+
classes = [
17+
'Apple Golden 1',
18+
'Avocado',
19+
'Lemon',
20+
'Mango',
21+
'Kiwi',
22+
'Banana',
23+
'Strawberry',
24+
'Raspberry'
25+
]
26+
27+
train_path_from = os.path.abspath('../large_files/fruits-360/Training')
28+
valid_path_from = os.path.abspath('../large_files/fruits-360/Validation')
29+
30+
train_path_to = os.path.abspath('../large_files/fruits-360-small/Training')
31+
valid_path_to = os.path.abspath('../large_files/fruits-360-small/Validation')
32+
33+
mkdir(train_path_to)
34+
mkdir(valid_path_to)
35+
36+
37+
for c in classes:
38+
link(train_path_from + '/' + c, train_path_to + '/' + c)
39+
link(valid_path_from + '/' + c, valid_path_to + '/' + c)

0 commit comments

Comments
 (0)