Skip to content

Instantly share code, notes, and snippets.

@szymonk92
Created September 8, 2017 02:23
Show Gist options
  • Save szymonk92/b2098cedffdd920c7eeae3fa38494852 to your computer and use it in GitHub Desktop.
Save szymonk92/b2098cedffdd920c7eeae3fa38494852 to your computer and use it in GitHub Desktop.
X, X_test, y, y_test = get_data()
batch_size = 32
num_classes = 2
epochs = 400
data_augmentation = False
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential([
Conv2D(32, (3, 3), padding='same', input_shape=X.shape[1:], activation='relu'),
Conv2D(32, (3, 3), padding='same', activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(128, (3, 3), padding='same', activation='relu'),
Conv2D(128, (3, 3), padding='same',activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(256, (3, 3), padding='same', activation='relu'),
Conv2D(256, (3, 3), padding='same', activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
# Conv2D(512, (3, 3), padding='same', activation='relu'),
# Conv2D(512, (3, 3), padding='same', activation='relu'),
# MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(1024, activation='relu'),
Dropout(0.5),
Dense(1024, activation='relu'),
Dropout(0.5),
Dense(num_classes),
Activation('softmax'),
])
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=1e-5, decay=1e-6)
# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
x_train = X.astype('float32')
x_test = X_test.astype('float32')
x_train /= 255
x_test /= 255
checkpointer = ModelCheckpoint(filepath='tmp\\weights.hdf5', verbose=1, save_best_only=True)
if not data_augmentation:
print('Not using data augmentation.')
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
callbacks=[checkpointer])
else:
print('Using real-time data augmentation.')
# This will do preprocessing and realtime data augmentation:
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
# Compute quantities required for feature-wise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(x_train)
# Fit the model on the batches generated by datagen.flow().
history = model.fit_generator(datagen.flow(x_train, y_train,
batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
callbacks=[checkpointer])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment