def create_model():
    alpha = seq()
    alpha.add(
        Conv2D(32, [3, 3],
               strides=1,
               padding='valid',
               activation='relu',
               input_shape=(image_width, image_height, 3)))
    alpha.add(Conv2D(32, [3, 3], strides=1, padding='valid',
                     activation='relu'))
    alpha.add(MaxP2D(pool_size=(2, 2)))

    alpha.add(Conv2D(64, [3, 3], strides=1, padding='valid',
                     activation='relu'))
    alpha.add(MaxP2D(pool_size=(2, 2)))

    alpha.add(Conv2D(64, [3, 3], strides=1, padding='valid',
                     activation='relu'))
    alpha.add(
        Conv2D(128, [3, 3], strides=1, padding='valid', activation='relu'))
    alpha.add(MaxP2D(pool_size=(2, 2)))

    alpha.add(Flatten())
    alpha.add(Dense(1024, activation='relu'))
    alpha.add(Dropout(0.4))
    alpha.add(Dense(36, activation='softmax'))

    return alpha
Example #2
0
def compile_fit(X, Y):
    #https://keras.io/guides/sequential_model/
    from keras.models import Sequential as seq
    from keras.layers import Dense, Activation, Dropout
    from keras.optimizers import SGD
    model = seq()
    model.add(Dense(128, input_shape=(len(X[0]), ), activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(len(Y[0]), activation='softmax'))
    #compile the model with loss, nd sochastic gradient descent
    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(lr=0.01,
                                decay=1e-6,
                                momentum=0.9,
                                nesterov=True),
                  metrics=['accuracy'])
    #convert the lists into nparrays
    x = np.array(X)
    y = np.array(Y)
    training_board = model.fit(x,
                               y,
                               validation_split=0.25,
                               epochs=200,
                               batch_size=2,
                               verbose=1)
    model.save('model.h5', training_board)
    return training_board, model
Example #3
0
def build_model(in_shape):
    print("[log] call build_model")
    model = seq()
    model.add(CV2(32, 4, input_shape=in_shape))
    model.add(Activation('relu'))
    model.add(mp2(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(CV2(64, 3))
    model.add(Activation('relu'))
    model.add(CV2(64, 3))
    model.add(mp2(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))
    # sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    print("[log] model compile")
    model.compile(loss="binary_crossentropy",
                  optimizer='rmsprop',
                  metrics=['accuracy'])
    return model
Example #4
0
from keras.datasets import mnist
(xtrain, ytrain), (xtest, ytest) = mnist.load_data()

xtrain = xtrain.reshape(60000, 28, 28, 1)
xtest = xtest.reshape(10000, 28, 28, 1)

from keras.utils import to_categorical as tc
ytrain = tc(ytrain)
ytest = tc(ytest)

from keras.models import Sequential as seq
from keras.layers import Dense, Conv2D, Flatten
model = seq()
model.add(Conv2D(2, kernel_size=3, activation="relu", input_shape=(28, 28, 1)))

i = 1
n = 4
for i in range(i):
    model.add(Conv2D(filters=n, kernel_size=3, activation="relu"))
    n = n * 2

model.add(Flatten())
model.add(Dense(10, activation="softmax"))

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.fit(xtrain, ytrain, epochs=1)

pred1 = model.evaluate(xtest, ytest)
Example #5
0
def dl(rawdata):

    str = rawdata['DATE']
    str_sp = []
    for i1 in range(len(str)):
        str_sp.append(str[i1].split('-'))
    date_cov = pd.DataFrame(str_sp, columns=['year', 'month', 'day'])
    rawdata_new = pd.concat([date_cov, rawdata.iloc[:, 1:]], axis=1)

    id_na = rawdata_new.isnull().sum().astype('int')
    ind = np.where(id_na > 0)
    prcp_n = rawdata_new.iloc[:, ind[0][0]].dropna(axis=0, how='any')
    hist, bin_n = np.histogram(prcp_n, density=True)
    prob = hist * np.diff(bin_n)
    prob.sum()
    choose_bin = np.random.choice(range(len(hist)), size=1, p=prob)
    l1 = np.matrix(np.where(rawdata_new.iloc[:, ind[0][0]].isnull())).T
    for j1 in range(np.shape(l1)[0]):
        rawdata_new.iloc[l1[j1],
                         ind[0][0]] = np.random.uniform(bin_n[choose_bin],
                                                        bin_n[choose_bin + 1],
                                                        size=1)
    rawdata_new.iloc[:, ind[0][0]].isnull().sum()

    rain_n = rawdata_new.iloc[:, ind[0][1]].dropna(axis=0, how='any')
    level, freq = np.unique(rain_n, return_counts=True)
    prob1 = freq / freq.sum()
    l2 = np.matrix(np.where(rawdata_new.iloc[:, ind[0][1]].isnull())).T
    for j2 in range(np.shape(l2)[0]):
        rawdata_new.iloc[l2[j2], ind[0][1]] = np.random.choice(level,
                                                               size=1,
                                                               p=prob1)
    rawdata_new.iloc[:, ind[0][1]].isnull().sum()

    rawdata_new.iloc[:, ind[0][1]] = rawdata_new.iloc[:,
                                                      ind[0][1]].astype('int')

    ratio = 0.7
    size = rawdata_new.shape[0]

    spl = int(ratio * size)
    id1 = np.random.choice(range(size), spl, replace=False)

    train = rawdata_new.iloc[id1, :]
    test = rawdata_new.iloc[~id1, :]
    id2 = rawdata_new.shape[1] - 1
    train_y = train.iloc[:, id2]
    test_y = test.iloc[:, id2]
    train_x = train.iloc[:, range(id2)]
    test_x = test.iloc[:, range(id2)]

    from keras.models import Sequential as seq
    from keras.layers.core import Activation, Dropout, Dense

    model = seq()
    model.add(
        Dense(units=300,
              input_dim=6,
              kernel_initializer='uniform',
              activation='relu'))
    model.add(Dropout(0.3))
    #    model.add(Dense(units = 5, input_dim = 6, kernel_initializer = 'uniform', activation = 'relu'))
    #    model.add(Dropout(0.3))
    model.add(
        Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.fit(np.array(train_x),
              np.array(train_y),
              epochs=50,
              batch_size=80,
              validation_split=0.3,
              verbose=2)
    #    model.fit(np.array(train_x), np.array(train_y), epochs = 50, batch_size = 80, validation_split = 0.3, verbose = 1)
    train_acc = model.evaluate(np.array(train_x), np.array(train_y))
    test_acc = model.evaluate(np.array(test_x), np.array(test_y))
    results = [train_acc[1], test_acc[1]]
    return results