-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python | ||
import sys, os | ||
import numpy as np | ||
import keras | ||
from keras.models import load_model | ||
|
||
def ensure_dir(file_path): | ||
directory = os.path.dirname(file_path) | ||
if len(directory) == 0: return | ||
if not os.path.exists(directory): | ||
os.makedirs(directory) | ||
|
||
# Parameter | ||
height = width = 48 | ||
num_classes = 7 | ||
input_shape = (height, width, 1) | ||
model_name = 'cnn2.h5' | ||
|
||
# Read the test data | ||
with open(sys.argv[1], "r+") as f: | ||
line = f.read().strip().replace(',', ' ').split('\n')[1:] | ||
raw_data = ' '.join(line) | ||
length = width*height+1 #1 is for label | ||
data = np.array(raw_data.split()).astype('float').reshape(-1, length) | ||
X = data[:, 1:] | ||
X /= 255 | ||
|
||
# Load model | ||
model = load_model(model_name) | ||
|
||
# Predict the test data | ||
X = X.reshape(X.shape[0], height, width, 1) | ||
ans = model.predict_classes(X) | ||
ans = list(ans) | ||
|
||
# Write prediction | ||
## check the folder of out.csv is exist; otherwise, make it | ||
ensure_dir(sys.argv[2]) | ||
|
||
result = [] | ||
for index, value in enumerate(ans): | ||
result.append("{0},{1}".format(index, value)) | ||
|
||
with open(sys.argv[2], "w+") as f: | ||
f.write("id,label\n") | ||
f.write("\n".join(result)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env python | ||
import sys, os | ||
import numpy as np | ||
import keras | ||
from keras.models import Sequential | ||
from keras.layers import Dense, Activation, Flatten | ||
from keras.layers import Conv2D, MaxPooling2D | ||
from keras import losses | ||
from keras import optimizers | ||
|
||
# Parameter | ||
height = width = 48 | ||
num_classes = 7 | ||
input_shape = (height, width, 1) | ||
batch_size = 32 | ||
epochs = 5 | ||
pool_size = (2, 2) | ||
model_name = 'cnn2.h5' | ||
isValid = 1 | ||
|
||
# Read the train data | ||
with open(sys.argv[1], "r+") as f: | ||
line = f.read().strip().replace(',', ' ').split('\n')[1:] | ||
raw_data = ' '.join(line) | ||
length = width*height+1 #1 is for label | ||
data = np.array(raw_data.split()).astype('float').reshape(-1, length) | ||
X = data[:, 1:] | ||
Y = data[:, 0] | ||
X /= 255 | ||
Y = Y.reshape(Y.shape[0], 1) | ||
Y = keras.utils.to_categorical(Y, num_classes) | ||
|
||
# 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 | ||
|
||
# Construct the model | ||
model = Sequential() | ||
model.add(Conv2D(25, (3, 3), input_shape=input_shape)) | ||
model.add(MaxPooling2D(pool_size=pool_size)) | ||
model.add(Conv2D(50, (3, 3))) | ||
model.add(MaxPooling2D(pool_size=pool_size)) | ||
model.add(Conv2D(100, (3, 3))) | ||
model.add(MaxPooling2D(pool_size=pool_size)) | ||
model.add(Flatten()) | ||
model.add(Dense(500)) | ||
model.add(Activation('softplus')) | ||
model.add(Dense(100)) | ||
model.add(Activation('softplus')) | ||
model.add(Dense(50)) | ||
model.add(Activation('softplus')) | ||
model.add(Dense(num_classes)) | ||
model.add(Activation('softmax')) | ||
model.summary() | ||
|
||
# Compile the model | ||
model.compile(loss='categorical_crossentropy', | ||
optimizer='adam', | ||
metrics=['accuracy']) | ||
|
||
# Fit the model | ||
if isValid: | ||
model.fit(X_train, Y_train, | ||
batch_size=batch_size, | ||
epochs=epochs, | ||
verbose=1, | ||
validation_data=(X_valid, Y_valid)) | ||
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) |