Kaggle competition: Digit Recognizer
warnings.filterwarnings("ignore", category=SourceChangeWarning)
 NUC_MODEL = ".. /input/hpacellsegmentatormodelweights/dpn_unet_nuclei_v1.pth"
CELL_MODEL = ".. /input/hpacellsegmentatormodelweights/dpn_unet_cell_3ch_v1.pth"
segmentator = cellsegmentator.CellSegmentator(
 NUC_MODEL,
CELL_MODEL,
scale_factor=0.25,
device="cuda",
padding=True,
multi_channel_model=True, 
); 
<load_from_csv>
from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPool2D
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
df = pd.read_csv('./cell_df.csv' )<create_dataframe>
np.random.seed(42 )
valid_transforms = A.Compose([
A.Resize(width=224, height=224),
A.Normalize() ,
ToTensorV2() ,
])
 class CellDataset(Dataset):
def __init__(self, data_dir, csv_file, transform=None):
super().__init__()
self.data_dir = data_dir
self.df = csv_file
self.transforms = transform
self.img_ids = self.df['image_id'].values
self.cell_ids = self.df['cell_id'].values
def __len__(self):
return len(self.img_ids)
 def get_image(self, index):
image_id = self.img_ids[index]
cell_id = self.cell_ids[index]
img_path = os.path.join(self.data_dir, 'cells', image_id + '_' + str(cell_id)+ '.jpg')
 img = cv2.imread(img_path)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 img = self.transforms(image=img)
 img = img['image']
return img
def __getitem__(self, index):
x = self.get_image(index)
 return x<create_dataframe>
def doSubmission(y_pred):
test_Id = np.arange(1, y_pred.size+1, dtype=np.int)
 pred_dict = {"ImageId": test_Id, "Label": y_pred}
df = pd.DataFrame(pred_dict)
 df.to_csv("sample_submission.csv", index=False, index_label=False )
test_dataset = CellDataset(data_dir='', csv_file=df, transform=valid_transforms )<load_pretrained>
df_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 df_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
test_loader = DataLoader(test_dataset,
batch_size=8,
shuffle=False,
num_workers=4,
drop_last=False )<choose_model_class>
y = df_train.label.to_numpy()
X = df_train.drop(columns=["label"] ).to_numpy(np.float64)
 X /= 255.0
class Net(nn.Module):
def __init__(self, name = 'efficientnet_b0', num_classes=19):
super(Net, self ).__init__()
self.model = timm.create_model(name, pretrained=False, num_classes=num_classes)
 def forward(self, x):
out = self.model(x)
 return out 
<feature_engineering>
X_totrain = X.reshape(X.shape[0], 28, 28, 1 )
def update_state_dict(state_dict):
new_state_dict = OrderedDict()
for key in state_dict.keys() :
new_state_dict['.'.join(key.split('.')[1:])] = state_dict[key]
return new_state_dict<load_pretrained>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y)
 X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
 X_test = X_test.reshape(X_test.shape[0], 28, 28, 1 )
model_b1_f0 = Net(name = 'efficientnet_b1')
 model_b1_f0.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b1-224-fold-0/epoch1-valid_loss_epoch0.118.pth')))
 model_b1_f0.cuda() ;
model_b1_f0.eval() ;
model_b1_f1 = Net(name = 'efficientnet_b1')
 model_b1_f1.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b1-224-fold-1/epoch1-valid_loss_epoch0.117.pth')))
 model_b1_f1.cuda() ;
model_b1_f1.eval() ;
model_b1_f2 = Net(name = 'efficientnet_b1')
 model_b1_f2.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b1-224-fold-4/epoch1-valid_loss_epoch0.119.pth')))
 model_b1_f2.cuda() ;
model_b1_f2.eval() ;
model_b1_f3 = Net(name = 'efficientnet_b1')
 model_b1_f3.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b1-224-fold-3/epoch1-valid_loss_epoch0.116.pth')))
 model_b1_f3.cuda() ;
model_b1_f3.eval() ;
model_b1_f4 = Net(name = 'efficientnet_b1')
 model_b1_f4.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b1-224-fold-44/epoch1-valid_loss_epoch0.117.pth')))
 model_b1_f4.cuda() ;
model_b1_f4.eval() ;
model_b0_f0 = Net(name = 'efficientnet_b0')
 model_b0_f0.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b0-224-fold-0/epoch1-valid_loss_epoch0.119.pth')))
 model_b0_f0.cuda() ;
model_b0_f0.eval() ;
model_b0_f1 = Net(name = 'efficientnet_b0')
 model_b0_f1.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b0-224-fold-1/epoch1-valid_loss_epoch0.118.pth')))
 model_b0_f1.cuda() ;
model_b0_f1.eval() ;
model_b0_f2 = Net(name = 'efficientnet_b0')
 model_b0_f2.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b0-224-fold-2/epoch1-valid_loss_epoch0.120.pth')))
 model_b0_f2.cuda() ;
model_b0_f2.eval() ;
model_b0_f3 = Net(name = 'efficientnet_b0')
 model_b0_f3.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b0-224-fold-3/epoch1-valid_loss_epoch0.117.pth')))
 model_b0_f3.cuda() ;
model_b0_f3.eval() ;
model_b0_f4 = Net(name = 'efficientnet_b0')
 model_b0_f4.load_state_dict(update_state_dict(torch.load('.. /input/efficientnet-b0-224-fold-4/epoch8-valid_loss_epoch0.117.pth')))
 model_b0_f4.cuda() ;
model_b0_f4.eval() ;<prepare_output>
y_cat = to_categorical(y, 10)
 y_train_cat = to_categorical(y_train, 10)
 y_test_cat = to_categorical(y_test, 10 )
pred = torch.FloatTensor()
pred = pred.cuda()<categorify>
test = df_test.to_numpy(np.float64)
 test = test.reshape(test.shape[0], 28, 28, 1)
 test /= 255.0
tta1 = tta.ClassificationTTAWrapper(model_b1_f0, tta.aliases.flip_transform())
 tta2 = tta.ClassificationTTAWrapper(model_b1_f1, tta.aliases.flip_transform())
 tta3 = tta.ClassificationTTAWrapper(model_b1_f2, tta.aliases.flip_transform())
 tta4 = tta.ClassificationTTAWrapper(model_b1_f3, tta.aliases.flip_transform())
 tta5 = tta.ClassificationTTAWrapper(model_b1_f4, tta.aliases.flip_transform())
 tta6 = tta.ClassificationTTAWrapper(model_b0_f0, tta.aliases.flip_transform())
 tta7 = tta.ClassificationTTAWrapper(model_b0_f1, tta.aliases.flip_transform())
 tta8 = tta.ClassificationTTAWrapper(model_b0_f2, tta.aliases.flip_transform())
 tta9 = tta.ClassificationTTAWrapper(model_b0_f3, tta.aliases.flip_transform())
 tta10 = tta.ClassificationTTAWrapper(model_b0_f4, tta.aliases.flip_transform() )<normalization>
def convNeuralNetwork(filters=256, kernel_size=(3, 3), pool_size=(2, 2), units=128, dropout=0.2):
cnn = Sequential()
cnn.add(Conv2D(filters=filters, kernel_size=kernel_size, strides=(1, 1),
input_shape=(28, 28, 1), activation="relu", padding="same"))
cnn.add(MaxPool2D(pool_size=pool_size, padding="same"))
cnn.add(Conv2D(filters=filters, kernel_size=kernel_size, strides=(1, 1),
activation="relu", padding="same"))
cnn.add(MaxPool2D(pool_size=pool_size, padding="same"))
cnn.add(Flatten())
 cnn.add(Dense(units=units, activation="relu"))
cnn.add(Dropout(dropout))
cnn.add(Dense(units=units, activation="relu"))
cnn.add(Dropout(dropout))
cnn.add(Dense(units=10, activation="softmax"))
cnn.compile(optimizer="adamax", loss="categorical_crossentropy", metrics=["accuracy"])
 return cnn
with torch.no_grad() :
for inp in tqdm.tqdm(test_loader):
bs, c, h, w = inp.size()
input_var = torch.autograd.Variable(inp.view(-1, c, h, w ).cuda())
 output =(tta1(input_var)+tta2(input_var)+\
tta3(input_var)+tta4(input_var)+\
tta5(input_var)+tta6(input_var)+\
tta7(input_var)+tta8(input_var)+tta9(input_var)+tta10(input_var)) /10
output_mean = output.view(bs, -1)
 pred = torch.cat(( pred, output_mean.data), 0 )<count_missing_values>
early_stopping = EarlyStopping(monitor="val_loss", patience=10, verbose=1,
restore_best_weights=True)
 cnn = convNeuralNetwork(filters=2048, units=1024)
 cnn_hist = cnn.fit(X_train, y_train_cat, validation_data=(X_test, y_test_cat),
epochs=50, batch_size=256, callbacks=[early_stopping] )
def isNaN(num):
return num != num<load_from_csv>
early_stopping = EarlyStopping(monitor="loss", patience=10, restore_best_weights=True)
 model_checkpoint = ModelCheckpoint(filepath="./", monitor="loss", verbose=1,
save_best_only=True, save_weights_only=True)
 model = convNeuralNetwork(filters=2048, units=1024)
 model_hist = model.fit(X_totrain, y_cat, batch_size=256, epochs=50,
callbacks=[early_stopping, model_checkpoint] )
cell_df = pd.read_csv('./cell_df.csv')
 cell_df['cls'] = ''<feature_engineering>
y_pred = model.predict(test ).argmax(1)
 doSubmission(y_pred )