Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
np.set_printoptions(precision=4)
 %config InlineBackend.figure_format = 'retina'
%matplotlib inline
init_notebook_mode(connected=True)
 warnings.filterwarnings("ignore")
 plt.style.use('fivethirtyeight')
 sns.set(font_scale=1.5)
DATA_PATH = '.. /input/melanoma-merged-external-data-512x512-jpeg'<normalization>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )
TEST_ROOT_PATH = f'{DATA_PATH}/512x512-test/512x512-test'
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, idx: int):
image_id = self.image_ids[idx]
image = cv2.imread(f'{TEST_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = image.astype(np.float32)/ 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<find_best_params>
def var_standardized(v):
stand=(v - v.mean())/ train.std()
return stand
def get_net() :
net = EfficientNet.from_name('efficientnet-b5')
 net._fc = nn.Linear(in_features=2048, out_features=2, bias=True)
 return net
net = get_net().cuda()<load_from_csv>
num_var = [f for f in train.columns if train.dtypes[f] != 'object']
num_var.remove('Survived')
 num_var.remove('PassengerId')
df_test = pd.read_csv(f'.. /input/siim-isic-melanoma-classification/test.csv', index_col='image_name')
 test_dataset = DatasetRetriever(
 image_ids=df_test.index.values,
transforms=get_valid_transforms() , 
)
 test_loader = torch.utils.data.DataLoader(
 test_dataset,
batch_size=8,
num_workers=2,
shuffle=False,
sampler=SequentialSampler(test_dataset),
pin_memory=False,
drop_last=False,
)<load_pretrained>
TRM=train.isna().sum().sum()
TSM=test.isna().sum().sum()
print(f'Mising Value percentage for train: {(TRM/ len(train)) *100}%')
 print(f'Mising Value percentage for test: {(TSM / len(test)) *100}%' )
checkpoint_path = '.. /input/melanoma-public-checkpoints/effnet5-best-score-checkpoint-015epoch-version2.bin'
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint);
net.eval() ;<concatenate>
print("- Mising Value:
", train.isna().sum() ,"
- Total of Mising Value : ", train.isna().sum().sum() )
result = {'image_name': [], 'target': []}
for images, image_names in tqdm(test_loader, total=len(test_loader)) :
with torch.no_grad() :
images = images.cuda().float()
outputs = net(images)
 y_pred = nn.functional.softmax(outputs, dim=1 ).data.cpu().numpy() [:,1]
result['image_name'].extend(image_names)
 result['target'].extend(y_pred)
 submission = pd.DataFrame(result )<save_to_csv>
print("- Mising Value:
", test.isna().sum() ,"
- Total of Mising Value : ", test.isna().sum().sum() )
submission.to_csv('submission.csv', index=False)
 submission['target'].hist(bins=100);<set_options>
train.Embarked.replace(np.nan , 'S' , inplace=True)
 test.Embarked.replace(np.nan , 'S' , inplace=True)
 train['Cabin'] = train['Cabin'].map(lambda x:0 if pd.notnull(x)== False else 1)
 test['Cabin'] = test['Cabin'].map(lambda x:0 if pd.notnull(x)== False else 1)
 train['Name'] = train['Name'].str.replace('(' , '')
 train['Name'] = train['Name'].str.replace(')' , '')
 test['Name'] = test['Name'].str.replace('(' , '')
 test['Name'] = test['Name'].str.replace(')' , '')
 mean_fare= pd.DataFrame(test.groupby('Pclass')[['Fare']].mean())
 for item, i in test['Fare'].iteritems() :
if pd.notnull(i)==False:
pclass_fare=test.Pclass.iloc[item]
test['Fare'].iloc[item]=mean_fare.iloc[pclass_fare-1][0]
%reload_ext autoreload
%autoreload 2
%matplotlib inline
%matplotlib inline 
<categorify>
mean_age= pd.DataFrame(test.groupby('Pclass')[['Age']].mean())
 mean_age
GlobalParams = collections.namedtuple('GlobalParams', [
'batch_norm_momentum', 'batch_norm_epsilon', 'dropout_rate',
'num_classes', 'width_coefficient', 'depth_coefficient',
'depth_divisor', 'min_depth', 'drop_connect_rate', 'image_size'])
 BlockArgs = collections.namedtuple('BlockArgs', [
'kernel_size', 'num_repeat', 'input_filters', 'output_filters',
'expand_ratio', 'id_skip', 'stride', 'se_ratio'])
 GlobalParams.__new__.__defaults__ =(None,)* len(GlobalParams._fields)
 BlockArgs.__new__.__defaults__ =(None,)* len(BlockArgs._fields)
 def relu_fn(x):

 return x * torch.sigmoid(x)
 def round_filters(filters, global_params):

 multiplier = global_params.width_coefficient
if not multiplier:
return filters
divisor = global_params.depth_divisor
min_depth = global_params.min_depth
filters *= multiplier
min_depth = min_depth or divisor
new_filters = max(min_depth, int(filters + divisor / 2)// divisor * divisor)
 if new_filters < 0.9 * filters:
new_filters += divisor
return int(new_filters)
 def round_repeats(repeats, global_params):

 multiplier = global_params.depth_coefficient
if not multiplier:
return repeats
return int(math.ceil(multiplier * repeats))
def drop_connect(inputs, p, training):

 if not training: return inputs
batch_size = inputs.shape[0]
keep_prob = 1 - p
random_tensor = keep_prob
random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype, device=inputs.device)
 binary_tensor = torch.floor(random_tensor)
 output = inputs / keep_prob * binary_tensor
return output
def get_same_padding_conv2d(image_size=None):

 if image_size is None:
return Conv2dDynamicSamePadding
else:
return partial(Conv2dStaticSamePadding, image_size=image_size)
 class Conv2dDynamicSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True):
super().__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]]*2
def forward(self, x):
ih, iw = x.size() [-2:]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2])
 return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 class Conv2dStaticSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs):
super().__init__(in_channels, out_channels, kernel_size, **kwargs)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]] * 2
assert image_size is not None
ih, iw = image_size if type(image_size)== list else [image_size, image_size]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
self.static_padding = nn.ZeroPad2d(( pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2))
else:
self.static_padding = Identity()
def forward(self, x):
x = self.static_padding(x)
 x = F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 return x
class Identity(nn.Module):
def __init__(self,):
super(Identity, self ).__init__()
def forward(self, input):
return input<define_search_space>
def impute_age(age_pclass):
Age = age_pclass[0]
Pclass = age_pclass[1]
if pd.isnull(Age):
if Pclass == 1:
return 38
elif Pclass == 2:
return 30
else:
return 25
else:
return Age
def efficientnet_params(model_name):

 params_dict = {
'efficientnet-b0':(1.0, 1.0, 224, 0.2),
'efficientnet-b1':(1.0, 1.1, 240, 0.2),
'efficientnet-b2':(1.1, 1.2, 260, 0.3),
'efficientnet-b3':(1.2, 1.4, 300, 0.3),
'efficientnet-b4':(1.4, 1.8, 380, 0.4),
'efficientnet-b5':(1.6, 2.2, 456, 0.4),
'efficientnet-b6':(1.8, 2.6, 528, 0.5),
'efficientnet-b7':(2.0, 3.1, 600, 0.5),
}
return params_dict[model_name]
class BlockDecoder(object):

 @staticmethod
def _decode_block_string(block_string):

 assert isinstance(block_string, str)
 ops = block_string.split('_')
 options = {}
for op in ops:
splits = re.split(r'(\d.*)', op)
 if len(splits)>= 2:
key, value = splits[:2]
options[key] = value
assert(( 's' in options and len(options['s'])== 1)or 
(len(options['s'])== 2 and options['s'][0] == options['s'][1]))
return BlockArgs(
 kernel_size=int(options['k']),
num_repeat=int(options['r']),
input_filters=int(options['i']),
output_filters=int(options['o']),
expand_ratio=int(options['e']),
id_skip=('noskip' not in block_string),
se_ratio=float(options['se'])if 'se' in options else None,
stride=[int(options['s'][0])])
 @staticmethod
def _encode_block_string(block):

 args = [
'r%d' % block.num_repeat,
'k%d' % block.kernel_size,
's%d%d' %(block.strides[0], block.strides[1]),
'e%s' % block.expand_ratio,
'i%d' % block.input_filters,
'o%d' % block.output_filters
]
if 0 < block.se_ratio <= 1:
args.append('se%s' % block.se_ratio)
 if block.id_skip is False:
args.append('noskip')
 return '_'.join(args)
 @staticmethod
def decode(string_list):

 assert isinstance(string_list, list)
 blocks_args = []
for block_string in string_list:
blocks_args.append(BlockDecoder._decode_block_string(block_string))
return blocks_args
@staticmethod
def encode(blocks_args):

 block_strings = []
for block in blocks_args:
block_strings.append(BlockDecoder._encode_block_string(block))
return block_strings
def efficientnet(width_coefficient=None, depth_coefficient=None, dropout_rate=0.2,
drop_connect_rate=0.2, image_size=None, num_classes=1000):

 blocks_args = [
'r1_k3_s11_e1_i32_o16_se0.25', 'r2_k3_s22_e6_i16_o24_se0.25',
'r2_k5_s22_e6_i24_o40_se0.25', 'r3_k3_s22_e6_i40_o80_se0.25',
'r3_k5_s11_e6_i80_o112_se0.25', 'r4_k5_s22_e6_i112_o192_se0.25',
'r1_k3_s11_e6_i192_o320_se0.25',
]
blocks_args = BlockDecoder.decode(blocks_args)
 global_params = GlobalParams(
 batch_norm_momentum=0.99,
batch_norm_epsilon=1e-3,
dropout_rate=dropout_rate,
drop_connect_rate=drop_connect_rate,
num_classes=num_classes,
width_coefficient=width_coefficient,
depth_coefficient=depth_coefficient,
depth_divisor=8,
min_depth=None,
image_size=image_size, 
)
 return blocks_args, global_params
def get_model_params(model_name, override_params):

 if model_name.startswith('efficientnet'):
w, d, s, p = efficientnet_params(model_name)
 blocks_args, global_params = efficientnet(
 width_coefficient=w, depth_coefficient=d, dropout_rate=p, image_size=s)
 else:
raise NotImplementedError('model name is not pre-defined: %s' % model_name)
 if override_params:
global_params = global_params._replace(**override_params)
 return blocks_args, global_params
url_map = {
'efficientnet-b0': 'http://storage.googleapis.com/public-models/efficientnet-b0-08094119.pth',
'efficientnet-b1': 'http://storage.googleapis.com/public-models/efficientnet-b1-dbc7070a.pth',
'efficientnet-b2': 'http://storage.googleapis.com/public-models/efficientnet-b2-27687264.pth',
'efficientnet-b3': 'http://storage.googleapis.com/public-models/efficientnet-b3-c8376fa2.pth',
'efficientnet-b4': 'http://storage.googleapis.com/public-models/efficientnet-b4-e116e8b3.pth',
'efficientnet-b5': 'http://storage.googleapis.com/public-models/efficientnet-b5-586e6cc6.pth',
}
def load_pretrained_weights(model, model_name, load_fc=True):

 state_dict = model_zoo.load_url(url_map[model_name])
 if load_fc:
model.load_state_dict(state_dict)
 else:
state_dict.pop('_fc.weight')
 state_dict.pop('_fc.bias')
 res = model.load_state_dict(state_dict, strict=False)
 assert str(res.missing_keys)== str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
print('Loaded pretrained weights for {}'.format(model_name))
class MBConvBlock(nn.Module):

 def __init__(self, block_args, global_params):
super().__init__()
self._block_args = block_args
self._bn_mom = 1 - global_params.batch_norm_momentum
self._bn_eps = global_params.batch_norm_epsilon
self.has_se =(self._block_args.se_ratio is not None)and(0 < self._block_args.se_ratio <= 1)
 self.id_skip = block_args.id_skip
Conv2d = get_same_padding_conv2d(image_size=global_params.image_size)
 inp = self._block_args.input_filters
oup = self._block_args.input_filters * self._block_args.expand_ratio
if self._block_args.expand_ratio != 1:
self._expand_conv = Conv2d(in_channels=inp, out_channels=oup, kernel_size=1, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 k = self._block_args.kernel_size
s = self._block_args.stride
self._depthwise_conv = Conv2d(
 in_channels=oup, out_channels=oup, groups=oup,
kernel_size=k, stride=s, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 if self.has_se:
num_squeezed_channels = max(1, int(self._block_args.input_filters * self._block_args.se_ratio))
self._se_reduce = Conv2d(in_channels=oup, out_channels=num_squeezed_channels, kernel_size=1)
 self._se_expand = Conv2d(in_channels=num_squeezed_channels, out_channels=oup, kernel_size=1)
 final_oup = self._block_args.output_filters
self._project_conv = Conv2d(in_channels=oup, out_channels=final_oup, kernel_size=1, bias=False)
 self._bn2 = nn.BatchNorm2d(num_features=final_oup, momentum=self._bn_mom, eps=self._bn_eps)
 def forward(self, inputs, drop_connect_rate=None):

 x = inputs
if self._block_args.expand_ratio != 1:
x = relu_fn(self._bn0(self._expand_conv(inputs)))
 x = relu_fn(self._bn1(self._depthwise_conv(x)))
 if self.has_se:
x_squeezed = F.adaptive_avg_pool2d(x, 1)
 x_squeezed = self._se_expand(relu_fn(self._se_reduce(x_squeezed)))
 x = torch.sigmoid(x_squeezed)* x
x = self._bn2(self._project_conv(x))
input_filters, output_filters = self._block_args.input_filters, self._block_args.output_filters
if self.id_skip and self._block_args.stride == 1 and input_filters == output_filters:
if drop_connect_rate:
x = drop_connect(x, p=drop_connect_rate, training=self.training)
 x = x + inputs
return x
class EfficientNet(nn.Module):

 def __init__(self, blocks_args=None, global_params=None):
super().__init__()
assert isinstance(blocks_args, list), 'blocks_args should be a list'
assert len(blocks_args)> 0, 'block args must be greater than 0'
self._global_params = global_params
self._blocks_args = blocks_args
Conv2d = get_same_padding_conv2d(image_size=global_params.image_size)
 bn_mom = 1 - self._global_params.batch_norm_momentum
bn_eps = self._global_params.batch_norm_epsilon
in_channels = 3
out_channels = round_filters(32, self._global_params)
 self._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._blocks = nn.ModuleList([])
 for block_args in self._blocks_args:
block_args = block_args._replace(
 input_filters=round_filters(block_args.input_filters, self._global_params),
output_filters=round_filters(block_args.output_filters, self._global_params),
num_repeat=round_repeats(block_args.num_repeat, self._global_params)
)
 self._blocks.append(MBConvBlock(block_args, self._global_params))
if block_args.num_repeat > 1:
block_args = block_args._replace(input_filters=block_args.output_filters, stride=1)
 for _ in range(block_args.num_repeat - 1):
self._blocks.append(MBConvBlock(block_args, self._global_params))
in_channels = block_args.output_filters
out_channels = round_filters(1280, self._global_params)
 self._conv_head = Conv2d(in_channels, out_channels, kernel_size=1, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._dropout = self._global_params.dropout_rate
self._fc = nn.Linear(out_channels, self._global_params.num_classes)
 def extract_features(self, inputs):

 x = relu_fn(self._bn0(self._conv_stem(inputs)))
 for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx)/ len(self._blocks)
 x = block(x, drop_connect_rate=drop_connect_rate)
 x = relu_fn(self._bn1(self._conv_head(x)))
 return x
def forward(self, inputs):

 x = self.extract_features(inputs)
 x = F.adaptive_avg_pool2d(x, 1 ).squeeze(-1 ).squeeze(-1)
 if self._dropout:
x = F.dropout(x, p=self._dropout, training=self.training)
 x = self._fc(x)
 return x
@classmethod
def from_name(cls, model_name, override_params=None):
cls._check_model_name_is_valid(model_name)
 blocks_args, global_params = get_model_params(model_name, override_params)
 return EfficientNet(blocks_args, global_params)
 @classmethod
def from_pretrained(cls, model_name, num_classes=1000):
model = EfficientNet.from_name(model_name, override_params={'num_classes': num_classes})
 return model
@classmethod
def get_image_size(cls, model_name):
cls._check_model_name_is_valid(model_name)
 _, _, res, _ = efficientnet_params(model_name)
 return res
@classmethod
def _check_model_name_is_valid(cls, model_name, also_need_pretrained_weights=False):

 num_models = 4 if also_need_pretrained_weights else 8
valid_models = ['efficientnet_b'+str(i)for i in range(num_models)]
if model_name.replace('-','_')not in valid_models:
raise ValueError('model_name should be one of: ' + ', '.join(valid_models))<load_pretrained>
for item, i in train['Age'].iteritems() :
if pd.notnull(i)==False:
Age_ver2 = impute_age([i, train.Pclass.iloc[item]])
 train['Age'].iloc[item] = Age_ver2
md_ef = EfficientNet.from_pretrained('efficientnet-b5', num_classes=1 )<load_from_csv>
for item, i in test['Age'].iteritems() :
if pd.notnull(i)==False:
Age_ver2 = impute_age([i, test.Pclass.iloc[item]])
 test['Age'].iloc[item] = Age_ver2
def get_df() :
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 return df, test_df
df, test_df = get_df()<feature_engineering>
train.isna().sum().sum()
bs = 32
sz = 224
tfms = get_transforms(do_flip=True,flip_vert=True )<compute_test_metric>
test.isna().sum().sum()
def qk(y_pred, y):
return torch.tensor(cohen_kappa_score(torch.round(y_pred), y, weights='quadratic'), device='cuda:0' )<load_pretrained>
train= pd.get_dummies(train, columns=['Sex', 'Embarked'], drop_first=True)
 train = pd.get_dummies(train, columns=['Pclass'], drop_first=True )
learn = Learner(data,
md_ef,
metrics = [qk],
model_dir="models" ).to_fp16()
learn.data.add_test(ImageList.from_df(test_df,
'.. /input/aptos2019-blindness-detection',
folder='test_images',
suffix='.png'))<train_model>
test= pd.get_dummies(test, columns=['Sex', 'Embarked'], drop_first=True)
 test= pd.get_dummies(test, columns=['Pclass'], drop_first=True )
learn.fit_one_cycle(10, 1e-3 )<load_pretrained>
train['Name'] = train.Name.str.extract('([A-Za-z]+)\.', expand=False)
 train['Name'] = train['Name'].replace(['Lady', 'Countess','Capt', 'Col',\
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 train['Name'] = train['Name'].replace('Mlle', 'Miss')
 train['Name'] = train['Name'].replace('Ms', 'Miss')
 train['Name'] = train['Name'].replace('Mme', 'Mrs')
 train= pd.get_dummies(train, columns=['Name'], drop_first=True)
learn.load('abcdef');<compute_test_metric>
test['Name'] = test.Name.str.extract('([A-Za-z]+)\.', expand=False)
 test['Name'] = test['Name'].replace(['Lady', 'Countess','Capt', 'Col',\
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 test['Name'] = test['Name'].replace('Mlle', 'Miss')
 test['Name'] = test['Name'].replace('Ms', 'Miss')
 test['Name'] = test['Name'].replace('Mme', 'Mrs')
 test= pd.get_dummies(test, columns=['Name'], drop_first=True )
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<save_to_csv>
train["Fam_size"] = train['SibSp'] + train["Parch"] + 1
test["Fam_size"] = test['SibSp'] + test["Parch"] + 1
def run_subm(learn=learn, coefficients=[0.5, 1.5, 2.5, 3.5]):
opt = OptimizedRounder()
preds,y = learn.get_preds(DatasetType.Test)
 tst_pred = opt.predict(preds, coefficients)
 test_df.diagnosis = tst_pred.astype(int)
 test_df.to_csv('submission.csv',index=False)
 print('done' )<import_modules>
test.duplicated().sum()
import numpy as np
import pandas as pd 
<import_modules>
train.duplicated().sum()
__all__ = [
'alexnet',
'densenet121', 'densenet169', 'densenet201', 'densenet161',
'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152',
'inceptionv3',
'squeezenet1_0', 'squeezenet1_1',
'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn',
'vgg19_bn', 'vgg19'
]
model_urls = {
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
'densenet121': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet121-fbdb23505.pth',
'densenet169': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet169-f470b90a4.pth',
'densenet201': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet201-5750cbb1e.pth',
'densenet161': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet161-347e6b360.pth',
'inceptionv3': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth',
'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
}
input_sizes = {}
means = {}
stds = {}
for model_name in __all__:
input_sizes[model_name] = [3, 224, 224]
means[model_name] = [0.485, 0.456, 0.406]
stds[model_name] = [0.229, 0.224, 0.225]
for model_name in ['inceptionv3']:
input_sizes[model_name] = [3, 299, 299]
means[model_name] = [0.5, 0.5, 0.5]
stds[model_name] = [0.5, 0.5, 0.5]
pretrained_settings = {}
for model_name in __all__:
pretrained_settings[model_name] = {
'imagenet': {
'url': model_urls[model_name],
'input_space': 'RGB',
'input_size': input_sizes[model_name],
'input_range': [0, 1],
'mean': means[model_name],
'std': stds[model_name],
'num_classes': 1000
}
}
def update_state_dict(state_dict):
pattern = re.compile(
 r'^ (.*denselayer\d+\.( ?:norm|relu|conv)) \.((?:[12])\.( ?:weight|bias|running_mean|running_var)) $')
 for key in list(state_dict.keys()):
res = pattern.match(key)
 if res:
new_key = res.group(1)+ res.group(2)
 state_dict[new_key] = state_dict[key]
del state_dict[key]
return state_dict
def load_pretrained(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
 state_dict = model_zoo.load_url(settings['url'])
 state_dict = update_state_dict(state_dict)
 model.load_state_dict(state_dict)
 model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
def modify_alexnet(model):
model._features = model.features
del model.features
model.dropout0 = model.classifier[0]
model.linear0 = model.classifier[1]
model.relu0 = model.classifier[2]
model.dropout1 = model.classifier[3]
model.linear1 = model.classifier[4]
model.relu1 = model.classifier[5]
model.last_linear = model.classifier[6]
del model.classifier
def features(self, input):
x = self._features(input)
 x = x.view(x.size(0), 256 * 6 * 6)
 x = self.dropout0(x)
 x = self.linear0(x)
 x = self.relu0(x)
 x = self.dropout1(x)
 x = self.linear1(x)
 return x
def logits(self, features):
x = self.relu1(features)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def alexnet(num_classes=1000, pretrained='imagenet'):
r
model = models.alexnet(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['alexnet'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_alexnet(model)
 return model
def modify_densenets(model):
model.last_linear = model.classifier
del model.classifier
def logits(self, features):
x = F.relu(features, inplace=True)
 x = F.avg_pool2d(x, kernel_size=7, stride=1)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def densenet121(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet121(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet121'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def densenet169(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet169(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet169'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def densenet201(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet201(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet201'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def densenet161(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet161(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet161'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def inceptionv3(num_classes=1000, pretrained='imagenet'):
r
model = models.inception_v3(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['inceptionv3'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model.last_linear = model.fc
del model.fc
def features(self, input):
x = self.Conv2d_1a_3x3(input)
 x = self.Conv2d_2a_3x3(x)
 x = self.Conv2d_2b_3x3(x)
 x = F.max_pool2d(x, kernel_size=3, stride=2)
 x = self.Conv2d_3b_1x1(x)
 x = self.Conv2d_4a_3x3(x)
 x = F.max_pool2d(x, kernel_size=3, stride=2)
 x = self.Mixed_5b(x)
 x = self.Mixed_5c(x)
 x = self.Mixed_5d(x)
 x = self.Mixed_6a(x)
 x = self.Mixed_6b(x)
 x = self.Mixed_6c(x)
 x = self.Mixed_6d(x)
 x = self.Mixed_6e(x)
 if self.training and self.aux_logits:
self._out_aux = self.AuxLogits(x)
 x = self.Mixed_7a(x)
 x = self.Mixed_7b(x)
 x = self.Mixed_7c(x)
 return x
def logits(self, features):
x = F.avg_pool2d(features, kernel_size=8)
 x = F.dropout(x, training=self.training)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 if self.training and self.aux_logits:
aux = self._out_aux
self._out_aux = None
return x, aux
return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def modify_resnets(model):
model.last_linear = model.fc
model.fc = None
def features(self, input):
x = self.conv1(input)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.maxpool(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
def logits(self, features):
x = self.avgpool(features)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def resnet18(num_classes=1000, pretrained='imagenet'):

 model = models.resnet18(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet18'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet34(num_classes=1000, pretrained='imagenet'):

 model = models.resnet34(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet34'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet50(num_classes=1000, pretrained='imagenet'):

 model = models.resnet50(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet50'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet101(num_classes=1000, pretrained='imagenet'):

 model = models.resnet101(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet101'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet152(num_classes=1000, pretrained='imagenet'):

 model = models.resnet152(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet152'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def modify_squeezenets(model):
model.dropout = model.classifier[0]
model.last_conv = model.classifier[1]
model.relu = model.classifier[2]
model.avgpool = model.classifier[3]
del model.classifier
def logits(self, features):
x = self.dropout(features)
 x = self.last_conv(x)
 x = self.relu(x)
 x = self.avgpool(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def squeezenet1_0(num_classes=1000, pretrained='imagenet'):
r
model = models.squeezenet1_0(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['squeezenet1_0'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_squeezenets(model)
 return model
def squeezenet1_1(num_classes=1000, pretrained='imagenet'):
r
model = models.squeezenet1_1(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['squeezenet1_1'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_squeezenets(model)
 return model
def modify_vggs(model):
model._features = model.features
del model.features
model.linear0 = model.classifier[0]
model.relu0 = model.classifier[1]
model.dropout0 = model.classifier[2]
model.linear1 = model.classifier[3]
model.relu1 = model.classifier[4]
model.dropout1 = model.classifier[5]
model.last_linear = model.classifier[6]
del model.classifier
def features(self, input):
x = self._features(input)
 x = x.view(x.size(0), -1)
 x = self.linear0(x)
 x = self.relu0(x)
 x = self.dropout0(x)
 x = self.linear1(x)
 return x
def logits(self, features):
x = self.relu1(features)
 x = self.dropout1(x)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def vgg11(num_classes=1000, pretrained='imagenet'):

 model = models.vgg11(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg11'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg11_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg11_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg11_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg13(num_classes=1000, pretrained='imagenet'):

 model = models.vgg13(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg13'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg13_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg13_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg13_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg16(num_classes=1000, pretrained='imagenet'):

 model = models.vgg16(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg16'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg16_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg16_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg16_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg19(num_classes=1000, pretrained='imagenet'):

 model = models.vgg19(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg19'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg19_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg19_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg19_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model 
<define_variables>
y_train = train.Survived
X_train = train.drop(['Survived', 'Ticket' ,'Fare_Range'] , axis=1)
 X_test = test.drop([ 'Ticket' ] , axis=1 )
__all__ = ['SENet', 'senet154', 'se_resnet50', 'se_resnet101', 'se_resnet152',
'se_resnext50_32x4d', 'se_resnext101_32x4d']
pretrained_settings = {
'senet154': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/senet154-c7b49a05.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet50': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet50-ce0d4300.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet101': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet101-7e38fcc6.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet152': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet152-d17c99b7.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnext50_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext50_32x4d-a260b3a4.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnext101_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
}
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self ).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
 self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1,
padding=0)
 self.relu = nn.ReLU(inplace=True)
 self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1,
padding=0)
 self.sigmoid = nn.Sigmoid()
def forward(self, x):
module_input = x
x = self.avg_pool(x)
 x = self.fc1(x)
 x = self.relu(x)
 x = self.fc2(x)
 x = self.sigmoid(x)
 return module_input * x
class Bottleneck(nn.Module):

 def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out = self.se_module(out)+ residual
out = self.relu(out)
 return out
class SEBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False)
 self.bn1 = nn.BatchNorm2d(planes * 2)
 self.conv2 = nn.Conv2d(planes * 2, planes * 4, kernel_size=3,
stride=stride, padding=1, groups=groups,
bias=False)
 self.bn2 = nn.BatchNorm2d(planes * 4)
 self.conv3 = nn.Conv2d(planes * 4, planes * 4, kernel_size=1,
bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNetBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEResNetBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False,
stride=stride)
 self.bn1 = nn.BatchNorm2d(planes)
 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1,
groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(planes)
 self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNeXtBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None, base_width=4):
super(SEResNeXtBottleneck, self ).__init__()
width = math.floor(planes *(base_width / 64)) * groups
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False,
stride=1)
 self.bn1 = nn.BatchNorm2d(width)
 self.conv2 = nn.Conv2d(width, width, kernel_size=3, stride=stride,
padding=1, groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(width)
 self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SENet(nn.Module):
def __init__(self, block, layers, groups, reduction, dropout_p=0.2,
inplanes=128, input_3x3=True, downsample_kernel_size=3,
downsample_padding=1, num_classes=1000):

 super(SENet, self ).__init__()
self.inplanes = inplanes
if input_3x3:
layer0_modules = [ 
('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
bias=False)) , 
('bn1', nn.BatchNorm2d(64)) , 
('relu1', nn.ReLU(inplace=True)) , 
('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
bias=False)) , 
('bn2', nn.BatchNorm2d(64)) , 
('relu2', nn.ReLU(inplace=True)) , 
('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,
bias=False)) , 
('bn3', nn.BatchNorm2d(inplanes)) , 
('relu3', nn.ReLU(inplace=True)) ,
]
else:
layer0_modules = [ 
('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,
padding=3, bias=False)) , 
('bn1', nn.BatchNorm2d(inplanes)) , 
('relu1', nn.ReLU(inplace=True)) ,
]
layer0_modules.append(( 'pool', nn.MaxPool2d(3, stride=2,
ceil_mode=True)))
 self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
self.layer1 = self._make_layer(
 block,
planes=64,
blocks=layers[0],
groups=groups,
reduction=reduction,
downsample_kernel_size=1,
downsample_padding=0 
)
 self.layer2 = self._make_layer(
 block,
planes=128,
blocks=layers[1],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer3 = self._make_layer(
 block,
planes=256,
blocks=layers[2],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer4 = self._make_layer(
 block,
planes=512,
blocks=layers[3],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.avg_pool = nn.AvgPool2d(7, stride=1)
 self.dropout = nn.Dropout(dropout_p)if dropout_p is not None else None
self.last_linear = nn.Linear(512 * block.expansion, num_classes)
 def _make_layer(self, block, planes, blocks, groups, reduction, stride=1,
downsample_kernel_size=1, downsample_padding=0):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=downsample_kernel_size, stride=stride,
padding=downsample_padding, bias=False),
nn.BatchNorm2d(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, groups, reduction, stride,
downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, groups, reduction))
return nn.Sequential(*layers)
 def features(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
def logits(self, x):
x = self.avg_pool(x)
 if self.dropout is not None:
x = self.dropout(x)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, x):
x = self.features(x)
 x = self.logits(x)
 return x
def initialize_pretrained_model(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
 settings['num_classes'], num_classes)
 model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
def senet154(num_classes=1000, pretrained='imagenet'):
model = SENet(SEBottleneck, [3, 8, 36, 3], groups=64, reduction=16,
dropout_p=0.2, num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['senet154'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet50(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 4, 6, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet50'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet101(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 4, 23, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet101'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet152(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 8, 36, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet152'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnext50_32x4d(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 6, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext50_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnext101_32x4d(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 23, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext101_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model<normalization>
scaler = StandardScaler()
Xs_train = scaler.fit_transform(X_train)
 Xs_test = scaler.transform(X_test )
sys.path.append('/kaggle/working/')
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 def get_se_resnet50_gem(pretrain):
if pretrain == 'imagenet':
model = se_resnet50(num_classes=1000, pretrained='imagenet')
 else:
model = se_resnet50(num_classes=1000, pretrained=None)
 model.avg_pool = GeM()
model.last_linear = nn.Linear(2048, 1)
 return model
def get_densenet121_gem(pretrain):
if pretrain == 'imagenet':
model = densenet121(num_classes=1000, pretrained='imagenet')
 else:
model = densenet121(num_classes=1000, pretrained=None)
 model.avg_pool = GeM()
model.last_linear = nn.Linear(1024, 1)
 return model<set_options>
logreg = LogisticRegression()
logreg.fit(Xs_train, y_train )
TEST_IMAGE_PATH = '/kaggle/input/aptos2019-blindness-detection/test_images'
device = torch.device("cuda")
 test_images = glob(os.path.join(TEST_IMAGE_PATH, '*.png')) 
<init_hyperparams>
Y_pred=logreg.predict(Xs_test )
def make_predictions(model, test_images, transforms, size=256, device=torch.device("cuda")) :
predictions = []
for i, im_path in enumerate(test_images):
image = Image.open(im_path)
 image = image.resize(( size, size), resample=Image.BILINEAR)
 image = transforms(image ).to(device)
 output = model(image.unsqueeze(0))
output_flip = model(torch.flip(image.unsqueeze(0), dims=(3,)))
 final_prediction =(output.item() +output_flip.item())/2
predictions.append(( os.path.splitext(im_path.split('/')[-1])[0], final_prediction))
return predictions<load_pretrained>
logreg.score(Xs_train, y_train )
MODEL_PATH = '.. /input/densenet121/model_densenet121_bs64_30.pth'
model = get_densenet121_gem(pretrain=False)
 model.to(device)
 model.load_state_dict(torch.load(MODEL_PATH, map_location='cuda:0'))
model.eval()
norm = transforms.Compose([transforms.ToTensor() ])
<choose_model_class>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submission2_LG.csv', index=False )
MODEL_PATH = '.. /input/seresnet50testpseudo/model10.pth'
model = get_se_resnet50_gem(pretrain=False)
 model.to(device)
 model.load_state_dict(torch.load(MODEL_PATH, map_location='cuda:0'))
model.eval()
norm = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
<choose_model_class>
lr_pipe2 = Pipeline([ 
('sscaler2', StandardScaler()), 
('logreg2', LogisticRegression(penalty='l1', C=0.1, solver='liblinear'))
])
MODEL_PATH = '.. /input/seresnet50pseudo-512/model30.pth'
model = get_se_resnet50_gem(pretrain=False)
 model.to(device)
 model.load_state_dict(torch.load(MODEL_PATH, map_location='cuda:0'))
model.eval()
norm = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 predictions_seresnet_512 = make_predictions(model, test_images, norm, size=512, device=torch.device("cuda"))<define_variables>
pipe_2_params = {'sscaler2__with_mean': [True, False],
'sscaler2__with_std': [True, False],
'logreg2__C': [0.1, 0.2,0.3],
'logreg2__solver':['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],
'logreg2__fit_intercept': [True, False],
'logreg2__penalty': ['l1', 'l2']}
final_predictions = predictions_seresnet_512 
<save_to_csv>
pipe_2_gridsearch = GridSearchCV(lr_pipe2,
pipe_2_params,
cv=5,
verbose=1 )
submission = pd.DataFrame(final_predictions)
 submission.columns = ['id_code','diagnosis']
submission.loc[submission.diagnosis < 0.75, 'diagnosis'] = 0
submission.loc[(0.75 <= submission.diagnosis)&(submission.diagnosis < 1.5), 'diagnosis'] = 1
submission.loc[(1.5 <= submission.diagnosis)&(submission.diagnosis < 2.5), 'diagnosis'] = 2
submission.loc[(2.5 <= submission.diagnosis)&(submission.diagnosis < 3.5), 'diagnosis'] = 3
submission.loc[3.5 <= submission.diagnosis, 'diagnosis'] = 4
submission['diagnosis'] = submission['diagnosis'].astype(int)
 submission.to_csv('submission.csv', index=False)
 submission.head()<install_modules>
pipe_2_gridsearch.fit(X_train, y_train);
!pip install.. /input/weights/timm-0.3.1-py3-none-any.whl<import_modules>
pipe_2_gridsearch.best_score_
device = "cuda:0" 
<import_modules>
pipe_2_gridsearch.best_estimator_
FeaturePyramidNetwork,
LastLevelMaxPool, 
)
 def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6, flatten=False):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
self.flatten = flatten
def forward(self, x):
x = gem(x, p=self.p, eps=self.eps)
 if self.flatten:
x = x.flatten(1)
 return x
def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class FrozenBatchNorm2d(torch.nn.Module):

 def __init__(
 self,
num_features: int,
eps: float = 1e-5,
n: Optional[int] = None, 
):
if n is not None:
warnings.warn("`n` argument is deprecated and has been renamed `num_features`",
DeprecationWarning)
 num_features = n
super(FrozenBatchNorm2d, self ).__init__()
self.eps = eps
self.register_buffer("weight", torch.ones(num_features))
self.register_buffer("bias", torch.zeros(num_features))
self.register_buffer("running_mean", torch.zeros(num_features))
self.register_buffer("running_var", torch.ones(num_features))
def _load_from_state_dict(
 self,
state_dict: dict,
prefix: str,
local_metadata: dict,
strict: bool,
missing_keys: List[str],
unexpected_keys: List[str],
error_msgs: List[str], 
):
num_batches_tracked_key = prefix + 'num_batches_tracked'
if num_batches_tracked_key in state_dict:
del state_dict[num_batches_tracked_key]
super(FrozenBatchNorm2d, self )._load_from_state_dict(
 state_dict, prefix, local_metadata, strict,
missing_keys, unexpected_keys, error_msgs)
 def forward(self, x: Tensor)-> Tensor:
w = self.weight.reshape(1, -1, 1, 1)
 b = self.bias.reshape(1, -1, 1, 1)
 rv = self.running_var.reshape(1, -1, 1, 1)
 rm = self.running_mean.reshape(1, -1, 1, 1)
 scale = w *(rv + self.eps ).rsqrt()
bias = b - rm * scale
return x * scale + bias
def __repr__(self)-> str:
return f"{self.__class__.__name__}({self.weight.shape[0]}, eps={self.eps})"
class Linear(nn.Linear):
def forward(self, input: torch.Tensor)-> torch.Tensor:
if torch.jit.is_scripting() :
bias = self.bias.to(dtype=input.dtype)if self.bias is not None else None
return F.linear(input, self.weight.to(dtype=input.dtype), bias=bias)
 else:
return F.linear(input, self.weight, self.bias)
 class backboneNet_efficient(nn.Module):
def __init__(self):
super(backboneNet_efficient, self ).__init__()
net = timm.create_model('tf_efficientnet_b4_ns', pretrained=False)
 layers_to_train = ['blocks']
for name, parameter in net.named_parameters() :
if all([not name.startswith(layer)for layer in layers_to_train]):
parameter.requires_grad_(False)
 self.num_features = 1792
self.conv_stem = net.conv_stem
self.bn1 = net.bn1
self.act1 = net.act1
self.block0 = net.blocks[0]
self.block1 = net.blocks[1]
self.block2 = net.blocks[2]
self.block3 = net.blocks[3]
self.block4 = net.blocks[4]
self.block5 = net.blocks[5]
self.block6 = net.blocks[6]
self.conv_head = net.conv_head
self.bn2 = net.bn2
self.act2 = net.act2
self.global_pool = net.global_pool
self.drop_rate = 0.4
self.rg_cls = Linear(self.num_features, 1, bias=True)
 self.cls_cls = Linear(self.num_features, 5, bias=True)
 self.ord_cls = Linear(self.num_features, 4, bias=True)
 def forward(self, x):
x1 = self.conv_stem(x)
 x2 = self.bn1(x1)
 x3 = self.act1(x2)
 x4 = self.block0(x1)
 x5 = self.block1(x4)
 x6 = self.block2(x5)
 x7 = self.block3(x6)
 x8 = self.block4(x7)
 x9 = self.block5(x8)
 x10 = self.block6(x9)
 x11 = self.conv_head(x10)
 x12 = self.bn2(x11)
 x13 = self.act2(x12)
 x14 = self.global_pool(x13)
 if self.drop_rate > 0.:
x14 = F.dropout(x14, p=self.drop_rate, training=self.training)
 x15 = self.rg_cls(x14)
 x16 = self.cls_cls(x14)
 x17 = self.ord_cls(x14)
 return x15, x16, x17 
<find_best_params>
pre = pipe_2_gridsearch.predict(X_test )
threshold = [0.75, 1.5, 2.5, 3.5]
def regress2class(out):
prediction = 0
for i in range(4):
prediction +=(out.data >= threshold[i] ).squeeze().cpu().item()
return prediction
def ordinal2class_prob(out):
pred_prob = torch.zeros(out.size(0), 5 ).cuda()
pred_prob[:, 0] =(1 - out[:, 0] ).squeeze()
pred_prob[:, 1] =(out[:, 0] *(1 - out[:, 1])).squeeze()
pred_prob[:, 2] =(out[:, 1] *(1 - out[:, 2])).squeeze()
pred_prob[:, 3] =(out[:, 2] *(1 - out[:, 3])).squeeze()
pred_prob[:, 4] = out[:, 3].squeeze().cpu()
return F.softmax(pred_prob, dim=1)
 def regress2class_prob(out):
pred_prob = torch.zeros(( out.size(0), 5)).cuda()
for i in range(out.size(0)) :
if out[i] < 4.:
l1 = int(math.floor(out[i]))
l2 = int(math.ceil(out[i]))
pred_prob[i][l1] = 1 -(out[i] - l1)
 pred_prob[i][l2] = 1 -(l2 - out[i])
 else:
pred_prob[i][4] = 1.
 return pred_prob
def combine3output(r_out, c_out, o_out):
R = regress2class(r_out.data)
 _, C = torch.max(c_out.data, 1)
 C = C.squeeze().item()
_, O = torch.max(o_out.data, 1)
 O = O.squeeze().item()
P =(R + C + O)/ 3.
 P = int(round(P))
return P<normalization>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": pre
})
 submission.to_csv('submission1_LG_pipline.csv', index=False )
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6, flatten=False):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
self.flatten = flatten
def forward(self, x):
x = gem(x, p=self.p, eps=self.eps)
 if self.flatten:
x = x.flatten(1)
 return x
def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class Regressor(nn.Module):
def __init__(self):
super(Regressor, self ).__init__()
self.backbone = timm.models.tf_efficientnet_b5_ns(pretrained=False)
 self.backbone.global_pool = GeM(flatten=True)
 self.regressor = nn.Linear(1000, 1)
 def forward(self, x):
x = self.backbone(x)
 out = self.regressor(x)
 out = torch.sigmoid(out)* 4.5
return out
class ThreeStage_Model(nn.Module):
def __init__(self, backbone=None):
super(ThreeStage_Model, self ).__init__()
self.backbone = timm.models.tf_efficientnet_b4_ns(pretrained=False)
 self.backbone.global_pool = GeM(flatten=True)
 self.classifier = nn.Sequential(
 nn.SiLU() ,
nn.Linear(1000, 500),
nn.SiLU() ,
nn.Linear(500, 5), 
)
 self.regressor = nn.Sequential(
 nn.SiLU() ,
nn.Linear(1000, 500),
nn.SiLU() ,
nn.Linear(500, 1), 
)
 self.ordinal = nn.Sequential(
 nn.SiLU() ,
nn.Linear(1000, 500),
nn.SiLU() ,
nn.Linear(500, 4), 
)
 self.final_regressor = nn.Sequential(
 nn.SiLU() ,
nn.Linear(10, 1), 
)
 def forward(self, x, final=False):
x = self.backbone(x)
 c_out = self.classifier(x)
 r_out = self.regressor(x)
 o_out = self.ordinal(x)
 if final:
out = torch.cat(( c_out, r_out, o_out), 1)
 out = self.final_regressor(out)
 out = torch.sigmoid(out)* 4.5
return out
else:
r_out = torch.sigmoid(r_out)* 4.5
o_out = torch.sigmoid(o_out)
 return c_out, r_out, o_out<load_from_csv>
lr_pipe2 = Pipeline([ 
('sscaler2', StandardScaler()), 
('knn', KNeighborsClassifier())
 ])
 pipe_2_params = {'sscaler2__with_mean': [True, False],
'sscaler2__with_std': [True, False],
'knn__n_neighbors': [3, 5, 7, 9, 11, 20, 50, 100],
'knn__weights': ['uniform', 'distance'],
'knn__metric': ['manhattan', 'euclidean']}
pipe_2_gridsearch = GridSearchCV(lr_pipe2,
pipe_2_params,
cv=5,
verbose=1 , scoring='roc_auc' )
test_ids = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 test_ids = np.squeeze(test_ids.values)
 transform1 = transforms.Compose([
trim() ,
cropTo4_3() ,
transforms.Resize(( 288, 384)) ,
transforms.ToTensor() ,
transforms.Normalize(mean=[0.384, 0.258, 0.174], std=[0.124, 0.089, 0.094]),
])
 transform2 = transforms.Compose([
transforms.Resize(( 280,280)) ,
transforms.CenterCrop(256),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 net1 = ThreeStage_Model()
net1.load_state_dict(torch.load(".. /input/weights/0.926_B4_3stage_5epoch_320finetune.pkl"))
net1 = net1.to(device)
 net1.eval()
net2 = backboneNet_efficient()
net2.load_state_dict(torch.load(".. /input/weights/1.pth", map_location='cpu'))
net2 = net2.to(device)
 net2.eval()
net3 = backboneNet_efficient()
net3.load_state_dict(torch.load(".. /input/weights/2.pth", map_location='cpu'))
net3 = net3.to(device)
 net3.eval()
net5 = backboneNet_efficient()
net5.load_state_dict(torch.load(".. /input/weights/0.929292_4", map_location='cpu'))
net5 = net5.to(device)
 net5.eval()<save_to_csv>
pipe_2_gridsearch.fit(X_train, y_train);
df = pd.DataFrame(submission, columns=["id_code", "diagnosis"])
 df.to_csv("submission.csv", index=False )<import_modules>
y_pre_GS_knn = pipe_2_gridsearch.predict(X_test )
from __future__ import print_function, absolute_import
import os
import sys
import time
import datetime
import argparse
import os.path as osp
import numpy as np
import random
from PIL import Image
import tqdm
import cv2
import csv
import math
import torchvision as tv
import torchvision
import torch.nn.functional as F
import torch.optim as optim
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from sklearn.metrics import f1_score
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torch.optim import lr_scheduler
from tqdm import tqdm
from torch.utils.data import Dataset
import torchvision.transforms as transforms
from tensorboardX import SummaryWriter<load_from_csv>
pipe_2_gridsearch.best_score_
name_file='.. /input/aptos2019-blindness-detection/test.csv'
csv_file=csv.reader(open(name_file,'r'))
content=[]
for line in csv_file:
content.append(line[0]+'.png')
 content=content[1:]<normalization>
pipe_2_gridsearch.best_estimator_
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class Baseline_single_gem(nn.Module):
def __init__(self, num_classes, loss_type="single BCE", **kwargs):
super(Baseline_single_gem, self ).__init__()
self.loss_type = loss_type
resnet50 = torchvision.models.densenet201(pretrained=False)
 self.base = nn.Sequential(*list(resnet50.children())[:-1])
 self.feature_dim = 1920
if self.loss_type == "single BCE":
self.ap = GeM()
self.classifiers = nn.Linear(in_features=self.feature_dim, out_features=num_classes)
 self.sigmoid = nn.Sigmoid()
self.dropout=nn.Dropout(0.5)
 self.cal_score=nn.Linear(in_features=num_classes, out_features=1)
 def freeze_base(self):
for p in self.base.parameters() :
p.requires_grad = False
def unfreeze_all(self):
for p in self.parameters() :
p.requires_grad = True
def forward(self, x1):
x = self.base(x1)
 map_feature=torch.tensor(x)
 if self.loss_type == "single BCE":
x = self.ap(x)
 x = self.dropout(x)
 x = x.view(x.size(0), -1)
 feat_m=torch.tensor(x)
 ys = self.classifiers(x)
 return ys
class Baseline_single(nn.Module):
def __init__(self, num_classes, loss_type="single BCE", **kwargs):
super(Baseline_single, self ).__init__()
self.loss_type = loss_type
resnet50 = torchvision.models.densenet201(pretrained=False)
 self.base = nn.Sequential(*list(resnet50.children())[:-1])
 self.feature_dim = 1920
if self.loss_type == "single BCE":
self.ap = nn.AdaptiveAvgPool2d(1)
 self.classifiers = nn.Linear(in_features=self.feature_dim, out_features=num_classes)
 self.sigmoid = nn.Sigmoid()
self.dropout=nn.Dropout(0.5)
 self.cal_score=nn.Linear(in_features=num_classes, out_features=1)
 def freeze_base(self):
for p in self.base.parameters() :
p.requires_grad = False
def unfreeze_all(self):
for p in self.parameters() :
p.requires_grad = True
def forward(self, x1):
x = self.base(x1)
 map_feature=torch.tensor(x)
 if self.loss_type == "single BCE":
x = self.ap(x)
 x = self.dropout(x)
 x = x.view(x.size(0), -1)
 feat_m=torch.tensor(x)
 ys = self.classifiers(x)
 return ys<train_on_grid>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": y_pre_GS_knn
})
 submission.to_csv('submission2_GS_knn.csv', index=False )
def cv_imread(file_path):
	cv_img=cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)
 	return cv_img
def change_size(image):
	
	b=cv2.threshold(image,15,255,cv2.THRESH_BINARY)
 	binary_image=b[1]
	binary_image=cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)
 	print(binary_image.shape)
 	x=binary_image.shape[0]
	print("高度x=",x)
 	y=binary_image.shape[1]
	print("宽度y=",y)
 	edges_x=[]
	edges_y=[]
	for i in range(x):
		for j in range(y):
			if binary_image[i][j]==255:
			
			
			 edges_x.append(i)
 			 edges_y.append(j)
 	left=min(edges_x)
 	right=max(edges_x)
 	width=right-left
	bottom=min(edges_y)
 	top=max(edges_y)
 	height=top-bottom
	pre1_picture=image[left:left+width,bottom:bottom+height]
	return pre1_picture
def crop_image1(img,tol=7):
	
	
		
	mask = img>tol
	return img[np.ix_(mask.any(1),mask.any(0)) ]
def crop_image_from_gray(img,tol=7):
	if img.ndim ==2:
		mask = img>tol
		return img[np.ix_(mask.any(1),mask.any(0)) ]
	elif img.ndim==3:
		gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 		mask = gray_img>tol
		
		check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0)) ].shape[0]
		if(check_shape == 0):
			return img
		else:
			img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0)) ]
			img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0)) ]
			img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0)) ]
	
			img = np.stack([img1,img2,img3],axis=-1)
 	
		return img
def load_ben_color(image, sigmaX=10):
	image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 	image = crop_image_from_gray(image)
 	image = cv2.resize(image,(492, 492))
	image=cv2.addWeighted(image,4, cv2.GaussianBlur(image ,(0,0), sigmaX),-4 ,128)
 		
	return image
def findCircle(image):
	
	hsv_img=cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
 	h_img=image[:,:,0]
	s_img=image[:,:,1]
	v_img=image[:,:,2]
	height,width=v_img.shape
	
	mask_v_a=cv2.adaptiveThreshold(v_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,int(max(height,width)/16)*2+1,1)
 	
	
	
	ratio=128/min(height,width)
 	msk=cv2.resize(mask_v_a,(int(width*ratio),int(height*ratio)) ,interpolation=cv2.INTER_CUBIC)
 	h,w=msk.shape
	msk_expand=np.zeros(( 3*h,3*w),np.uint8)
 	msk_expand[h:2*h,w:2*w]=msk
	
	
	long_edge=max(h,w)
 	r0=round(0.3*long_edge)
 	r1=round(0.7*long_edge)
 	
	circles=cv2.HoughCircles(msk_expand,cv2.HOUGH_GRADIENT,1,90,param1 = 50,param2 = 5,minRadius = r0,maxRadius = r1)
 	
	if circles is None:
		c_x=width/2
		c_y=height/2
		radius=0.55*max(height,width)
 	else:	
		
		circles = np.uint16(np.around(circles))
		c_x=(circles[0,0,0]-w)/ratio
		c_y=(circles[0,0,1]-h)/ratio
		radius=circles[0,0,2]/ratio
	
	return c_x,c_y,radius
def circleCrop(c_x,c_y,radius,height,width):
	if math.floor(radius+c_y)>height:
		y0=max(math.ceil(c_y-radius),0);
		y1=height;
		if math.floor(radius+c_x)>width:
			x1=width
		else:
			x1=math.floor(radius+c_x)
 		if math.floor(c_x-radius<0):
			x0=0
		else:
			x0=math.floor(c_x-radius)
 	elif math.ceil(c_y-radius)<0:
		y0=0
		y1=min(math.floor(c_y+radius),height)
 		if math.floor(radius+c_x)>width:
			x1=width
		else:
			x1=math.floor(radius+c_x)
 		if math.floor(c_x-radius<0):
			x0=0
		else:
			x0=math.floor(c_x-radius)
 	else:
		y0=math.ceil(c_y-radius)
 		y1=math.floor(c_y+radius)
 		x0=math.ceil(c_x-radius)
 		x1=math.floor(c_x+radius)
 	return x0,x1,y0,y1
			
def trimFundus(image):
	c_x,c_y,radius=findCircle(image)
 	height=image.shape[0]
	width=image.shape[1]
	x0,x1,y0,y1=circleCrop(c_x,c_y,radius,height,width)
 	
	trimed=image[y0:y1,x0:x1,:]
	return trimed
def load_ben_yuan(image,sigmaX=10):
	image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 	image = crop_image_from_gray(image)
 	image = cv2.resize(image,(512, 512))
	return image
PARAM = 92
def Radius_Reduction(img,PARAM):
h,w,c=img.shape
Frame=np.zeros(( h,w,c),dtype=np.uint8)
 cv2.circle(Frame,(int(math.floor(w/2)) ,int(math.floor(h/2))),int(math.floor(( h*PARAM)/float(2*100))),(255,255,255), -1)
 Frame1=cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)
 img1 =cv2.bitwise_and(img,img,mask=Frame1)
 return img1
def info_image(im):
cy = im.shape[0]//2
midline = im[cy,:]
midline = np.where(midline>midline.mean() /3)[0]
if len(midline)>im.shape[1]//2:
x_start, x_end = np.min(midline), np.max(midline)
 else:
x_start, x_end = im.shape[1]//10, 9*im.shape[1]//10
cx =(x_start + x_end)/2
r =(x_end - x_start)/2
return cx, cy, r
def resize_image(im, img_size, augmentation=False):
cx, cy, r = info_image(im)
 scaling = img_size/(2*r)
 rotation = 0
if augmentation:
scaling *= 1 + 0.3 *(np.random.rand() -0.5)
 rotation = 360 * np.random.rand()
M = cv2.getRotationMatrix2D(( cx,cy), rotation, scaling)
 M[0,2] -= cx - img_size/2
M[1,2] -= cy - img_size/2
return cv2.warpAffine(im, M,(img_size, img_size))
def subtract_median_bg_image(im):
k = np.max(im.shape)//20*2+1
bg = cv2.medianBlur(im, k)
 return cv2.addWeighted(im, 4, bg, -4, 128)
 def subtract_gaussian_bg_image(im):
bg = cv2.GaussianBlur(im ,(0,0), 10)
 return cv2.addWeighted(im, 4, bg, -4, 128)
 def open_img(fn, size):
"Open image in `fn`, subclass and overwrite for custom behavior."
image = cv2.imread(fn)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = resize_image(image, size)
 image = subtract_gaussian_bg_image(image)
 image = Radius_Reduction(image, PARAM)
 image = crop_image_from_gray(image)
 image = cv2.resize(image,(512,512))
return image
def get_preds(arr):
mask = arr == 0
return np.clip(np.where(mask.any(1), mask.argmax(1), 5)- 1, 0, 4)
 cnt_t=0
class eye_dataset(Dataset):
	
	def __init__(self, txt_path,transform=None,transform2=None):
		imgs = []
		for img in txt_path:
			imgs.append(img)
 		self.imgs = imgs
		self.transform = transform
		self.transform2 = transform2
	def __getitem__(self, index):
		fn= self.imgs[index]
		
		
		
		img=cv2.imread('/kaggle/input/aptos2019-blindness-detection/test_images/'+fn)
 		
		img_copy=img.copy()
		img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 		img_copy=img.copy()
		try:
			img=trimFundus(img)
 			img = cv2.resize(img,(512, 512))
		except Exception as e:
			
			print(e)
 			img = crop_image_from_gray(img_copy)
 			img = cv2.resize(img,(512, 512))
		
		
		img = Image.fromarray(img)
 		
		
		if self.transform is not None:
			img1 = self.transform(img)
 			img2 = self.transform2(img)
 		
		return img1,img2, fn[:-4]
	def __len__(self):
		return len(self.imgs)
 class eye_dataset_circle(Dataset):
	
	def __init__(self, txt_path,transform=None):
		imgs = []
		for img in txt_path:
			imgs.append(img)
 		self.imgs = imgs
		self.transform = transform
	def __getitem__(self, index):
		fn= self.imgs[index]
		
		
		
		
		img=open_img('/kaggle/input/aptos2019-blindness-detection/test_images/'+fn,530)
 		img = Image.fromarray(img)
 		
		
		if self.transform is not None:
			img = self.transform(img)
 		
		
		return img, fn[:-4]
	def __len__(self):
		return len(self.imgs)
 class eye_dataset_orl(Dataset):
	
	def __init__(self, txt_path,transform=None,transform2=None,transform3=None,transform4=None):
		imgs = []
		for img in txt_path:
			imgs.append(img)
 		self.imgs = imgs
		self.transform = transform
		self.transform2 = transform2
		self.transform3 = transform3
		self.transform4 = transform4
	def __getitem__(self, index):
		fn= self.imgs[index]
		
		
		
		img=cv2.imread('/kaggle/input/aptos2019-blindness-detection/test_images/'+fn)
 		img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 		img=crop_image_from_gray(img)
 		img3_=open_img('/kaggle/input/aptos2019-blindness-detection/test_images/'+fn,530)
 		img4_=open_img('/kaggle/input/aptos2019-blindness-detection/test_images/'+fn,530)
 		img3_ = Image.fromarray(img3_)
 		img4_ = Image.fromarray(img4_)
 		img=cv2.resize(img,(512, 512))
		img = Image.fromarray(img)
 		
		
		if self.transform is not None:
			img1 = self.transform(img)
 			img2 = self.transform2(img)
 			img3 = self.transform3(img3_)
 			img4 = self.transform4(img4_)
 			
		
		
		return img1,img2,img3,img4, fn[:-4]
	def __len__(self):
		return len(self.imgs )<categorify>
knn = KNeighborsClassifier()
def load_para_dict(model1):
state_dict_1=torch.load(model1)
 new_state_dict = OrderedDict()
for k, v in state_dict_1.items() :
if 'module' in k:
name = k[7:]
else:
name=k
new_state_dict[name] = v
return new_state_dict<set_options>
knn.fit(Xs_train, y_train )
%reload_ext autoreload
%autoreload 2
%matplotlib inline 
<import_modules>
knn.score(Xs_train, y_train )
from fastai import *
from fastai.vision import *
import pandas as pd
import matplotlib.pyplot as plt<set_options>
cross_val_score(knn, Xs_train, y_train, cv=5 ).mean()
print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )<define_variables>
pre=knn.predict(X_test )
PATH = Path('.. /input/aptos2019-blindness-detection' )<load_from_csv>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": pre
})
 submission.to_csv('submission2_KNN.csv', index=False )
df = pd.read_csv(PATH/'train.csv')
 df.head()<set_options>
model=RandomForestClassifier()
param={'n_estimators':[100,200,300],
'max_depth':[1,3,5,7],
'criterion':["gini"],
'max_features': [1,3,5],
"min_samples_split": [2,3,5]
}
clf=GridSearchCV(estimator=model,
param_grid=param,
scoring="accuracy",
verbose=1,
n_jobs=-1,
cv=5)
 clf.fit(X_train, y_train)
 clf.best_estimator_
clf.best_score_
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED = 999
seed_everything(SEED )<feature_engineering>
pre=clf.predict(X_test)
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 df.head(10 )<define_variables>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": pre
})
 submission.to_csv('submission2_RF_GS.csv', index=False )
len_df = len(df)
 len_df<set_options>
lr_pipe2 = Pipeline([ 
('sscaler2', StandardScaler()), 
('rf', RandomForestClassifier() 
)
 ])
 pipe_2_params = {'sscaler2__with_mean': [True, False],
'sscaler2__with_std': [True, False],
'rf__bootstrap': [True],
'rf__max_depth': [1,3,5,7],
'rf__max_features': [1, 3,5],
'rf__criterion':["gini"],
'rf__min_samples_leaf': [3, 4, 5],
'rf__min_samples_split': [2,3,5],
'rf__n_estimators': [100, 200,300]}
pipe_2_gridsearch = GridSearchCV(lr_pipe2,
pipe_2_params,
cv=5,
verbose=1 , scoring="accuracy" , n_jobs=-1 )
im = Image.open(df['path'][1])
 width, height = im.size
print(width,height)
 im.show()<define_variables>
pipe_2_gridsearch.fit(X_train, y_train);
bs = 64
sz=224<define_variables>
pipe_2_gridsearch.best_score_
data.show_batch(rows=3, figsize=(7,6))<compute_test_metric>
pre= pipe_2_gridsearch.predict(X_test )
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )<choose_model_class>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": pre
})
 submission.to_csv('submission2_RF_pip_GS.csv', index=False )
learn = cnn_learner(data, base_arch=models.resnet50, metrics = [quadratic_kappa] )<find_best_params>
lr_pipe3 = Pipeline([ 
('sscaler2', StandardScaler()), 
('dt', DecisionTreeClassifier() 
)
 ])
 pipe_3_params = {'sscaler2__with_mean': [True, False],
'sscaler2__with_std': [True, False],
'dt__max_depth': [10],
'dt__random_state':[100],
'dt__max_features': [1, 3,5],
'dt__criterion':["gini"],
'dt__min_samples_leaf': [10],
'dt__min_samples_split': [2,3,4]}
pipe_3_gridsearch = GridSearchCV(lr_pipe3,
pipe_3_params,
cv=5,
verbose=1 , scoring="accuracy" , n_jobs=-1)
learn.lr_find() 
<train_model>
pipe_3_gridsearch.fit(X_train, y_train);
learn.fit_one_cycle(5,max_lr = 1e-2 )<train_model>
pipe_3_gridsearch.best_score_
learn.fit_one_cycle(6, max_lr=slice(1e-6,1e-3))<set_options>
pre= pipe_3_gridsearch.predict(X_test )
learn.export()
learn.save('stage-2' )<find_best_params>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": pre
})
 submission.to_csv('submission2_DT_pip_GS.csv', index=False )
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<predict_on_test>
tree = DecisionTreeClassifier(criterion='gini',max_depth=10,random_state=100,min_samples_leaf=10)
 tree.fit(X_train,y_train)
 y_predicted = tree.predict(Xs_test)
valid_preds = learn.get_preds(ds_type=DatasetType.Valid )<import_modules>
tree.score(Xs_train, y_train )
import numpy as np
import pandas as pd
import os
import scipy as sp
from functools import partial
from sklearn import metrics
from collections import Counter
import json<compute_test_metric>
pre= tree.predict(X_test )
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<train_model>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": pre
})
 submission.to_csv('submission2_DT.csv', index=False )
optR = OptimizedRounder()
optR.fit(valid_preds[0],valid_preds[1] )<load_from_csv>
SVM = SVC()
SVM.fit(Xs_train, y_train)
 SVM_predictions = SVM.predict(Xs_test)
 SVM.score(Xs_train, y_train )
sample_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 sample_df.head()<define_variables>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": SVM_predictions
})
 submission.to_csv('submission2_SVM.csv', index=False )
learn.data.add_test(ImageList.from_df(sample_df,'.. /input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))<feature_engineering>
scores ={'LR_pip': 0.772,'LR': 0.770,'Knn_pip': 0.779, 'Knn': 0.669, 'RF_GS': 0.787,
'RF_pip_GS': 0.775, 'DT_pip_GS': 0.760, 'DT':0.779,
'SVM': 0.779}