Kaggle competition: Digit Recognizer
test_ds = get_test_dataset(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
 labels = ['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']
mean =(models1[0].predict(test_images_ds)
 +models1[1].predict(test_images_ds)
 +models1[2].predict(test_images_ds)
 +models1[3].predict(test_images_ds)
 +models1[4].predict(test_images_ds)) /5.0<categorify>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.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' )<save_to_csv>
x_train = train.drop('label', axis=1)/255.0
y_label = train['label'].values
x_test = test/255.0
submission = pd.DataFrame(mean, columns = labels)
 submission.insert(0, "StudyInstanceUID", test_ids, False)
 submission['StudyInstanceUID'] = submission['StudyInstanceUID'].apply(lambda x: x.rstrip(".jpg"))
submission.to_csv('submission.csv', index=False )<train_model>
datagen = ImageDataGenerator(
 rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
)
print("Done" )<define_variables>
import tensorflow as tf
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>
class ResidualUnit(tf.keras.layers.Layer):
def __init__(self, filters, strides=1, activation='relu', **kwargs):
super().__init__(**kwargs)
 self.activation = tf.keras.activations.get(activation)
 self.main_layers = [
tf.keras.layers.Conv2D(filters, 3, strides=strides, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
self.activation,
tf.keras.layers.Conv2D(filters, 3, strides=1, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
self.activation,
tf.keras.layers.Conv2D(filters, 3, strides=1, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
]
self.skip_layers = []
if strides > 1:
self.skip_layers = [
tf.keras.layers.Conv2D(filters, 1, strides=strides, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
]
def call(self, inputs):
Z = inputs
for layer in self.main_layers:
Z=layer(Z)
 skip_Z = inputs
for layer in self.skip_layers:
skip_Z = layer(skip_Z)
 return self.activation(Z +skip_Z )
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>
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(64,(7,7), input_shape=(28, 28, 1), padding='SAME'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(2, 2))
prev_filters = 64
for filters in [64]*2 + [128]*2 + [256]*2:
print(filters)
 strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit(filters, strides=strides))
prev_filters = filters
model.add(tf.keras.layers.GlobalAvgPool2D())
 model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dropout(0.5*0.5))
model.add(tf.keras.layers.Dense(int(filters/2), activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
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>
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['acc'])
 checkpoint = ModelCheckpoint(
 filepath=f'resnet-{int(time.time())}.dhf5',
monitor='loss',
save_best_only=True 
)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.8**x)
 callbacks = [checkpoint, annealer]
transforms_test = albumentations.Compose([
Resize(image_size, image_size),
Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2()
] )<load_from_csv>
batch_size = 64
history = model.fit(datagen.flow(X_train, y_label, batch_size=batch_size),
epochs = 30,
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=callbacks, )
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 test['file_path'] = test.StudyInstanceUID.apply(lambda x: os.path.join('.. /input/ranzcr-clip-catheter-line-classification/test', f'{x}.jpg'))
target_cols = test.iloc[:, 1:12].columns.tolist()
test_dataset = RANZCRDataset(test, 'test', transform=transforms_test)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=24 )<choose_model_class>
model.evaluate(X_train, y_label )
if submit:
test_preds_1 = []
for i in range(len(enet_type)) :
if enet_type[i] == 'resnet200d':
print('resnet200d loaded')
 model = RANZCRResNet200D(enet_type[i], out_dim=len(target_cols))
model = model.to(device)
 model.load_state_dict(torch.load(model_path[i], map_location='cuda:0'))
if tta:
test_preds_1 += [tta_inference_func(test_loader)]
else:
test_preds_1 += [inference_func(test_loader)]<load_from_csv>
def predict_proba(X, model, num_samples):
preds = [model(X, training=True)for _ in range(num_samples)]
return np.stack(preds ).mean(axis=0)
 def predict_class(X, model, num_samples):
proba_preds = predict_proba(X, model, num_samples)
 return np.argmax(proba_preds, axis=1 )
submission = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 submission[target_cols] = np.mean(test_preds_1, axis=0 )<import_modules>
y_pred = predict_class(X_test, model, 10 )
from pathlib import Path
import random
from scipy.sparse import coo_matrix
import gc
from joblib import Parallel, delayed
import typing as tp
from torch.utils import data<define_variables>
res = pd.DataFrame(y_pred, columns=['Label'])
 res.index = res.index + 1
res.index.rename('ImageId', inplace=True)
 res.to_csv('res.csv' )
ROOT = Path.cwd().parent
INPUT = ROOT / "input"
OUTPUT = ROOT / "output"
DATA = INPUT / "ranzcr-clip-catheter-line-classification"
TRAIN = DATA / "train"
TEST = DATA / "test"
TRAINED_MODEL = INPUT / "ranzcr-clip-weights-for-multi-head-model-v2"
TMP = ROOT / "tmp"
TMP.mkdir(exist_ok=True)
 RANDAM_SEED = 1086
N_CLASSES = 11
FOLDS = [0, 1, 2, 3, 4]
N_FOLD = len(FOLDS)
 IMAGE_SIZE =(512, 512)
 CONVERT_TO_RANK = False
FAST_COMMIT = False
CLASSES = [
'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'
]<load_from_csv>
successive_outputs = [layer.output for layer in model.layers[0:]]
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)
 img = random.choice(X_train)
 plt.imshow(img, cmap=plt.cm.binary)
 plt.show()
for p in DATA.iterdir() :
print(p.name)
 train = pd.read_csv(DATA / "train.csv")
 smpl_sub = pd.read_csv(DATA / "sample_submission.csv" )<split>
successive_feature_maps = visualization_model.predict(img)
 layer_names = [layer.name for layer in model.layers]
for layer_name, feature_map in zip(layer_names, successive_feature_maps):
if len(feature_map.shape)== 4:
n_features = feature_map.shape[-1]
size = feature_map.shape[1]
pic_num_per_row = n_features // 8 + 1
display_grid_arr = []
display_grid = np.zeros(( size, size * pic_num_per_row))
for i in range(n_features):
if i>0 and(i % pic_num_per_row == 0 or i == n_features):
display_grid_arr.append(display_grid)
 display_grid = np.zeros(( size, size * pic_num_per_row))
index = i % pic_num_per_row
x = feature_map[0, :, :, i]
x -= x.mean()
x /= x.std()
x *= 64
x += 128
x = np.clip(x, 0, 255 ).astype('uint8')
 display_grid[:, index * size :(index + 1)* size] = x
row_num = len(display_grid_arr)
 scale = 200./ n_features
fig, axes = plt.subplots(row_num, 1, figsize=(scale * pic_num_per_row, scale * row_num))
fig.suptitle(layer_name, fontsize=40)
 for i in range(row_num):
axes[i].imshow(display_grid_arr[i], aspect='auto', cmap='viridis' )
if FAST_COMMIT and len(smpl_sub)== 3582:
smpl_sub = smpl_sub.iloc[:64 * 3].reset_index(drop=True )<categorify>
y_pred = model.predict_classes(X_train )
def multi_label_stratified_group_k_fold(label_arr: np.array, gid_arr: np.array, n_fold: int, seed: int=42):

 np.random.seed(seed)
 random.seed(seed)
 start_time = time.time()
n_train, n_class = label_arr.shape
gid_unique = sorted(set(gid_arr))
n_group = len(gid_unique)
 gid2aid = dict(zip(gid_unique, range(n_group)))
 aid_arr = np.vectorize(lambda x: gid2aid[x] )(gid_arr)
 cnts_by_class = label_arr.sum(axis=0)
 col, row = np.array(sorted(enumerate(aid_arr), key=lambda x: x[1])).T
cnts_by_group = coo_matrix(
(np.ones(len(label_arr)) ,(row, col))
).dot(coo_matrix(label_arr)).toarray().astype(int)
 del col
del row
cnts_by_fold = np.zeros(( n_fold, n_class), int)
 groups_by_fold = [[] for fid in range(n_fold)]
group_and_cnts = list(enumerate(cnts_by_group))
np.random.shuffle(group_and_cnts)
 print("finished preparation", time.time() - start_time)
 for aid, cnt_by_g in sorted(group_and_cnts, key=lambda x: -np.std(x[1])) :
best_fold = None
min_eval = None
for fid in range(n_fold):
cnts_by_fold[fid] += cnt_by_g
fold_eval =(cnts_by_fold / cnts_by_class ).std(axis=0 ).mean()
cnts_by_fold[fid] -= cnt_by_g
if min_eval is None or fold_eval < min_eval:
min_eval = fold_eval
best_fold = fid
cnts_by_fold[best_fold] += cnt_by_g
groups_by_fold[best_fold].append(aid)
 print("finished assignment.", time.time() - start_time)
 gc.collect()
idx_arr = np.arange(n_train)
 for fid in range(n_fold):
val_groups = groups_by_fold[fid]
val_indexs_bool = np.isin(aid_arr, val_groups)
 train_indexs = idx_arr[~val_indexs_bool]
val_indexs = idx_arr[val_indexs_bool]
print("[fold {}]".format(fid), end=" ")
 print("n_group:(train, val)=({}, {})".format(n_group - len(val_groups), len(val_groups)) , end=" ")
 print("n_sample:(train, val)=({}, {})".format(len(train_indexs), len(val_indexs)))
 yield train_indexs, val_indexs<concatenate>
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score
label_arr = train[CLASSES].values
group_id = train.PatientID.values
train_val_indexs = list(
 multi_label_stratified_group_k_fold(label_arr, group_id, N_FOLD, RANDAM_SEED))<feature_engineering>
conf_max = confusion_matrix(y_label, y_pred)
 conf_max
train["fold"] = -1
for fold_id,(trn_idx, val_idx)in enumerate(train_val_indexs):
train.loc[val_idx, "fold"] = fold_id
train.groupby("fold")[CLASSES].sum()<train_model>
diff_num = 5
<choose_model_class><EOS> 
a_1d = norm_conf_max.flatten()
idx_1d = a_1d.argsort() [-diff_num:]
x_idx, y_idx = np.unravel_index(idx_1d, norm_conf_max.shape)
 print(x_idx, y_idx )