Kaggle competition: Tabular Playground Series - Jan 2021
work_dir = '.. /input/cassava-leaf-disease-classification/'
train_path = '/kaggle/input/cassava-leaf-disease-classification/train_images'<define_variables>
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 display(train.head() )
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 123
seed_everything(seed)
 warnings.filterwarnings('ignore' )<load_from_csv>
z = np.abs(stats.zscore(train))
print(z )
data = pd.read_csv(work_dir + 'train.csv')
 print(data['label'].value_counts() )<categorify>
print(len(train))
train = train[(z < 3 ).all(axis=1)]
print(len(train))
with open(work_dir + 'label_num_to_disease_map.json')as f:
real_labels = json.load(f)
 real_labels = {int(k):v for k,v in real_labels.items() }
data['class_name'] = data['label'].map(real_labels)
 real_labels<split>
test = pd.read_csv(input_path / 'test.csv', index_col='id' )
train, test = train_test_split(data, test_size = 0.05, random_state = 123, stratify = data['class_name'] )<define_variables>
for df in [train, test]:
df['sum']=train.sum(axis=0)
 df['min'] = train[features].min(axis=0)
 df['max'] = train[features].max(axis=0)
 df['mean'] = train[features].mean(axis=0)
 df['median'] = train[features].median(axis=0)
 df['skew'] = train[features].skew(axis=0)
 df['kurtosis'] = train[features].kurtosis(axis=0)
 display(test.head() )
IMG_SIZE = 300
size =(IMG_SIZE,IMG_SIZE)
 n_CLASS = 5
BATCH_SIZE = 15<define_variables>
submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id')
 display(submission.head() )
datagen_train = ImageDataGenerator(
 preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest', 
)
 datagen_val = ImageDataGenerator(
 preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,
)<prepare_x_and_y>
target = train.pop('target')
 X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.90, random_state=42 )
train_set = datagen_train.flow_from_dataframe(
 train,
directory=train_path,
seed=123,
x_col='image_id',
y_col='class_name',
target_size = size,
class_mode='categorical',
interpolation='nearest',
shuffle = True,
batch_size = BATCH_SIZE, 
)
 test_set = datagen_val.flow_from_dataframe(
 test,
directory=train_path,
seed=123,
x_col='image_id',
y_col='class_name',
target_size = size,
class_mode='categorical',
interpolation='nearest',
shuffle=True,
batch_size=BATCH_SIZE,
)<choose_model_class>
params={
'n_estimators': 300,
'subsample': 0.7,
'colsample_bytree': 0.7,
'eta': 0.035282124803629786,
'max_depth': 11,
'min_child_weight': 10,
'tree_method':'gpu_hist',
'random_state':42
}
model = xgb.XGBRegressor(**params)
 model.fit(X_train, y_train)
 preds = model.predict(X_test)
 print(mean_squared_error(preds, y_test)**0.5 )
def create_model() :
model = Sequential()
model.add(
 EfficientNetB5(
 input_shape =(IMG_SIZE, IMG_SIZE, 3),
include_top = False,
weights='imagenet',
drop_connect_rate=0.6, 
)
)
 model.add(GlobalAveragePooling2D())
 model.add(Flatten())
 model.add(Dense(
 256,
activation='relu',
bias_regularizer=tf.keras.regularizers.L1L2(l1=0.01, l2=0.001)
))
model.add(Dropout(0.5))
model.add(Dense(n_CLASS, activation = 'softmax'))
return model
leaf_model = create_model()
leaf_model.summary()<define_variables>
model.fit(train, target)
 submission['target'] = model.predict(test)
 submission.to_csv('xgb_regression.csv' )