Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<concatenate>
warnings.filterwarnings("ignore")
 DIR_INPUT = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection'
DIR_TRAIN = f'{DIR_INPUT}/train'
DIR_TEST = f'{DIR_INPUT}/test'
DIR_WEIGHTS = '.. /input/chest-x-ray-abnormalities-detection'
WEIGHTS_FILE = f'{DIR_WEIGHTS}/model_state.pth'
X = pd.concat([train_features, train_features_denoised], axis=1 ).drop(['seg_id_denoised', 'target_denoised'], axis=1)
 X_test = pd.concat([test_features, test_features_denoised], axis=1 ).drop(['seg_id_denoised', 'target_denoised'], axis=1)
 X = X[:-1]
y = y[:-1]<choose_model_class>
test_df = pd.read_csv(f'{DIR_INPUT}/sample_submission.csv')
 test_df.shape
n_fold = 10
folds = KFold(n_splits=n_fold, shuffle=True, random_state=11 )<init_hyperparams>
def get_test_transform() :
return A.Compose([
ToTensorV2()
] )
params = {'num_leaves': 128,
'min_child_samples': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"subsample_freq": 5,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'colsample_bytree': 0.1
}
result_dict_lgb = artgor_utils.train_model_regression(X=X, X_test=X_test, y=y, params=params, folds=folds, model_type='lgb',
eval_metric='mae', plot_feature_importance=True )<save_to_csv>
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(
 pretrained=False,
pretrained_backbone=False,
)
submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv', index_col='seg_id')
 submission['time_to_failure'] = result_dict_lgb['prediction']
print(submission.head())
 submission.to_csv('submission.csv' )<save_to_csv>
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 num_classes = 15
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
 model.load_state_dict(torch.load(WEIGHTS_FILE, map_location=device))
model.eval()
x = model.to(device )
sub1 = pd.read_csv('.. /input/lanl-features/submission_1.csv')
 sub1.to_csv('submission_1.csv', index=False )<normalization>
def collate_fn(batch):
return tuple(zip(*batch))
test_dataset = VinBigTestDataset(test_df, DIR_TEST, get_test_transform())
 test_data_loader = DataLoader(
 test_dataset,
batch_size=6,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)
scaler = StandardScaler()
scaler.fit(X)
 X_train_scaled = pd.DataFrame(scaler.transform(X), columns=X.columns)
 X_test_scaled = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns )<train_on_grid>
def format_prediction_string(labels, boxes, scores):
pred_strings = []
for j in zip(labels, scores, boxes):
pred_strings.append("{0} {1:.4f} {2} {3} {4} {5}".format(
 j[0], j[1], j[2][0], j[2][1], j[2][2], j[2][3]))
return " ".join(pred_strings )
%%time
n = 10
neigh = NearestNeighbors(n, n_jobs=-1)
 neigh.fit(X_train_scaled)
 dists, _ = neigh.kneighbors(X_train_scaled, n_neighbors=n)
 mean_dist = dists.mean(axis=1)
 max_dist = dists.max(axis=1)
 min_dist = dists.min(axis=1)
 X_train_scaled['mean_dist'] = mean_dist
X_train_scaled['max_dist'] = max_dist
X_train_scaled['min_dist'] = min_dist
test_dists, _ = neigh.kneighbors(X_test_scaled, n_neighbors=n)
 test_mean_dist = test_dists.mean(axis=1)
 test_max_dist = test_dists.max(axis=1)
 test_min_dist = test_dists.min(axis=1)
 X_test_scaled['mean_dist'] = test_mean_dist
X_test_scaled['max_dist'] = test_max_dist
X_test_scaled['min_dist'] = test_min_dist 
<init_hyperparams>
detection_threshold = 0.2
results = []
with torch.no_grad() :
for images, image_ids in test_data_loader:
images = list(image.to(device)for image in images)
 outputs = model(images)
 for i, image in enumerate(images):
image_id = image_ids[i]
result = {
'image_id': image_id,
'PredictionString': '14 1.0 0 0 1 1'
}
boxes = outputs[i]['boxes'].data.cpu().numpy()
labels = outputs[i]['labels'].data.cpu().numpy()
scores = outputs[i]['scores'].data.cpu().numpy()
if len(boxes)> 0:
labels = labels - 1
labels[labels == -1] = 14
selected = scores >= detection_threshold
boxes = boxes[selected].astype(np.int32)
 scores = scores[selected]
labels = labels[selected]
if len(boxes)> 0:
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(labels, boxes, scores)
 }
results.append(result)
params = {'num_leaves': 32,
'min_data_in_leaf': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"bagging_freq": 5,
"bagging_fraction": 0.8126672064208567,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'feature_fraction': 0.1
}
result_dict_lgb = artgor_utils.train_model_regression(X=X, X_test=X_test, y=y, params=params, folds=folds, model_type='lgb',
eval_metric='mae', plot_feature_importance=True )<save_to_csv>
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.head()
<define_variables><EOS> 
test_df.to_csv('submission.csv', index=False )