Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<compute_test_metric>
!pip install -q efficientnet
print('Mean Absolute Error is {:.5f}'.format(mean_absolute_error(y_test, lgbm.predict(X_test, num_iteration=lgbm.best_iteration_), sample_weight=validation_weights)))
 print('R2 score is {:.2%}'.format(r2_score(y_test, lgbm.predict(X_test, num_iteration=lgbm.best_iteration_), sample_weight=validation_weights)) )<load_from_csv>
%matplotlib inline
print("Tensorflow version " + tf.__version__ )
test = reduce_mem_usage(pd.read_csv(folder+'test_V2.csv'))<categorify>
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("plant-pathology-2020-fgvc7")
 EPOCHS = 40
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
time_0 = datetime.datetime.now()
test, cAll = preprocess(test, cOrigGP)
 time_1 = datetime.datetime.now()
print('Preprocessing took {} seconds'.format(( time_1 - time_0 ).seconds))<predict_on_test>
def format_path(st):
return GCS_DS_PATH + '/images/' + st + '.jpg'
y_sub_unfull = lgbm.predict(X_sub, num_iteration=lgbm.best_iteration_ )<prepare_output>
train = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
 test = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 sub = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 train_paths = train.image_id.apply(format_path ).values
test_paths = test.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values
train_paths, valid_paths, train_labels, valid_labels = train_test_split(
 train_paths, train_labels, test_size=0.15, random_state=2020 )
sub = test[['Id']]
sub['winPlacePerc'] = y_sub_unfull<save_to_csv>
img_size = 768
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)
 if label is None:
return image
else:
return image, label
sub.to_csv('sub_LGBM_unfull.csv',index=False )<prepare_x_and_y>
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)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( valid_paths, valid_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )
X_train = df[cAll]
y_train = df[cY]
train_weights =(1/df['teamSize'] )<train_model>
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(train_labels.shape[1], activation="softmax" )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(optimizer='nadam', loss='categorical_crossentropy',metrics=['categorical_accuracy'])
 return model
with strategy.scope() :
model1 = get_model(EfficientNetB7)
 model1.load_weights("/kaggle/input/tf-zoo-models-on-tpu-efficientnetb7/my_ef_net_b7.h5")
time_0 = datetime.datetime.now()
lgbm = LGBMRegressor(objective='mae', n_estimators=250,
learning_rate=0.3, num_leaves=200,
n_jobs=-1, random_state=RANDOM_STATE, verbose=1)
 lgbm.fit(X_train, y_train, sample_weight=train_weights)
 time_1 = datetime.datetime.now()
print('Training took {} seconds.Best iteration is {}'.format(( time_1 - time_0 ).seconds, lgbm.best_iteration_))<predict_on_test>
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(train_labels.shape[1], activation="softmax" )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(optimizer='nadam', loss='categorical_crossentropy',metrics=['categorical_accuracy'])
 return model
with strategy.scope() :
model2 = get_model(DenseNet201)
 model2.load_weights("/kaggle/input/tf-zoo-models-on-tpu-densenet201/my_dense_net_201.h5")
y_sub_full = lgbm.predict(X_sub, num_iteration=lgbm.best_iteration_)
 sub2 = test[['Id']]
sub2['winPlacePerc'] = y_sub_full<save_to_csv>
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(train_labels.shape[1], activation="softmax" )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(optimizer='nadam', loss='categorical_crossentropy',metrics=['categorical_accuracy'])
 return model
with strategy.scope() :
model3 = get_model(InceptionResNetV2)
 model3.load_weights("/kaggle/input/tf-zoo-models-on-tpu/InceptionResNetV2.h5")
<load_from_csv><EOS> 
best_alpha = 0.52
bad_alpha = 0.33
print('Вычисляем предсказания...')
 probabilities1 = model1.predict(test_dataset, verbose=1)
 probabilities3 = model3.predict(test_dataset, verbose=1)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities3
print(probabilities)
 sub.loc[:, 'healthy':] = probabilities
sub.to_csv('submission.csv', index=False)
 sub.head()