Kaggle competition: Digit Recognizer
if not torch.cuda.is_available() :
device = torch.device("cpu")
 else:
device = torch.device("cuda")
 print(device )<load_pretrained>
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from keras.utils.np_utils import to_categorical
model_dir = TRAINED_MODEL
test_dir = TEST_RESIZED
test_file_list = [ 
(test_dir / f"{img_id}.png", [-1] * 11)
 for img_id in smpl_sub["StudyInstanceUID"].values]
test_loader = get_dataloaders_for_inference(test_file_list, batch_size=64)
 test_preds_arr = np.zeros(( N_FOLD, len(smpl_sub), N_CLASSES))
for fold_id in FOLDS:
print(f"[fold {fold_id}]")
 stgs = load_setting_file(model_dir / f"fold{fold_id}" / "settings.yml")
 stgs["model"]["params"]["pretrained"] = False
model = MultiHeadResNet200D(**stgs["model"]["params"])
 model_path = model_dir / f"best_model_fold{fold_id}.pth"
model.load_state_dict(torch.load(model_path, map_location=device))
test_pred = run_inference_loop(stgs, model, test_loader, device)
 test_preds_arr[fold_id] = test_pred
del model
torch.cuda.empty_cache()
gc.collect()<prepare_output>
train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 test=pd.read_csv('.. /input/digit-recognizer/test.csv' )
sub = smpl_sub.copy()
sub[CLASSES] = test_preds_arr.mean(axis=0 )<prepare_output>
x_train=train.drop(['label'],1)
 y_train=train['label']
Final_Submission = smpl_sub.copy()
Final_Submission[CLASSES] =.50 * sub[CLASSES] +.50 * submission[CLASSES]<save_to_csv>
x_train=np.array(x_train)
 test=np.array(test )
Final_Submission.to_csv("submission.csv", index=False )<set_options>
x_train=x_train/255
test=test/255
sys.path.append('.. /input/pytorch-images-seresnet')
 warnings.filterwarnings('ignore')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )<define_variables>
target=x_train.reshape(-1,28,28,1)
 test=test.reshape(-1,28,28,1)
 y_train=np.array(y_train)
 label=to_categorical(y_train)
 label.shape
IMAGE_SIZE = 640
BATCH_SIZE = 128
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH = '.. /input/resnet200d-public/resnet200d_320_CV9632.pth'<load_from_csv>
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv' )<categorify>
model=Sequential([
Conv2D(32,(5,5), activation='relu' , input_shape=(28,28,1)) ,
MaxPooling2D(pool_size=(2,2)) ,
Conv2D(64,(5,5), activation ='relu'),
MaxPooling2D(pool_size=(2,2)) ,
Dropout(0.25),
Conv2D(64,(3,3), activation ='relu'),
MaxPooling2D(pool_size=(2,2)) ,
Dropout(0.25),
Flatten() ,
Dense(64, activation='relu'),
Dense(10, activation='softmax')
 ] )
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize(
),
ToTensorV2() ,
] )<choose_model_class>
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'] )
class ResNet200D(nn.Module):
def __init__(self, model_name='resnet200d_320'):
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, 11)
 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<categorify>
model.fit(target,label,epochs=40,batch_size=64 )
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() + y_preds2.sigmoid().to('cpu' ).numpy())/ 2
avg_preds.append(y_preds)
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs<choose_model_class>
Y_pred = model.predict(test)
 Y_pred_classes = np.argmax(Y_pred,axis = 1 )
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH)['model'])
 model.eval()
models = [model.to(device)]<load_pretrained>
submission_data = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )
test_dataset = TestDataset(test, transform=get_transforms())
 test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False,
num_workers=4 , pin_memory=True)
 predictions = inference(models, test_loader, device )<save_to_csv>
submission_data['Label']=Y_pred_classes
target_cols = test.iloc[:, 1:12].columns.tolist()
test[target_cols] = predictions
test[['StudyInstanceUID'] + target_cols].to_csv('submission.csv', index=False)
 test.head()<define_variables>
submission_data.to_csv('submit.csv' ,index=False )
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']
fast_sub = False
fast_sub_path = '.. /input/xxxxxx/your_submission.csv'<set_options>
def test_output(i):
plt.imshow(x_train[i],cmap='gray')
 predicted=np.argmax(model.predict(target[i].reshape(-1,28,28,1)))
 actual=np.argmax(label[i])
 plt.xlabel(f'predicted= {predicted} Actual= {actual}' )
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>
from PIL import Image, ImageGrab
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>
def predict_digit1(img):
img = Image.open(img)
 plt.imshow(img)
 img = img.convert('L', dither=Image.NONE)
 img = img.resize(( 28,28))
img = np.array(img)
 img=np.invert(img)
 predicted=np.argmax(model.predict(img.reshape(-1,28,28,1)))
 plt.xlabel(f'Predicted= {predicted}' )
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>
predict_digit1('.. /input/temporary/Images/images.jfif' )
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>
predict_digit1('.. /input/temporary/Images/download.png' )
if submit:
test_preds = []
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 += [tta_inference_func(test_loader)]
else:
test_preds += [inference_func(test_loader)]
submission = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 submission[target_cols] = np.mean(test_preds, axis=0)
 submission.to_csv('submission.csv', index=False)
 else:
pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv' ).to_csv('submission.csv', index=False )<load_from_csv>
predict_digit1('.. /input/temporary/Images/531-5314816_handwritten-1-number-9-hand-written-png-transparent.png' )
<categorify><EOS> 
predict_digit1('.. /input/temporary/Images/1.jpg' )