Kaggle competition: Hackathon Auto_matic
learn = cnn_learner(
 data,
models.resnet50,
metrics = [accuracy,quadratic_kappa],
model_dir = Path('.. /kaggle/working'),
path = Path("."),
pretrained=True
)<train_model>
from fastai.vision import *
from fastai.metrics import error_rate
learn.fit_one_cycle(5, slice(1e-2, 1e-1))<save_model>
path=pathlib.Path(os.path.normpath('.. /input/car_data/car_data'))
learn.save('resnet50-1' )<predict_on_test>
train=path/'train'
test=path/'test'
preds, _ = learn.get_preds(ds_type=DatasetType.Test )<load_from_csv>
tfms=get_transforms()
submission = pd.read_csv(path/'sample_submission.csv')
 submission.head()<data_type_conversions>
data=ImageDataBunch.from_folder(path=path,valid='test',bs=32,ds_tfms=tfms,size=(320,320)).normalize(imagenet_stats )
preds = np.array(preds.argmax(1)).astype(int ).tolist()
preds[:5]<prepare_output>
data.show_batch(rows=3,figsize=(12,12))
submission['diagnosis'] = preds
submission.head()<set_options>
learn=cnn_learner(data,models.resnet50,pretrained=True,metrics=error_rate,model_dir='/tmp/model/' )
warnings.simplefilter(action='ignore', category=FutureWarning )<save_to_csv>
learn.fit_one_cycle(10 )
submission.to_csv('submission.csv', index = False )<set_options>
learn.save('stage-1')
%reload_ext autoreload
%autoreload 2
%matplotlib inline
%matplotlib inline 
<define_variables>
learn.lr_find()
package_path = '.. /input/efficientnet/efficientnet-pytorch/EfficientNet-PyTorch/'
sys.path.append(package_path)
<load_pretrained>
learn.fit_one_cycle(10,max_lr=slice(3e-06,4e-06))
num_classes = 1
md_ef = EfficientNet.from_name('efficientnet-b5')
 md_ef.load_state_dict(torch.load('.. /input/efficientnet-pytorch/efficientnet-b5-586e6cc6.pth'))
in_features = md_ef._fc.in_features
md_ef._fc = nn.Linear(in_features, num_classes)
 md_ef.cuda()<load_from_csv>
learn.save('stage-2' )
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>
root='.. /input/car_data/car_data/test/'
images=list()
for i in listofSubdirectory:
images.append(os.listdir(root+i))
images
bs = 64
sz = 224
tfms = get_transforms(do_flip=True,flip_vert=True )<compute_test_metric>
learn.load('stage-2' )
def qk(y_pred, y):
return torch.tensor(cohen_kappa_score(torch.round(y_pred), y, weights='quadratic'), device='cuda:0' )<load_pretrained>
learn.export('/tmp/model/export.pkl' )
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')) 
<load_pretrained>
learn=load_learner('/tmp/model/' )
learn.load('abcdef');<compute_train_metric>
columns = ['Id','Predicted']
listofprediction=list()
for i in range(len(listofSubdirectory)) :
for j in images[i]:
pt=root+listofSubdirectory[i]+'/'+j
img=open_image(pt)
 name=j.replace('.jpg','')
 _,pred_idx,output=learn.predict(img)
 listofprediction.append([name,pred_idx.item() ])
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']<predict_on_test>
array=np.array(listofprediction )
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)
 tst_pred = tst_pred.astype(int)
 return tst_pred<prepare_output>
submission=pd.DataFrame(array,columns=columns)
 submission
preds1 = run_subm()<set_options>
submission.to_csv('submission.csv',index=False )