TrainOnlyDense - Kerasy Documentation
Author: Shuto
Description: Brief explanation of TrainOnlyDense and introduction of modules that can be used in Kerasy

### TrainOnlyDense

- 2019-08-31(土)

In [1]:

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt

In [2]:

    from kerasy.layers.convolutional import Conv2D
    from kerasy.layers.pool import MaxPooling2D
    from kerasy.layers.core import Input, Dense, Flatten
    from kerasy.engine.sequential import Sequential

In [3]:

    cv_path = "/Users/iwasakishuto/Github/portfolio/Kerasy/doc/theme/img/MNIST-sample/0.png"

In [4]:

    image = np.expand_dims(cv2.imread(cv_path, 0), axis=2)/255

In [5]:

    plt.imshow(cv2.cvtColor(cv2.imread(cv_path), cv2.COLOR_BGR2RGB))
    plt.show()

In [6]:

    model = Sequential()
    model.add(Input(input_shape=(28,28,1)))
    model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='linear', padding="same"))
    model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='linear', padding="same"))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(128, activation='tanh'))
    model.add(Dense(10, activation='softmax'))

In [7]:

    model.compile(optimizer='sgd', loss="categorical_crossentropy")

In [8]:

    for layer in model.layers:
        print(f"== {layer.name} ==")
        print(f"output shape: {layer.output_shape}")
        print(f"kernel shape: {layer._losses.get('kernel', np.zeros(shape=())).shape}")
        print(f"bias shape  : {layer._losses.get('bias', np.zeros(shape=())).shape}")

    == input_1 ==
    output shape: (28, 28, 1)
    kernel shape: ()
    bias shape  : ()
    == conv2d_1 ==
    output shape: (28, 28, 32)
    kernel shape: (3, 3, 1, 32)
    bias shape  : (32,)
    == conv2d_2 ==
    output shape: (28, 28, 64)
    kernel shape: (3, 3, 32, 64)
    bias shape  : (64,)
    == maxpooling2d_1 ==
    output shape: (14, 14, 64)
    kernel shape: ()
    bias shape  : ()
    == flatten_1 ==
    output shape: (12544,)
    kernel shape: ()
    bias shape  : ()
    == dense_1 ==
    output shape: (128,)
    kernel shape: (128, 12544)
    bias shape  : (128, 1)
    == dense_2 ==
    output shape: (10,)
    kernel shape: (10, 128)
    bias shape  : (10, 1)

In [9]:

    #=== Train Only Dense Layer ===
    model.layers[1].trainable = False
    model.layers[2].trainable = False

In [10]:

    # #=== Train Only Convolutional Layer ===
    # model.layers[-1].trainable = False
    # model.layers[-2].trainable = False

In [11]:

    x_train = np.expand_dims(image, axis=0)

In [12]:

    original_pred = model.predict(x_train)
    print(f"original prediction: {np.argmax(original_pred)}\n{original_pred}")

    original prediction: 2
    [[0.09963766 0.1004707  0.10146399 0.09917924 0.10046313 0.0988132
      0.09983802 0.10139949 0.10078803 0.09794654]]

In [13]:

    ans_label = np.argmin(original_pred)
    print(f"ans_label: {ans_label}")

    ans_label: 9

In [14]:

    y_true = np.zeros(shape=(10,))
    y_true[ans_label] = 1
    y_true = y_true.reshape(1,-1)

In [15]:

    model.fit(x=x_train, y=y_true, epochs=30)

    [1/30]
    [2/30]
    [3/30]
    [4/30]
    [5/30]
    [6/30]
    [7/30]
    [8/30]
    [9/30]
    [10/30]
    [11/30]
    [12/30]
    [13/30]
    [14/30]
    [15/30]
    [16/30]
    [17/30]
    [18/30]
    [19/30]
    [20/30]
    [21/30]
    [22/30]
    [23/30]
    [24/30]
    [25/30]
    [26/30]
    [27/30]
    [28/30]
    [29/30]
    [30/30]

In [16]:

    final_pred = model.predict(x_train)
    print(f"  Answer: {ans_label}")
    print(f"  original prediction: {np.argmax(original_pred)}\n{original_pred}")
    print(f"  final prediction: {np.argmax(final_pred)}\n{final_pred}")
    print(f"  Difference:\n{np.where((final_pred-original_pred)>0, '+', '-')}")

      Answer: 9
      original prediction: 2
    [[0.09963766 0.1004707  0.10146399 0.09917924 0.10046313 0.0988132
      0.09983802 0.10139949 0.10078803 0.09794654]]
      final prediction: 9
    [[0.00293264 0.002827   0.00315045 0.00266858 0.00292554 0.00257199
      0.00294734 0.00316633 0.00319796 0.97361218]]
      Difference:
    [['-' '-' '-' '-' '-' '-' '-' '-' '-' '+']]

In [17]:

    from Functions.TexWriter import CNNbackprop2tex

In [18]:

    kh, kw = (3,3)
    sh, sw = (1,1)
    IH, IW = (5,5)
    OH, OW = (3,3)

In [19]:

    CNNbackprop2tex(IH,IW,OH,OW,kh,kw,sh,sw, path="sample.tex")

    Your tex file saved at '/Users/iwasakishuto/Github/portfolio/Kerasy/pelican/pelican-src/DeepLearning/sample.tex'
    Please run the following code. Then, /Users/iwasakishuto/Github/portfolio/Kerasy/pelican/pelican-src/DeepLearning/sample.pdf will be created.
    ==============================
    $ platex /Users/iwasakishuto/Github/portfolio/Kerasy/pelican/pelican-src/DeepLearning/sample.tex
    $ dvipdfmx /Users/iwasakishuto/Github/portfolio/Kerasy/pelican/pelican-src/DeepLearning/sample.dvi

    ! platex /Users/iwasakishuto/Github/portfolio/Kerasy/pelican/pelican-src/DeepLearning/sample.tex
    ! dvipdfmx /Users/iwasakishuto/Github/portfolio/Kerasy/pelican/pelican-src/DeepLearning/sample.dvi

« Previous Next »