Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<set_options>
print(tf.__version__)
 print(tf.keras.__version__ )
np.random.seed(42)
 warnings.filterwarnings('ignore' )<load_from_csv>
!pip install efficientnet
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )<string_transform>
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()
X_train = train["question_text"].fillna("fillna" ).values
y_train = train["target"].values
X_test = test["question_text"].fillna("fillna" ).values
max_features = 40000
maxlen = 50
embed_size = 300
tokenizer = text.Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(X_train)+ list(X_test))
X_train = tokenizer.texts_to_sequences(X_train)
 X_test = tokenizer.texts_to_sequences(X_test)
 x_train = sequence.pad_sequences(X_train, maxlen=maxlen)
 x_test = sequence.pad_sequences(X_test, maxlen=maxlen )<feature_engineering>
img = plt.imread('.. /input/plant-pathology-2020-fgvc7/images/Train_0.jpg')
 print(img.shape)
 plt.imshow(img )
def get_coefs(word, *arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.rstrip().rsplit(' ')) for o in open(EMBEDDING_FILE))
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<predict_on_test>
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_paths = train.image_id.apply(lambda x: GCS_DS_PATH + '/images/' + x + '.jpg' ).values
test_paths = test.image_id.apply(lambda x: GCS_DS_PATH + '/images/' + x + '.jpg' ).values
train_labels = train.loc[:, 'healthy':].values
class F1Evaluation(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
 y_pred =(y_pred > 0.35 ).astype(int)
 score = f1_score(self.y_val, y_pred)
 print("
F1 Score - epoch: %d - score: %.6f
" %(epoch+1, score))<choose_model_class>
nb_classes = 4
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
img_size = 224
EPOCHS = 10
filter_sizes = [1,2,3,5]
num_filters = 36
def get_model() :
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = SpatialDropout1D(0.4 )(x)
 conv_0 = Conv1D(num_filters, kernel_size=(filter_sizes[0]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_1 = Conv1D(num_filters, kernel_size=(filter_sizes[1]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_2 = Conv1D(num_filters, kernel_size=(filter_sizes[2]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_3 = Conv1D(num_filters, kernel_size=(filter_sizes[3]),
kernel_initializer='he_normal', activation='elu' )(x)
 maxpool_0 = MaxPool1D(pool_size=(maxlen - filter_sizes[0] + 1))(conv_0)
 maxpool_1 = MaxPool1D(pool_size=(maxlen - filter_sizes[1] + 1))(conv_1)
 maxpool_2 = MaxPool1D(pool_size=(maxlen - filter_sizes[2] + 1))(conv_2)
 maxpool_3 = MaxPool1D(pool_size=(maxlen - filter_sizes[3] + 1))(conv_3)
 z = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2, maxpool_3])
 z = Flatten()(z)
 z = BatchNormalization()(z)
 outp = Dense(1, activation="sigmoid" )(z)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
model = get_model()<train_model>
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_augment(image, label=None, seed=2020):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 image = tf.image.random_crop(image, size=[img_size, img_size, 3])
 if label is None:
return image
else:
return image, label
batch_size = 1024
epochs = 4
X_tra, X_val, y_tra, y_val = train_test_split(x_train, y_train, train_size=0.95,
random_state=233)
 F1_Score = F1Evaluation(validation_data=(X_val, y_val), interval=1)
 hist = model.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs,
validation_data=(X_val, y_val),
callbacks=[F1_Score], verbose=True )<choose_model_class>
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
 )
filter_sizes = [1,2,3,5]
num_filters = 36
def get_model() :
inp = Input(shape=(maxlen,))
x = Lambda(lambda x: K.reverse(x,axes=-1))(inp)
 x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(x)
 x = SpatialDropout1D(0.4 )(x)
 conv_0 = Conv1D(num_filters, kernel_size=(filter_sizes[0]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_1 = Conv1D(num_filters, kernel_size=(filter_sizes[1]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_2 = Conv1D(num_filters, kernel_size=(filter_sizes[2]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_3 = Conv1D(num_filters, kernel_size=(filter_sizes[3]),
kernel_initializer='he_normal', activation='elu' )(x)
 maxpool_0 = MaxPool1D(pool_size=(maxlen - filter_sizes[0] + 1))(conv_0)
 maxpool_1 = MaxPool1D(pool_size=(maxlen - filter_sizes[1] + 1))(conv_1)
 maxpool_2 = MaxPool1D(pool_size=(maxlen - filter_sizes[2] + 1))(conv_2)
 maxpool_3 = MaxPool1D(pool_size=(maxlen - filter_sizes[3] + 1))(conv_3)
 z = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2, maxpool_3])
 z = Flatten()(z)
 z = BatchNormalization()(z)
 outp = Dense(1, activation="sigmoid" )(z)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
model_flip = get_model() 
<train_model>
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )
hist_flip = model_flip.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs,
validation_data=(X_val, y_val),
callbacks=[F1_Score], verbose=True )<predict_on_test>
LR_START = 0.001
LR_MAX = 0.001 * strategy.num_replicas_in_sync
LR_MIN = 0.0001
LR_RAMPUP_EPOCHS = 10
LR_SUSTAIN_EPOCHS = 2
LR_EXP_DECAY =.6
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)
 early_stopping = tf.keras.callbacks.EarlyStopping(
 monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto',
baseline=None, restore_best_weights=False 
)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
val_y_pred1 = model.predict(X_val, batch_size=1024, verbose = True)
 val_y_pred2 = model_flip.predict(X_val, batch_size=1024, verbose = True)
<compute_test_metric>
def get_model() :
base_model = efn.EfficientNetB7(weights='imagenet', include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
x = base_model.output
out = Dense(2048, activation="softmax" )(x)
 predictions = Dense(nb_classes, activation="softmax" )(out)
 return Model(inputs=base_model.input, outputs=predictions )
val_y_pred = np.mean([val_y_pred1,val_y_pred2],axis = 0 )<predict_on_test>
with strategy.scope() :
model = get_model()
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'] )
y_pred1 = model.predict(x_test, batch_size=1024, verbose = True)
 y_pred2 = model_flip.predict(x_test, batch_size=1024, verbose = True)
 y_pred = np.mean([y_pred1,y_pred2],axis = 0 )<compute_test_metric>
%%time
model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback, early_stopping],
epochs=40
)
np.corrcoef([y_pred1[:,0],y_pred2[:,0]] )<compute_test_metric>
%%time
probs = model.predict(test_dataset )
<save_to_csv><EOS> 
sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()