What I would like to know:
I have built and trained a CNN model in keras and have been able to calculate predictions, but I would like to know the details of the process of what is happening numerically in the prediction step.
What I tried:
(1) Calculate the score with the trained Keras model.
(2) Coded my CNN and calculated the score and compared it with (1) (→ discrepancy).
(3) Checked the source code of Keras and Tensorflow on GitHub and investigated the difference between predict() of Keras and my CNN in terms of numerical processing.
I built such a model in keras.
model = Sequential()
mode.add(Conv2D(20,kernel_size=(1,5), strides=(1,1), padding='valid', input_shape=(24,13,1), activation='relu'))
mode.add(Conv2D(20,kernel_size=(1,9), strides=(1,1), padding='valid', activation='relu'))
mode.add(Conv2D(20,kernel_size=(24,1), strides=(1,1), padding='valid', activation='relu'))
mode.add(Flatten())
mode.add(Dense(20,activation='relu'))
mode.add(Dense(2,activation='sigmoid'))
model.load_weights(model_path / '[model_filename].h5')
model.summary()
On the other hand, I created my predictive CNN code this way. I omit the code but I have succeeded in getting the values of kernel and bias.
my CNN code(only dense and Convolution)
def mydense(ind, ker, bia, func):
tem_dense = np.dot(ker.T, ind) + bia
return func(tem_dense)
def myconv(ind, ker, bia, func):
# input sample data's shape
H = ind.shape[0]
W = ind.shape[1]
# kernel' shape
Hf = ker.shape[0]
Wf = ker.shape[1]
K = ker.shape[3]
# result of convolution
u = np.zeros((H - Hf + 1, W - Wf + 1, K))
# output
z = np.zeros((u.shape))
for k in range(0,K):
for n in range(0,W - Wf + 1):
for m in range(0, H - Hf + 1):
u[m,n,k] = (ind[m:m+Hf, n:n+Wf, :] * ker[:,:,:,k] + bia[k]).sum()
z = np.array([func(i) for i in u])
return z
The results are very different from model.predict().
What I have already looked into:
I have looked further at predict() and other source code, but I needed help from those in the know.
As in the convolution process (Conv2D or Conv), I would like to know in detail what is happening numerically in the Dense process as well.
Any help would be appreciated.