File size: 1,901 Bytes
8e681e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from keras.utils.data_utils import get_file
from config import MODEL_HDF5_PATH


LABELS = np.loadtxt("labels.txt", dtype=object, delimiter="\n")


def initialize_model():
    # import the necessary packages
    from keras.models import Sequential
    from keras.layers import BatchNormalization
    from keras.layers.convolutional import Conv2D, MaxPooling2D
    from keras.layers.core import Flatten, Dropout, Dense

    # CONV => RELU => POOL
    cnn = Sequential()
    inputShape = (224, 224, 3)
    chanDim = -1
    classes = 101
    # Sequence of Convolution (scan filters), BatchNormalization (normalize numbers),
    #  MaxPooling (shrink tensor down), Dropout (prevent overfit)
    cnn.add(
        Conv2D(32, (3, 3), padding="same", input_shape=inputShape, activation="relu")
    )
    cnn.add(BatchNormalization(axis=chanDim))
    cnn.add(MaxPooling2D(pool_size=(3, 3)))
    cnn.add(Dropout(rate=0.25))
    cnn.add(Conv2D(64, (3, 3), padding="same", activation="relu"))
    cnn.add(BatchNormalization(axis=chanDim))
    cnn.add(Conv2D(64, (3, 3), padding="same", activation="relu"))
    cnn.add(BatchNormalization(axis=chanDim))
    cnn.add(MaxPooling2D(pool_size=(2, 2)))
    cnn.add(Dropout(rate=0.25))
    cnn.add(Conv2D(128, (3, 3), padding="same", activation="relu"))
    cnn.add(BatchNormalization(axis=chanDim))
    cnn.add(Conv2D(128, (3, 3), padding="same", activation="relu"))
    cnn.add(BatchNormalization(axis=chanDim))
    cnn.add(MaxPooling2D(pool_size=(2, 2)))
    cnn.add(Dropout(rate=0.25))
    cnn.add(Flatten())
    cnn.add(Dense(1024, activation="relu"))
    cnn.add(BatchNormalization())
    cnn.add(Dropout(rate=0.5))
    # softmax classifier
    cnn.add(Dense(classes, activation="softmax"))

    return cnn


CNN = initialize_model()


CNN.load_weights(
    get_file(
        "weights.hdf5",
        MODEL_HDF5_PATH,
        cache_dir="."
    )
)