Kaggle competition: Petals to the Metal - Flower Classification on TPU
<SOS> metric: macrofscore Kaggle data source: petals-to-the-metal-flower-classification-on-tpu<set_options>
!pip install -q efficientnet
py.init_notebook_mode(connected=True)
 %matplotlib inline
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
from tensorflow.keras.applications.densenet import DenseNet121, DenseNet169, DenseNet201
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2
from tensorflow.keras.applications.nasnet import NASNetLarge
from efficientnet.tfkeras import EfficientNetB7, EfficientNetL2, EfficientNetB0, EfficientNetB1
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 PassengerId=test['PassengerId']
train.head()<feature_engineering>
%matplotlib inline
print("Tensorflow version " + tf.__version__ )
full_data=[train,test]
train['name_length']=train['Name'].apply(len)
 test['name_length']=test['Name'].apply(len)
 train['has_cabin']=train["Cabin"].apply(lambda x:0 if type(x)==float else 1)
 test['has_cabin']=test["Cabin"].apply(lambda x:0 if type(x)==float else 1)
 for dataline in full_data:
dataline["familysize"]=dataline['SibSp']+dataline['Parch']+1
for dataline in full_data:
dataline["isalone"]=0
dataline.loc[dataline['familysize']==1,'isalone']=1
dataline['Embarked']= dataline['Embarked'].fillna('S')
 dataline['Fare']=dataline['Fare'].fillna(train['Fare'].median())
 train['categoricalfare']=pd.cut(train['Fare'],4)
 for dataset in full_data:
age_avg=dataset['Age'].mean()
age_std=dataset['Age'].std()
age_null_count=dataset['Age'].isnull().sum()
age_null_random_list=np.random.randint(age_avg-age_std,age_avg+age_std,size=age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])]=age_null_random_list
dataset['Age']=dataset['Age'].astype(int)
 train['categoricalage']=pd.cut(train['Age'],5)
 def get_title(name):
title_search=re.search('([A-Za-z]+)\.',name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in full_data:
dataset['Title']=dataset['Name'].apply(get_title)
 for dataset in full_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train.head() 
<categorify>
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
for dataset in full_data:
dataset['Sex']=dataset['Sex'].map({'female':0,'male':1} ).astype(int)
 title_mapping={"Mr":1,"Miss":2,"Mrs":3,"Master":4,"Rare":5}
dataset['Title']=dataset['Title'].map(title_mapping)
 dataset['Title']=dataset['Title'].fillna(0)
 dataset['Embarked']=dataset['Embarked'].map({'S':0,'C':1,'Q':2})
 dataset.loc[dataset['Fare']<=7.91,'Fare']=0
dataset.loc[(dataset['Fare']>7.91)&(dataset['Fare']<=14.454),'Fare']=1
dataset.loc[(dataset['Fare']>14.454)&(dataset['Fare']<=31),'Fare']=2
dataset.loc[dataset['Fare']>31,'Fare']=3
dataset['Fare']=dataset['Fare'].astype(int)
 dataset.loc[dataset['Age']<=16,'Age']=0
dataset.loc[(dataset['Age']>16)&(dataset['Age']<=32),'Age']=1
dataset.loc[(dataset['Age']>32)&(dataset['Age']<=48),'Age']=2
dataset.loc[(dataset['Age']>48)&(dataset['Age']<=64),'Age']=3
dataset.loc[dataset['Age']>64,'Age']=4
train.head()<drop_column>
IMAGE_SIZE = [512, 512]
EPOCHS = 30
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 SEED = 2020
drop_elements=['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp']
train=train.drop(drop_elements,axis=1)
 train=train.drop(['categoricalage', 'categoricalfare'],axis=1)
 test=test.drop(drop_elements,axis=1)
<find_best_model_class>
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):

 ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def data_augment(image, label):
flag = 3
coef_1 = random.randint(70, 90)* 0.01
coef_2 = random.randint(70, 90)* 0.01
if flag == 1:
image = tf.image.random_flip_left_right(image, seed=SEED)
 elif flag == 2:
image = tf.image.random_flip_up_down(image, seed=SEED)
 else:
image = tf.image.random_crop(image, [int(IMAGE_SIZE[0]*coef_1), int(IMAGE_SIZE[0]*coef_2), 3],seed=SEED)
 return image, label
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset() :
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=False)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))
ntrain=train.shape[0]
ntest=test.shape[0]
SEED=0
nfolds=5
kf=KFold(n_splits=nfolds,random_state=SEED)
 class SKlearnHelper(object):
def __init__(self,clf,seed=0,params=None):
self.clf=clf(**params)
 def train(self,x_train,y_train):
self.clf.fit(x_train,y_train)
 def predict(self,x):
return self.clf.predict(x)
 def fit(self,x,y):
return self.clf.fit(x,y)
 def feature_importances(self,x,y):
print(self.clf.fit(x,y ).feature_importances_)
<predict_on_test>
LR_START = 0.00001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.75
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]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
def get_oof(clf,x_train,y_train,x_test):
oof_train=np.zeros(( ntrain,))
oof_test=np.zeros(( ntest,))
oof_test_skf=np.empty(( nfolds,ntest))
for i,(train_index, test_index)in enumerate(kf.split(x_train)) :
x_tr=x_train[train_index]
y_tr=y_train[train_index]
x_te=x_train[test_index]
clf.train(x_tr,y_tr)
 oof_train[test_index]=clf.predict(x_te)
 oof_test_skf[i,:]=clf.predict(x_test)
 oof_test[:]=oof_test_skf.mean(axis=0)
 return oof_train.reshape(-1,1),oof_test.reshape(-1,1)
<init_hyperparams>
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 return Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model = get_model(EfficientNetB1)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
rf_params={
'n_jobs':-1,
'n_estimators':500,
'warm_start':True,
'max_depth':6,
'min_samples_leaf':2,
'max_features':'sqrt',
'verbose':0
}
et_params={
'n_jobs':-1,
'n_estimators':500,
'max_depth':8,
'min_samples_leaf':2,
'verbose':0
}
ada_params={
'n_estimators':500,
'learning_rate':0.75
}
gb_params={
'n_estimators':500,
'max_depth':8,
'min_samples_leaf':2,
'verbose':0
}
svc_params={
'kernel':'linear',
'C':0.025
}
knn_params={
'n_neighbors':3
}
dst_params={
'max_depth':8
}<choose_model_class>
history = model.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback, ModelCheckpoint(filepath='my_ef_net_b1.h5', monitor='val_loss',
save_best_only=True)],
validation_data=get_validation_dataset() ,
workers = 3 )
rf=SKlearnHelper(clf=RandomForestClassifier,seed=SEED,params=rf_params)
 et=SKlearnHelper(clf=ExtraTreesClassifier,seed=SEED,params=et_params)
 ada=SKlearnHelper(clf=AdaBoostClassifier,seed=SEED,params=ada_params)
 gb=SKlearnHelper(clf=GradientBoostingClassifier,seed=SEED,params=gb_params)
 svc=SKlearnHelper(clf=SVC,seed=SEED,params=svc_params)
 knn=SKlearnHelper(clf=KNeighborsClassifier,seed=SEED,params=knn_params)
 dst=SKlearnHelper(clf=DecisionTreeClassifier,seed=SEED,params=dst_params )<prepare_x_and_y>
model = tf.keras.models.load_model('my_ef_net_b1.h5' )
<categorify><EOS> 
test_ds = get_test_dataset(ordered=True)
 print('Вычисляем предсказания...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Создание файла submission.csv...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='' )