Kaggle competition: Digit Recognizer
!nvidia-smi<categorify>
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow import keras
from keras.utils.np_utils import to_categorical
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPool2D
def encode_binary_mask(mask: np.ndarray)-> t.Text:

 if mask.dtype != np.bool:
raise ValueError(
 "encode_binary_mask expects a binary mask, received dtype == %s" %
mask.dtype)
 mask = np.squeeze(mask)
 if len(mask.shape)!= 2:
raise ValueError(
 "encode_binary_mask expects a 2d mask, received shape == %s" %
mask.shape)
 mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
 mask_to_encode = mask_to_encode.astype(np.uint8)
 mask_to_encode = np.asfortranarray(mask_to_encode)
 encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]
binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
 base64_str = base64.b64encode(binary_str)
 return base64_str.decode('ascii')
 def binary_mask_to_ascii(mask, mask_val=1):

 mask = np.where(mask==mask_val, 1, 0 ).astype(np.bool)
 if mask.dtype != np.bool:
raise ValueError(f"encode_binary_mask expects a binary mask, received dtype == {mask.dtype}")
 mask = np.squeeze(mask)
 if len(mask.shape)!= 2:
raise ValueError(f"encode_binary_mask expects a 2d mask, received shape == {mask.shape}")
 mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
 mask_to_encode = mask_to_encode.astype(np.uint8)
 mask_to_encode = np.asfortranarray(mask_to_encode)
 encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]
binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
 base64_str = base64.b64encode(binary_str)
 return base64_str.decode()<load_pretrained>
righe, colonne = 28,28
n_classi = 10
test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
test = test / 255.0
X_train = X_train.values.reshape(-1,righe,colonne,1)
 test = test.values.reshape(-1,righe,colonne,1)
 y_train = to_categorical(y_train, num_classes = n_classi)
 plt.figure(figsize=(15,4.5))
for i in range(30):
plt.subplot(3, 10, i+1)
 plt.imshow(X_train[i].reshape(( righe,colonne)) ,cmap=plt.cm.binary)
 plt.axis('off')
 plt.subplots_adjust(wspace=-0.1, hspace=-0.1)
 plt.show()
def read_img(image_id, color, train_or_test='test', image_size=None):
filename = f'.. /input/hpa-single-cell-image-classification/{train_or_test}/{image_id}_{color}.png'
img = cv2.imread(filename, 0)
 return img
class HPADatasetTest(Dataset):
def __init__(self, image_ids, mode='test'):
self.image_ids = image_ids
self.mode = mode
def __len__(self):
return len(self.image_ids)
 def __getitem__(self, index):
try:
image_id = self.image_ids[index]
red = read_img(image_id, "red", self.mode, 0)
 green = read_img(image_id, "green", self.mode, 0)
 blue = read_img(image_id, "blue", self.mode, 0)
 yellow = read_img(image_id, "yellow", self.mode, 0)
 image = np.stack([blue, green, red, yellow], axis=-1)
 image_512 = cv2.resize(image,(512, 512)).transpose(2,0,1 ).astype(np.float32)
 image_768 = cv2.resize(image,(768, 768)).transpose(2,0,1 ).astype(np.float32)
 cell_mask = np.load(f'{mask_dir}/{image_id}.npz')['arr_0']
cell_mask = cv2.resize(cell_mask,(image.shape[0], image.shape[1]), interpolation=cv2.INTER_NEAREST)
 encs = ''
masked_images = []
cells_128 = []
cells_256 = []
center_cells = []
for cell_id in range(1, np.max(cell_mask)+1):
bmask =(cell_mask == cell_id ).astype(np.uint8)
 enc = encode_binary_mask(bmask==1)
 x, y, w, h = cv2.boundingRect(bmask)
 max_l = max(w, h)
 cx = x + w // 2
cy = y + h // 2
x1 = max(0, cx - max_l // 2)
 x1 = min(x1, image.shape[1] - max_l)
 y1 = max(0, cy - max_l // 2)
 y1 = min(y1, image.shape[0] - max_l)
 tmp = image.copy()
tmp[bmask==0] = 0
cropped_cell_orig = tmp[y1:y1+max_l, x1:x1+max_l]
cropped_cell_128 = cv2.resize(cropped_cell_orig,(128, 128))
cells_128.append(cropped_cell_128)
 cropped_cell_256 = cv2.resize(cropped_cell_orig,(256, 256))
cells_256.append(cropped_cell_256)
 masked = cv2.resize(tmp,(image_size, image_size))
masked_images.append(masked)
 cropped_cell = cv2.resize(tmp[y:y+h, x:x+w], 
(int(w / image.shape[1] * 768),
int(h / image.shape[0] * 768)) 
)
 final_size = 512
new_h, new_w, _ = cropped_cell.shape
new_h = final_size if cropped_cell.shape[0] > final_size else new_h
new_w = final_size if cropped_cell.shape[1] > final_size else new_w
cropped_cell = cv2.resize(cropped_cell,(new_w, new_h))
center_cell = np.zeros(( final_size, final_size, 4))
center = final_size // 2
h_start = max(0,center-cropped_cell.shape[0]//2)
 h_end = min(final_size,h_start+cropped_cell.shape[0])
 w_start = max(0,center-cropped_cell.shape[1]//2)
 w_end = min(final_size,w_start+cropped_cell.shape[1])
 center_cell[h_start:h_end,w_start:w_end,:] = cropped_cell
center_cells.append(center_cell)
 if encs == '':
encs += enc
else:
encs = encs + ' ' + enc
if len(masked_images)> 0:
masked_images = np.stack(masked_images ).transpose(0, 3, 1, 2 ).astype(np.float32)
 cells_128 = np.stack(cells_128 ).transpose(0, 3, 1, 2 ).astype(np.float32)
 cells_256 = np.stack(cells_256 ).transpose(0, 3, 1, 2 ).astype(np.float32)
 center_cells = np.stack(center_cells ).transpose(0, 3, 1, 2 ).astype(np.float32)
 else:
masked_images = np.zeros(( 4, 4, image_size, image_size))
cells_128 = np.zeros(( 4, 4, 128, 128))
cells_256 = np.zeros(( 4, 4, 256, 256))
for ch in range(4):
image_512[ch] /= orig_mean[ch]
image_768[ch] /= orig_mean[ch]
masked_images[:, ch] /= orig_mean[ch]
cells_128[:, ch] /= orig_mean[ch]
cells_256[:, ch] /= orig_mean[ch]
center_cells[:, ch] /= orig_mean[ch]
except:
image_id = ''
encs = ''
image_512 = np.zeros(( 4, 512, 512))
image_768 = np.zeros(( 4, 768, 768))
masked_images = np.zeros(( 5, 4, image_size, image_size))
cells_128 = np.zeros(( 5, 4, 128, 128))
cells_256 = np.zeros(( 5, 4, 256, 256))
center_cells = np.zeros(( 5, 4, 512, 512))
return image_id, encs, {
'512': torch.tensor(image_512),
'768': torch.tensor(image_768),
'masked': torch.tensor(masked_images),
'cells_128': torch.tensor(cells_128),
'cells_256': torch.tensor(cells_256),
'center_cells': torch.tensor(center_cells)
 } 
<create_dataframe>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
dataset = HPADatasetTest(df_sub.ID.values, mode='test')
 dataloader = DataLoader(dataset, batch_size=1, num_workers=2 )<choose_model_class>
nets = 7
model = [0] *nets
for i in range(nets):
model[i] = Sequential()
model[i].add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(righe,colonne,1)))
 model[i].add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model[i].add(MaxPool2D(pool_size=(2,2)))
 model[i].add(Dropout(0.25))
model[i].add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model[i].add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model[i].add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model[i].add(Dropout(0.25))
model[i].add(Flatten())
 model[i].add(Dense(256, activation = "relu"))
model[i].add(Dropout(0.5))
model[i].add(Dense(n_classi, activation = "softmax"))
model[i].compile(loss="categorical_crossentropy",
optimizer='adam',
metrics=['accuracy'] )
class enetv2(nn.Module):
def __init__(self, enet_type, out_dim=num_classes):
super(enetv2, self ).__init__()
self.enet = timm.create_model(enet_type, False)
 if('efficientnet' in enet_type)or('mixnet' in enet_type):
self.enet.conv_stem.weight = nn.Parameter(self.enet.conv_stem.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.classifier.in_features, out_dim)
 self.enet.classifier = nn.Identity()
elif('resnet' in enet_type or 'resnest' in enet_type)and 'vit' not in enet_type:
self.enet.conv1[0].weight = nn.Parameter(self.enet.conv1[0].weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.fc.in_features, out_dim)
 self.enet.fc = nn.Identity()
elif 'rexnet' in enet_type or 'regnety' in enet_type or 'nf_regnet' in enet_type:
self.enet.stem.conv.weight = nn.Parameter(self.enet.stem.conv.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.head.fc.in_features, out_dim)
 self.enet.head.fc = nn.Identity()
elif 'resnext' in enet_type:
self.enet.conv1.weight = nn.Parameter(self.enet.conv1.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.fc.in_features, out_dim)
 self.enet.fc = nn.Identity()
elif 'hrnet_w32' in enet_type:
self.enet.conv1.weight = nn.Parameter(self.enet.conv1.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.classifier.in_features, out_dim)
 self.enet.classifier = nn.Identity()
elif 'densenet' in enet_type:
self.enet.features.conv0.weight = nn.Parameter(self.enet.features.conv0.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.classifier.in_features, out_dim)
 self.enet.classifier = nn.Identity()
elif 'ese_vovnet39b' in enet_type or 'xception41' in enet_type:
self.enet.stem[0].conv.weight = nn.Parameter(self.enet.stem[0].conv.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.head.fc.in_features, out_dim)
 self.enet.head.fc = nn.Identity()
elif 'dpn' in enet_type:
self.enet.features.conv1_1.conv.weight = nn.Parameter(self.enet.features.conv1_1.conv.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.classifier.in_channels, out_dim)
 self.enet.classifier = nn.Identity()
elif 'inception' in enet_type:
self.enet.features[0].conv.weight = nn.Parameter(self.enet.features[0].conv.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.last_linear.in_features, out_dim)
 self.enet.last_linear = nn.Identity()
elif 'vit_base_resnet50' in enet_type:
self.enet.patch_embed.backbone.stem.conv.weight = nn.Parameter(self.enet.patch_embed.backbone.stem.conv.weight.repeat(1,n_ch//3+1,1,1)[:, :n_ch])
 self.myfc = nn.Linear(self.enet.head.in_features, out_dim)
 self.enet.head = nn.Identity()
else:
raise
def forward(self, x):
x = self.enet(x)
 h = self.myfc(x)
 return h 
<define_variables>
learning_rate = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 batch = 64
epochs = 5
H = [0] * nets
for j in range(nets):
X_train2, X_val2, y_train2, y_val2 = train_test_split(X_train, y_train, test_size = 0.1)
 H[j] = model[j].fit_generator(datagen.flow(X_train2,y_train2, batch_size=batch),
epochs = epochs, validation_data =(X_val2,y_val2),
verbose = 1, steps_per_epoch=X_train2.shape[0] // batch
,callbacks=[learning_rate])
kernel_types = {
'resnet50d_512_multilabel_8flips_ss22rot45_co2_lr1e4_bs32_focal_ext_15epo': {
'model_class': 'enetv2',
'folds': [1],
'enet_type': 'resnet50d',
'input_type': ['512', 'masked'],
},
'rex150_512_multilabel_8flips_ss22rot45_co7_lr3e4_bs32_ext_cellpseudo2full_15epo': {
'model_class': 'enetv2',
'folds': [0],
'enet_type': 'rexnet_150',
'input_type': ['512', 'masked'],
},
'densenet121_512_multilabel_8flips_ss22rot45_co2_lr1e4_bs32_focal_ext_15epo': {
'model_class': 'enetv2',
'folds': [2],
'enet_type': 'densenet121',
'input_type': ['512', 'masked'],
},
'b0_512_multilabel_8flips_ss22rot45_co7_lr1e4_bs32_focal_ext_15epo': {
'model_class': 'enetv2',
'folds': [3],
'enet_type': 'tf_efficientnet_b0_ns',
'input_type': ['512', 'masked'],
},
'resnet101d_512_multilabel_8flips_ss22rot45_co7_lr1e4_bs32_focal_ext_15epo': {
'model_class': 'enetv2',
'folds': [4],
'enet_type': 'resnet101d',
'input_type': ['512', 'masked'],
},
'dpn68b_512_multilabel_8flips_ss22rot45_co7_lr1e4_bs32_focal_ext_15epo': {
'model_class': 'enetv2',
'folds': [0],
'enet_type': 'dpn68b',
'input_type': ['512', 'masked'],
},
'densenet169_512_multilabel_8flips_ss22rot45_co2_lr1e4_bs32_focal_ext_15epo': {
'model_class': 'enetv2',
'folds': [1],
'enet_type': 'densenet169',
'input_type': ['512', 'masked'],
},
'b0_3d128_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [2],
'enet_type': 'tf_efficientnet_b0_ns',
'input_type': ['cells_128'],
},
'resnet50d_3d128_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_15epo': {
'model_class': 'enetv2',
'folds': [0],
'enet_type': 'resnet50d',
'input_type': ['cells_128'],
},
'mixnet_m_3d128_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_15epo': {
'model_class': 'enetv2',
'folds': [0],
'enet_type': 'mixnet_m',
'input_type': ['cells_128'],
},
'densenet121_3d128_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [3],
'enet_type': 'densenet121',
'input_type': ['cells_128'],
},
'b0_3d256_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [0],
'enet_type': 'tf_efficientnet_b0_ns',
'input_type': ['cells_256'],
},
'b1_3d256_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [3],
'enet_type': 'tf_efficientnet_b1_ns',
'input_type': ['cells_256'],
},
'densenet121_3d256_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [2],
'enet_type': 'densenet121',
'input_type': ['cells_256'],
},
'dpn68b_3d256_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [4],
'enet_type': 'dpn68b',
'input_type': ['cells_256'],
},
'mixnet_m_3d256_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [2],
'enet_type': 'mixnet_m',
'input_type': ['cells_256'],
},
'resnet50d_3d256_multilabel_lw41_8flips_ss22rot45_lr1e4_bs32cell16_ext_2019_15epo': {
'model_class': 'enetv2',
'folds': [1],
'enet_type': 'resnet50d',
'input_type': ['cells_256'],
},
}<load_pretrained>
results = np.zeros(( test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 sub = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 sub.to_csv("submission.csv",index=False )
def load_state_dict(model, model_file):
for folder in model_dirs:
model_path = os.path.join(folder, model_file)
 if os.path.exists(model_path):
state_dict = torch.load(model_path)
 state_dict = {k[7:] if k.startswith('module.')else k: state_dict[k] for k in state_dict.keys() }
model.load_state_dict(state_dict, strict=True)
 model.eval()
return model
raise
models = []
input_types = []
for key in kernel_types.keys() :
for fold in kernel_types[key]['folds']:
model = eval(kernel_types[key]['model_class'] )(
 kernel_types[key]['enet_type'], 
)
 model = model.to(device)
 model_file = f'{key}_final_fold{fold}.pth'
print(f'loading {model_file}...')
 model = load_state_dict(model, model_file)
 models.append(model)
 input_types.append(kernel_types[key]['input_type'])
 n_models = len(models)
 print('done!')
 print('model count:', n_models )<load_pretrained>