Kaggle competition: NFL Big Data Bowl
df = pd.read_csv(".. /input/train.csv")
 print("Total number of unique ids:",df.Id.count())
 print("Total number of images:", df.Id.count() *4 )<load_from_csv>
env = nflrush.make_env()
path_to_train = '/kaggle/input/train/'
data = pd.read_csv('/kaggle/input/train.csv')
 train_dataset_info = []
for name, labels in zip(data['Id'], data['Target'].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)
 train_ids, test_ids, train_targets, test_target = train_test_split(
 data['Id'], data['Target'], test_size=0.2, random_state=42 )<train_model>
import keras
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping
from keras.optimizers import Adam
from keras.utils import plot_model
import keras.backend as K
import matplotlib.patches as patches
class data_generator:
def create_train(dataset_info, batch_size, shape, augument=True):
while True:
random_indexes = np.random.choice(len(dataset_info), batch_size)
 batch_images = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_labels = np.zeros(( batch_size, 28))
for i, idx in enumerate(random_indexes):
image = data_generator.load_image(
 dataset_info[idx]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images[i] = image
batch_labels[i][dataset_info[idx]['labels']] = 1
yield batch_images, batch_labels
def load_image(path, shape):
R = np.array(Image.open(path+'_red.png'))
G = np.array(Image.open(path+'_green.png'))
B = np.array(Image.open(path+'_blue.png'))
Y = np.array(Image.open(path+'_yellow.png'))
image = np.stack((
R,
G, 
(B+Y)/2),-1)
 image = cv2.resize(image,(shape[0], shape[1]))
image = np.divide(image, 255)
 return image
def augment(image):
augment_img = iaa.Sequential([
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<train_model>

 TARGET = 'Yards'
dropcols = [TARGET]
dropcols += ['HomeTeamAbbr', 'VisitorTeamAbbr', 'DisplayName', 'PlayerCollegeName',
"NflId", "GameId", 'PlayId', "Season", "NflIdRusher",
"JerseyNumber", 'PlayerBirthDate']
dropcols += ["GameWeather", 'FieldPosition', 'StadiumType', 'Location', 'Stadium',
'WindSpeed', "GameClock", 'Humidity', "Temperature",
'WindDirection', 'Turf']
dropcols += ['FieldPosition', 'OffensePersonnel', 'DefensePersonnel',
'PossessionTeam', 'Week', 'PlayDirection',
'Team', 'TeamName', 'TimeHandoff', 'TimeSnap', 'OffenseFormation']
input_shape=(299,299,3)
 train_datagen = data_generator.create_train(
 train_dataset_info, 5, input_shape, augument=True)
 images, labels = next(train_datagen)
 fig, ax = plt.subplots(1,5,figsize=(25,5))
for i in range(5):
ax[i].imshow(images[i])
 print('min: {0}, max: {1}'.format(images.min() , images.max()))<choose_model_class>
train_df = pd.read_csv(".. /input/nfl-big-data-bowl-2020/train.csv", low_memory=False)
 print('Train size: ', train_df.shape )
def create_model(input_shape, n_out):
input_tensor = Input(shape=input_shape)
 base_model = InceptionResNetV2(include_top=False,
weights='imagenet',
input_shape=input_shape)
 bn = BatchNormalization()(input_tensor)
 x = base_model(bn)
 x = Conv2D(32, kernel_size=(1,1), activation='relu' )(x)
 x = Flatten()(x)
 x = Dropout(0.5 )(x)
 x = Dense(1024, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 output = Dense(n_out, activation='sigmoid' )(x)
 model = Model(input_tensor, output)
 return model<compute_test_metric>
train_df['TeamName'] = np.nan
train_df.loc[train_df['Team'] == 'home', 'TeamName'] = train_df.loc[train_df['Team'] == 'home', 'HomeTeamAbbr']
train_df.loc[train_df['Team'] == 'away', 'TeamName'] = train_df.loc[train_df['Team'] == 'away', 'VisitorTeamAbbr']
def f1(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def show_history(history):
fig, ax = plt.subplots(1, 3, figsize=(15,5))
ax[0].set_title('loss')
 ax[0].plot(history.epoch, history.history["loss"], label="Train loss")
 ax[0].plot(history.epoch, history.history["val_loss"], label="Validation loss")
 ax[1].set_title('f1')
 ax[1].plot(history.epoch, history.history["f1"], label="Train f1")
 ax[1].plot(history.epoch, history.history["val_f1"], label="Validation f1")
 ax[2].set_title('acc')
 ax[2].plot(history.epoch, history.history["acc"], label="Train acc")
 ax[2].plot(history.epoch, history.history["val_acc"], label="Validation acc")
 ax[0].legend()
ax[1].legend()
ax[2].legend()<train_model>
fillnan_dict_OffenseFormation = train_df.groupby('TeamName')['OffenseFormation'].apply(pd.Series.mode)
 fillnan_dict_StadiumType = train_df.groupby('HomeTeamAbbr')['StadiumType'].apply(pd.Series.mode)
 fillnan_dict_WindSpeed = train_df.groupby(['Week','GameId'])['WindSpeed'].apply(pd.Series.mode)
 fillnan_dict_WindDirection = train_df.groupby(['Week','GameId'])['WindDirection'].apply(pd.Series.mode)
 fillnan_dict_OffenseFormation.index = fillnan_dict_OffenseFormation.index.droplevel(1)
 fillnan_dict_OffenseFormation = fillnan_dict_OffenseFormation.to_dict()
fillnan_dict_StadiumType.index = fillnan_dict_StadiumType.index.droplevel(1)
 fillnan_dict_StadiumType = fillnan_dict_StadiumType.to_dict()
fillnan_dict_WindSpeed.index = fillnan_dict_WindSpeed.index.droplevel(1)
 fillnan_dict_WindSpeed = fillnan_dict_WindSpeed.to_dict()
fillnan_dict_WindDirection.index = fillnan_dict_WindDirection.index.droplevel(1)
 fillnan_dict_WindDirection = fillnan_dict_WindDirection.to_dict()
yardsleft_target_mean = None
oh = OneHotEncoder(sparse=False)
 le = LabelEncoder()
scaler = StandardScaler()
position_cols = list(train_df['Position'].unique())
 train_df['IsRusher'] = train_df.NflId == train_df.NflIdRusher
dropcols += ['IsRusher']
model = create_model(
 input_shape,
n_out=28)
 checkpointer = ModelCheckpoint(
 '/kaggle/working/InceptionResNetV2.model',
verbose=2, save_best_only=True)
 BATCH_SIZE = 10
INPUT_SHAPE =(299,299,3)
 train_generator = data_generator.create_train(
 train_dataset_info[train_ids.index], BATCH_SIZE, INPUT_SHAPE, augument=False)
 validation_generator = data_generator.create_train(
 train_dataset_info[test_ids.index], 256, INPUT_SHAPE, augument=False)
 model.layers[0].trainable = True
model.compile(
 loss='binary_crossentropy',
optimizer=Adam(1e-4),
metrics=['acc', f1])
 history = model.fit_generator(
 train_generator,
steps_per_epoch=500,
validation_data=next(validation_generator),
epochs=35,
verbose=1,
callbacks=[checkpointer])
 show_history(history )<save_to_csv>
def preprocess(train_df, test=False):
global dropcols,position_cols
train_df.loc[train_df.VisitorTeamAbbr == "ARI", 'VisitorTeamAbbr'] = "ARZ"
train_df.loc[train_df.HomeTeamAbbr == "ARI", 'HomeTeamAbbr'] = "ARZ"
train_df.loc[train_df.VisitorTeamAbbr == "BAL", 'VisitorTeamAbbr'] = "BLT"
train_df.loc[train_df.HomeTeamAbbr == "BAL", 'HomeTeamAbbr'] = "BLT"
train_df.loc[train_df.VisitorTeamAbbr == "CLE", 'VisitorTeamAbbr'] = "CLV"
train_df.loc[train_df.HomeTeamAbbr == "CLE", 'HomeTeamAbbr'] = "CLV"
train_df.loc[train_df.VisitorTeamAbbr == "HOU", 'VisitorTeamAbbr'] = "HST"
train_df.loc[train_df.HomeTeamAbbr == "HOU", 'HomeTeamAbbr'] = "HST"
WindMap={'SW':"SW", 'NNE':"NE", 'SE':"SE",'East':"E", np.nan:np.nan, 'NE':"NE", 'North':"N", 'S':"S", 'Northwest':"NW",
'SouthWest':"SW", 'ENE':"NE", 'ESE':"SE", 'SSW':"SW", 'NW':"NW", 'Northeast':"NE", 'From S':"S", 'W':"W",
'South':"S", 'West-Southwest':"SW", 'E':"E", '13':'13','N':"N", 'NNW':"NW",
'South Southeast':"SE", 'SSE':"SE", 'West':"W", 'WSW':"SW", 'From SW':"SW", 'WNW':"NW", 's':"S",
'NorthEast':"NE", 'from W':"W", 'W-NW':"NW", 'South Southwest':"SW", 'Southeast':"SE",
'From WSW':"SW", 'West Northwest':"NW", 'Calm':"Calm", 'From SSE':"SE", 'From W':"W",
'East North East':"NE", 'From ESE':"SE", 'EAST':"E", 'East Southeast':"SE",
'From SSW':"SW", '8':"8", 'North East':"NE", 'Southwest':"SW", 'North/Northwest':"NW",
'From NNE':"NE", '1':"NE", 'N-NE':"NE", 'W-SW':"SW", 'From NNW':"NW"}
Turf_map={"Grass":"Grass","grass":"Grass",'Twenty-Four/Seven Turf':"FieldTurf",
'DD GrassMaster':'DD GrassMaster','A-Turf Titan':'A-Turf Titan',
"FieldTurf 360":"FieldTurf","FieldTurf360":"FieldTurf",'UBU Sports Speed S5-M':'UBU Sports Speed S5-M',
"Field Turf":"FieldTurf","Natural":"Natural", "Natural Grass":"Natural",
"Natural grass":"Natural","Naturall Grass":"Natural",'SISGrass':'Grass',
'UBU Speed Series-S5-M':"UBU Speed Series-S5-M","Artifical":"Artifical","Artificial":"Artifical"}
Type_map={'Outdoor':'Outdoor','Outdoors':'Outdoor','Oudoor':'Outdoor'
,'Ourdoor':'Outdoor','Outside':'Outdoor','Outdor':'Outdoor'
,'Outddors':'Outdoor','Retractable Roof':'Retractable Roof',
'Retr.Roof-Closed':'Retractable Roof','Retr.Roof - Closed':'Retractable Roof',
'Retr.Roof Closed':'Retractable Roof', "Outdoor Retr Roof-Open":"Outdoor",
"Indoors":"Indoor","Indoor":"Indoor","Indoor, Roof Closed":"Indoor",
"Doom":"Doom","Domed, closed":"Doom","Domed, open":"Doom","Closed Dome":"Doom"
,"Bowl":"Bowl","Open":"Outdoor","Cloudy":"Outdoor","Domed, Open":"Doom"
,'Retr.Roof-Open':'Retractable Roof',"Retr.Roof - Open":"Retractable Roof",
"Indoor, Open Roof":"Indoor","Heinz Field" :"Heinz Field"}
if test:
train_df['TeamName'] = np.nan
train_df.loc[train_df['Team'] == 'home', 'TeamName'] = train_df.loc[train_df['Team'] == 'home', 'HomeTeamAbbr']
train_df.loc[train_df['Team'] == 'away', 'TeamName'] = train_df.loc[train_df['Team'] == 'away', 'VisitorTeamAbbr']
train_df.StadiumType = train_df.StadiumType.map(Type_map)
 train_df.Turf = train_df.Turf.map(Turf_map)
 train_df.WindDirection = train_df.WindDirection.map(WindMap)
 train_df['OffenseFormation'] = train_df.set_index('TeamName')['OffenseFormation'].fillna(fillnan_dict_OffenseFormation ).values
train_df['WindSpeed'] = train_df.set_index(['Week','GameId'])['WindSpeed'].fillna(fillnan_dict_WindSpeed ).values
train_df['WindDirection'] = train_df.set_index(['Week','GameId'])['WindDirection'].fillna(fillnan_dict_WindDirection ).values
def transform_gameclock(txt):
txt = txt.split(':')
 ans = int(txt[0])*60 + int(txt[1])+ int(txt[2])/60
return ans
train_df['GameClock'] = train_df['GameClock'].apply(transform_gameclock)
 transform_time = lambda x: datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ")
 train_df['TimeHandoff'] = train_df['TimeHandoff'].apply(transform_time)
 train_df['TimeSnap'] = train_df['TimeSnap'].apply(transform_time)
 train_df['TimeDelta'] =(train_df['TimeHandoff'] - train_df['TimeSnap'] ).dt.seconds
transform_birthdaytime = lambda x: datetime.datetime.strptime(x, "%m/%d/%Y")
 train_df['PlayerBirthDate'] = train_df['PlayerBirthDate'].apply(transform_birthdaytime)
 seconds_in_year = 60*60*24*365.25
train_df['PlayerAge'] =(train_df['TimeHandoff'] - train_df['PlayerBirthDate'] ).dt.seconds / seconds_in_year
transform_playerheight = lambda x: 12*int(x.split('-')[0])+int(x.split('-')[1])
 train_df['PlayerHeight'] = train_df['PlayerHeight'].apply(transform_playerheight)
 train_df['WindSpeed'] = train_df['WindSpeed'].apply(lambda x: x.lower().replace('mph', '' ).strip() if not pd.isna(x)else x)
 train_df['WindSpeed'] = train_df['WindSpeed'].apply(lambda x:(int(x.split('-')[0])+int(x.split('-')[1])) /2 if not pd.isna(x)and '-' in x else x)
 train_df['WindSpeed'] = train_df['WindSpeed'].apply(lambda x:(int(x.split() [0])+int(x.split() [-1])) /2 if not pd.isna(x)and type(x)!=float and 'gusts up to' in x else x)
 def personnel_split(x, pattern):
s = re.findall(f"([1-9]){pattern}", x)
 if len(s)> 0:
return int(s[0])
 else:
return 0
train_df['OffensePersonnel_RB'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='RB')
 train_df['OffensePersonnel_DB'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='DB')
 train_df['OffensePersonnel_DL'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='DL')
 train_df['OffensePersonnel_LB'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='LB')
 train_df['OffensePersonnel_OL'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='OL')
 train_df['OffensePersonnel_QB'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='QB')
 train_df['OffensePersonnel_TE'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='TE')
 train_df['OffensePersonnel_WR'] = train_df['OffensePersonnel'].apply(personnel_split, pattern='WR')
 train_df['DefensePersonnel_DB'] = train_df['DefensePersonnel'].apply(personnel_split, pattern='DB')
 train_df['DefensePersonnel_DL'] = train_df['DefensePersonnel'].apply(personnel_split, pattern='DL')
 train_df['DefensePersonnel_LB'] = train_df['DefensePersonnel'].apply(personnel_split, pattern='LB')
 train_df['DefensePersonnel_OL'] = train_df['DefensePersonnel'].apply(personnel_split, pattern='OL')
 train_df['Orientation'] = np.where(train_df['Season'] == 2017,(360+train_df['Orientation']+90 ).mod(360), train_df['Orientation'])
 train_df['Dir_rad'] = np.mod(90 - train_df.Dir, 360)* math.pi/180.0
train_df['YardLine_std'] = np.where(train_df.FieldPosition == train_df.PossessionTeam, train_df.YardLine, 100 - train_df.YardLine)
 train_df['X'] = np.where(train_df['PlayDirection'] == 'right', train_df['X'], 120-train_df['X'])
 train_df['Y'] = np.where(train_df['PlayDirection'] == 'right', train_df['Y'], 160/3 - train_df['Y'])
 train_df['Orientation'] = np.where(train_df['PlayDirection'] == 'right', train_df['Orientation'],(360 - train_df['Orientation'] ).mod(360))
train_df['Dir'] = np.where(train_df['PlayDirection'] == 'right', train_df['Dir'], np.mod(np.pi + train_df['Dir_rad'], 2*np.pi))
dropcols += ['YardLine']
tmp = np.where(train_df['FieldPosition'] == train_df['HomeTeamAbbr'], 100 - train_df['YardLine'], train_df['YardLine'])
 train_df['YardsLeft'] = np.where(train_df['PlayDirection'] == 'right', tmp, 100 - tmp)
 if not test:
train_df.drop(train_df.index[(train_df['YardsLeft'] < train_df['Yards'])|(( train_df['YardsLeft']-100)> train_df['Yards'])], inplace=True)
 del tmp
train_df['IsRusher'] =(train_df['NflId'] == train_df['NflIdRusher'])
 dropcols.append('IsRusher')
 if not test:
oh.fit(train_df[['OffenseFormation']])
 val = oh.transform(train_df[['OffenseFormation']])
 count = 0
for j in range(val.shape[1]):
train_df[f'x{count}'] = val[:, j]
count += 1
if not test:
tmp = train_df.groupby(by=['PlayId', 'Position'] ).size().unstack(level=1)
 tmp = tmp.fillna(0)
 tmp = tmp.astype(int)
 tmp.columns = [f"Position_{i}" for i in tmp.columns]
train_df = pd.merge(train_df, tmp, left_on='PlayId', right_index=True, how='left')
 else:
tmp = train_df.groupby(by=['PlayId', 'Position'] ).size().unstack(level=1)
 for c in position_cols:
if c not in tmp.columns:
tmp[c] = np.nan
tmp.columns = [f"Position_{i}" for i in tmp.columns]
tmp = tmp.fillna(0)
 tmp = tmp.astype(int)
 train_df = pd.merge(train_df, tmp, left_on='PlayId', right_index=True, how='left')
 dropcols += ['Position']
if not test:
tmp = pd.get_dummies(train_df['Down'])
 tmp.columns = [f"Down_{i}" for i in tmp.columns]
train_df = pd.concat([train_df, tmp], axis=1)
 else:
downs = [1, 2, 3, 4]
down = train_df['Down'].iloc[0]
downs.remove(down)
 train_df[f'Down_{down}'] = 1
for d in downs:
train_df[f'Down_{d}'] = 0
dropcols += ['Down']
if not test:
tmp = pd.get_dummies(train_df['Quarter'])
 tmp.columns = [f"Quarter_{i}" for i in tmp.columns]
train_df = pd.concat([train_df, tmp], axis=1)
 else:
downs = [1, 2, 3, 4, 5]
down = train_df['Quarter'].iloc[0]
downs.remove(down)
 train_df[f'Quarter_{down}'] = 1
for d in downs:
train_df[f'Quarter_{d}'] = 0
dropcols += ['Quarter']
train_df = feature_engineering(train_df, test)
 train_df = train_df.sort_values(by=['PlayId', 'Team', 'IsRusher', 'JerseyNumber'])
 return train_df
submit = pd.read_csv('.. /input/sample_submission.csv')
 predicted = []
for name in tqdm(submit['Id']):
path = os.path.join('.. /input/test/', name)
 image = data_generator.load_image(path, INPUT_SHAPE)
 score_predict = model.predict(image[np.newaxis])[0]
label_predict = np.arange(28)[score_predict>=0.1]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label)
 submit['Predicted'] = predicted
submit.to_csv('submission.csv', index=False )<load_from_csv>
%%time
train_df = preprocess(train_df )
df = pd.read_csv(".. /input/train.csv")
 print("Total number of unique ids:",df.Id.count())
 print("Total number of images:", df.Id.count() *4 )<load_from_csv>
cat_features = []
dense_features = []
for col in train_df.columns:
if col in dropcols:
continue
if train_df[col].dtype =='object':
cat_features.append(col)
 if train_df[col].isnull().sum() > 0:
print("fillna %s", col)
 train_df[col] = train_df[col].fillna(train_df[col].mode())
 print("*cat*", col, len(train_df[col].unique()))
else:
dense_features.append(col)
 if train_df[col].isnull().sum() > 0:
print("fillna %s", col)
 train_df[col] = train_df[col].fillna(train_df[col].mean())
 print("!dense!", col, len(train_df[col].unique()))
path_to_train = '/kaggle/input/train/'
data = pd.read_csv('/kaggle/input/train.csv')
 train_dataset_info = []
for name, labels in zip(data['Id'], data['Target'].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)
 train_ids, test_ids, train_targets, test_target = train_test_split(
 data['Id'], data['Target'], test_size=0.2, random_state=42 )<train_model>
play_train_df = None
game_features = []
player_features = []
for col in train_df.columns:
if col not in dropcols:
if train_df[col].dtype != np.object and train_df[col][:22].std() > 1e-10:
player_features.append(col)
 if train_df[col].dtype == np.object and train_df[col][:22].nunique() != 1:
player_features.append(col)
 game_features = [i for i in train_df.columns if i not in dropcols and i not in player_features]
class data_generator:
def create_train(dataset_info, batch_size, shape, augument=True):
while True:
random_indexes = np.random.choice(len(dataset_info), batch_size)
 batch_images = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_labels = np.zeros(( batch_size, 28))
for i, idx in enumerate(random_indexes):
image = data_generator.load_image(
 dataset_info[idx]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images[i] = image
batch_labels[i][dataset_info[idx]['labels']] = 1
yield batch_images, batch_labels
def load_image(path, shape):
R = np.array(Image.open(path+'_red.png'))
G = np.array(Image.open(path+'_green.png'))
B = np.array(Image.open(path+'_blue.png'))
Y = np.array(Image.open(path+'_yellow.png'))
image = np.stack((
R,
G, 
(B+Y)/2),-1)
 image = cv2.resize(image,(shape[0], shape[1]))
image = np.divide(image, 255)
 return image
def augment(image):
augment_img = iaa.Sequential([
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<train_model>
def transform(train_df, player_features, game_features,test=False):
X_train = np.array(train_df[player_features] ).reshape(-1, len(player_features)*22)
 X_play_col = np.zeros(shape=(X_train.shape[0], len(game_features)))
 for i, col in enumerate(game_features):
X_play_col[:, i] = train_df[col][::22]
X_train = X_play_col
y_train = np.zeros(shape=(X_train.shape[0], 199))
if not test:
for i,yard in enumerate(train_df[TARGET][::22]):
y_train[i, yard+99:] = np.ones(shape=(1, 100-yard))
if not test:
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 return X_train, y_train
input_shape=(299,299,3)
 train_datagen = data_generator.create_train(
 train_dataset_info, 5, input_shape, augument=True)
 images, labels = next(train_datagen)
 fig, ax = plt.subplots(1,5,figsize=(25,5))
for i in range(5):
ax[i].imshow(images[i])
 print('min: {0}, max: {1}'.format(images.min() , images.max()))<choose_model_class>
def crps(y_true, y_pred):
return K.mean(K.square(y_true - K.cumsum(y_pred, axis=1)) , axis=1 )
def create_model(input_shape, n_out):
model = Sequential()
model.add(InceptionResNetV2(include_top=False,input_shape= input_shape, pooling='avg', weights="imagenet"))
model.add(Dense(1616))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(n_out, activation='sigmoid'))
return model<compute_train_metric>
def get_model() :
x = keras.layers.Input(shape=[X_train.shape[1]])
 a = keras.layers.Dense(units=512, input_shape=[X_train.shape[1]], name="dens_1" )(x)
 a = keras.layers.ReLU()(a)
 a = keras.layers.BatchNormalization()(a)
 a = keras.layers.Dense(units=1024 )(a)
 a = keras.layers.ReLU()(a)
 a = keras.layers.BatchNormalization()(a)
 a = keras.layers.Dense(units=512 )(a)
 a = keras.layers.ReLU()(a)
 a = keras.layers.BatchNormalization()(a)
 output = keras.layers.Dense(units=199, activation='softmax', name = "real_output" )(a)
 out_reg = keras.layers.Dense(1, activation=None, name = "out_reg" )(a)
 model = keras.models.Model(inputs=[x], outputs=[output, out_reg])
 return model
def train_model(X_train, y_train, X_val, y_val, batch_size=64, epochs=200):
model = get_model()
model.compile(optimizer=Adam(learning_rate=0.005, beta_1=0.9, beta_2=0.99), loss=[crps, keras.losses.mae],
loss_weights=[1.0, 0.01])
 er = EarlyStopping(patience=10, min_delta=1e-4, restore_best_weights=True, monitor='val_loss')
 model.fit(X_train, y_train, epochs=epochs, callbacks=[er], validation_data=[X_val, y_val], batch_size=batch_size)
 return model
def f1(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def focal_loss_fixed(y_true, y_pred):
gamma=2
alpha=.25
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
pt_1 = K.clip(pt_1, 1e-3,.999)
 pt_0 = K.clip(pt_0, 1e-3,.999)
 return -K.sum(alpha * K.pow(1.- pt_1, gamma)* K.log(pt_1)) -K.sum(( 1-alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0))
def show_history(history):
fig, ax = plt.subplots(1, 3, figsize=(15,5))
ax[0].set_title('loss')
 ax[0].plot(history.epoch, history.history["loss"], label="Train loss")
 ax[0].plot(history.epoch, history.history["val_loss"], label="Validation loss")
 ax[1].set_title('f1')
 ax[1].plot(history.epoch, history.history["f1"], label="Train f1")
 ax[1].plot(history.epoch, history.history["val_f1"], label="Validation f1")
 ax[2].set_title('acc')
 ax[2].plot(history.epoch, history.history["acc"], label="Train acc")
 ax[2].plot(history.epoch, history.history["val_acc"], label="Validation acc")
 ax[0].legend()
ax[1].legend()
ax[2].legend()<train_model>
X_train, y_train = transform(train_df, player_features, game_features )
model = create_model(
 input_shape,
n_out=28)
 checkpointer = ModelCheckpoint(
 '/kaggle/working/InceptionResNetV2.model',
verbose=2, save_best_only=True)
 BATCH_SIZE = 10
INPUT_SHAPE =(299,299,3)
 train_generator = data_generator.create_train(
 train_dataset_info[train_ids.index], BATCH_SIZE, INPUT_SHAPE, augument=False)
 validation_generator = data_generator.create_train(
 train_dataset_info[test_ids.index], 256, INPUT_SHAPE, augument=False)
 model.layers[0].trainable = True
model.compile(
 loss= focal_loss_fixed,
optimizer=Adam(0.5e-3,beta_1=0.9, beta_2=0.99),
metrics=['acc', f1])
 history = model.fit_generator(
 train_generator,
steps_per_epoch=500,
validation_data=next(validation_generator),
epochs=30,
verbose=1,
callbacks=[checkpointer])
 show_history(history )<save_to_csv>
feature_name = [f"{i}_{k}" for k in range(int(( X_train.shape[1]-len(game_features)) / len(player_features)))for i in player_features] + game_features
submit = pd.read_csv('.. /input/sample_submission.csv')
 predicted = []
for name in tqdm(submit['Id']):
path = os.path.join('.. /input/test/', name)
 image = data_generator.load_image(path, INPUT_SHAPE)
 score_predict = model.predict(image[np.newaxis])[0]
label_predict = np.arange(28)[score_predict>=0.1]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label)
 submit['Predicted'] = predicted
submit.to_csv('submission.csv', index=False )<set_options>
X_train_df = pd.DataFrame(X_train, columns=feature_name )
warnings.filterwarnings("ignore")
 %load_ext autoreload
%autoreload<data_type_conversions>
y_train_raw = train_df['Yards'].iloc[::22].values
def open_4_channel(self,fname):
fname = str(fname)
 if fname.endswith('.png'):
fname = fname[:-4]
colors = ['red','green','blue','yellow']
flags = cv2.IMREAD_GRAYSCALE
img = [cv2.imread(fname+'_'+color+'.png', flags ).astype(np.float32)/255
for color in colors]
x = np.stack(img, axis=-1)
 return Image(pil2tensor(x, np.float32 ).float() )<categorify>
models = []
losses = []
batch_size = 64
epochs = 100
groups = train_df['GameId'].iloc[::22].values
tr_inds =(groups != 2018123011)&(groups != 2018123012)&(groups != 2018123013)&(groups != 2018123014)&(groups != 2018123015)
 val_inds = ~tr_inds
trainX, valX = X_train[tr_inds], X_train[val_inds]
trainY, valY = y_train[tr_inds], y_train[val_inds]
y_train_raw_tr = y_train_raw[tr_inds]
y_train_raw_val = y_train_raw[val_inds]
model = train_model(trainX, [trainY, y_train_raw_tr], valX, [valY, y_train_raw_val], batch_size, epochs)
 models.append(model)
 losses.append(model.history.history['val_real_output_loss'][-1])
 print("CROSS-VALID: ", np.mean(np.array(losses)) )
def show_batch_cstm(self, rows:int=5, ds_type:DatasetType=DatasetType.Train, **kwargs)->None:
"Show a batch of data in `ds_type` on a few `rows`."
x,y = self.one_batch(ds_type, True, True)
 x=x[:,:3,:,:]
if self.train_ds.x._square_show: rows = rows ** 2
xs = [self.train_ds.x.reconstruct(grab_idx(x, i, self._batch_first)) for i in range(rows)]
if has_arg(self.train_ds.y.reconstruct, 'x'):
ys = [self.train_ds.y.reconstruct(grab_idx(y, i), x=x)for i,x in enumerate(xs)]
else : ys = [self.train_ds.y.reconstruct(grab_idx(y, i)) for i in range(rows)]
print(ys)
 self.train_ds.x.show_xys(xs, ys, **kwargs)
<load_from_csv>
def make_pred(df, sample, env, models):
pred = np.mean([model.predict(df)[0] for model in models], axis=0)
 pred /= len(models)
 pred = np.clip(pred, 0, 1)
 return pred
path='.'
input_fldr=Path('.. /input/')
 trn_fldr=f'{input_fldr}/train/'
test_fldr=f'{input_fldr}/test/'
trn_lbl=f'{input_fldr}/train.csv'
sample_csv = f'{input_fldr}/sample_submission.csv'
df_trn=pd.read_csv(f'{input_fldr}/train.csv')
 df_trn.head() 
<define_variables>
def post_process(y_pred, df):
yardsleft = df['YardsLeft'].iloc[0]
max_back = -(99 - yardsleft)+ 99
max_push = yardsleft + 99
y_pred[0, :max_back] = 0
y_pred[max_push+1:] = 0
y_pred = y_pred / np.sum(y_pred, axis=1)
 return y_pred
<categorify><EOS> 
for test, sample in tqdm.tqdm(env.iter_test()):
test = preprocess(test, test=True)
 test_tmp = test.copy()
df, _ = transform(test_tmp, player_features, game_features, test=True)
 pred = make_pred(df, sample, env, models)
 pred = post_process(pred, test)
 pred = np.cumsum(pred, axis=1)
 pred = pd.DataFrame(data=pred,columns=sample.columns)
 pred = pred.clip(0, 1)
 env.predict(pred)
 env.write_submission_file()