Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<choose_model_class>
sys.path.append('.. /input/pytorch-images-seresnet')
 warnings.filterwarnings('ignore')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=7,
verbose=1,
factor=0.1,
min_lr=1e-8 )<choose_model_class>
IMAGE_SIZE = 640
BATCH_SIZE = 128
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH = '.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth'
def build_norm_model() :
inputs = layers.Input(shape=(28, 28, 1))
x = layers.Conv2D(filters=64, kernel_size=(5, 5), strides=(1, 1), padding='same', input_shape=(28, 28, 1))(inputs)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.5 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Flatten()(x)
 x = layers.Dense(units=256 )(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.5 )(x)
 output = layers.Dense(10, activation='softmax' )(x)
 return Model(inputs=inputs, outputs=output )<predict_on_test>
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv' )
def make_prediction(model, x):
y_pred = model.predict(x)
 return np.argmax(y_pred, axis=1 )<categorify>
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize(
),
ToTensorV2() ,
] )
<train_model>
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
final_model = build_norm_model()
final_model.compile(optimizer=optimizers.RMSprop(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
 history_final = final_model.fit_generator(train_datagen.flow(X_train, y_train, batch_size=1024),
steps_per_epoch=100,
epochs=120,
validation_data=(X_valid, y_valid),
callbacks=[learning_rate_reduction],
verbose=1 )<save_to_csv>
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
y_result = make_prediction(final_model, X_test)
 sample_df['label'] = y_result
sample_df.to_csv('submission.csv',index=False )<set_options>
model = SeResNet152D()
model.load_state_dict(torch.load(MODEL_PATH)['model'])
 model.eval()
models = [model.to(device)]
plt.style.use('ggplot')
 %matplotlib inline
np.random.RandomState(42)
<load_from_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 )
<choose_model_class><EOS> 
target_cols = test.iloc[:, 1:12].columns.tolist()
test[target_cols] = predictions
test[['StudyInstanceUID'] + target_cols].to_csv('submission.csv', index=False)
 test.head()