Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<split>
from fastai import *
from fastai.vision import *
from fastai.metrics import accuracy,error_rate
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.1, random_state = 53)
 X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 53 )<normalization>
class CustomImageItemList(ImageList):
def open(self, fn):
img = fn.reshape(28,28)
 img = np.stack(( img,)*3, axis=-1)
 return Image(pil2tensor(img, dtype=np.float32))
@classmethod
def from_csv_custom(cls, path:PathOrStr, csv_name:str, imgIdx:int=1, header:str='infer', **kwargs)->'ItemList':
df = pd.read_csv(Path(path)/csv_name, header=header)
 res = super().from_df(df, path=path, cols=0, **kwargs)
 res.items = df.iloc[:,imgIdx:].apply(lambda x: x.values / 255.0, axis=1 ).values
return res
scaler = preprocessing.StandardScaler()<normalization>
path = '.. /input'
X_train_scalled = scaler.fit_transform(X_train)
 X_val_scalled = scaler.transform(X_val)
 X_test_scalled = scaler.transform(X_test )<choose_model_class>
test = CustomImageItemList.from_csv_custom(path=path, csv_name='test.csv', imgIdx=0)
 data =(CustomImageItemList.from_csv_custom(path=path, csv_name='train.csv')
.split_by_rand_pct (.2)
.label_from_df(cols='label')
.add_test(test, label=0)
.databunch(bs=64, num_workers=0)
.normalize(imagenet_stats))
linreg = LinearRegression()<train_model>
learn = cnn_learner(data, models.resnet50, metrics=error_rate,model_dir="/tmp/model/" )
linreg.fit(X_train_scalled, y_train )<predict_on_test>
learn.fit_one_cycle(4 )
y_val_pred = linreg.predict(X_val_scalled )<predict_on_test>
learn.save("model_1", return_path=True )
y_train_pred = linreg.predict(X_train_scalled )<create_dataframe>
learn.fit_one_cycle(1 )
data = pd.DataFrame({'Actual':y_val, 'Predicted':y_val_pred})
 data<compute_test_metric>
learn.load('model_1' )
r2_score(y_val, y_val_pred )<compute_test_metric>
learn.lr_find()
r2_score(y_train, y_train_pred )<compute_test_metric>
learn.unfreeze()
learn.fit_one_cycle(10, max_lr=slice(1e-5,1e-4))
mae = metrics.mean_absolute_error(y_val, y_val_pred)
 mse = metrics.mean_squared_error(y_val, y_val_pred)
 rmse = np.sqrt(metrics.mean_absolute_error(y_val, y_val_pred))
print("Mean Absolute Error")
 print(mae)
 print()
print("Mean Squared Error")
 print(mse)
 print()
print("Root Mean Squared Error")
 print(rmse )<create_dataframe>
predictions, *_ = learn.get_preds(DatasetType.Test)
 labels = np.argmax(predictions, 1)
 submission_df = pd.DataFrame({'ImageId': list(range(1,len(labels)+1)) , 'Label': labels})
 submission_df.to_csv(f'submission.csv', index=False )