Skip to content

Instantly share code, notes, and snippets.

@abearman
Last active June 21, 2020 16:45
Show Gist options
  • Save abearman/916673e9a0f12bcf5ac2723b8b5eb819 to your computer and use it in GitHub Desktop.
Save abearman/916673e9a0f12bcf5ac2723b8b5eb819 to your computer and use it in GitHub Desktop.
Showing that a simple ResNet50 Keras network run on CIFAR10 does not have reproducible validation loss with learning rate = 0
import keras
import numpy as np
from keras.datasets import cifar10
from keras.applications.resnet50 import ResNet50
from keras.layers import GlobalAveragePooling2D, Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from skimage.transform import resize
from IPython import embed
NUM_CLASSES = 10
BATCH_SIZE = 2
NUM_EPOCHS = 15
use_data_aug = True
# img_arr is of shape (n, h, w, c)
def resize_image_arr(img_arr):
x_resized_list = []
for i in range(img_arr.shape[0]):
img = img_arr[0]
resized_img = resize(img, (224, 224))
x_resized_list.append(resized_img)
return np.stack(x_resized_list)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train[0:2]
y_train = y_train[0:2]
x_test = x_test[0:2]
y_test = y_test[0:2]
# Resize image arrays
x_train = resize_image_arr(x_train)
x_test = resize_image_arr(x_test)
# 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)
# Normalize the data
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
base_model = ResNet50(include_top=False, weights='imagenet')
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(512, activation='relu')(x)
# and a logistic layer -- 10 classes for CIFAR10
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.0, decay=0.0)
# Let's train the model using RMSprop
model.compile(loss='binary_crossentropy',
optimizer=opt,
metrics=['accuracy'])
if not use_data_aug:
model.fit(x_train, y_train,
batch_size=BATCH_SIZE,
epochs=NUM_EPOCHS,
validation_data=(x_test, y_test),
shuffle=False)
else:
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, # randomly shift images horizontally (fraction of total width)
height_shift_range=0, # 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),
epochs=NUM_EPOCHS,
validation_data=(x_test, y_test),
workers=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment