본문 바로가기

VISION

Keras Basics

1. Design model

from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPooling2D, Dropout
from keras.models import Model

def Mymodel(input):
    X_train = Input(input)

    X = ZeroPadding2D((3, 3))(X_train)
    X = Conv2D(32, (7, 7), strides = (1, 1))(X)
    X = BatchNormalization(axis = 3)(X)
    X = Activation('relu')(X)

    X = MaxPooling2D((2, 2))(X)
	X = Flatten()(X)
    X = Dense(1, activation='sigmoid')(X)	#fully connected layer

    model = Model(inputs = X_input, outputs = X, name='Mymodel')	# Create model.

    return model

 

2. Create model

mymodel = Mymodel(X_train)

 

3. Compile model

mymodel.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])

 

4. Train model

mymodel.fit(x = X_train, y = Y_train, epochs = 100, batch_size = 64)

 

5. Evaluate model

prediction = Mymodel.evaluate(x = X_test, y = Y_test)

print ("Loss = " + str(prediction[0]))
print ("Test Accuracy = " + str(prediction[1]))