Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<categorify>
bool_random_flip_left_right=0
bool_random_flip_up_down=0
bool_random_brightness=0
bool_random_contrast=0
bool_random_hue=0
bool_random_saturation=0
cutmix_rate=0.
 mixup_rate= 0.
 gridmask_rate = 0.
 pre_trained='imagenet'
dense_activation='softmax'
bool_lr_scheduler=0
tta_times=0
cross_validation_folds=5
bool_focal_loss = 0
label_smoothing_rate=0.
def addDamageDealtInformation(df):
df_res = df
df_res['mean_damage_dealt_team'] = df_res.groupby(['matchId', 'groupId'])['damageDealt'].transform(np.mean)
 df_res['max_damage_dealt_team'] = df_res.groupby(['matchId', 'groupId'])['damageDealt'].transform(np.max)
 df_res['min_damage_dealt_team'] = df_res.groupby(['matchId', 'groupId'])['damageDealt'].transform(np.min)
 df_res['mean_damage_dealt_all'] = df_res.groupby('matchId')['damageDealt'].transform(np.mean)
 df_res['max_damage_dealt_all'] = df_res.groupby('matchId')['damageDealt'].transform(np.max)
 df_res['min_damage_dealt_all'] = df_res.groupby('matchId')['damageDealt'].transform(np.min)
 return df_res
train = addDamageDealtInformation(train )<categorify>
bool_tta = tta_times and max(bool_random_flip_left_right,
bool_random_flip_up_down,
bool_random_brightness,
bool_random_contrast,
bool_random_hue,
bool_random_saturation)
 print(bool_tta)
 assert(bool_focal_loss and label_smoothing_rate)== 0 , 'focal_loss和label_smoothing最多同时使用一个'
assert(tta_times and cross_validation_folds)== 0 , 'focal_loss和label_smoothing最多同时使用一个'
def addDistanceWalked(df):
df_res = df
df_res['mean_distance_walked_team'] = df_res.groupby(['matchId', 'groupId'])['walkDistance'].transform(np.mean)
 df_res['max_distance_walked_team'] = df_res.groupby(['matchId', 'groupId'])['walkDistance'].transform(np.max)
 df_res['min_distance_walked_team'] = df_res.groupby(['matchId', 'groupId'])['walkDistance'].transform(np.min)
 df_res['mean_distance_walked_all'] = df_res.groupby(['matchId'])['walkDistance'].transform(np.mean)
 df_res['max_distance_walked_all'] = df_res.groupby(['matchId'])['walkDistance'].transform(np.max)
 df_res['min_distance_walked_all'] = df_res.groupby(['matchId'])['walkDistance'].transform(np.min)
 df_res['delta_mean_distance_walked_team_all'] = df_res.mean_distance_walked_team - df_res.mean_distance_walked_all
return df_res
train = addDistanceWalked(train )<categorify>
!pip install -U efficientnet
!pip install tensorflow_addons
print(tf.__version__)
 print(tf.keras.__version__ )
def addWeaponsAcquired(df):
df_res = df
df_res['mean_weapons_acquired_team'] = df_res.groupby(['matchId', 'groupId'])['weaponsAcquired'].transform(np.mean)
 df_res['max_weapons_acquired_team'] = df_res.groupby(['matchId', 'groupId'])['weaponsAcquired'].transform(np.max)
 df_res['min_weapons_acquired_team'] = df_res.groupby(['matchId', 'groupId'])['weaponsAcquired'].transform(np.min)
 df_res['mean_weapons_acquired_all'] = df_res.groupby(['matchId'])['weaponsAcquired'].transform(np.mean)
 df_res['max_weapons_acquired_all'] = df_res.groupby(['matchId'])['weaponsAcquired'].transform(np.max)
 df_res['min_weapons_acquired_all'] = df_res.groupby(['matchId'])['weaponsAcquired'].transform(np.min)
 df_res['delta_mean_weapons_acquired_team_all'] = df_res.mean_weapons_acquired_team - df_res.mean_weapons_acquired_all
return df_res
train = addWeaponsAcquired(train )<feature_engineering>
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
img_size = 768
EPOCHS = 18
lr_if_without_scheduler = 0.0003
nb_classes = 4
print('BATCH_SIZE是：',BATCH_SIZE)
 path='.. /input/plant-pathology-2020-fgvc7/'
train = pd.read_csv(path + 'train.csv')
 test = pd.read_csv(path + 'test.csv')
 sub = pd.read_csv(path + 'sample_submission.csv')
 train_labels = train.loc[:, 'healthy':].values
print('
',train_labels[0])
 train_paths = train.image_id.apply(lambda x: path + '/images/' + x + '.jpg' ).values
img = plt.imread(train_paths[0])
 print('
',img.shape)
 plt.imshow(img)
 if tpu:
path = GCS_DS_PATH
train_paths = train.image_id.apply(lambda x: path + '/images/' + x + '.jpg' ).values
test_paths = test.image_id.apply(lambda x: path + '/images/' + x + '.jpg' ).values
other_features = ['assists', 'boosts', 'DBNOs', 'headshotKills', 'heals',
'revives', 'vehicleDestroys', 'killStreaks', 'roadKills']
def addOtherDeltaFeatures(df):
df_res = df
for f in other_features:
df_res['mean_%s_team' % f] = df_res.groupby(['matchId', 'groupId'])[f].transform(np.mean)
 df_res['mean_%s_all' % f] = df_res.groupby('matchId')[f].transform(np.mean)
 df_res['delta_mean_%s_team_all' % f] = df_res['mean_%s_team' % f] - df_res['mean_%s_all' % f]
return df_res
train = addOtherDeltaFeatures(train)
<drop_column>
def decode_image(filename, label=None, image_size=(img_size, img_size)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_aug(image, label=None):
if bool_random_flip_left_right:
image = tf.image.random_flip_left_right(image)
 if bool_random_flip_up_down:
image = tf.image.random_flip_up_down(image)
 if bool_random_brightness:
image = tf.image.random_brightness(image,0.2)
 if bool_random_contrast:
image = tf.image.random_contrast(image,0.6,1.4)
 if bool_random_hue:
image = tf.image.random_hue(image,0.07)
 if bool_random_saturation:
image = tf.image.random_saturation(image,0.5,1.5)
 if label is None:
return image
else:
return image, label
corr = train[['winPlacePerc',
'delta_median_ranking_team_all',
'delta_median_kill_place_team_all',
'delta_mean_distance_walked_team_all',
'delta_mean_weapons_acquired_team_all',
'delta_mean_assists_team_all',
'delta_mean_boosts_team_all', 'delta_mean_DBNOs_team_all',
'delta_mean_headshotKills_team_all', 'delta_mean_heals_team_all',
'delta_mean_revives_team_all', 'delta_mean_vehicleDestroys_team_all',
'delta_mean_killStreaks_team_all', 'delta_mean_roadKills_team_all']].corr()<categorify>
def cutmix(image, label, PROBABILITY = cutmix_rate):
DIM = img_size
imgs = []; labs = []
for j in range(BATCH_SIZE):
P = tf.cast(tf.random.uniform([], 0, 1)<= PROBABILITY, tf.int32)
 k = tf.cast(tf.random.uniform([], 0, BATCH_SIZE), tf.int32)
 x = tf.cast(tf.random.uniform([], 0, DIM), tf.int32)
 y = tf.cast(tf.random.uniform([], 0, DIM), tf.int32)
 b = tf.random.uniform([], 0, 1)
 WIDTH = tf.cast(DIM * tf.math.sqrt(1-b),tf.int32)* P
ya = tf.math.maximum(0,y-WIDTH//2)
 yb = tf.math.minimum(DIM,y+WIDTH//2)
 xa = tf.math.maximum(0,x-WIDTH//2)
 xb = tf.math.minimum(DIM,x+WIDTH//2)
 one = image[j,ya:yb,0:xa,:]
two = image[k,ya:yb,xa:xb,:]
three = image[j,ya:yb,xb:DIM,:]
middle = tf.concat([one,two,three],axis=1)
 img = tf.concat([image[j,0:ya,:,:],middle,image[j,yb:DIM,:,:]],axis=0)
 imgs.append(img)
 a = tf.cast(WIDTH*WIDTH/DIM/DIM,tf.float32)
 lab1 = label[j,]
lab2 = label[k,]
labs.append(( 1-a)*lab1 + a*lab2)
 image2 = tf.reshape(tf.stack(imgs),(BATCH_SIZE,DIM,DIM,3))
label2 = tf.reshape(tf.stack(labs),(BATCH_SIZE, nb_classes))
return image2,label2
train.to_pickle('train.pkl')
 test = addNbPlayersFeature(test)
 test = addNbKillsFeature(test)
 test = addRankingQuantileInformation(test)
 test = addKillPlaceQuantileInformation(test)
 test = addDistanceWalked(test)
 test = addWeaponsAcquired(test)
 test = addDamageDealtInformation(test)
 test = addOtherDeltaFeatures(test)
 test.to_pickle('test.pkl' )<import_modules>
def mixup(image, label, PROBABILITY = mixup_rate):
DIM = img_size
imgs = []; labs = []
for j in range(BATCH_SIZE):
k = tf.cast(tf.random.uniform([],0,BATCH_SIZE),tf.int32)
 a = tf.random.uniform([],0,1)
 P = tf.cast(tf.random.uniform([], 0, 1)<= PROBABILITY, tf.int32)
 if P==1:
a=0.
 img1 = image[j,]
img2 = image[k,]
imgs.append(( 1-a)*img1 + a*img2)
 lab1 = label[j,]
lab2 = label[k,]
labs.append(( 1-a)*lab1 + a*lab2)
 image2 = tf.reshape(tf.stack(imgs),(BATCH_SIZE,DIM,DIM,3))
label2 = tf.reshape(tf.stack(labs),(BATCH_SIZE,nb_classes))
return image2,label2
from sklearn.linear_model import LinearRegression, Ridge 
<define_variables>
def transform(image, inv_mat, image_shape):
h, w, c = image_shape
cx, cy = w//2, h//2
new_xs = tf.repeat(tf.range(-cx, cx, 1), h)
 new_ys = tf.tile(tf.range(-cy, cy, 1), [w])
 new_zs = tf.ones([h*w], dtype=tf.int32)
 old_coords = tf.matmul(inv_mat, tf.cast(tf.stack([new_xs, new_ys, new_zs]), tf.float32))
old_coords_x, old_coords_y = tf.round(old_coords[0, :] + w//2), tf.round(old_coords[1, :] + h//2)
 clip_mask_x = tf.logical_or(old_coords_x<0, old_coords_x>w-1)
 clip_mask_y = tf.logical_or(old_coords_y<0, old_coords_y>h-1)
 clip_mask = tf.logical_or(clip_mask_x, clip_mask_y)
 old_coords_x = tf.boolean_mask(old_coords_x, tf.logical_not(clip_mask))
old_coords_y = tf.boolean_mask(old_coords_y, tf.logical_not(clip_mask))
new_coords_x = tf.boolean_mask(new_xs+cx, tf.logical_not(clip_mask))
new_coords_y = tf.boolean_mask(new_ys+cy, tf.logical_not(clip_mask))
old_coords = tf.cast(tf.stack([old_coords_y, old_coords_x]), tf.int32)
 new_coords = tf.cast(tf.stack([new_coords_y, new_coords_x]), tf.int64)
 rotated_image_values = tf.gather_nd(image, tf.transpose(old_coords))
rotated_image_channel = list()
for i in range(c):
vals = rotated_image_values[:,i]
sparse_channel = tf.SparseTensor(tf.transpose(new_coords), vals, [h, w])
 rotated_image_channel.append(tf.sparse.to_dense(sparse_channel, default_value=0, validate_indices=False))
return tf.transpose(tf.stack(rotated_image_channel), [1,2,0])
 def random_rotate(image, angle, image_shape):
def get_rotation_mat_inv(angle):
angle = math.pi * angle / 180
cos_val = tf.math.cos(angle)
 sin_val = tf.math.sin(angle)
 one = tf.constant([1], tf.float32)
 zero = tf.constant([0], tf.float32)
 rot_mat_inv = tf.concat([cos_val, sin_val, zero, -sin_val, cos_val, zero, zero, zero, one], axis=0)
 rot_mat_inv = tf.reshape(rot_mat_inv, [3,3])
 return rot_mat_inv
angle = float(angle)* tf.random.normal([1],dtype='float32')
 rot_mat_inv = get_rotation_mat_inv(angle)
 return transform(image, rot_mat_inv, image_shape)
 def GridMask(image_height, image_width, d1, d2, rotate_angle=1, ratio=0.5):
h, w = image_height, image_width
hh = int(np.ceil(np.sqrt(h*h+w*w)))
 hh = hh+1 if hh%2==1 else hh
d = tf.random.uniform(shape=[], minval=d1, maxval=d2, dtype=tf.int32)
 l = tf.cast(tf.cast(d,tf.float32)*ratio+0.5, tf.int32)
 st_h = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 st_w = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 y_ranges = tf.range(-1 * d + st_h, -1 * d + st_h + l)
 x_ranges = tf.range(-1 * d + st_w, -1 * d + st_w + l)
 for i in range(0, hh//d+1):
s1 = i * d + st_h
s2 = i * d + st_w
y_ranges = tf.concat([y_ranges, tf.range(s1,s1+l)], axis=0)
 x_ranges = tf.concat([x_ranges, tf.range(s2,s2+l)], axis=0)
 x_clip_mask = tf.logical_or(x_ranges < 0 , x_ranges > hh-1)
 y_clip_mask = tf.logical_or(y_ranges < 0 , y_ranges > hh-1)
 clip_mask = tf.logical_or(x_clip_mask, y_clip_mask)
 x_ranges = tf.boolean_mask(x_ranges, tf.logical_not(clip_mask))
y_ranges = tf.boolean_mask(y_ranges, tf.logical_not(clip_mask))
hh_ranges = tf.tile(tf.range(0,hh), [tf.cast(tf.reduce_sum(tf.ones_like(x_ranges)) , tf.int32)])
 x_ranges = tf.repeat(x_ranges, hh)
 y_ranges = tf.repeat(y_ranges, hh)
 y_hh_indices = tf.transpose(tf.stack([y_ranges, hh_ranges]))
x_hh_indices = tf.transpose(tf.stack([hh_ranges, x_ranges]))
y_mask_sparse = tf.SparseTensor(tf.cast(y_hh_indices, tf.int64), tf.zeros_like(y_ranges), [hh, hh])
 y_mask = tf.sparse.to_dense(y_mask_sparse, 1, False)
 x_mask_sparse = tf.SparseTensor(tf.cast(x_hh_indices, tf.int64), tf.zeros_like(x_ranges), [hh, hh])
 x_mask = tf.sparse.to_dense(x_mask_sparse, 1, False)
 mask = tf.expand_dims(tf.clip_by_value(x_mask + y_mask, 0, 1), axis=-1)
 mask = random_rotate(mask, rotate_angle, [hh, hh, 1])
 mask = tf.image.crop_to_bounding_box(mask,(hh-h)//2,(hh-w)//2, image_height, image_width)
 return mask
def apply_grid_mask(image, image_shape, PROBABILITY = gridmask_rate):
AugParams = {
'd1' : 100,
'd2': 160,
'rotate' : 45,
'ratio' : 0.3
}
mask = GridMask(image_shape[0], image_shape[1], AugParams['d1'], AugParams['d2'], AugParams['rotate'], AugParams['ratio'])
 if image_shape[-1] == 3:
mask = tf.concat([mask, mask, mask], axis=-1)
 mask = tf.cast(mask,tf.float32)
 P = tf.cast(tf.random.uniform([], 0, 1)<= PROBABILITY, tf.int32)
 if P==1:
return image*mask
else:
return image
def gridmask(img_batch, label_batch):
return apply_grid_mask(img_batch,(img_size,img_size, 3)) , label_batch
features_selected = [
'delta_median_kill_place_team_all',
'delta_mean_distance_walked_team_all',
'delta_mean_weapons_acquired_team_all',
'delta_mean_boosts_team_all',
'delta_mean_heals_team_all',
'delta_mean_killStreaks_team_all',
'winPlacePerc'
]<choose_model_class>
def get_train_dataset(train_paths,train_labels):
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels.astype(np.float32)))
.map(decode_image, num_parallel_calls=AUTO)
.cache() 
.map(data_aug, num_parallel_calls=AUTO)
.shuffle(512)
.batch(BATCH_SIZE,drop_remainder=True)
)
 if cutmix_rate:
print('启用cutmix')
 train_dataset =train_dataset.map(cutmix, num_parallel_calls=AUTO)
 if mixup_rate:
print('启用mixup')
 train_dataset =train_dataset.map(mixup, num_parallel_calls=AUTO)
 if gridmask_rate:
print('启用gridmask')
 train_dataset =train_dataset.map(gridmask, num_parallel_calls=AUTO)
 if(cutmix_rate or mixup_rate):
train_dataset =train_dataset.unbatch().shuffle(512 ).batch(BATCH_SIZE)
 train_dataset = train_dataset.repeat().prefetch(AUTO)
 return train_dataset
reg = LinearRegression() 
<drop_column>
def get_validation_dataset(valid_paths,valid_labels):
dataset = tf.data.Dataset.from_tensor_slices(( valid_paths, valid_labels))
dataset = dataset.map(decode_image, num_parallel_calls=AUTO)
 dataset = dataset.cache()
dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
train_linear = train[features_selected].dropna()
test_linear = test[[f for f in features_selected if f!='winPlacePerc'] + ['Id']]<predict_on_test>
def re_produce_test_dataset(test_paths):
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
)
 if bool_tta:
test_dataset = test_dataset.cache().map(data_aug, num_parallel_calls=AUTO)
 test_dataset = test_dataset.batch(BATCH_SIZE)
 return test_dataset
Y_train = train_linear.winPlacePerc
X_train = train_linear.drop(columns=['winPlacePerc'], axis=1)
 reg = reg.fit(X_train, Y_train)
 first_pred = reg.predict(X_train )<compute_test_metric>
LR_START = 0.00003
LR_MAX = 0.0003 * strategy.num_replicas_in_sync
LR_MIN = 0.00003
LR_RAMPUP_EPOCHS = 3
LR_SUSTAIN_EPOCHS = 4
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
if bool_lr_scheduler:
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
np.mean(abs(first_pred-Y_train))<load_pretrained>
def get_model() :
with strategy.scope() :
base_model = efn.EfficientNetB0(weights=pre_trained, include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(nb_classes, activation=dense_activation )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 if label_smoothing_rate:
print('启用label_smoothing')
 my_loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=label_smoothing_rate)
 elif bool_focal_loss:
my_loss = tfa.losses.SigmoidFocalCrossEntropy(reduction=tf.keras.losses.Reduction.AUTO)
 else:
my_loss='categorical_crossentropy'
model.compile(optimizer=tf.keras.optimizers.Adam(lr_if_without_scheduler),
loss=my_loss,
metrics=['accuracy'])
 return model
train = pd.read_pickle('train.pkl')
 test = pd.read_pickle('test.pkl')
<define_variables>
callbacks=[]
if bool_lr_scheduler:
callbacks.append(lr_callback )
features_selected = [
'assists',
'boosts',
'heals',
'killPlace',
'killPoints',
'kills',
'killStreaks',
'matchDuration',
'numGroups',
'rankPoints',
'teamKills',
'winPoints',
'winPlacePerc',
'nb_players_total',
'nb_players_team',
'nb_kills_team',
'median_ranking_team',
'median_ranking_all',
'median_kill_place_team',
'delta_median_ranking_team_all',
'delta_median_kill_place_team_all',
'mean_damage_dealt_team',
'max_damage_dealt_team',
'min_damage_dealt_team',
'mean_damage_dealt_all',
'max_damage_dealt_all',
'min_damage_dealt_all',
'mean_distance_walked_team',
'max_distance_walked_team',
'min_distance_walked_team',
'mean_distance_walked_all',
'max_distance_walked_all',
'min_distance_walked_all',
'max_weapons_acquired_team',
'min_weapons_acquired_team',
'mean_weapons_acquired_team',
'max_weapons_acquired_all',
'min_weapons_acquired_all',
'mean_weapons_acquired_all',
'mean_assists_all',
'mean_assists_team',
'mean_boosts_all',
'mean_boosts_team',
'mean_DBNOs_all',
'mean_DBNOs_team',
'delta_mean_heals_team_all',
'delta_mean_killStreaks_team_all',
'delta_mean_roadKills_team_all',
] 
<split>
if not cross_validation_folds:
model = get_model()
model.fit(
 get_train_dataset(train_paths,train_labels),
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=callbacks,
epochs=EPOCHS 
)
 if bool_tta:
probabilities = []
for i in range(tta_times+1):
print('TTA Number: ',i,'
')
 test_dataset = re_produce_test_dataset(test_paths)
 probabilities.append(model.predict(test_dataset))
y_pred = np.mean(probabilities,axis =0)
 else:
test_dataset = re_produce_test_dataset(test_paths)
 y_pred = model.predict(test_dataset )
<prepare_x_and_y><EOS> 
sub.loc[:, 'healthy':] = y_pred
sub.to_csv('submission.csv', index=False)
 sub.head()