From: https://www.kaggle.com/xiuchengwang/cnn-keras-starter-senet-50
Author: XiuCheng Wang
Score: 0.339
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
import os
print(os.listdir("../input"))
# Any results you write to the current directory are saved as output.
import os, sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import skimage.io
from skimage.transform import resize
from imgaug import augmenters as iaa
from tqdm import tqdm
import PIL
from PIL import Image
import cv2
from sklearn.utils import class_weight, shuffle
from keras.losses import binary_crossentropy
import keras.backend as K
import tensorflow as tf
from sklearn.metrics import f1_score, fbeta_score
from keras.utils import Sequence
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model
from keras.layers import *
from keras.applications import *
from keras.callbacks import ModelCheckpoint
from keras.utils.vis_utils import plot_model
from keras import metrics
from keras.optimizers import Adam
from keras import backend as K
import keras
from keras.models import Model
WORKERS = 2
CHANNEL = 3
import warnings
warnings.filterwarnings("ignore")
SIZE = 224
NUM_CLASSES = 1103
beta_f2=2
# Load dataset info
path_to_train = '../input/imet-2019-fgvc6/train/'
data = pd.read_csv('../input/imet-2019-fgvc6/train.csv')
train_dataset_info = []
for name, labels in zip(data['id'], data['attribute_ids'].str.split(' ')):
train_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label) for label in labels])})
train_dataset_info = np.array(train_dataset_info)
gamma = 2.0
epsilon = K.epsilon()
def focal_loss(y_true, y_pred):
pt = y_pred * y_true + (1-y_pred) * (1-y_true)
pt = K.clip(pt, epsilon, 1-epsilon)
CE = -K.log(pt)
FL = K.pow(1-pt, gamma) * CE
loss = K.sum(FL, axis=1)
return loss
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
class data_generator(Sequence):
def create_train(dataset_info, batch_size, shape, augument=True):
assert shape[2] == 3
while True:
dataset_info = shuffle(dataset_info)
for start in range(0, len(dataset_info), batch_size):
end = min(start + batch_size, len(dataset_info))
batch_images = []
X_train_batch = dataset_info[start:end]
batch_labels = np.zeros((len(X_train_batch), NUM_CLASSES))
for i in range(len(X_train_batch)):
image = data_generator.load_image(
X_train_batch[i]['path'], shape)
if augument:
image = data_generator.augment(image)
batch_images.append(image/255.)
batch_labels[i][X_train_batch[i]['labels']] = 1
yield np.array(batch_images, np.float32), batch_labels
def create_valid(dataset_info, batch_size, shape, augument=False):
assert shape[2] == 3
while True:
# dataset_info = shuffle(dataset_info)
for start in range(0, len(dataset_info), batch_size):
end = min(start + batch_size, len(dataset_info))
batch_images = []
X_train_batch = dataset_info[start:end]
batch_labels = np.zeros((len(X_train_batch), NUM_CLASSES))
for i in range(len(X_train_batch)):
image = data_generator.load_image(
X_train_batch[i]['path'], shape)
if augument:
image = data_generator.augment(image)
batch_images.append(image/255.)
batch_labels[i][X_train_batch[i]['labels']] = 1
yield np.array(batch_images, np.float32), batch_labels
def load_image(path, shape):
image = cv2.imread(path+'.png')
image = cv2.resize(image, (SIZE, SIZE))
return image
def augment(image):
augment_img = iaa.Sequential([
# sometimes(
# iaa.OneOf([
# # iaa.AddToHueAndSaturation((-20, 20)),
# # iaa.Add((-10, 10), per_channel=0.5),
# # iaa.Multiply((0.9, 1.1), per_channel=0.5),
# # # iaa.GaussianBlur((0, 0.5)), # blur images with a sigma between 0 and 3.0
# # iaa.ContrastNormalization((0.8, 1.2), per_channel=0.5), # improve or worsen the contrast
# # iaa.Sharpen(alpha=(0, 0.2), lightness=(0.8, 1.2)), # sharpen images
# # iaa.Emboss(alpha=(0, 0.5), strength=(0, 0.5)), # emboss images
# iaa.Crop(percent=(0, 0.1))
# ])
# ),
iaa.OneOf([
iaa.Affine(rotate=0),
iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270),
iaa.Fliplr(0.5),
# iaa.Flipud(0.5),
])], random_order=True)
image_aug = augment_img.augment_image(image)
return image_aug
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model
from keras.layers import *
from keras.applications.xception import Xception
from keras.callbacks import ModelCheckpoint
from keras import metrics
from keras.optimizers import Adam
from keras import backend as K
import keras
from keras.models import Model
# reference link: https://gist.github.com/drscotthawley/d1818aabce8d1bf082a6fb37137473ae
from keras.callbacks import Callback
def get_1cycle_schedule(lr_max=1e-3, n_data_points=8000, epochs=200, batch_size=40, verbose=0):
"""
Creates a look-up table of learning rates for 1cycle schedule with cosine annealing
See @sgugger's & @jeremyhoward's code in fastai library: https://github.com/fastai/fastai/blob/master/fastai/train.py
Wrote this to use with my Keras and (non-fastai-)PyTorch codes.
Note that in Keras, the LearningRateScheduler callback (https://keras.io/callbacks/#learningratescheduler) only operates once per epoch, not per batch
So see below for Keras callback
Keyword arguments:
lr_max chosen by user after lr_finder
n_data_points data points per epoch (e.g. size of training set)
epochs number of epochs
batch_size batch size
Output:
lrs look-up table of LR's, with length equal to total # of iterations
Then you can use this in your PyTorch code by counting iteration number and setting
optimizer.param_groups[0]['lr'] = lrs[iter_count]
"""
if verbose > 0:
print("Setting up 1Cycle LR schedule...")
pct_start, div_factor = 0.3, 25. # @sgugger's parameters in fastai code
lr_start = lr_max/div_factor
lr_end = lr_start/1e4
n_iter = (n_data_points * epochs // batch_size) + 1 # number of iterations
a1 = int(n_iter * pct_start)
a2 = n_iter - a1
# make look-up table
lrs_first = np.linspace(lr_start, lr_max, a1) # linear growth
lrs_second = (lr_max-lr_end)*(1+np.cos(np.linspace(0,np.pi,a2)))/2 + lr_end # cosine annealing
lrs = np.concatenate((lrs_first, lrs_second))
return lrs
class OneCycleScheduler(Callback):
"""My modification of Keras' Learning rate scheduler to do 1Cycle learning
which increments per BATCH, not per epoch
Keyword arguments
**kwargs: keyword arguments to pass to get_1cycle_schedule()
Also, verbose: int. 0: quiet, 1: update messages.
Sample usage (from my train.py):
lrsched = OneCycleScheduler(lr_max=1e-4, n_data_points=X_train.shape[0],
epochs=epochs, batch_size=batch_size, verbose=1)
"""
def __init__(self, **kwargs):
super(OneCycleScheduler, self).__init__()
self.verbose = kwargs.get('verbose', 0)
self.lrs = get_1cycle_schedule(**kwargs)
self.iteration = 0
def on_batch_begin(self, batch, logs=None):
lr = self.lrs[self.iteration]
K.set_value(self.model.optimizer.lr, lr) # here's where the assignment takes place
if self.verbose > 0:
print('\nIteration %06d: OneCycleScheduler setting learning '
'rate to %s.' % (self.iteration, lr))
self.iteration += 1
def on_epoch_end(self, epoch, logs=None): # this is unchanged from Keras LearningRateScheduler
logs = logs or {}
logs['lr'] = K.get_value(self.model.optimizer.lr)
self.iteration = 0
def squeeze_excite_block(input, ratio=16):
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = init._keras_shape[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
if K.image_data_format() == 'channels_first':
se = Permute((3, 1, 2))(se)
x = multiply([init, se])
return x
def spatial_squeeze_excite_block(input):
se = Conv2D(1, (1, 1), activation='sigmoid', use_bias=False,
kernel_initializer='he_normal')(input)
x = multiply([input, se])
return x
def channel_spatial_squeeze_excite(input, ratio=16):
cse = squeeze_excite_block(input, ratio)
sse = spatial_squeeze_excite_block(input)
x = add([cse, sse])
return x
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from keras.regularizers import l2
from keras.utils import conv_utils
from keras.utils.data_utils import get_file
from keras.engine.topology import get_source_inputs
from keras_applications.imagenet_utils import _obtain_input_shape
from keras.applications.resnet50 import preprocess_input
from keras.applications.imagenet_utils import decode_predictions
from keras import backend as K
__all__ = ['SEResNet', 'SEResNet50', 'SEResNet101', 'SEResNet154', 'preprocess_input', 'decode_predictions']
WEIGHTS_PATH = ""
WEIGHTS_PATH_NO_TOP = ""
def SEResNet(input_shape=None,
initial_conv_filters=64,
depth=[3, 4, 6, 3],
filters=[64, 128, 256, 512],
width=1,
bottleneck=False,
weight_decay=1e-4,
include_top=True,
weights=None,
input_tensor=None,
pooling=None,
classes=1000):
if weights not in {'imagenet', None}:
raise ValueError('The `weights` argument should be either '
'`None` (random initialization) or `imagenet` '
'(pre-training on ImageNet).')
if weights == 'imagenet' and include_top and classes != 1000:
raise ValueError('If using `weights` as imagenet with `include_top`'
' as true, `classes` should be 1000')
assert len(depth) == len(filters), "The length of filter increment list must match the length " \
"of the depth list."
# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=32,
data_format=K.image_data_format(),
require_flatten=False)
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
x = _create_se_resnet(classes, img_input, include_top, initial_conv_filters,
filters, depth, width, bottleneck, weight_decay, pooling)
# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
model = Model(inputs, x, name='resnext')
# load weights
return model
def SEResNet50(input_shape=None,
width=1,
bottleneck=True,
weight_decay=1e-4,
include_top=True,
weights=None,
input_tensor=None,
pooling=None,
classes=1000):
return SEResNet(input_shape,
width=width,
bottleneck=bottleneck,
weight_decay=weight_decay,
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
pooling=pooling,
classes=classes)
def SEResNet101(input_shape=None,
width=1,
bottleneck=True,
weight_decay=1e-4,
include_top=True,
weights=None,
input_tensor=None,
pooling=None,
classes=1000):
return SEResNet(input_shape,
depth=[3, 6, 23, 3],
width=width,
bottleneck=bottleneck,
weight_decay=weight_decay,
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
pooling=pooling,
classes=classes)
def SEResNet154(input_shape=(SIZE,SIZE,3),
width=1,
bottleneck=True,
weight_decay=1e-4,
include_top=True,
weights=None,
input_tensor=None,
pooling=None,
classes=1103):
return SEResNet(input_shape,
depth=[3, 8, 36, 3],
width=width,
bottleneck=bottleneck,
weight_decay=weight_decay,
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
pooling=pooling,
classes=classes)
def _resnet_block(input, filters, k=1, strides=(1, 1)):
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
x = BatchNormalization(axis=channel_axis)(input)
x = Activation('relu')(x)
if strides != (1, 1) or init._keras_shape[channel_axis] != filters * k:
init = Conv2D(filters * k, (1, 1), padding='same', kernel_initializer='he_normal',
use_bias=False, strides=strides)(x)
x = Conv2D(filters * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False, strides=strides)(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = Conv2D(filters * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
# squeeze and excite block
x = squeeze_excite_block(x)
m = add([x, init])
return m
def _resnet_bottleneck_block(input, filters, k=1, strides=(1, 1)):
''' Adds a pre-activation resnet block with bottleneck layers
Args:
input: input tensor
filters: number of output filters
k: width factor
strides: strides of the convolution layer
Returns: a keras tensor
'''
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
bottleneck_expand = 4
x = BatchNormalization(axis=channel_axis)(input)
x = Activation('relu')(x)
if strides != (1, 1) or init._keras_shape[channel_axis] != bottleneck_expand * filters * k:
init = Conv2D(bottleneck_expand * filters * k, (1, 1), padding='same', kernel_initializer='he_normal',
use_bias=False, strides=strides)(x)
x = Conv2D(filters * k, (1, 1), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = Conv2D(filters * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False, strides=strides)(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = Conv2D(bottleneck_expand * filters * k, (1, 1), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
# squeeze and excite block
x = squeeze_excite_block(x)
m = add([x, init])
return m
def _create_se_resnet(classes, img_input, include_top, initial_conv_filters, filters,
depth, width, bottleneck, weight_decay, pooling):
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
N = list(depth)
# block 1 (initial conv block)
x = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, strides=(2, 2),
kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
# block 2 (projection block)
for i in range(N[0]):
if bottleneck:
x = _resnet_bottleneck_block(x, filters[0], width)
else:
x = _resnet_block(x, filters[0], width)
# block 3 - N
for k in range(1, len(N)):
if bottleneck:
x = _resnet_bottleneck_block(x, filters[k], width, strides=(2, 2))
else:
x = _resnet_block(x, filters[k], width, strides=(2, 2))
for i in range(N[k] - 1):
if bottleneck:
x = _resnet_bottleneck_block(x, filters[k], width)
else:
x = _resnet_block(x, filters[k], width)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
if include_top:
x = GlobalAveragePooling2D()(x)
x = Dense(classes, use_bias=False, kernel_regularizer=l2(weight_decay),
activation='softmax')(x)
else:
if pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = GlobalMaxPooling2D()(x)
return x
# create callbacks list
from keras.callbacks import (ModelCheckpoint, LearningRateScheduler,
EarlyStopping, ReduceLROnPlateau,CSVLogger)
from sklearn.model_selection import train_test_split
epochs = 40; batch_size = 16
checkpoint = ModelCheckpoint('../working/Resnet50_focal.h5', monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = True)
reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4,
verbose=1, mode='auto', epsilon=0.0001)
early = EarlyStopping(monitor="val_loss",
mode="min",
patience=9)
csv_logger = CSVLogger(filename='../working/training_log.csv',
separator=',',
append=True)
# split data into train, valid
indexes = np.arange(train_dataset_info.shape[0])
train_indexes, valid_indexes = train_test_split(indexes, test_size=0.15, random_state=8)
# create train and valid datagens
train_generator = data_generator.create_train(
train_dataset_info[train_indexes], batch_size, (SIZE,SIZE,3), augument=True)
train_generator_warmup = data_generator.create_train(
train_dataset_info[train_indexes], batch_size, (SIZE,SIZE,3), augument=False)
validation_generator = data_generator.create_valid(
train_dataset_info[valid_indexes], batch_size, (SIZE,SIZE,3), augument=False)
lrsched = OneCycleScheduler(lr_max=1e-4, n_data_points=len(train_indexes),
epochs=1, batch_size=batch_size, verbose=0)
# callbacks_list = [checkpoint, csv_logger, lrsched]
callbacks_list = [checkpoint, csv_logger, reduceLROnPlat]
# warm up model
model = SEResNet154()
for layer in model.layers:
layer.trainable = True
model.compile(
loss='binary_crossentropy',
optimizer=Adam(1e-3))
# model.summary()
model.fit_generator(
train_generator_warmup,
steps_per_epoch=np.ceil(float(len(train_indexes)) / float(batch_size)),
epochs=2,
max_queue_size=16, workers=WORKERS, use_multiprocessing=True,
verbose=1)
# train all layers
model.compile(loss='binary_crossentropy',
# loss=focal_loss,
optimizer=Adam(lr=1e-4))
model.fit_generator(
train_generator,
steps_per_epoch=np.ceil(float(len(train_indexes)) / float(batch_size)),
validation_data=validation_generator,
validation_steps=np.ceil(float(len(valid_indexes)) / float(batch_size)),
epochs=5,
verbose=1,
max_queue_size=16, workers=WORKERS, use_multiprocessing=True,
callbacks=callbacks_list)
print(os.listdir('../working/'))
submit = pd.read_csv('../input/imet-2019-fgvc6/sample_submission.csv')
model.load_weights('../working/Resnet50_focal.h5')
predicted = []
'''Search for the best threshold regarding the validation set'''
BATCH = 512
fullValGen = data_generator.create_valid(
train_dataset_info[valid_indexes], BATCH, (SIZE,SIZE,3))
n_val = round(train_dataset_info.shape[0]*0.15)//BATCH
print(n_val)
lastFullValPred = np.empty((0, NUM_CLASSES))
lastFullValLabels = np.empty((0, NUM_CLASSES))
for i in tqdm(range(n_val+1)):
im, lbl = next(fullValGen)
scores = model.predict(im)
lastFullValPred = np.append(lastFullValPred, scores, axis=0)
lastFullValLabels = np.append(lastFullValLabels, lbl, axis=0)
print(lastFullValPred.shape, lastFullValLabels.shape)
def my_f2(y_true, y_pred):
assert y_true.shape[0] == y_pred.shape[0]
tp = np.sum((y_true == 1) & (y_pred == 1))
tn = np.sum((y_true == 0) & (y_pred == 0))
fp = np.sum((y_true == 0) & (y_pred == 1))
fn = np.sum((y_true == 1) & (y_pred == 0))
p = tp / (tp + fp + K.epsilon())
r = tp / (tp + fn + K.epsilon())
f2 = (1+beta_f2**2)*p*r / (p*beta_f2**2 + r + 1e-15)
return f2
def find_best_fixed_threshold(preds, targs, do_plot=True):
score = []
thrs = np.arange(0, 0.5, 0.01)
for thr in tqdm(thrs):
score.append(my_f2(targs, (preds > thr).astype(int) ))
score = np.array(score)
pm = score.argmax()
best_thr, best_score = thrs[pm], score[pm].item()
print(f'thr={best_thr:.3f}', f'F2={best_score:.3f}')
if do_plot:
plt.plot(thrs, score)
plt.vlines(x=best_thr, ymin=score.min(), ymax=score.max())
plt.text(best_thr+0.03, best_score-0.01, f'$F_{2}=${best_score:.3f}', fontsize=14);
plt.show()
return best_thr, best_score
best_thr, best_score = find_best_fixed_threshold(lastFullValPred, lastFullValLabels, do_plot=True)
for i, name in tqdm(enumerate(submit['id'])):
path = os.path.join('../input/imet-2019-fgvc6/test/', name)
image = data_generator.load_image(path, (SIZE,SIZE,3))
score_predict = model.predict(image[np.newaxis]/255.)
# print(score_predict)
label_predict = np.arange(NUM_CLASSES)[score_predict[0]>=best_thr]
# print(label_predict)
str_predict_label = ' '.join(str(l) for l in label_predict)
predicted.append(str_predict_label)
submit['attribute_ids'] = predicted
submit.to_csv('submission.csv', index=False)