Skip to content

Instantly share code, notes, and snippets.

@andrewssobral
Last active March 13, 2021 16:54
Show Gist options
  • Save andrewssobral/d05f9e6910bc035049dd5977fe124dae to your computer and use it in GitHub Desktop.
Save andrewssobral/d05f9e6910bc035049dd5977fe124dae to your computer and use it in GitHub Desktop.
tensorflow_keras_cifar10
import os, json, shutil
import keras
from keras.callbacks import TensorBoard
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
# https://github.com/tensorflow/tensorflow/issues/24828
try:
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
except:
pass
NUM_CLASSES = 10
DATA_AUGMENTATION = False
NUM_EPOCHS = 3
LEARNING_RATE = 0.0001
BATCH_SIZE = 32
WEIGHT_DECAY = 1e-6
OPTIMIZER = 'Adam' # Adam, RMSPROP, SGD
TENSORBOARD_ENABLED = "False"
######################## AUTOML SETTINGS ##########################
# SEARCH_SPACE:
# {"OPTIMIZER": choice(["Adam", "SGD", "RMSprop"]),
# "LEARNING_RATE": choice([0.0001, 0.00025]),
# "BATCH_SIZE": choice([32, 64]),
# "WEIGHT_DECAY": choice([0.0005, 0.005])}
"""
NUM_EPOCHS = int(variables.get("NUM_EPOCHS"))
LEARNING_RATE = float(variables.get("LEARNING_RATE"))
BATCH_SIZE = int(variables.get("BATCH_SIZE"))
WEIGHT_DECAY = float(variables.get("WEIGHT_DECAY"))
OPTIMIZER = variables.get("OPTIMIZER")
TENSORBOARD_ENABLED = variables.get("TENSORBOARD_ENABLED")
input_variables = variables.get("INPUT_VARIABLES")
if input_variables is not None and input_variables != '':
input_variables = json.loads(input_variables)
LEARNING_RATE = input_variables["LEARNING_RATE"]
BATCH_SIZE = input_variables["BATCH_SIZE"]
WEIGHT_DECAY = input_variables["WEIGHT_DECAY"]
OPTIMIZER = input_variables["OPTIMIZER"]
"""
###################################################################
OPTIMIZER = OPTIMIZER.upper()
# Create a TensorBoard instance with the path to the logs directory
if TENSORBOARD_ENABLED.lower() == "true":
DOCKER_LOG_PATH = variables.get("DOCKER_LOG_PATH")
PA_JOB_ID = "job_ID_" + variables.get("PA_JOB_ID")
PATH = os.path.join(DOCKER_LOG_PATH, PA_JOB_ID)
print('PATH: ', PATH)
if os.path.isdir(PATH):
try:
print('Removing existing path')
shutil.rmtree(PATH)
except:
print('Error while deleting directory')
os.mkdir(PATH)
tensorboard = TensorBoard(log_dir=PATH)
print('-' * 30)
print('NUM_CLASSES: ', NUM_CLASSES)
print('DATA_AUGMENTATION: ', DATA_AUGMENTATION)
print('NUM_EPOCHS: ', NUM_EPOCHS)
print('LEARNING_RATE: ', LEARNING_RATE)
print('BATCH_SIZE: ', BATCH_SIZE)
print('WEIGHT_DECAY: ', WEIGHT_DECAY)
print('OPTIMIZER: ', OPTIMIZER)
print('-' * 30)
# The data, split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)
y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)
print(x_train.shape[1:])
# Implemnting above stack into code.
model = Sequential()
# Adding layers to the sequential model
# .add() will push the layer into the stack(sequential model)
# filter -> 32
# strides ->(3,3)
model.add(Conv2D(32, (3, 3), padding='same',input_shape=x_train.shape[1:]))
# Reulu is an activation function faster than sigmoid and reduce the liklehod of vanishing gradient
model.add(Activation('relu'))
# We dont have to define parameters again.
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
# Maxpooling is done for downsampling the input and reducing its dimensionality
# and also help over-fitting by providing an abstracted form of the representation
model.add(MaxPooling2D(pool_size=(2, 2)))
# Drop put is a regularization technique where randomly selected neurons are ignored during training
# Here 25% connection will drop
model.add(Dropout(0.25))
# Depth is changed into 64
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Conver matrix into a vector by streching it.
model.add(Flatten())
# Dense layer is use to collect all the info
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
# Number of target classes
model.add(Dense(NUM_CLASSES))
# Softmax convert probabilty into binary number.
model.add(Activation('softmax'))
# Initiate RMSprop optimizer
if OPTIMIZER == 'RMSPROP':
opt = keras.optimizers.rmsprop(lr=LEARNING_RATE, decay=WEIGHT_DECAY)
elif OPTIMIZER == 'SGD':
opt = keras.optimizers.SGD(lr=LEARNING_RATE, decay=WEIGHT_DECAY)
else:
opt = keras.optimizers.Adam(lr=LEARNING_RATE, decay=WEIGHT_DECAY)
# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
if not DATA_AUGMENTATION:
print('Not using data augmentation.')
if TENSORBOARD_ENABLED.lower() == "true":
model.fit(x_train, y_train,
batch_size =BATCH_SIZE,
epochs=NUM_EPOCHS,
validation_data=(x_test, y_test),
shuffle=True, callbacks=[tensorboard])
else:
model.fit(x_train, y_train,
batch_size =BATCH_SIZE,
epochs=NUM_EPOCHS,
validation_data=(x_test, y_test),
shuffle=True)
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().
model.fit_generator(
datagen.flow(x_train, y_train, batch_size=BATCH_SIZE),
steps_per_epoch=x_train.shape[0],
epochs=NUM_EPOCHS,
validation_data=(x_test, y_test)
)
# Score trained model.
scores = model.evaluate(x_test, y_test, verbose=1)
loss = scores[0]
acc = scores[1]
print('Test loss:', loss)
print('Test accuracy:', acc)
######################## AUTOML SETTINGS ##########################
"""
token = variables.get("TOKEN")
# Convert from JSON to dict
token = json.loads(token)
# Return the loss value
result_map = {'token': token, 'loss': loss}
print('result_map: ', result_map)
resultMap.put("RESULT_JSON", json.dumps(result_map))
# To appear in Job Analytics
resultMap.put("LOSS", str(loss))
resultMap.put("ACCURACY", str(acc))
"""
###################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment