Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
clf.score(valid_X, valid_Y)
 acc = round(clf.score(valid_X, valid_Y)* 100, 2)
 print("RandomForest accuracy(validation set):", acc )<compute_test_metric>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
print(metrics.classification_report(valid_Y, preds, target_names=['Not Survived', 'Survived']))<define_variables>
from fastai.vision import *
from fastai.metrics import error_rate
predictors = ['Sex', 'Age', 'Pclass', 'Fare', 'Parch', 'SibSp']
target = 'Survived'<prepare_x_and_y>
DATA_PATH = ".. /input/dogs-vs-cats-redux-kernels-edition"
TRAIN_PATH = DATA_PATH + "/train"
TEST_PATH = DATA_PATH + "/test"
path = untar_data("",DATA_PATH,DATA_PATH); path; path.ls()
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values<train_model>
fnames = get_image_files(TRAIN_PATH)
 pat = r'([^/]+)\.\d+\.jpg$'
fnames[:5]
clf.fit(train_X, train_Y )<predict_on_test>
bs=64
data = ImageDataBunch.from_name_re(TRAIN_PATH, fnames, pat, ds_tfms=get_transforms() , size=499, bs=bs
).normalize(imagenet_stats)
 data.show_batch(rows=3, figsize=(7,6))
preds = clf.predict(valid_X )<compute_test_metric>
learn = cnn_learner(data, models.resnet18, metrics=error_rate, pretrained=True )
clf.score(train_X, train_Y)
 acc = round(clf.score(train_X, train_Y)* 100, 2)
 print("RandomForest accuracy(train set):", acc )<compute_test_metric>
learn.model_dir = "/tmp"
os.access('/tmp', os.W_OK )
clf.score(valid_X, valid_Y)
 acc = round(clf.score(valid_X, valid_Y)* 100, 2)
 print("RandomForest accuracy(validation set):", acc )<compute_test_metric>
learn.fit_one_cycle(3)
 learn.save('stage-1' )
print(metrics.classification_report(valid_Y, preds, target_names=['Not Survived', 'Survived']))<define_variables>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )
predictors = ['Sex', 'Age', 'Pclass', 'Fare', 'Parch', 'SibSp', 'FamilySize', 'Title']
target = 'Survived'<prepare_x_and_y>
learn.unfreeze()
learn.fit_one_cycle(1)
 learn.load('stage-1')
 learn.lr_find()
learn.recorder.plot()
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values<train_model>
learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-5))
clf.fit(train_X, train_Y )<predict_on_test>
learn.save('stage-tunned-34' )
preds = clf.predict(valid_X )<compute_test_metric>
test_fnames = get_image_files(TEST_PATH)
 test_fnames[:5]
clf.score(train_X, train_Y)
 acc = round(clf.score(train_X, train_Y)* 100, 2)
 print("RandomForest accuracy(train set):", acc )<compute_test_metric>
img = open_image(test_fnames[3])
 learn.predict(img )
clf.score(valid_X, valid_Y)
 acc = round(clf.score(valid_X, valid_Y)* 100, 2)
 print("RandomForest accuracy(validation set):", acc )<compute_test_metric>
learn.predict(img)[2][1].item()
print(metrics.classification_report(valid_Y, preds, target_names=['Not Survived', 'Survived']))<define_variables>
submission = {"id":[],"label":[]}
idpat = r''
for f in test_fnames:
img = open_image(f)
 fname = f.name
id = fname[:fname.rfind(".")]
submission["id"].append(id)
 submission["label"].append(learn.predict(img)[2][1].item() )
predictors = ['FamilySize', 'Title', 'Class*Age']
target = 'Survived'<prepare_x_and_y>
result = pd.DataFrame(submission)
 result.to_csv('submission.csv', index=False )