-
Notifications
You must be signed in to change notification settings - Fork 30
/
deep learning using convolution neural networks.txt
138 lines (70 loc) · 3.3 KB
/
deep learning using convolution neural networks.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
cnn uses convolution that recognizes images
input-> convolution + relu -> pooling -> convolution + relu _> pooling -> flatten -> fully connected-> softmax -> bicycle
input_shape (width, height, channels)
input_shape (28,28,3)
width=28 and height=28 and three channels
from keras.layers import Dense, Conv2D, Flatten
model=Sequential()
#black and white
model.add(Conv2D(filters=32, kernel_size=3, input_shape(28,28,1),
activation='relu'))
model.add(Conv2D(8, kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
img=image.load_img(img_path, target_size=(244,244))
#turn it into an array
img= image.img_to_array(img)
#expand the dimensions (224,224,3) into (1,244,224,3)
img= np.expand_dims(img,axis=0)
img=preprocess_input(img)
from keras.applications.resnet50 import ResNet50, decode_predictions
model=ResNet50(weights='imagenet')
preds=model.predict(img)
print('Predicted:', decode_predictions(preds, top=1)[0])
>>> Convolution networks
# Import the Conv2D and Flatten layers and instantiate model
from keras.models import Sequential
from keras.layers import Conv2D,Flatten
#create the model
model = Sequential()
# Add a convolutional layer of 32 filters of size 28 pixels by 28 pixels with a 3x3 filter matrix
model.add(Conv2D(32, input_shape=(28, 28, 1), kernel_size=3, activation='relu'))
# Add a convolutional layer of 16 filters of size 3x3
model.add(Conv2D(16, kernel_size=3, activation='relu'))
# Flatten the previous layer output
model.add(Flatten())
# Add as many outputs as classes with softmax activation
model.add(Dense(10, activation='softmax'))
# Obtain a reference to the outputs of the first layer
layer_output = model.layers[0].output
# Build a model using the model's input and the first layer output
first_layer_model = Model(inputs = model.input, outputs = layer_output)
# Use this model to predict on X_test
activations = first_layer_model.predict(X_test)
# Plot the activations of first digit of X_test for the 15th filter
axs[0].matshow(activations[0,:,:,14], cmap = 'viridis')
# Do the same but for the 18th filter now
axs[1].matshow(activations[0,:,:,17], cmap = 'viridis')
plt.show()
#model.compile(optimizer='adam', loss='categorical_crossentropy',
#metrics=['accuracy'])
#model.fit(X_train, y_train, validation_data=(X_test,y_test),epochs=3)
# Import image and preprocess_input
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
# Load the image with the right target size for your model
img = image.load_img(img_path, target_size=(224,224))
# Turn it into an array
img_array = image.img_to_array(img)
# Expand the dimensions of the image
img_expanded = np.expand_dims(img_array, axis = 0)
# Pre-process the img in the same way original images were
img_ready = preprocess_input(img_expanded)
# Instantiate a ResNet50 model with 'imagenet' weights
model=ResNet50(weights='imagenet')
# Predict with ResNet50 on your already processed img
preds = model.predict(img_ready)
# Decode the first 3 predictions
print('Predicted:', decode_predictions(preds, top=3)[0])