Kaggle competition: Aerial Cactus Identification
dot_data = export_graphviz(model, out_file=None, feature_names=train_x.columns, class_names=['0', '1'],
filled=True, rounded=False,special_characters=True, precision=7)
 graph = graphviz.Source(dot_data)
 graph<predict_on_test>
!pip install pretrainedmodels
DatasetType, ImageList, Path, accuracy, cnn_learner, error_rate,
get_transforms, imagenet_stats, models, Learner)
 print(os.listdir(".. /input/"))
print(dir(tmodels))
print(dir(models))
train_dir = ".. /input/train/train"
test_dir = ".. /input/test/train"
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv(".. /input/sample_submission.csv")
 data_folder = Path(".. /input")
 ')
 test_img = ImageList.from_df(test, path=data_folder/'test', folder='test')
 def predict(learn: Learner, name: str):
preds, _ = learn.get_preds(ds_type=DatasetType.Test)
 test['has_cactus'] = preds.numpy() [:, 0]
test.to_csv('submission_{}.csv'.format(name), index=False)
 print('Finish creating submission_{}.csv'.format(name))
id_ = range(len(learn.recorder.losses))
loss_df = pd.DataFrame(
 {'id': id_, 'loss': np.array(learn.recorder.losses)})
 loss_df.to_csv('loss_{}.csv'.format(name), index=False)
 print('Finish creating loss_{}.csv'.format(name))
preds, targets = learn.get_preds(ds_type=DatasetType.Train)
 preds_label = np.argmax(preds.numpy() , axis=1)
 id_ = range(len(preds))
train_pred_df = pd.DataFrame({'id': id_, 'preds': preds.numpy(
)[:, 0], 'preds_label': preds_label, 'targets': targets.numpy() })
 train_pred_df.to_csv('./train_pred_{}.csv'.format(name))
print('Finish creating train_pred_{}.csv'.format(name))
correct_count = np.equal(preds_label, targets.numpy() ).sum()
len_preds = len(preds)
 incorrect_count = len_preds - correct_count
fpr, tpr = metrics.roc_curve(preds[:, 0], targets)
 fpr, tpr = fpr.numpy() , tpr.numpy()
FP = np.floor(fpr * len_preds)
 FN = incorrect_count - FP
TP = np.floor(tpr * len_preds)
 TN = correct_count - TP
id_ = range(len(fpr))
train_index_df = pd.DataFrame(
 {'id': id_, 'fpr': fpr, 'tpr': tpr, 'TP': TP, 'TN': TN, 'FP': FP, 'FN': FN})
 train_index_df.to_csv('./train_index_{}.csv'.format(name))
print('Finish creating train_index_{}.csv'.format(name))
learn.export('./model_{}.pth'.format(name), destroy=True)
 def get_model(model_name: str, pretrained: bool, seq: bool = False, pname: str = 'imagenet', **kwargs):
pretrained = pname if pretrained else None
model = getattr(ptms, model_name )(
 pretrained=pretrained, **kwargs)
 return nn.Sequential(*model.children())if seq else model
def dpn68b(pretrained: bool = False): return get_model(
 'dpn68b', pretrained, pname='imagenet+5k', seq=True)
 def dpn92(pretrained: bool = False): return get_model(
 'dpn92', pretrained, pname='imagenet+5k', seq=True)
 def dpn107(pretrained: bool = False): return get_model(
 'dpn107', pretrained, pname='imagenet+5k', seq=True)
 def se_resnet50(pretrained: bool = False): return get_model(
 'se_resnet50', pretrained)
 def se_resnet101(pretrained: bool = False): return get_model(
 'se_resnet101', pretrained)
 def se_resnet152(pretrained: bool = False): return get_model(
 'se_resnet152', pretrained)
 model_normal_dict = {
'densenet': tmodels.densenet161,
'vgg': tmodels.vgg16_bn,
'resnet': tmodels.resnet101,
'resnext': tmodels.resnext101_32x8d,
'se_resnet': se_resnet101,
}
model_layer_dict = {
'densenet': {
'121': tmodels.densenet121,
'161': None,
'169': tmodels.densenet169
},
'vgg': {
'13_bn': tmodels.vgg13_bn,
'16_bn': None,
'19_bn': tmodels.vgg19_bn,
},
'resnet': {
'50': tmodels.resnet50,
'101': None,
'152': tmodels.resnet152,
},
'resnext': {
'101': tmodels.resnext101_32x8d,
'50': tmodels.resnext50_32x4d,
'169': None,
},
'se_resnet': {
'50': se_resnet50,
'101': None,
'152': se_resnet152,
}
}
lr_dict = {
'1e-1': 1e-1,
'1e-3': 1e-3,
}
optim_dict = {
'adam': torch.optim.Adam,
'rprop': torch.optim.Rprop,
}
one_cycle_dict = {
'not-use-one-cycle': False,
}
transform_dict = {
'dofilp': [True, True],
'nothing': [True, True],
}