-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn-y-network-2.1.2.py
158 lines (93 loc) · 2.77 KB
/
cnn-y-network-2.1.2.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense,Activation,Dropout,Conv2D,MaxPool2D
from tensorflow.keras.layers import Flatten,concatenate,Input
from tensorflow.keras.utils import to_categorical,plot_model
from tensorflow.keras.datasets import mnist
import graphviz
import pydot
# In[2]:
# load datasets (mnist)
(x_train,y_train),(x_test,y_test)=mnist.load_data()
# In[3]:
#compute the number of labels
nu_labels=len(np.unique(y_train))
# In[4]:
#convert into one_hot vector
y_train=to_categorical(y_train)
y_test=to_categorical(y_test)
# In[5]:
# input image dimensions
img_size=x_train.shape[1]
# In[6]:
# resize and normalize
x_train=np.reshape(x_train,[-1,img_size,img_size,1])
x_test=np.reshape(x_test,[-1,img_size,img_size,1])
x_train=x_train.astype('float32')/255
x_test=x_test.astype('float32')/255
# In[7]:
# network parameters
input_shape=(img_size,img_size,1)
batch_size=32
kernel=3
droupout=.4
num_filters=32
# In[8]:
# left branch of Y network
left_inputs=Input(shape=input_shape)
x=left_inputs
filters=num_filters
# 3 layers of Conv2D-Dropout-MaxPooling2D
# number of filters doubles after each layer (32-64-128)
for i in range(3):
x=Conv2D(filters=filters,kernel_size=kernel,
padding='same',activation='relu')(x)
x=Dropout(droupout)(x)
x=MaxPool2D()(x)
filters*=2
# In[9]:
# right branch of Y network
right_inputs=Input(shape=input_shape)
y=right_inputs
filters=num_filters
# 3 layers of Conv2D-Dropout-MaxPooling2D
# number of filters doubles after each layer (32-64-128)
for i in range(3):
y=Conv2D(filters=filters,kernel_size=kernel,
padding='same',activation='relu',dilation_rate=2)(y)
y=Dropout(droupout)(y)
y=MaxPool2D()(y)
filters*=2
# In[10]:
# merge left and right branches outputs
y=concatenate([x,y])
# feature maps to vector before connecting to Dense
y=Flatten()(y)
y=Dropout(droupout)(y)
outputs=Dense(nu_labels,activation='softmax')(y)
# In[11]:
# build the model in functional API
model=Model([left_inputs,right_inputs],outputs)
# In[12]:
# verify the model using graph
plot_model(model, to_file='cnn-y-network.png', show_shapes=True)
# verify the model using layer text description
model.summary()
# In[13]:
model.compile(loss='categorical_crossentropy',optimizer='adam',
metrics=['accuracy'])
# In[14]:
# train the model with input images and labels
model.fit([x_train,x_train],y_train,
validation_data=([x_test,x_test],y_test),epochs=20,batch_size=batch_size)
# In[15]:
# model accuracy on test dataset
score = model.evaluate([x_test, x_test],
y_test,
batch_size=batch_size,
verbose=0)
print("\nTest accuracy: %.1f%%" % (100.0 * score[1]))
# In[ ]: