Kaggle competition: Digit Recognizer
study_inst_id_list = [
study_inst_id.numpy().decode('utf-8')for image, study_inst_id in raw_test_ds
]
print(study_inst_id_list[ :10 ])
 print(study_inst_id_list[ -10: ] )<prepare_x_and_y>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
def drop_study_inst_id(image, study_inst_id):
return image
def preprocess_image(image):
image_seg = tf.image.resize(image,(seg_image_size, seg_image_size))
image_seg = image_seg / 255.0
image_cls = tf.image.resize(image,(cls_image_size, cls_image_size))
return(( image_seg, image_cls),)
 def make_test_dataset() :
ds = load_dataset(tfrec_file_names)
 ds = ds.map(drop_study_inst_id, num_parallel_calls=AUTOTUNE)
 ds = ds.map(preprocess_image, num_parallel_calls=AUTOTUNE)
 ds = ds.batch(batch_size)
 ds = ds.prefetch(AUTOTUNE)
 return ds<create_dataframe>
train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train_df.head()
g = sns.countplot(train_df['label'])
 train_df['label'].value_counts()
test_ds = make_test_dataset()
test_ds<init_hyperparams>
X_train = train_df.drop(['label'],1)
 Y_train = train_df['label']
X_train = X_train/255.0
test = test_df/255.0
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train,num_classes=10 )
def load_model(weight_file_name):
weight_file_path = os.path.join(model_dir, weight_file_name)
 model = tf.keras.models.load_model(weight_file_path)
 return model
def make_seg_masks(x):
fold_seg_masks = tf.stack(x, axis=0)
 average_seg_masks = \
tf.math.reduce_mean(fold_seg_masks, axis=0)
 return average_seg_masks
def make_cls_inputs(x):
cls_images = x[0]
seg_masks = x[1]
seg_masks = tf.image.resize(
 seg_masks,(cls_image_size, cls_image_size))
seg_masks = seg_masks * 255.0
cls_inputs = tf.concat([cls_images, seg_masks], axis=-1)
 return cls_inputs
def make_model(cls_model_name):
seg_images = tf.keras.Input(
 shape=(seg_image_size, seg_image_size, 3),
name="seg_images")
 seg_outputs = []
for seg_model_name in seg_model_names:
seg_model = load_model(seg_model_name)
 seg_output = seg_model(seg_images)
 seg_outputs.append(seg_output)
 seg_masks = L.Lambda(
 make_seg_masks, name="seg_masks" )(seg_outputs)
 cls_images = tf.keras.Input(
 shape=(cls_image_size, cls_image_size, 3),
name="cls_images")
 cls_inputs = L.Lambda(
 make_cls_inputs, name="cls_inputs" )(
 [cls_images, seg_masks])
 cls_model = load_model(cls_model_name)
 ett, others, pred = cls_model(cls_inputs)
 model = tf.keras.Model(
 inputs=[seg_images, cls_images],
outputs=pred,
name="infer_model")
 return model<find_best_params>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<prepare_output>
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import BatchNormalization, Conv2D, Dense, Dropout, Flatten,MaxPooling2D
from keras.callbacks import ReduceLROnPlateau
from keras.applications import ResNet50
df_sub['StudyInstanceUID'] = study_inst_id_list<define_variables>
datagen = ImageDataGenerator(
 rotation_range= 10,
zoom_range= 0.2,
width_shift_range=0.1,
height_shift_range = 0.1,
horizontal_flip = False,
vertical_flip = False 
)
 datagen.fit(X_train )
target_cols = [
'ETT - Abnormal',
'ETT - Borderline',
'ETT - Normal',
'NGT - Abnormal',
'NGT - Borderline',
'NGT - Incompletely Imaged',
'NGT - Normal',
'CVC - Abnormal',
'CVC - Borderline',
'CVC - Normal',
'Swan Ganz Catheter Present'
]<feature_engineering>
model = Sequential([
Conv2D(64,(5,5),padding='same',input_shape=(28,28,1),activation='relu'),
Conv2D(64,(5,5),padding='same',activation='relu'),
MaxPooling2D(2,2),
Dropout(0.2),
Conv2D(64,(3,3),padding='same',activation='relu'),
Conv2D(64,(3,3),padding='same',activation='relu'),
MaxPooling2D(2,2),
Dropout(0.5),
Flatten() ,
Dense(128,activation='relu'),
Dense(128,activation='relu'),
Dropout(0.2),
Dense(10,activation='softmax')
 ])
 model.summary()
df_subs = [df_sub.copy() for _ in range(PROBS.shape[0])]
for i, this_sub in enumerate(df_subs):
this_sub[target_cols] = PROBS[i]
this_sub[target_cols] = \
this_sub[target_cols].rank(pct=True )<feature_engineering>
rms = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0)
 adam = Adam(lr=0.001 )
rank_values = \
[this_sub[target_cols].values for this_sub in df_subs]
df_sub[target_cols] = \
np.stack(rank_values, 0 ).mean(0 )<save_to_csv>
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics='acc' )
df_sub.to_csv('submission.csv', index=False)
 !head submission.csv<define_variables>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
batch_size = 1
image_size = 512
tta = True
submit = True
enet_type = ['resnet200d'] * 5
model_path = ['.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold0_cv953.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold1_cv955.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold2_cv955.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold3_cv957.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold4_cv954.pth']<set_options>
history = model.fit(
 datagen.flow(X_train,Y_train, batch_size=32),
epochs = 30,
validation_data =(X_val,Y_val),
verbose = 1,
steps_per_epoch=X_train.shape[0] // 32,
callbacks=[learning_rate_reduction]
)
sys.path.append('.. /input/pytorch-image-models/pytorch-image-models-master')
 sys.path.append('.. /input/timm-pytorch-image-models/pytorch-image-models-master')
 DEBUG = False
%matplotlib inline
device = torch.device('cuda')if not DEBUG else torch.device('cpu' )<choose_model_class>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
class RANZCRResNet200D(nn.Module):
def __init__(self, model_name='resnet200d', out_dim=11, pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, out_dim)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output<concatenate>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("new3.csv",index=False )