<jupyter_start><jupyter_text>Load Dataset<jupyter_code>x_train = np.load('dataset/x_train.npy').astype(np.float32)
y_train = np.load('dataset/y_train.npy').astype(np.float32)
x_val = np.load('dataset/x_val.npy').astype(np.float32)
y_val = np.load('dataset/y_val.npy').astype(np.float32)

print(x_train.shape, y_train.shape)
print(x_val.shape, y_val.shape)<jupyter_output>(2586, 26, 34, 1) (2586, 1)
(288, 26, 34, 1) (288, 1)<jupyter_text>Preview<jupyter_code>plt.subplot(2, 1, 1)
plt.title(str(y_train[0]))
plt.imshow(x_train[0].reshape((26, 34)), cmap='gray')
plt.subplot(2, 1, 2)
plt.title(str(y_val[4]))
plt.imshow(x_val[4].reshape((26, 34)), cmap='gray')<jupyter_output><empty_output><jupyter_text>Data Augmentation<jupyter_code>train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=10,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2
)

val_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow(
    x=x_train, y=y_train,
    batch_size=32,
    shuffle=True
)

val_generator = val_datagen.flow(
    x=x_val, y=y_val,
    batch_size=32,
    shuffle=False
)<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>inputs = Input(shape=(26, 34, 1))

net = Conv2D(32, kernel_size=3, strides=1, padding='same', activation='relu')(inputs)
net = MaxPooling2D(pool_size=2)(net)

net = Conv2D(64, kernel_size=3, strides=1, padding='same', activation='relu')(net)
net = MaxPooling2D(pool_size=2)(net)

net = Conv2D(128, kernel_size=3, strides=1, padding='same', activation='relu')(net)
net = MaxPooling2D(pool_size=2)(net)

net = Flatten()(net)

net = Dense(512)(net)
net = Activation('relu')(net)
net = Dense(1)(net)
outputs = Activation('sigmoid')(net)

model = Model(inputs=inputs, outputs=outputs)

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])

model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 26, 34, 1)         0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 26, 34, 32)        320       
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 13, 17, 32)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 13, 17, 64)        18496     
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 6, 8, 64)          0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 6, 8, 128)         73856     
_________________________________________________________________
max_poolin[...]<jupyter_text>Train<jupyter_code>start_time = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')

model.fit_generator(
    train_generator, epochs=50, validation_data=val_generator,
    callbacks=[
        ModelCheckpoint('models/%s.h5' % (start_time), monitor='val_acc', save_best_only=True, mode='max', verbose=1),
        ReduceLROnPlateau(monitor='val_acc', factor=0.2, patience=10, verbose=1, mode='auto', min_lr=1e-05)
    ]
)<jupyter_output>Epoch 1/50
81/81 [==============================] - 4s 54ms/step - loss: 0.4922 - acc: 0.7573 - val_loss: 0.2625 - val_acc: 0.9167

Epoch 00001: val_acc improved from -inf to 0.91667, saving model to models/2018_12_17_22_58_35.h5
Epoch 2/50
81/81 [==============================] - 4s 44ms/step - loss: 0.2703 - acc: 0.8950 - val_loss: 0.1172 - val_acc: 0.9653

Epoch 00002: val_acc improved from 0.91667 to 0.96528, saving model to models/2018_12_17_22_58_35.h5
Epoch 3/50
81/81 [==============================] - 4s 45ms/step - loss: 0.1702 - acc: 0.9332 - val_loss: 0.0950 - val_acc: 0.9618

Epoch 00003: val_acc did not improve from 0.96528
Epoch 4/50
81/81 [==============================] - 4s 48ms/step - loss: 0.1452 - acc: 0.9482 - val_loss: 0.0695 - val_acc: 0.9722

Epoch 00004: val_acc improved from 0.96528 to 0.97222, saving model to models/2018_12_17_22_58_35.h5
Epoch 5/50
81/81 [==============================] - 3s 42ms/step - loss: 0.1268 - acc: 0.9579 - val_loss: 0.0532 - val_acc[...]<jupyter_text>Confusion Matrix<jupyter_code>from sklearn.metrics import accuracy_score, confusion_matrix
import seaborn as sns

model = load_model('models/%s.h5' % (start_time))

y_pred = model.predict(x_val/255.)
y_pred_logical = (y_pred > 0.5).astype(np.int)

print ('test acc: %s' % accuracy_score(y_val, y_pred_logical))
cm = confusion_matrix(y_val, y_pred_logical)
sns.heatmap(cm, annot=True)<jupyter_output>test acc: 1.0<jupyter_text>Distribution of Prediction<jupyter_code>ax = sns.distplot(y_pred, kde=False)<jupyter_output>
{'fraction_non_alphanumeric': 0.17426876661894047, 'fraction_numerical': 0.08856616895070567, 'mean_word_length': 5.269230769230769, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '225401', 'license': 'MIT', 'n_tokens_mistral': 2066, 'n_tokens_neox': 1771, 'n_words': 312, 'path': 'make_model/train.ipynb', 'repo_name': 'suwonraison900206/Hackathon_EyeDear', 'size': '4889'}