Skip to content

Commit

Permalink
cnn+dropout: reproduce: 0.60379
Browse files Browse the repository at this point in the history
  • Loading branch information
orbxball committed Apr 14, 2017
1 parent 7a3076c commit e2ce047
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion hw3/test_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def ensure_dir(file_path):
height = width = 48
num_classes = 7
input_shape = (height, width, 1)
model_name = 'cnn_d3.h5'
model_name = 'try.h5'

# Read the test data
with open(sys.argv[1], "r+") as f:
Expand Down
41 changes: 13 additions & 28 deletions hw3/train_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
height = width = 48
num_classes = 7
input_shape = (height, width, 1)
batch_size = 32
batch_size = 128
epochs = 20
pool_size = (2, 2)
model_name = 'cnn_d3.h5'
model_name = 'try.h5'
isValid = 1

# Read the train data
Expand All @@ -33,26 +32,20 @@
# Change data into CNN format
X = X.reshape(X.shape[0], height, width, 1)

# Split data
if isValid:
valid_num = 1000
X_train, Y_train = X[:-valid_num], Y[:-valid_num]
X_valid, Y_valid = X[-valid_num:], Y[-valid_num:]
else:
X_train, Y_train = X, Y
X_train, Y_train = X, Y

# Construct the model
model = Sequential()
model.add(Conv2D(25, (3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(50, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Conv2D(50, (3, 3), activation='relu'))
model.add(Conv2D(100, (3, 3), activation='relu'))
model.add(AveragePooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.5))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
Expand All @@ -69,20 +62,12 @@
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_valid, Y_valid))
validation_split=0.2)
else:
model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1)

# Evaluate the test data
if isValid:
score = model.evaluate(X_valid, Y_valid, verbose=0)
else:
score = model.evaluate(X_train, Y_train, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# Save model
model.save(model_name)

0 comments on commit e2ce047

Please sign in to comment.