Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<feature_engineering>
print("Tensorflow version " + tf.__version__ )
for i in list1:
new_data['title_is'+i]=new_data['title'].fillna('' ).apply(lambda x: 1 if i in x else 0 )<feature_engineering>
AUTOTUNE = tf.data.experimental.AUTOTUNE
GCS_PATH = ".. /input/cassava-leaf-disease-classification"
GCS_PATH_STRATIFICATED =".. /input/cassava-recreate-stratificated-tfrecords"
REPLICAS = strategy.num_replicas_in_sync
BATCH_SIZE = 128
AUG_BATCH = BATCH_SIZE
IMAGE_SIZE = [512, 512]
DIM = IMAGE_SIZE[0]
CLASSES = ['0', '1', '2', '3', '4']
EPOCHS = 40
FOLDS = 5
debug = True
TTA = 2
WGTS = [1/FOLDS]*FOLDS
calc_oof = False
for col in ['tagline', 'overview','title']:
new_data['len_' + col] =new_data[col].fillna('' ).apply(lambda x: len(x))
new_data['words_' + col] = new_data[col].fillna('' ).apply(lambda x: len(x.split(' ')))
 new_data=new_data.drop(col,axis=1 )<feature_engineering>
test_df = pd.read_csv(GCS_PATH + '/sample_submission.csv')
 train_df = pd.read_csv(GCS_PATH + '/train.csv' )
new_data['budget_popularity']=new_data['budget']*1.0/new_data['popularity']<count_values>
files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/test_tfrecords/*.tfrec')) )
a=new_data.groupby(['release_year'] ).release_month.value_counts()
b=new_data.groupby(['release_year','release_month'] ).release_day.value_counts()
new_data[(new_data['release_year']==2017)&(new_data['release_day']==30)] 
<categorify>
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0
new_data['_releaseYear_popularity_ratio'] = new_data['release_year'] / new_data['popularity']
new_data['_releaseYear_popularity_ratio2'] = new_data['popularity'] / new_data['release_year']
new_data['runtime_to_mean_year'] = new_data['runtime'] / new_data.groupby("release_year")["runtime"].transform('mean')
 new_data['popularity_to_mean_year'] = new_data['popularity'] / new_data.groupby("release_year")["popularity"].transform('mean')
 new_data['budget_to_mean_year'] = new_data['budget'] / new_data.groupby("release_year")["budget"].transform('mean')
<feature_engineering>
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform_mat(image, DIM=IMAGE_SIZE[0]):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d, [DIM, DIM,3] )
<feature_engineering>
def to_float32(image, label):
return tf.cast(image, tf.float32), label
<split>
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
new_train=new_data.loc[train.index]
new_test=new_data.loc[test.index]<train_model>
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
x=new_train.drop('revenue',axis=1)
 y=new_train['revenue']
x=x.fillna(0)
 decision_tree = DecisionTreeRegressor(max_depth = 6)
 decision_tree.fit(x, y)
<train_model>
def read_tfrecord(example, labeled):
tfrecord_format = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64)
 } if labeled else {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, tfrecord_format)
 image = decode_image(example['image'])
 if labeled:
label = tf.cast(example['target'], tf.int32)
 return image, label
idnum = example['image_name']
return image, idnum
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
 model_XG = xgboost.XGBRegressor()
model_XG.fit(x_train,y_train)
 y_predict_rf = model_XG.predict(x_test)
 print(mean_squared_error(y_test, y_predict_rf)) 
<drop_column>
def read_unlabeled_tfrecord(example, return_image_name):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name'] if return_image_name else '0'
X_test=new_test.drop('revenue',axis=1)
 X_test=X_test.fillna(0 )<predict_on_test>
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=AUTOTUNE)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(partial(read_tfrecord, labeled=labeled), num_parallel_calls=AUTOTUNE)
 return dataset
ypred=model_XG.predict(X_test )<prepare_x_and_y>
TRAINING_FILENAMES = tf.io.gfile.glob('.. /input/cassava-leaf-disease-classification/train_tfrecords/' + '*train*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob('.. /input/cassava-leaf-disease-classification/test_tfrecords/' + 'ld_test*.tfrec' )
dtrain = xgb.DMatrix(x, y)
 dtest = xgb.DMatrix(X_test )<train_on_grid>
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 = int(count_data_items(TRAINING_FILENAMES)*(FOLDS-1.) /FOLDS)
 NUM_VALIDATION_IMAGES = int(count_data_items(TRAINING_FILENAMES)*(1./FOLDS))
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))
xgb_params = {
'eta': 0.08,
'max_depth': 15,
'subsample': 0.7,
'colsample_bytree': 0.7,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'silent': 1
}
cv_output = xgb.cv(xgb_params, dtrain, num_boost_round=1000, early_stopping_rounds=400, verbose_eval=50, show_stdv=False)
 cv_output[['train-rmse-mean', 'test-rmse-mean']].plot()<predict_on_test>
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image, label
y_predict1 = abs(model.predict(dtest))<prepare_x_and_y>
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTOTUNE)
 return dataset
train=x
y_train=np.log1p(y)
 test=X_test
y=y_train<compute_train_metric>
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse = np.sqrt(-cross_val_score(model, train.values,y_train.values, scoring="neg_mean_squared_error", cv=kf))
return(rmse)
 def eval_model(model, name):
start_time = time.time()
score = rmsle_cv(model)
 print("{} score: {:.4f}({:.4f}), execution time: {:.1f}".format(name, score.mean() , score.std() , time.time() -start_time))<choose_model_class>
def onehot(image,label):
CLASSES = 5
return image,tf.one_hot(label,CLASSES )
mod_lasso = make_pipeline(RobustScaler() , Lasso(alpha=0.005, random_state=1))
eval_model(mod_lasso, "lasso")
 mod_enet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
eval_model(mod_enet, "enet")
 mod_cat = CatBoostRegressor(iterations=10000, learning_rate=0.01,
depth=5, eval_metric='RMSE',
colsample_bylevel=0.7, random_seed = 17, silent=True,
bagging_temperature = 0.2, early_stopping_rounds=200)
 eval_model(mod_cat, "cat")
 mod_gboost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=10, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state=5)
 eval_model(mod_gboost, "gboost")
 mod_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=10,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state=7, nthread=-1)
 eval_model(mod_xgb, "xgb")
 mod_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=1023,
learning_rate=0.05, n_estimators=650,
max_bin=58, bagging_fraction=0.80,max_depth=10,
bagging_freq=5, feature_fraction=0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf=7, min_sum_hessian_in_leaf=11)
 eval_model(mod_lgb, "lgb" )<train_model>
def get_validation_dataset(dataset, do_onehot=True):
dataset = dataset.batch(BATCH_SIZE)
 if do_onehot: dataset = dataset.map(onehot, num_parallel_calls=AUTOTUNE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTOTUNE)
 return dataset
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features)
 mod_stacked = StackingAveragedModels(base_models =(mod_cat, mod_xgb, mod_gboost, mod_lgb), meta_model = mod_lasso)
 eval_model(mod_stacked, "stacked" )<predict_on_test>
def get_dataset(files, augment = False, shuffle = False, repeat = False,
labeled=True, return_image_names=False, batch_size=16, dim=512):
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTOTUNE)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024*8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTOTUNE)
 else:
ds = ds.map(lambda example: read_unlabeled_tfrecord(example, return_image_names),
num_parallel_calls=AUTOTUNE)
 ds = ds.map(lambda img, imgname_or_label:(prepare_image(img, augment=augment, dim=dim),
imgname_or_label),
num_parallel_calls=AUTOTUNE)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTOTUNE)
 return ds
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def predict(model):
model.fit(train.values,y_train.values)
 train_pred = model.predict(train.values)
 pred = np.expm1(model.predict(test.values))
print(rmsle(y_train, train_pred))
return(pred )<predict_on_test>
class DataGenerator(Sequence):
def __init__(self, path, list_IDs, labels, batch_size, img_size, img_channel):
self.path = path
self.list_IDs = list_IDs
self.labels = labels
self.batch_size = batch_size
self.img_size = img_size
self.img_channel = img_channel
self.indexes = np.arange(len(self.list_IDs))
def __len__(self):
return int(np.floor(len(self.list_IDs)/self.batch_size))
def __getitem__(self, index):
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_temp = [self.list_IDs[k] for k in indexes]
X, y = self.__data_generation(list_IDs_temp)
 return X, y
def __data_generation(self, list_IDs_temp):
X = np.empty(( self.batch_size, self.img_size, self.img_size, self.img_channel))
y = np.empty(( self.batch_size, 5), dtype=int)
 for i, ID in enumerate(list_IDs_temp):
data_file = cv2.imread(self.path+ID)
 img = cv2.resize(data_file,(self.img_size, self.img_size))
X[i, ] = img
y[i, ] = self.labels[i]
X = X.astype('float32')
 X /= 255.
 return X, y
prediction1=0
prediction = predict(mod_lasso)
 prediction = predict(mod_enet)
 prediction = predict(mod_xgb)
 prediction1+=prediction
prediction = predict(mod_gboost)
 prediction1+=prediction
prediction = predict(mod_lgb)
 prediction1+=prediction
prediction = predict(mod_stacked)
 prediction1+=prediction
prediction<save_to_csv>
new_test['id']=new_test.index
new_test['revenue']=prediction
new_test[['id','revenue']].to_csv('submission_Dragon2.csv', index=False)
 new_test[['id','revenue']].head()<save_to_csv>
test_generator = DataGenerator('.. /input/cassava-leaf-disease-classification/'+'test_images/', test_df['image_id'], test_df['label'], 1, DIM, 3 )
new_test['revenue']=prediction1/4
new_test[['id','revenue']].to_csv('submission_Dragon3.csv', index=False)
 new_test[['id','revenue']].head()<define_variables>

 BASE_WEIGHTS_PATH = 'https://storage.googleapis.com/keras-applications/'
WEIGHTS_HASHES = {
'b0':('902e53a9f72be733fc0bcb005b3ebbac',
'50bc09e76180e00e4465e1a485ddc09d'),
'b1':('1d254153d4ab51201f1646940f018540',
'74c4e6b3e1f6a1eea24c589628592432'),
'b2':('b15cce36ff4dcbd00b6dd88e7857a6ad',
'111f8e2ac8aa800a7a99e3239f7bfb39'),
'b3':('ffd1fdc53d0ce67064dc6a9c7960ede0',
'af6d107764bb5b1abb91932881670226'),
'b4':('18c95ad55216b8f92d7e70b3a046e2fc',
'ebc24e6d6c33eaebbd558eafbeedf1ba'),
'b5':('ace28f2a6363774853a83a0b21b9421a',
'38879255a25d3c92d5e44e04ae6cec6f'),
'b6':('165f6e37dce68623721b423839de8be5',
'9ecce42647a20130c1f39a5d4cb75743'),
'b7':('8c03f828fec3ef71311cd463b6759d99',
'cbcfe4450ddf6f3ad90b1b398090fe4a'),
}
DEFAULT_BLOCKS_ARGS = [{
'kernel_size': 3,
'repeats': 1,
'filters_in': 32,
'filters_out': 16,
'expand_ratio': 1,
'id_skip': True,
'strides': 1,
'se_ratio': 0.25
}, {
'kernel_size': 3,
'repeats': 2,
'filters_in': 16,
'filters_out': 24,
'expand_ratio': 6,
'id_skip': True,
'strides': 2,
'se_ratio': 0.25
}, {
'kernel_size': 5,
'repeats': 2,
'filters_in': 24,
'filters_out': 40,
'expand_ratio': 6,
'id_skip': True,
'strides': 2,
'se_ratio': 0.25
}, {
'kernel_size': 3,
'repeats': 3,
'filters_in': 40,
'filters_out': 80,
'expand_ratio': 6,
'id_skip': True,
'strides': 2,
'se_ratio': 0.25
}, {
'kernel_size': 5,
'repeats': 3,
'filters_in': 80,
'filters_out': 112,
'expand_ratio': 6,
'id_skip': True,
'strides': 1,
'se_ratio': 0.25
}, {
'kernel_size': 5,
'repeats': 4,
'filters_in': 112,
'filters_out': 192,
'expand_ratio': 6,
'id_skip': True,
'strides': 2,
'se_ratio': 0.25
}, {
'kernel_size': 3,
'repeats': 1,
'filters_in': 192,
'filters_out': 320,
'expand_ratio': 6,
'id_skip': True,
'strides': 1,
'se_ratio': 0.25
}]
CONV_KERNEL_INITIALIZER = {
'class_name': 'VarianceScaling',
'config': {
'scale': 2.0,
'mode': 'fan_out',
'distribution': 'truncated_normal'
}
}
DENSE_KERNEL_INITIALIZER = {
'class_name': 'VarianceScaling',
'config': {
'scale': 1./ 3.,
'mode': 'fan_out',
'distribution': 'uniform'
}
}
def EfficientNet(
 width_coefficient,
depth_coefficient,
default_size,
dropout_rate=0.2,
drop_connect_rate=0.2,
depth_divisor=8,
activation='swish',
blocks_args='default',
model_name='efficientnet',
include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
classifier_activation='softmax', 
):

 if blocks_args == 'default':
blocks_args = DEFAULT_BLOCKS_ARGS
if not(weights in {'imagenet', None} or os.path.exists(weights)) :
raise ValueError('The `weights` argument should be either '
'`None`(random initialization), `imagenet` '
'(pre-training on ImageNet), '
'or the path to the weights file to be loaded.')
 if weights == 'imagenet' and include_top and classes != 1000:
raise ValueError('If using `weights` as `"imagenet"` with `include_top`'
' as true, `classes` should be 1000')
 input_shape = imagenet_utils.obtain_input_shape(
 input_shape,
default_size=default_size,
min_size=32,
data_format=backend.image_data_format() ,
require_flatten=include_top,
weights=weights)
 if input_tensor is None:
img_input = layers.Input(shape=input_shape)
 else:
if not backend.is_keras_tensor(input_tensor):
img_input = layers.Input(tensor=input_tensor, shape=input_shape)
 else:
img_input = input_tensor
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
def round_filters(filters, divisor=depth_divisor):

 filters *= width_coefficient
new_filters = max(divisor, int(filters + divisor / 2)// divisor * divisor)
 if new_filters < 0.9 * filters:
new_filters += divisor
return int(new_filters)
 def round_repeats(repeats):

 return int(math.ceil(depth_coefficient * repeats))
x = img_input
x = layers.Normalization(axis=bn_axis )(x)
 x = layers.ZeroPadding2D(
 padding=imagenet_utils.correct_pad(x, 3),
name='stem_conv_pad' )(x)
 x = layers.Conv2D(
 round_filters(32),
3,
strides=2,
padding='valid',
use_bias=False,
kernel_initializer=CONV_KERNEL_INITIALIZER,
name='stem_conv' )(x)
 x = layers.BatchNormalization(axis=bn_axis, name='stem_bn' )(x)
 x = layers.Activation(activation, name='stem_activation' )(x)
 blocks_args = copy.deepcopy(blocks_args)
 b = 0
blocks = float(sum(args['repeats'] for args in blocks_args))
for(i, args)in enumerate(blocks_args):
assert args['repeats'] > 0
args['filters_in'] = round_filters(args['filters_in'])
 args['filters_out'] = round_filters(args['filters_out'])
 for j in range(round_repeats(args.pop('repeats'))):
if j > 0:
args['strides'] = 1
args['filters_in'] = args['filters_out']
x = block(
 x,
activation,
drop_connect_rate * b / blocks,
name='block{}{}_'.format(i + 1, chr(j + 97)) ,
**args)
 b += 1
x = layers.Conv2D(
 round_filters(1280),
1,
padding='same',
use_bias=False,
kernel_initializer=CONV_KERNEL_INITIALIZER,
name='top_conv' )(x)
 x = layers.BatchNormalization(axis=bn_axis, name='top_bn' )(x)
 x = layers.Activation(activation, name='top_activation' )(x)
 if include_top:
x = layers.GlobalAveragePooling2D(name='avg_pool' )(x)
 if dropout_rate > 0:
x = layers.Dropout(dropout_rate, name='top_dropout' )(x)
 imagenet_utils.validate_activation(classifier_activation, weights)
 x = layers.Dense(
 classes,
activation=classifier_activation,
kernel_initializer=DENSE_KERNEL_INITIALIZER,
name='predictions' )(x)
 else:
if pooling == 'avg':
x = layers.GlobalAveragePooling2D(name='avg_pool' )(x)
 elif pooling == 'max':
x = layers.GlobalMaxPooling2D(name='max_pool' )(x)
 if input_tensor is not None:
inputs = layer_utils.get_source_inputs(input_tensor)
 else:
inputs = img_input
model = training.Model(inputs, x, name=model_name)
 if weights == 'imagenet':
if include_top:
file_suffix = '.h5'
file_hash = WEIGHTS_HASHES[model_name[-2:]][0]
else:
file_suffix = '_notop.h5'
file_hash = WEIGHTS_HASHES[model_name[-2:]][1]
file_name = model_name + file_suffix
weights_path = data_utils.get_file(
 file_name,
BASE_WEIGHTS_PATH + file_name,
cache_subdir='models',
file_hash=file_hash)
 model.load_weights(weights_path)
 elif weights is not None:
model.load_weights(weights)
 return model
def block(inputs,
activation='swish',
drop_rate=0.,
name='',
filters_in=32,
filters_out=16,
kernel_size=3,
strides=1,
expand_ratio=1,
se_ratio=0.,
id_skip=True):

 bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
filters = filters_in * expand_ratio
if expand_ratio != 1:
x = layers.Conv2D(
 filters,
1,
padding='same',
use_bias=False,
kernel_initializer=CONV_KERNEL_INITIALIZER,
name=name + 'expand_conv' )(
 inputs)
 x = layers.BatchNormalization(axis=bn_axis, name=name + 'expand_bn' )(x)
 x = layers.Activation(activation, name=name + 'expand_activation' )(x)
 else:
x = inputs
if strides == 2:
x = layers.ZeroPadding2D(
 padding=imagenet_utils.correct_pad(x, kernel_size),
name=name + 'dwconv_pad' )(x)
 conv_pad = 'valid'
else:
conv_pad = 'same'
x = layers.DepthwiseConv2D(
 kernel_size,
strides=strides,
padding=conv_pad,
use_bias=False,
depthwise_initializer=CONV_KERNEL_INITIALIZER,
name=name + 'dwconv' )(x)
 x = layers.BatchNormalization(axis=bn_axis, name=name + 'bn' )(x)
 x = layers.Activation(activation, name=name + 'activation' )(x)
 if 0 < se_ratio <= 1:
filters_se = max(1, int(filters_in * se_ratio))
se = layers.GlobalAveragePooling2D(name=name + 'se_squeeze' )(x)
 se = layers.Reshape(( 1, 1, filters), name=name + 'se_reshape' )(se)
 se = layers.Conv2D(
 filters_se,
1,
padding='same',
activation=activation,
kernel_initializer=CONV_KERNEL_INITIALIZER,
name=name + 'se_reduce' )(
 se)
 se = layers.Conv2D(
 filters,
1,
padding='same',
activation='sigmoid',
kernel_initializer=CONV_KERNEL_INITIALIZER,
name=name + 'se_expand' )(se)
 x = layers.multiply([x, se], name=name + 'se_excite')
 x = layers.Conv2D(
 filters_out,
1,
padding='same',
use_bias=False,
kernel_initializer=CONV_KERNEL_INITIALIZER,
name=name + 'project_conv' )(x)
 x = layers.BatchNormalization(axis=bn_axis, name=name + 'project_bn' )(x)
 if id_skip and strides == 1 and filters_in == filters_out:
if drop_rate > 0:
x = layers.Dropout(
 drop_rate, noise_shape=(None, 1, 1, 1), name=name + 'drop' )(x)
 x = layers.add([x, inputs], name=name + 'add')
 return x
@keras_export('keras.applications.efficientnet.EfficientNetB0',
'keras.applications.EfficientNetB0')
 def EfficientNetB0(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.0,
1.0,
224,
0.2,
model_name='efficientnetb0',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB1',
'keras.applications.EfficientNetB1')
 def EfficientNetB1(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.0,
1.1,
240,
0.2,
model_name='efficientnetb1',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB2',
'keras.applications.EfficientNetB2')
 def EfficientNetB2(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.1,
1.2,
260,
0.3,
model_name='efficientnetb2',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB3',
'keras.applications.EfficientNetB3')
 def EfficientNetB3(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.2,
1.4,
300,
0.3,
model_name='efficientnetb3',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB4',
'keras.applications.EfficientNetB4')
 def EfficientNetB4(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.4,
1.8,
380,
0.4,
model_name='efficientnetb4',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB5',
'keras.applications.EfficientNetB5')
 def EfficientNetB5(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.6,
2.2,
456,
0.4,
model_name='efficientnetb5',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB6',
'keras.applications.EfficientNetB6')
 def EfficientNetB6(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 1.8,
2.6,
528,
0.5,
model_name='efficientnetb6',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.EfficientNetB7',
'keras.applications.EfficientNetB7')
 def EfficientNetB7(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
return EfficientNet(
 2.0,
3.1,
600,
0.5,
model_name='efficientnetb7',
include_top=include_top,
weights=weights,
input_tensor=input_tensor,
input_shape=input_shape,
pooling=pooling,
classes=classes,
**kwargs)
 @keras_export('keras.applications.efficientnet.preprocess_input')
 def preprocess_input(x, data_format=None):
return x
@keras_export('keras.applications.efficientnet.decode_predictions')
 def decode_predictions(preds, top=5):

 return imagenet_utils.decode_predictions(preds, top=top )
package_path = '.. /input/vision-transformer-pytorch/VisionTransformer-Pytorch'
sys.path.append(package_path)
<define_variables>
def get_model(weights='imagenet'):
inp = tf.keras.layers.Input(shape=(DIM,DIM,3))
base = EfficientNetB0(input_shape=(DIM,DIM,3),weights=None,include_top=False)
 x = base(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = tf.keras.layers.Flatten()(x)
 x = tf.keras.layers.Dense(5,activation='softmax' )(x)
 model = tf.keras.Model(inputs=inp, outputs=x)
 opt = tf.keras.optimizers.Adam(learning_rate=0.001)
 loss = tf.keras.losses.CategoricalCrossentropy(
 from_logits=False, label_smoothing=0.0001,
name='categorical_crossentropy' 
)
 model.compile(optimizer=opt,loss=loss,metrics=['categorical_accuracy'])
 return model
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path )<import_modules>
skf = KFold(n_splits=FOLDS,shuffle=True,random_state=12)
 for fold,(idxT,idxV)in enumerate(skf.split(np.arange(5))):
if fold==(FOLDS-1):
idxTT = idxT; idxVV = idxV
print('
print('Fold',fold,'has TRAIN:',idxT,'VALID:',idxV )
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
import timm
from scipy.ndimage.interpolation import zoom
from sklearn.metrics import log_loss<init_hyperparams>
pred = np.zeros(( test_df.shape[0],5))
for fold,(idxT,idxV)in enumerate(skf.split(np.arange(5))):
print() ; print('
print('
print('
files_train = tf.io.gfile.glob([GCS_PATH_STRATIFICATED + '/train%.2i*.tfrec'%x for x in idxT])
 files_valid = tf.io.gfile.glob([GCS_PATH_STRATIFICATED + '/train%.2i*.tfrec'%x for x in idxV])
 NUM_TRAINING_IMAGES = int(count_data_items(files_train))
NUM_VALIDATION_IMAGES = int(count_data_items(files_valid))
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} validation images,'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES))
model = get_model()
print('Loading best model...')
 model.load_weights('.. /input/cassavaefnetb0/fold-%i.h5'%fold)
 print('Predicting Test with TTA...')
 tta_steps = TTA
predictions = []
for i in tqdm(range(tta_steps)) :
preds = model.predict_generator(test_generator, verbose=1)
 predictions.append(preds)
 pred += np.mean(predictions, axis=0)/FOLDS
del model; z = gc.collect()
CFG = {
'fold_num': 10,
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 384,
'epochs': 60,
'train_bs': 28,
'valid_bs': 32,
'lr': 1e-2,
'num_workers': 5,
'accum_iter': 1,
'verbose_step': 2,
'device': 'cuda:0',
'tta': 6,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}<load_from_csv>
ds = get_dataset(files_test, augment=False, repeat=False, dim=IMAGE_SIZE[0],
labeled=False, return_image_names=True)
 image_names = np.array([img_name.numpy().decode("utf-8")
 for img, img_name in iter(ds.unbatch())] )
<count_values><EOS> 
prediction = np.argmax(pred, axis=1)
 test_df['label'] = prediction
test_df = test_df[["image_id","label"]]
test_df.to_csv('submission.csv',index=False)
 test_df