Kaggle competition: Plant Seedlings Classification
training_set[columns_impute_train[7]].fillna(value="SBrkr",inplace=True )<count_values>
from fastai import *
from fastai.vision import *
import matplotlib.pyplot as plt
training_set[columns_impute_train[8]].value_counts()<filter>
path = Path('input/train/' )
training_set[training_set[columns_impute_train[8]].isnull() ]<groupby>
np.random.seed(42)
 data = ImageDataBunch.from_folder(path,test='.. /test', ds_tfms=get_transforms() ,valid_pct=0.25,size=299,bs=32,num_workers=0)
 data.normalize(imagenet_stats )
print(training_set.groupby(columns_impute_train[8])["SalePrice"].mean() )<feature_engineering>
data.show_batch(rows=3,figsize=(7,6))
training_set.loc[(training_set[columns_impute_train[8]].isnull())&(training_set["SalePrice"] < 110000.00),"GarageType"] = "CarPort"
training_set.loc[(training_set[columns_impute_train[8]].isnull())&(training_set["SalePrice"] >= 110000.00)&(training_set["SalePrice"] < 140000.00),"GarageType"] = "Detchd"
training_set.loc[(training_set[columns_impute_train[8]].isnull())&(training_set["SalePrice"] >= 140000.00)&(training_set["SalePrice"] < 210000.00),"GarageType"] = "Attchd"
training_set.loc[(training_set[columns_impute_train[8]].isnull())&(training_set["SalePrice"] >= 210000.00),"GarageType"] = "BuiltIn"<drop_column>
learn = create_cnn(data,models.resnet50,metrics=error_rate )
columns_impute_train.drop(columns_impute_train[9] )<count_values>
class FocalLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, logits=False, reduction='elementwise_mean'):
super(FocalLoss, self ).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduction = reduction
def forward(self, inputs, targets):
if self.logits:
BCE_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction='none')
 else:
BCE_loss = F.binary_cross_entropy(inputs, targets, reduction='none')
 pt = torch.exp(-BCE_loss)
 F_loss = self.alpha *(1-pt)**self.gamma * BCE_loss
if self.reduction is None:
return F_loss
else:
return torch.mean(F_loss )
training_set[columns_impute_train[10]].value_counts()<filter>
learn.loss_fn = FocalLoss()
training_set[training_set[columns_impute_train[10]].isnull() ]<groupby>
learn.fit_one_cycle(7,slice(1e-2))
print(training_set.groupby(columns_impute_train[10])["SalePrice"].mean() )<feature_engineering>
learn.save('stg-1' )
training_set.loc[(training_set[columns_impute_train[10]].isnull())&(training_set["SalePrice"] < 160000.00),"GarageFinish"] = "Unf"
training_set.loc[(training_set[columns_impute_train[10]].isnull())&(training_set["SalePrice"] >= 160000.00)&(training_set["SalePrice"] < 220000.00),"GarageFinish"] = "RFn"
training_set.loc[(training_set[columns_impute_train[10]].isnull())&(training_set["SalePrice"] >= 220000.00),"GarageFinish"] = "Fin"<count_values>
learn.unfreeze()
learn.fit_one_cycle(6,max_lr=slice(1e-4,1e-3))
training_set[columns_impute_train[11]].value_counts()<groupby>
data = ImageDataBunch.from_folder(path,test='.. /test', ds_tfms=get_transforms() ,valid_pct=0.25,size=350,bs=32,num_workers=0)
 data.normalize(imagenet_stats )
print(training_set.groupby(columns_impute_train[11])["SalePrice"].mean() )<feature_engineering>
interp = ClassificationInterpretation.from_learner(learn)
 print(metrics.classification_report(interp.y_true.numpy() , interp.pred_class.numpy() ,target_names =data.classes))
training_set.loc[(training_set[columns_impute_train[11]].isnull())&(training_set["SalePrice"] < 100000.00),"GarageQual"] = "Po"
training_set.loc[(training_set[columns_impute_train[11]].isnull())&(training_set["SalePrice"] >= 100000.00)&(training_set["SalePrice"] < 140000.00),"GarageQual"] = "Fa"
training_set.loc[(training_set[columns_impute_train[11]].isnull())&(training_set["SalePrice"] >= 140000.00)&(training_set["SalePrice"] < 220000.00),"GarageQual"] = "Ta"
training_set.loc[(training_set[columns_impute_train[11]].isnull())&(training_set["SalePrice"] >= 220000.00),"GarageQual"] = "Ex"<count_values>
learn.save('stg-2' )
training_set[columns_impute_train[12]].value_counts()<groupby>
learn.data=data
print(training_set.groupby(columns_impute_train[12])["SalePrice"].mean() )<data_type_conversions>
learn.unfreeze()
learn.fit_one_cycle(3,max_lr=slice(1e-5,1e-4))
training_set[columns_impute_train[12]].fillna(value="TA",inplace=True )<drop_column>
preds,y=learn.get_preds(ds_type=DatasetType.Test )
training_set.drop(columns=columns_too_many_missing,inplace=True )<drop_column>
preds = np.argmax(preds, axis = 1)
 preds_classes = [data.classes[i] for i in preds]
testing_set.drop(columns=columns_too_many_missing,inplace=True )<define_variables>
submission = pd.DataFrame({ 'file': os.listdir('input/test'), 'species': preds_classes })
 submission.to_csv('test_classification_results.csv', index=False )
<categorify><EOS> 
def change_fc_data(learn, train_index, val_index):
tmpl = f'_{learn.models.name}_{learn.data_.sz}.bc'
names = [os.path.join(learn.tmp_path, p+tmpl)for p in('x_act', 'x_act_val', 'x_act_test')]
learn.get_activations()
act, val_act, test_act = [bcolz.open(p)for p in names]
data_x = np.vstack([val_act, act])
 data_y = np.array(list(learn.data.val_y)+ list(learn.data.trn_y))
train_x = data_x[train_index]
valid_x = data_x[val_index]
train_y = data_y[train_index]
valid_y = data_y[val_index]
learn.fc_data = ImageClassifierData.from_arrays(learn.data_.path, 
(train_x, train_y), 
(valid_x, valid_y), learn.data_.bs, classes=learn.data_.classes,
test = test_act if learn.data_.test_dl else None, num_workers=8)
 return learn