Kaggle competition: Digit Recognizer
!pip install timm<set_options>
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv' )
sys.path.append('.. /input/pytorch-images-seresnet')
 warnings.filterwarnings('ignore')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 IMAGE_SIZE = 640
BATCH_SIZE = 128
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH_resnet200d = '.. /input/resnet200d-public/resnet200d_320_CV9632.pth'
MODEL_PATH_seresnet152d = '.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth'
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['StudyInstanceUID'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}.jpg'
image = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize(
),
ToTensorV2() ,
])
 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
class SeResNet152D(nn.Module):
def __init__(self, model_name='seresnet152d_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
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
models200D = []
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH_resnet200d)['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 models152D = []
model = SeResNet152D()
model.load_state_dict(torch.load(MODEL_PATH_seresnet152d)['model'])
 model.eval()
model.to(device)
 models152D.append(model)
 test_dataset = TestDataset(test, transform=get_transforms())
 test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False,
num_workers=4 , pin_memory=True)
 predictions200d = inference(models200D, test_loader, device)
 predictions152d = inference(models152D, test_loader, device)
 predictions =(2 * predictions200d + predictions152d)/ 3.0
target_cols = test.iloc[:, 1:12].columns.tolist()
test[target_cols] = predictions
test[['StudyInstanceUID'] + target_cols].to_csv('submission.csv', index=False)
 test.head() 
<load_from_csv>
X = train_data.drop(['label'], axis=1)
 y = train_data['label']
del train_data
batch_size = 1
image_size = 512
tta = True
submit =(len(pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')) == 3582)
 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 = True
fast_sub_path = '.. /input/submissions/submission_RANZCR_CLiP.csv'<set_options>
X = X / 255.; test_data = test_data / 255.
 X = X.values.reshape(-1, 28, 28, 1)
 test_data = test_data.values.reshape(-1, 28, 28, 1 )
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')
 print(device )<choose_model_class>
import tensorflow as tf
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 = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[28, 28, 1]),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(units=256, activation='relu'),
tf.keras.layers.Dropout (.15),
tf.keras.layers.Dense(units=128, activation='relu'),
tf.keras.layers.Dropout (.1),
tf.keras.layers.Dense(units=64, activation='relu'),
tf.keras.layers.Dense(units=10, activation='softmax')
 ])
 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='acc',
patience=5,
factor=0.5,
min_lr=0.00001
)
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>
y = tf.keras.utils.to_categorical(y, num_classes=10)
 datagen = tf.keras.preprocessing.image.ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X)
 Xtrain, Xvalid, ytrain, yvalid = train_test_split(X, y, test_size=.1, random_state=333 )
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>
history = model.fit(datagen.flow(Xtrain, ytrain, batch_size=100), validation_data=(Xvalid, yvalid), epochs = 100, callbacks=[learning_rate_reduction] )
submit = False
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>
ypred = model.predict(test_data)
 for i in range(ypred.shape[0]):
pred = ypred[i, :]
ypred[i, 0] = list(pred ).index(pred.max() )
<load_from_csv><EOS> 
submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submission.iloc[:, 1] = ypred[:, 0].astype(np.int)
 submission.to_csv('submission.csv', index=False )