Skip to content

Commit

Permalink
preprocessing: best@64, 0.6857, truth: 0.9446/0.6690 -> 0.65896
Browse files Browse the repository at this point in the history
  • Loading branch information
orbxball committed Apr 16, 2017
1 parent e2ce047 commit 52fceaf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 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 = 'try.h5'
model_name = 'pre2.h5'

# Read the test data
with open(sys.argv[1], "r+") as f:
Expand Down
70 changes: 50 additions & 20 deletions hw3/train_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import sys, os
import numpy as np
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, Dropout
from keras.layers import Dense, Activation, Flatten, Dropout, LeakyReLU
from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D
from keras import losses
from keras import optimizers
Expand All @@ -13,8 +14,9 @@
num_classes = 7
input_shape = (height, width, 1)
batch_size = 128
epochs = 20
model_name = 'try.h5'
epochs = 100
zoom_range = 0.05
model_name = 'pre2.h5'
isValid = 1

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

X_train, Y_train = X, Y
# Split the data
if isValid:
valid_num = 3000
permu = np.random.permutation(X.shape[0])
X_train, Y_train = X[permu[:-valid_num]], Y[permu[:-valid_num]]
X_valid, Y_valid = X[permu[-valid_num:]], Y[permu[-valid_num:]]
else:
X_train, Y_train = X, Y

# Construct the model
model = Sequential()
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(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(LeakyReLU(alpha=0.03))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3)))
model.add(LeakyReLU(alpha=0.03))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3, 3)))
model.add(LeakyReLU(alpha=0.03))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Dropout(0.25))

model.add(Conv2D(256, (3, 3)))
model.add(LeakyReLU(alpha=0.03))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

model.summary()

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

# Image PreProcessing
train_gen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
zoom_range=[1-zoom_range, 1+zoom_range],
horizontal_flip=True)
train_gen.fit(X_train)

# Fit the model
if isValid:
model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_split=0.2)
model.fit_generator(train_gen.flow(X_train, Y_train, batch_size=batch_size),
steps_per_epoch=10*X_train.shape[0]//batch_size,
epochs=epochs,
validation_data=(X_valid, Y_valid))
else:
model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1)
model.fit_generator(train_gen.flow(X_train, Y_train, batch_size=batch_size),
steps_per_epoch=10*X_train.shape[0]//batch_size,
epochs=epochs)

# Save model
model.save(model_name)

0 comments on commit 52fceaf

Please sign in to comment.