Сверточный вывод модели и свертка вручную на входе выборки дают разные значения - PullRequest
0 голосов
/ 05 мая 2019

Я пытаюсь восстановить выход первого слоя свертки на вводе выборки двумя способами. Во-первых, непосредственно получим выходные данные первого конвективного слоя, используя модель, изученную на входе образца. Во-вторых, вручную перемещайтесь по вводным выборкам с весами и смещениями и генерируйте выходные данные.

Когда я сравниваю результаты обоих методов, значения не совсем совпадают.

Первый метод: Обучите модель и восстановите выход первого слоя Conv на образце.

Второй метод: обучите модель, используйте изученные веса и смещения каждого фильтра и получите выходные данные слоя Conv.

# Packages, Data and Model
import numpy as np
np.random.seed(37)
import pandas as pd
import keras
import tensorflow as tf
tf.set_random_seed(89)
import random as rn
rn.seed(1254)
from keras import backend as k
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
sess = tf.Session(graph = tf.get_default_graph(), config = session_conf)
k.set_session(sess)

import matplotlib.pyplot as plt
from keras.layers import *
from keras import backend as k
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense, Flatten
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import *
import itertools
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from sklearn.model_selection import train_test_split
import math
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, StratifiedKFold
from keras.models import Sequential, Model
from keras.layers import Input, Flatten, Dense, Dropout, Convolution2D, Conv2D, MaxPooling2D, Lambda, GlobalMaxPooling2D, GlobalAveragePooling2D, BatchNormalization, Activation, AveragePooling2D, Concatenate
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.utils import np_utils
from keras.callbacks import CSVLogger
#%matplotlib inline
keras.backend.set_image_data_format('channels_last')
from keras import initializers


# DATA
# 500 training example data:: each example is 1832 dimensional long
# where dimensional element is encoded using 5 dimensional one hot encoding
# for demonstration lets's use randomly initiated integers
X = np.random.random_integers(0, 1, size=(500, 1832, 5))
y = np.random.random_integers(0, 1, size=(500, 1))

print("X.shape : ", X.shape) ## (2237, 95, 95, 1)
print("y.shape : ", y.shape) ## (2237, 1)

train_index = 450
test_index = 450

# HYPERPARAMETERS
ACTIVATION = 'relu'
KERNEL_INITIALIZER = initializers.glorot_uniform(seed=90)
BIAS_INITIALIZER='zeros'
INPUT_SHAPE=X.shape[1:]
OUTPUT_ACTIVATION = 'sigmoid'

OPTIMIZER = Adam
LEARNING_RATE = 0.0001
BETA_1 = 0.9
BETA_2 = 0.999
EPSILON = None
DECAY = 0.0
AMSGRAD = False
LOSS = 'binary_crossentropy'
METRICS = ['accuracy']

BATCH_SIZE = 64
EPOCHS = 15
SHUFFLE = True
VALIDATION_SPLIT = 0.05

FIRST_CONV_LYR = 'conv_lyr_1'
FIRST_CONV_LYR_FILTERS = 20

FIRST_DENSE_LYR = 'dense_lyr_1'
FIRST_DENSE_LYR_UNITS = 50

VALID_PADDING = 'valid'
SAME_PADDING = 'same'
USE_BIAS = True
VERBOSE = 2
DILATION_RATE=1

# MODEL
model_21 = Sequential()
model_21.add(Conv1D(filters=FIRST_CONV_LYR_FILTERS, kernel_size = (2), strides=(2), activation = ACTIVATION, 
                    padding = VALID_PADDING, input_shape =INPUT_SHAPE , kernel_initializer=KERNEL_INITIALIZER, 
                    dilation_rate=DILATION_RATE, bias_initializer=BIAS_INITIALIZER, name =FIRST_CONV_LYR ))

model_21.add(MaxPooling1D()) # input_shape=(None, 1802, 20)

model_21.add(Flatten()) #::: GMP does not need flatten()

model_21.add(Dense(1, activation = OUTPUT_ACTIVATION))

model_21.compile(optimizer=OPTIMIZER(lr = LEARNING_RATE, beta_1=BETA_1, beta_2=BETA_2, epsilon=EPSILON, decay=DECAY, 
                                     amsgrad=AMSGRAD), loss = LOSS, metrics = METRICS,) 

mdl_21 = model_21.fit(X[:train_index], y[:train_index], batch_size = BATCH_SIZE, epochs = EPOCHS, verbose = VERBOSE, 
                      shuffle=SHUFFLE, validation_split=VALIDATION_SPLIT) 

accuracy_21 = model_21.evaluate(X[test_index:], y[test_index:])

print(model_21.metrics_names[1], accuracy_21[1]*100)

print("The accuracy of Model 21 is : ", accuracy_21)

# PREDICTION
prediction_21 = model_21.predict(X[test_index:]) # predict() takes <class 'numpy.ndarray'> as input
predicted_classes_21 = model_21.predict_classes(X[test_index:])
print("The predicted classes are : ", predicted_classes_21[:, 0])
print("Actual classes are : ", y[test_index:][:, 0]) # [:, 0]



# Model Summary
model_21.summary()

Слой (тип) Выходная форма Параметр #
conv_lyr_1 (Conv1D) (нет, 916, 20) 220
max_pooling1d_1 (MaxPooling1 (Нет, 458, 20) 0
flatten_1 (Flatten) (нет, 9160) 0
density_1 (Плотный) (Нет, 1) 9161
Всего параметров: 9 381 Обучаемые параметры: 9,381 Не обучаемые параметры: 0

# Layer weights and biases
first_layer_weights = model_21.layers[0].get_weights()[0]
first_layer_biases  = model_21.layers[0].get_weights()[1]
fourth_layer_weights = model_21.layers[3].get_weights()[0]
fourth_layer_biases  = model_21.layers[3].get_weights()[1]


# Conv layer Output from Keras model using a sample test index input at index 450 
from keras.models import Model

model = model_21  # include here your original model

layer_name = 'conv_lyr_1'
intermediate_conv_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output)
intermediate_conv_output = intermediate_conv_layer_model.predict(X[test_index:test_index+1])


# Manually calculating Conv layer Output using a sample test index 450 input with weights of each filter and biases of the learned model
conv_v = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
for i in range(20):
    for j in range(0, len(X[test_index]), 2):
        conv_1 = np.sum(np.multiply(X[test_index][j:j+2], first_layer_weights[:,:,i])) + first_layer_biases[i]
        conv_1 = np.float32("{0:.8f}".format(conv_1))
        conv_v[i].append(conv_1)

# Comparing both outputs: intermediate_conv_output and conv_v[filter#]
for fil in range(20):
    print("##############################################")
    print("##############################################")
    print("Filter ", fil+1)
    for i in range(0, 916):
        #print("Index ", i)
        #print(conv_v[fil][i], intermediate_conv_output[:,i,fil][0])
        if(conv_v[fil][i] == intermediate_conv_output[:,i,fil][0]):
            print("Values equal at index :", i)
    print("\n")

Выход

##############################################
##############################################
Filter  1
Values equal at index : 0
Values equal at index : 2
Values equal at index : 3
Values equal at index : 8
Values equal at index : 9
Values equal at index : 10
Values equal at index : 13
Values equal at index : 15
Values equal at index : 16
Values equal at index : 18
Values equal at index : 25
Values equal at index : 26
Values equal at index : 27
Values equal at index : 29
Values equal at index : 31
Values equal at index : 32
Values equal at index : 33
Values equal at index : 34
Values equal at index : 36
Values equal at index : 37
Values equal at index : 41
Values equal at index : 43
Values equal at index : 44
Values equal at index : 51
Values equal at index : 52
Values equal at index : 53
Values equal at index : 55
Values equal at index : 58
Values equal at index : 60
Values equal at index : 63
Values equal at index : 68
Values equal at index : 69
Values equal at index : 87
Values equal at index : 88
Values equal at index : 89
Values equal at index : 91
Values equal at index : 93
Values equal at index : 95
Values equal at index : 97
Values equal at index : 98
Values equal at index : 99
Values equal at index : 100
Values equal at index : 101
Values equal at index : 103
Values equal at index : 104
Values equal at index : 105
Values equal at index : 109
Values equal at index : 110
Values equal at index : 114
Values equal at index : 115
Values equal at index : 116
Values equal at index : 118
Values equal at index : 126
Values equal at index : 128
Values equal at index : 129
Values equal at index : 131
Values equal at index : 132
Values equal at index : 134
Values equal at index : 135
Values equal at index : 137
Values equal at index : 138
Values equal at index : 140
Values equal at index : 141
Values equal at index : 142
Values equal at index : 143
Values equal at index : 144
Values equal at index : 146
Values equal at index : 148
Values equal at index : 156
Values equal at index : 157
Values equal at index : 158
Values equal at index : 163
Values equal at index : 168
Values equal at index : 169
Values equal at index : 170
Values equal at index : 171
Values equal at index : 174
Values equal at index : 178
Values equal at index : 180
Values equal at index : 184
Values equal at index : 186
Values equal at index : 190
Values equal at index : 191
Values equal at index : 195
Values equal at index : 199
Values equal at index : 200
Values equal at index : 201
Values equal at index : 202
Values equal at index : 204
Values equal at index : 205
Values equal at index : 212
Values equal at index : 213
Values equal at index : 217
Values equal at index : 219
Values equal at index : 221
Values equal at index : 222
Values equal at index : 225
Values equal at index : 226
Values equal at index : 228
Values equal at index : 229
Values equal at index : 233
Values equal at index : 234
Values equal at index : 235
Values equal at index : 236
Values equal at index : 238
Values equal at index : 240
Values equal at index : 241
Values equal at index : 242
Values equal at index : 243
Values equal at index : 246
Values equal at index : 248
Values equal at index : 250
Values equal at index : 251
Values equal at index : 253
Values equal at index : 256
Values equal at index : 260
Values equal at index : 262
Values equal at index : 263
Values equal at index : 266
Values equal at index : 267
Values equal at index : 268
Values equal at index : 269
Values equal at index : 271
Values equal at index : 273
Values equal at index : 279
Values equal at index : 294
Values equal at index : 295
Values equal at index : 296
Values equal at index : 297
Values equal at index : 301
Values equal at index : 302
Values equal at index : 303
Values equal at index : 304
Values equal at index : 305
Values equal at index : 310
Values equal at index : 313
Values equal at index : 319
Values equal at index : 321
Values equal at index : 326
Values equal at index : 327
Values equal at index : 329
Values equal at index : 330
Values equal at index : 331
Values equal at index : 332
Values equal at index : 334
Values equal at index : 335
Values equal at index : 337
Values equal at index : 338
Values equal at index : 339
Values equal at index : 341
Values equal at index : 342
Values equal at index : 343
Values equal at index : 346
Values equal at index : 348
Values equal at index : 350
Values equal at index : 354
Values equal at index : 355
Values equal at index : 356
Values equal at index : 359
Values equal at index : 363
Values equal at index : 367
Values equal at index : 368
Values equal at index : 369
Values equal at index : 376
Values equal at index : 378
Values equal at index : 379
Values equal at index : 380
Values equal at index : 381
Values equal at index : 382
Values equal at index : 383
Values equal at index : 389
Values equal at index : 392
Values equal at index : 396
Values equal at index : 400
Values equal at index : 401
Values equal at index : 404
Values equal at index : 411
Values equal at index : 412
Values equal at index : 418
Values equal at index : 419
Values equal at index : 420
Values equal at index : 424
Values equal at index : 425
Values equal at index : 427
Values equal at index : 428
Values equal at index : 435
Values equal at index : 436
Values equal at index : 437
Values equal at index : 438
Values equal at index : 439
Values equal at index : 441
Values equal at index : 443
Values equal at index : 444
Values equal at index : 446
Values equal at index : 448
Values equal at index : 451
Values equal at index : 452
Values equal at index : 456
Values equal at index : 458
Values equal at index : 462
Values equal at index : 463
Values equal at index : 469
Values equal at index : 473
Values equal at index : 474
Values equal at index : 475
Values equal at index : 478
Values equal at index : 481
Values equal at index : 482
Values equal at index : 484
Values equal at index : 485
Values equal at index : 487
Values equal at index : 489
Values equal at index : 490
Values equal at index : 491
Values equal at index : 493
Values equal at index : 499
Values equal at index : 511
Values equal at index : 513
Values equal at index : 517
Values equal at index : 523
Values equal at index : 524
Values equal at index : 526
Values equal at index : 527
Values equal at index : 528
Values equal at index : 530
Values equal at index : 532
Values equal at index : 533
Values equal at index : 534
Values equal at index : 536
Values equal at index : 539
Values equal at index : 540
Values equal at index : 544
Values equal at index : 548
Values equal at index : 549
Values equal at index : 551
Values equal at index : 554
Values equal at index : 557
Values equal at index : 561
Values equal at index : 562
Values equal at index : 566
Values equal at index : 567
Values equal at index : 573
Values equal at index : 578
Values equal at index : 579
Values equal at index : 580
Values equal at index : 583
Values equal at index : 584
Values equal at index : 591
Values equal at index : 593
Values equal at index : 598
Values equal at index : 600
Values equal at index : 601
Values equal at index : 603
Values equal at index : 607
Values equal at index : 609
Values equal at index : 610
Values equal at index : 611
Values equal at index : 612
Values equal at index : 616
Values equal at index : 619
Values equal at index : 621
Values equal at index : 622
Values equal at index : 624
Values equal at index : 627
Values equal at index : 629
Values equal at index : 631
Values equal at index : 640
Values equal at index : 642
Values equal at index : 646
Values equal at index : 651
Values equal at index : 653
Values equal at index : 656
Values equal at index : 658
Values equal at index : 659
Values equal at index : 660
Values equal at index : 661
Values equal at index : 667
Values equal at index : 668
Values equal at index : 670
Values equal at index : 672
Values equal at index : 674
Values equal at index : 675
Values equal at index : 676
Values equal at index : 678
Values equal at index : 680
Values equal at index : 681
Values equal at index : 683
Values equal at index : 684
Values equal at index : 685
Values equal at index : 688
Values equal at index : 690
Values equal at index : 691
Values equal at index : 692
Values equal at index : 693
Values equal at index : 694
Values equal at index : 696
Values equal at index : 698
Values equal at index : 700
Values equal at index : 703
Values equal at index : 705
Values equal at index : 709
Values equal at index : 713
Values equal at index : 715
Values equal at index : 718
Values equal at index : 719
Values equal at index : 720
Values equal at index : 721
Values equal at index : 728
Values equal at index : 730
Values equal at index : 736
Values equal at index : 738
Values equal at index : 740
Values equal at index : 742
Values equal at index : 745
Values equal at index : 746
Values equal at index : 748
Values equal at index : 752
Values equal at index : 753
Values equal at index : 755
Values equal at index : 757
Values equal at index : 760
Values equal at index : 762
Values equal at index : 765
Values equal at index : 770
Values equal at index : 771
Values equal at index : 772
Values equal at index : 774
Values equal at index : 775
Values equal at index : 778
Values equal at index : 780
Values equal at index : 781
Values equal at index : 784
Values equal at index : 788
Values equal at index : 791
Values equal at index : 792
Values equal at index : 794
Values equal at index : 798
Values equal at index : 802
Values equal at index : 805
Values equal at index : 810
Values equal at index : 815
Values equal at index : 817
Values equal at index : 818
Values equal at index : 819
Values equal at index : 821
Values equal at index : 822
Values equal at index : 825
Values equal at index : 826
Values equal at index : 828
Values equal at index : 829
Values equal at index : 830
Values equal at index : 832
Values equal at index : 835
Values equal at index : 836
Values equal at index : 837
Values equal at index : 838
Values equal at index : 841
Values equal at index : 842
Values equal at index : 843
Values equal at index : 852
Values equal at index : 855
Values equal at index : 857
Values equal at index : 859
Values equal at index : 860
Values equal at index : 862
Values equal at index : 863
Values equal at index : 864
Values equal at index : 866
Values equal at index : 867
Values equal at index : 868
Values equal at index : 869
Values equal at index : 874
Values equal at index : 876
Values equal at index : 881
Values equal at index : 883
Values equal at index : 885
Values equal at index : 891
Values equal at index : 895
Values equal at index : 897
Values equal at index : 898
Values equal at index : 899
Values equal at index : 901
Values equal at index : 904
Values equal at index : 906
Values equal at index : 907
Values equal at index : 908
Values equal at index : 909
Values equal at index : 911
Values equal at index : 913


##############################################
##############################################
Filter  2
Values equal at index : 2
Values equal at index : 4
Values equal at index : 8
Values equal at index : 9
Values equal at index : 16
Values equal at index : 18
Values equal at index : 24
Values equal at index : 26
Values equal at index : 29
Values equal at index : 32
Values equal at index : 33
Values equal at index : 35
Values equal at index : 36
Values equal at index : 37
Values equal at index : 45
Values equal at index : 49
Values equal at index : 53
Values equal at index : 54
Values equal at index : 60
Values equal at index : 61
Values equal at index : 71
Values equal at index : 78
Values equal at index : 86
Values equal at index : 91
Values equal at index : 92
Values equal at index : 93
Values equal at index : 95
Values equal at index : 97
Values equal at index : 99
Values equal at index : 103
Values equal at index : 113
Values equal at index : 115
Values equal at index : 128
Values equal at index : 131
Values equal at index : 133
Values equal at index : 140
Values equal at index : 141
Values equal at index : 152
Values equal at index : 154
Values equal at index : 156
Values equal at index : 157
Values equal at index : 166
Values equal at index : 171
Values equal at index : 172
Values equal at index : 173
Values equal at index : 180
Values equal at index : 182
Values equal at index : 191
Values equal at index : 201
Values equal at index : 212
Values equal at index : 214
Values equal at index : 216
Values equal at index : 217
Values equal at index : 221
Values equal at index : 224
Values equal at index : 225
Values equal at index : 226
Values equal at index : 230
Values equal at index : 235
Values equal at index : 238
Values equal at index : 240
Values equal at index : 241
Values equal at index : 242
Values equal at index : 243
Values equal at index : 245
Values equal at index : 246
Values equal at index : 247
Values equal at index : 257
Values equal at index : 259
Values equal at index : 265
Values equal at index : 266
Values equal at index : 268
Values equal at index : 286
Values equal at index : 288
Values equal at index : 292
Values equal at index : 296
Values equal at index : 297
Values equal at index : 301
Values equal at index : 304
Values equal at index : 309
Values equal at index : 311
Values equal at index : 319
Values equal at index : 326
Values equal at index : 327
Values equal at index : 331
Values equal at index : 332
Values equal at index : 334
Values equal at index : 341
Values equal at index : 346
Values equal at index : 349
Values equal at index : 359
Values equal at index : 360
Values equal at index : 362
Values equal at index : 368
Values equal at index : 369
Values equal at index : 372
Values equal at index : 377
Values equal at index : 382
Values equal at index : 383
Values equal at index : 388
Values equal at index : 395
Values equal at index : 398
Values equal at index : 401
Values equal at index : 407
Values equal at index : 410
Values equal at index : 416
Values equal at index : 418
Values equal at index : 419
Values equal at index : 422
Values equal at index : 436
Values equal at index : 437
Values equal at index : 438
Values equal at index : 440
Values equal at index : 442
Values equal at index : 443
Values equal at index : 447
Values equal at index : 453
Values equal at index : 459
Values equal at index : 462
Values equal at index : 478
Values equal at index : 481
Values equal at index : 482
Values equal at index : 488
Values equal at index : 490
Values equal at index : 498
Values equal at index : 500
Values equal at index : 502
Values equal at index : 503
Values equal at index : 508
Values equal at index : 510
Values equal at index : 515
Values equal at index : 517
Values equal at index : 532
Values equal at index : 539
Values equal at index : 544
Values equal at index : 550
Values equal at index : 551
Values equal at index : 552
Values equal at index : 557
Values equal at index : 561
Values equal at index : 566
Values equal at index : 573
Values equal at index : 578
Values equal at index : 586
Values equal at index : 589
Values equal at index : 591
Values equal at index : 592
Values equal at index : 598
Values equal at index : 600
Values equal at index : 604
Values equal at index : 606
Values equal at index : 607
Values equal at index : 610
Values equal at index : 617
Values equal at index : 619
Values equal at index : 621
Values equal at index : 624
Values equal at index : 628
Values equal at index : 629
Values equal at index : 631
Values equal at index : 632
Values equal at index : 636
Values equal at index : 637
Values equal at index : 643
Values equal at index : 645
Values equal at index : 650
Values equal at index : 656
Values equal at index : 660
Values equal at index : 665
Values equal at index : 672
Values equal at index : 674
Values equal at index : 678
Values equal at index : 679
Values equal at index : 683
Values equal at index : 684
Values equal at index : 693
Values equal at index : 696
Values equal at index : 697
Values equal at index : 701
Values equal at index : 704
Values equal at index : 706
Values equal at index : 712
Values equal at index : 717
Values equal at index : 718
Values equal at index : 727
Values equal at index : 728
Values equal at index : 735
Values equal at index : 736
Values equal at index : 740
Values equal at index : 744
Values equal at index : 755
Values equal at index : 757
Values equal at index : 759
Values equal at index : 761
Values equal at index : 766
Values equal at index : 768
Values equal at index : 771
Values equal at index : 776
Values equal at index : 778
Values equal at index : 781
Values equal at index : 783
Values equal at index : 784
Values equal at index : 791
Values equal at index : 796
Values equal at index : 797
Values equal at index : 800
Values equal at index : 801
Values equal at index : 810
Values equal at index : 813
Values equal at index : 816
Values equal at index : 825
Values equal at index : 826
Values equal at index : 828
Values equal at index : 829
Values equal at index : 830
Values equal at index : 835
Values equal at index : 842
Values equal at index : 844
Values equal at index : 850
Values equal at index : 851
Values equal at index : 860
Values equal at index : 864
Values equal at index : 874
Values equal at index : 885
Values equal at index : 886
Values equal at index : 893
Values equal at index : 897
Values equal at index : 900
Values equal at index : 901
Values equal at index : 907
Values equal at index : 913


##############################################
##############################################
Filter  3
Values equal at index : 2
Values equal at index : 5
Values equal at index : 8
Values equal at index : 24
Values equal at index : 37
Values equal at index : 58
Values equal at index : 71
Values equal at index : 84
Values equal at index : 86
Values equal at index : 91
...... and so on excluded because of character limitation.

Почему значения для обоих выходов не равны. Еще один связанный с этим вопрос, выполняющий np.max (0, значения, превышающие размер двух последовательных конв выходов), также не дает вывод, сгенерированный слоем пула с тем же индексным номером входной выборки 450.

Заранее спасибо !!!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...