Skip to content

Instantly share code, notes, and snippets.

View cafielo's full-sized avatar
💭
I may be slow to respond.

Joonwon Lee cafielo

💭
I may be slow to respond.
View GitHub Profile
@cafielo
cafielo / mnist01.py
Created October 31, 2017 15:29
1. Mnist load
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.layers.normalization import BatchNormalization
batch_size = 128
@cafielo
cafielo / mnist02.py
Created October 31, 2017 15:33
2. reshape and preprocessing dataset
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
@cafielo
cafielo / mnist03.py
Created October 31, 2017 15:35
3. build a network
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(28,28,1)))
model.add(Activation('relu'))
BatchNormalization(axis=-1)
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
BatchNormalization(axis=-1)
@cafielo
cafielo / mnist04.py
Created October 31, 2017 15:36
4. train, test and save the model
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=2,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=1)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let target = URLSchemeHandler.shared.parse(url: url)
window?.rootViewController?.present(target, animated: true, completion: nil)
return true
}
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
let isControllTapped = touch.view is UIControl
return !isControllTapped
}
}
protocol Expandable {
var isExpanded: Bool { get set }
}
class Bag: Expandable {
var isExpanded = true
}
class CarryOn: Expandable {
var isExpanded = true
protocol Expandable: class {
var isExpanded: Bool { get set }
}
protocol Expandable: AnyObject {
var isExpanded: Bool { get set }
}
extension String {
func trimTrailingWhitespaces() -> String {
return self.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
}
}
let string = "there is string with trailing whitespace "
let trimmed = string.trimTrailingWhitespaces()