Kaggle competition: House Prices - Advanced Regression Techniques
class EfficientNetB5(nn.Module):
def __init__(self, model_name='tf_efficientnet_b5_ns'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.classifier.in_features
self.model.global_pool = nn.Identity()
self.model.classifier = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.classifier = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.classifier(pooled_features)
 return output<categorify>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() + y_preds2.sigmoid().to('cpu' ).numpy())/ 2
avg_preds.append(y_preds)
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs<choose_model_class>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
model = EfficientNetB5()
model.load_state_dict(torch.load(MODEL_PATH)['model'])
 model.eval()
models = [model.to(device)]<load_pretrained>
all_X = pd.concat(( train.loc[:, 'MSSubClass':'SaleCondition'],
test.loc[:, 'MSSubClass':'SaleCondition']))
test_dataset = TestDataset(test, transform=get_transforms())
 test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False,
num_workers=4 , pin_memory=True)
 predictions = inference(models, test_loader, device )<save_to_csv>
numnic_feats = all_X.dtypes[all_X.dtypes!='object'].index
all_X[numnic_feats] = all_X[numnic_feats].apply(lambda x:(x-x.mean())/x.std())
target_cols = test.iloc[:, 1:12].columns.tolist()
test[target_cols] = predictions
test[['StudyInstanceUID'] + target_cols].to_csv('submission.csv', index=False)
 test.head()<define_variables>
all_X = pd.get_dummies(all_X, dummy_na=True )
effnet_path = '.. /input/efficientnet-pytorch/'
iterstrat_path = '.. /input/iterative-stratification/iterative-stratification-master'
sys.path.append(effnet_path)
 sys.path.append(iterstrat_path )<import_modules>
all_X.isnull().sum()
warnings.filterwarnings("ignore")
<set_options>
all_X = all_X.fillna(all_X.mean() )
IMAGE_SIZE =(512, 512)
 PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True
IMAGE_BACKEND = 'cv2'
FOLD = 0
def set_seed(seed=0):
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.deterministic = True
set_seed()<define_variables>
num_train = train.shape[0]
X_train = all_X[:num_train].as_matrix()
X_test = all_X[num_train:].as_matrix()
y_train = train.SalePrice.as_matrix()
data_dir = ".. /input/ranzcr-clip-catheter-line-classification/"
path_checkpoints_dir = "./checkpoints"
path_submissions_dir = "./"
path_trained_models = ".. /input/000-010-sgdr-ensemble-4"
path_test_dir= os.path.join(data_dir, 'test')
 path_sample_submission_file= os.path.join(data_dir, 'sample_submission.csv')
 submission_file = pd.read_csv(path_sample_submission_file)
 path_test_images = [os.path.join(path_test_dir, i + ".jpg")for i in submission_file.StudyInstanceUID.values]<train_model>
X_train = nd.array(X_train)
 y_train = nd.array(y_train)
 y_train.reshape(( num_train, 1))
X_test = nd.array(X_test )
def resize_one_image(input_path, output_path, image_size):
image = cv2.imread(input_path)
 image = cv2.resize(image, image_size)
 cv2.imwrite(output_path, image)
 def resize_image_batch(input_dir, output_dir, image_size):

 if not os.path.isdir(output_dir):
os.mkdir(output_dir)
 input_paths = [os.path.join(input_dir, image_name)for image_name in os.listdir(input_dir)]
output_paths = [os.path.join(output_dir, image_name)for image_name in os.listdir(input_dir)]
image_sizes = [image_size]*len(input_paths)
 _ = Parallel(n_jobs=-1, verbose=3 )(delayed(resize_one_image )(ipath, opath, img_size)for ipath, opath, img_size in zip(input_paths, output_paths, image_sizes))<define_variables>
square_loss = gluon.loss.L2Loss()
def get_rmse_log(net, X_train,y_train):
num_train = X_train.shape[0]
clipped_preds = nd.clip(net(X_train), 1, float('inf'))
return nd.sqrt(2*nd.sum(square_loss(nd.log(clipped_preds),nd.log(y_train)) /num_train)).asscalar()
path_resized_test_image_dir = os.path.join('./', "test_resized")
 print(path_resized_test_image_dir )<load_from_csv>
def get_net() :
net = gluon.nn.Sequential()
with net.name_scope() :
net.add(gluon.nn.Dense(128, activation='relu'))
net.add(gluon.nn.Dropout(0.01))
net.add(gluon.nn.Dense(1))
net.initialize()
return net
submission_file = pd.read_csv(path_sample_submission_file)
 path_test_images = [os.path.join(path_resized_test_image_dir, i + ".jpg")for i in submission_file.StudyInstanceUID.values]<train_model>
%matplotlib inline
mpl.rcParams['figure.dpi']= 120
def train(net, X_train, y_train, X_test, y_test, epochs,
verbose_epoch, learning_rate, weight_decay):
train_loss = []
if X_test is not None:
test_loss = []
batch_size = 100
dataset_train = gluon.data.ArrayDataset(X_train, y_train)
 data_iter_train = gluon.data.DataLoader(
 dataset_train, batch_size,shuffle=True)
 trainer = gluon.Trainer(net.collect_params() , 'adam',
{'learning_rate': learning_rate,
'wd': weight_decay})
 net.collect_params().initialize(force_reinit=True)
 for epoch in range(epochs):
for data, label in data_iter_train:
with autograd.record() :
output = net(data)
 loss = square_loss(output, label)
 loss.backward()
trainer.step(batch_size)
 cur_train_loss = get_rmse_log(net, X_train, y_train)
 if epoch > verbose_epoch:
print("Epoch %d, train loss: %f" %(epoch, cur_train_loss))
train_loss.append(cur_train_loss)
 if X_test is not None:
cur_test_loss = get_rmse_log(net, X_test, y_test)
 test_loss.append(cur_test_loss)
 plt.plot(train_loss)
 plt.legend(['train'])
 if X_test is not None:
plt.plot(test_loss)
 plt.legend(['train','test'])
 plt.show()
if X_test is not None:
return cur_train_loss, cur_test_loss
else:
return cur_train_loss
path_test_dir = ".. /input/ranzcr-clip-catheter-line-classification/test"
resize_image_batch(path_test_dir, path_resized_test_image_dir, IMAGE_SIZE )<import_modules>
def k_fold_cross_valid(k, epochs, verbose_epoch, X_train, y_train,
learning_rate, weight_decay):
assert k > 1
fold_size = X_train.shape[0] // k
train_loss_sum = 0.0
test_loss_sum = 0.0
for test_i in range(k):
X_val_test = X_train[test_i * fold_size:(test_i + 1)* fold_size, :]
y_val_test = y_train[test_i * fold_size:(test_i + 1)* fold_size]
val_train_defined = False
for i in range(k):
if i != test_i:
X_cur_fold = X_train[i * fold_size:(i + 1)* fold_size, :]
y_cur_fold = y_train[i * fold_size:(i + 1)* fold_size]
if not val_train_defined:
X_val_train = X_cur_fold
y_val_train = y_cur_fold
val_train_defined = True
else:
X_val_train = nd.concat(X_val_train, X_cur_fold, dim=0)
 y_val_train = nd.concat(y_val_train, y_cur_fold, dim=0)
 net = get_net()
train_loss, test_loss = train(
 net, X_val_train, y_val_train, X_val_test, y_val_test,epochs, verbose_epoch, learning_rate, weight_decay)
 train_loss_sum += train_loss
print("Test loss: %f" % test_loss)
 test_loss_sum += test_loss
return train_loss_sum / k, test_loss_sum / k
class ImageDataset:
def __init__(
 self,
image_paths,
targets=None,
augmentations=None,
backend="cv2",
channel_first=True,
grayscale=False,
grayscale_as_rgb=False, 
):

 if grayscale is False and grayscale_as_rgb is True:
raise Exception("Invalid combination of " "arguments 'grayscale=False' and 'grayscale_as_rgb=True'")
 self.image_paths = image_paths
self.targets = targets
self.augmentations = augmentations
self.backend = backend
self.channel_first = channel_first
self.grayscale = grayscale
self.grayscale_as_rgb = grayscale_as_rgb
def __len__(self):
return len(self.image_paths)
 def __getitem__(self, item):
if self.backend == "pil":
image = Image.open(self.image_paths[item])
 if self.grayscale is True and self.grayscale_as_rgb is True:
image = image.convert('RGB')
 image = np.array(image)
 if self.augmentations is not None:
augmented = self.augmentations(image=image)
 image = augmented["image"]
elif self.backend == "cv2":
if self.grayscale is False or self.grayscale_as_rgb is True:
image = cv2.imread(self.image_paths[item])
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 else:
image = cv2.imread(self.image_paths[item], cv2.IMREAD_GRAYSCALE)
 if self.augmentations is not None:
augmented = self.augmentations(image=image)
 image = augmented["image"]
else:
raise Exception("Backend not implemented")
 if not isinstance(image, torch.Tensor):
if self.channel_first is True and image.ndim == 3:
image = np.transpose(image,(2, 0, 1)).astype(np.float32)
 image = torch.tensor(image)
 if len(image.size())== 2:
image = image.unsqueeze(0)
 if self.targets is not None:
targets = self.targets[item]
targets = torch.tensor(targets)
 else:
targets = torch.tensor([])
 return {
"image": image,
"targets": targets,
}<create_dataframe>
k = 5
epochs = 40
verbose_epoch = 35
learning_rate = 0.1
weight_decay = 130
train_loss, test_loss = k_fold_cross_valid(k, epochs, verbose_epoch, X_train,
y_train, learning_rate, weight_decay)
 print("%d-fold validation: Avg train loss: %f, Avg test loss: %f" % 
(k, train_loss, test_loss))
test_dataset = ImageDataset(
 path_test_images,
None,
augmentations=test_augmentation,
backend=IMAGE_BACKEND,
channel_first=True,
grayscale=True,
grayscale_as_rgb=True,
)<load_pretrained>
def learn(epochs, verbose_epoch, X_train, y_train, test, learning_rate,
weight_decay):
net = get_net()
train(net, X_train, y_train, None, None, epochs, verbose_epoch,
learning_rate, weight_decay)
 preds = net(X_test ).asnumpy()
test['SalePrice'] = pd.Series(preds.reshape(1, -1)[0])
 submission = pd.concat([test['Id'], test['SalePrice']], axis=1)
 submission.to_csv('submission.csv', index=False )
<choose_model_class><EOS> 
learn(epochs, verbose_epoch, X_train, y_train, test, learning_rate,
weight_decay )