Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<choose_model_class>
device = torch.device("cuda:0")
 ImageFile.LOAD_TRUNCATED_IMAGES = True
class InferenceDataset(tdata.Dataset):
def __init__(self, attention_model, img_path, df, level, tile_size, num_tiles, scale, transform=None):
self.img_path = img_path
self.tile_maker = TileMaker(tile_size, attention_tiles_num, scale)
 self.imgs_names = list(df.image_id)
 self.df = df
self.level = level
self.num_tiles = num_tiles
self.transform = transform
self.attention_model = attention_model
self.attention_memory = {}
def __len__(self):
return len(self.attention_dataset.imgs_names)
 def __getitem__(self, idx):
name = self.imgs_names[idx]
img = skimage.io.MultiImage(os.path.join(self.img_path, name + '.tiff')) [-self.level]
best_tiles = self.get_best_tiles(img, name, transform_id=0)
 images = self.make_square(best_tiles)
 best_tiles = self.get_best_tiles(img[128:-128], name, transform_id=1)
 images2 = self.make_square(best_tiles)
 best_tiles = self.get_best_tiles(img[:, 128:-128], name, transform_id=2)
 images3 = self.make_square(best_tiles)
 best_tiles = self.get_best_tiles(img[128:-128, 128:-128], name, transform_id=3)
 images4 = self.make_square(best_tiles)
 return images, images2, images3, images4, name
def __len__(self):
return len(self.imgs_names)
 def make_square(self, tiles):
idxes = list(range(self.num_tiles))
image_size = tiles[0].shape[0]
n_rows = int(self.num_tiles**0.5)
 images = np.zeros(( image_size * n_rows, image_size * n_rows, 3))
for h in range(n_rows):
for w in range(n_rows):
i = h * n_rows + w
this_img = tiles[idxes[i]]
this_img = 255 - this_img
if self.transform is not None:
this_img = self.transform(image=this_img)['image']
h1 = h * image_size
w1 = w * image_size
images[h1:h1 + image_size, w1:w1 + image_size] = this_img
if self.transform is not None:
images = self.transform(image=images)['image']
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 images = torch.tensor(images)
 return images
def get_best_tiles(self, img, name, transform_id):
tiles = self.tile_maker(img)
 tiles_bag = self.make_bag(tiles)
 memory_name = name + '_' + str(transform_id)
 if memory_name in self.attention_memory:
weights = self.attention_memory[memory_name]
else:
weights = 0
for attention_model in self.attention_model:
with torch.no_grad() :
features = attention_model.feature_extractor(tiles_bag.to(device))
features = features.view(-1, attention_tiles_num, features.shape[-1])
 weights += attention_model.head.attention_pool.compute_weights(features)
 weights /= len(self.attention_model)
 weights = weights.reshape(-1 ).argsort(0 ).cpu().numpy() [::-1]
self.attention_memory[memory_name] = weights.astype(np.int32)
 best_tiles = [tiles[w] for w in weights[:self.num_tiles]]
return best_tiles
def make_bag(self, tiles):
images = []
for tile in tiles:
tile = 255 - tile
if self.transform is not None:
tile = self.transform(image=tile)['image']
tile = tile.astype(np.float32)
 images.append(tile)
 images = np.stack(images, axis=0)
 images = images.astype(np.float32)
 images = images.transpose(0, 3, 1, 2)
 images /= 255
images = torch.tensor(images)
 return images<create_dataframe>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(2020 )
dfs_preds = []
previous_memory = None
for model, hparams in zip(models, hparams_list):
dataset = InferenceDataset(attention_models, image_folder, df, hparams.level, hparams.tile_size, hparams.n_tiles, hparams.scale)
 if previous_memory is not None:
dataset.attention_memory = previous_memory
ids = []
predictions = []
with torch.no_grad() :
for idx in range(len(dataset)) :
imgs, imgs2, imgs3, imgs4, name = dataset[idx]
imgs, imgs2, imgs3, imgs4 = imgs.unsqueeze(0), imgs2.unsqueeze(0), imgs3.unsqueeze(0), imgs4.unsqueeze(0)
 logits = model(imgs.to(device))
logits = logits + model(imgs2.to(device))
logits = logits + model(imgs3.to(device))
logits = logits + model(imgs4.to(device))
logits = logits/4
if hparams.loss == 'bce':
pred = logits.sigmoid().sum(1 ).round()
elif hparams.loss == 'mse':
pred =(logits.sigmoid().squeeze(1)* 5 ).round()
pred = pred.detach().cpu().numpy()
ids.append(name)
 predictions.append(pred)
 ids = np.array(ids)
 predictions = np.concatenate(predictions, axis=0)
 pred_df = pd.DataFrame({'id': ids, 'preds': predictions})
 dfs_preds.append(pred_df)
 previous_memory = dataset.attention_memory.copy()<concatenate>
learning_rate=1e-5
epochs=15
num_workers=2
batch_size=16
IMG_SIZE = 256
for i in range(len(dfs_preds)) :
PREDS.append(dfs_preds[i]['preds'].values )<import_modules>
train2015df = pd.read_csv('.. /input/retinopathy-train-2015/rescaled_train_896/trainLabels.csv')
 train2015df["id_code"]=train2015df["image"]
train2015df["diagnosis"]=train2015df["level"]
train2015df=train2015df.drop(columns=["image","level"])
 traindf = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 traindf2 = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 _,cvdf=train_test_split(traindf2, test_size=0.25, random_state=2019, stratify=traindf2.diagnosis)
 testdf = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 traindf=traindf.append(train2015df )
from scipy.stats import mode<save_to_csv>
train = '.. /input/aptos2019-blindness-detection/train_images/'
test = '.. /input/aptos2019-blindness-detection/test_images/'
train_2015 = '.. /input/retinopathy-train-2015/rescaled_train_896/rescaled_train_896/'
def expand_path(p):
p = str(p)
 if isfile(train + p + ".png"):
return train +(p + ".png")
 if isfile(train_2015 + p + '.png'):
return train_2015 +(p + ".png")
 if isfile(test + p + ".png"):
return test +(p + ".png")
 return p
def p_show(imgs, label_name=None, per_row=3):
n = len(imgs)
 rows =(n + per_row - 1)//per_row
cols = min(per_row, n)
 fig, axes = plt.subplots(rows,cols, figsize=(15,15))
for ax in axes.flatten() : ax.axis('off')
 for i,(p, ax)in enumerate(zip(imgs, axes.flatten())) :
img = Image.open(expand_path(p))
ax.imshow(img)
 ax.set_title(train_df[train_df.id_code == p].diagnosis.values )
arr = np.stack(PREDS, axis=1)
 majority_vote = arr.mean(axis=1 ).round().astype(int)
 sub_df = pd.DataFrame({'image_id': ids, 'isup_grade': majority_vote})
 sub_df.to_csv('submission.csv', index=False )<define_variables>
class RetinopathyDatasetTest(Dataset):
def __init__(self, data, transform, dtype='train',mode='train'):
self.data = data
self.transform = transform
self.dtype=dtype
self.mode=mode
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
img_name=expand_path(self.data.loc[idx, 'id_code'])
 image = cv2.imread(img_name)
 image = load_ben_color(image, sigmaX=10)
 image = Image.fromarray(image)
 image = self.transform(image)
 label=self.data['diagnosis'][idx]
if(self.mode=='test'):
return {'image': image}
elif(self.mode=='train'):
return image,label
else :
return {'image': image},label
MODEL_VERSION = 'v10' 
<define_variables>
train_df=traindf
train_df=train_df.reset_index(drop=True)
 cvdf=cvdf.reset_index(drop=True)
 sample_df=pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 transform = transforms.Compose([
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(( -120, 120)) ,
transforms.ToTensor() ,
transforms.Normalize([0.55387044 ,0.5336158 , 0.5232084 ], [0.2063595 , 0.18827829, 0.15147245])
 ])
 test_transform = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize([0.55387044 ,0.5336158 , 0.5232084 ], [0.2063595 , 0.18827829, 0.15147245])
 ])
 train_dataset = RetinopathyDatasetTest(data=train_df,
transform=transform,dtype='train',mode='train')
 test_dataset = RetinopathyDatasetTest(sample_df,
transform=test_transform,dtype='test',mode='test')
 cv_dataset= RetinopathyDatasetTest(cvdf,
transform=transform,dtype='train',mode='train')
 tr, val = train_test_split(train_df.diagnosis, stratify=train_df.diagnosis, test_size=0.25)
 train_sampler = SubsetRandomSampler(list(tr.index))
valid_sampler = SubsetRandomSampler(list(val.index))
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,sampler=valid_sampler, num_workers=num_workers)
 cv_loader=torch.utils.data.DataLoader(cv_dataset, batch_size=batch_size, num_workers=num_workers )
TEST_RUN = False
pd.set_option('display.max_columns', 50)
 KAGGLE_DATA_FOLDER = '/kaggle/input/m5-forecasting-accuracy'
BACKWARD_LAG = 60
END_DAY = 1913
BEGIN_DATE = '2013-08-01'
BEGIN_DAY = str(( datetime.strptime(BEGIN_DATE, '%Y-%m-%d')- datetime.strptime('2011-01-29', '%Y-%m-%d')).days)
 TRAIN_SPLIT = '2016-03-27'
EVAL_SPLIT = '2016-04-24'
print(datetime.strptime(EVAL_SPLIT, '%Y-%m-%d'))
TASK_TYPE='CPU'<define_variables>
model = torch.load(".. /input/pytorch-resnext/complete_modelv50.pth" )
CALENDAR_DTYPES = {
'date': 'str',
'wm_yr_wk': 'int16',
'weekday': 'category',
'wday': 'int16',
'month': 'int16',
'year': 'int16',
'd': 'object',
'event_name_1': 'category',
'event_type_1': 'category',
'event_name_2': 'category',
'event_type_2': 'category',
'snap_CA': 'int16',
'snap_TX': 'int16',
'snap_WI': 'int16'
}
PARSE_DATES = ['date']
SALES_PRICES_DTYPES = {
'store_id': 'category',
'item_id': 'category',
'wm_yr_wk': 'int16',
'sell_price': 'float32'
}<load_pretrained>
def finetune(model,learningRate,n_epochs=epochs):
criterion=nn.MSELoss()
optimizer=optim.Adam(model.parameters() ,lr=learningRate,weight_decay=1e-5)
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
 save_file_name='resnetx101.pt'
max_epochs_stop=5
print_every=1
for param in model.parameters() :
param.requires_grad = True
optimizer=optim.Adam(model.parameters() ,lr=learningRate,weight_decay=1e-5)
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
 epochs_no_improve = 0
valid_loss_min = np.Inf
valid_max_acc = 0
history = []
try:
print(f'Model has been trained for: {model.epochs} epochs.
 ')
 except:
model.epochs = 0
print(f'Starting Training from Scratch.
 ')
 overall_start = timer()
for epoch in(range(n_epochs)) :
torch.cuda.empty_cache()
train_loss = 0.0
valid_loss = 0.0
train_mse = 0
valid_mse = 0
model.train()
start = timer()
for ii,(data, target)in enumerate(( train_loader)) :
data, target =data.cuda() , target.cuda().float()
target=target.unsqueeze(1)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item()
print(
 f'Epoch: {epoch}\t{100 *(ii + 1)/ len(train_loader):.2f}% complete.{timer() - start:.2f} seconds elapsed in epoch.',
end='\r')
 else:
model.epochs += 1
with torch.no_grad() :
model.eval()
for data, target in cv_loader:
data, target = data.cuda() , target.cuda().float()
target=target.unsqueeze(1)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item()
train_loss = train_loss / len(train_loader)
 valid_loss = valid_loss / len(cv_loader)
 history.append([train_loss, valid_loss,scheduler.get_lr() [0]])
 if(epoch + 1)% print_every == 0:
print(
 f'
Epoch: {epoch} \tTraining Loss: {train_loss:.4f} \tValidation Loss: {valid_loss:.4f}' 
)
 print('lr:', scheduler.get_lr() [0])
 if valid_loss < valid_loss_min:
torch.save(model.state_dict() , save_file_name)
 epochs_no_improve = 0
valid_loss_min = valid_loss
best_epoch = epoch
else:
epochs_no_improve += 1
if epochs_no_improve >= max_epochs_stop:
print(f'
Early Stopping! Total epochs: {epoch}.Best epoch: {best_epoch} with loss: {valid_loss_min:.2f}')
 total_time = timer() - overall_start
print(f'{total_time:.2f} total seconds elapsed.{total_time /(epoch+1):.2f} seconds per epoch.')
 model.load_state_dict(torch.load(save_file_name))
model.optimizer = optimizer
history = pd.DataFrame(history,columns=['train_loss', 'valid_loss','Learning_rate'])
 return model,history
scheduler.step()
model.optimizer = optimizer
total_time = timer() - overall_start
print(f'
Best epoch: {best_epoch} with loss: {valid_loss_min:.2f} ')
 print(f'{total_time:.2f} total seconds elapsed.{total_time /(epoch+1):.2f} seconds per epoch.')
 history = pd.DataFrame(history,columns=['train_loss', 'valid_loss','Learning_rate'])
 return model,history
def load_data(train=True):

 print("Loading train and validation data")
 numcols = [f"d_{day}" for day in range(int(BEGIN_DAY),END_DAY+1)]
catcols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numcol:"float32" for numcol in numcols}
dtype.update({col: "category" for col in catcols if col != "id"})
 sales_train_validation = pd.read_csv(os.path.join(KAGGLE_DATA_FOLDER, 'sales_train_validation.csv'),
usecols=catcols+numcols, dtype=dtype)
 for col in catcols:
if col != "id":
sales_train_validation[col] = sales_train_validation[col].cat.codes.astype("int16")
 sales_train_validation[col] -= sales_train_validation[col].min()
if not train:
for day in range(END_DAY+1, END_DAY+28+1):
sales_train_validation[f"d_{day}"] = 0
value_vars = [column for column in sales_train_validation.columns
if(column.startswith('d_')and int(column.replace('d_', '')) >= END_DAY - BACKWARD_LAG)]
else:
value_vars = [col for col in sales_train_validation.columns
if(col.startswith('d_')and(int(col.replace('d_', '')) >= int(BEGIN_DAY)))]
print("Shape:", sales_train_validation.shape)
 print("Memory usage(Mb)before melting:", sales_train_validation.memory_usage().sum() / 1024**2)
 sales_train_validation = pd.melt(
 sales_train_validation,
id_vars = ['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'],
value_vars=value_vars,
var_name = 'day',
value_name = 'sales')
 print("Completed melting, new shape:", sales_train_validation.shape)
 print("Colums after melting:", sales_train_validation.columns)
 print("Memory usage(Mb)after melting:", sales_train_validation.memory_usage().sum() / 1024**2)
 columns = ['item_id', 'dept_id', 'cat_id', 'store_id', 'state_id']
print("Loading calendar")
 calendar = pd.read_csv(os.path.join(KAGGLE_DATA_FOLDER, 'calendar.csv'), dtype=CALENDAR_DTYPES, parse_dates=['date'])
 print("Calendar columns: ", calendar.columns)
 print("Memory usage(Mb)calendar: ", calendar.memory_usage().sum() / 1024**2)
 calendar.rename(columns={'d':'day'}, inplace=True)
 for col, col_dtype in CALENDAR_DTYPES.items() :
if col_dtype == "category":
calendar[col] = calendar[col].cat.codes.astype("int16")
 calendar[col] -= calendar[col].min()
sales_train_validation = sales_train_validation.merge(calendar, on="day", copy=False)
 del calendar; gc.collect()
print("Merged calendar(in place)")
 print("Memory usage(Mb)after merging calendar:", sales_train_validation.memory_usage().sum() / 1024**2)
 print("Colums after merge:", sales_train_validation.columns)
 sell_prices = pd.read_csv(os.path.join(KAGGLE_DATA_FOLDER, 'sell_prices.csv'), dtype=SALES_PRICES_DTYPES)
 for col, col_dtype in SALES_PRICES_DTYPES.items() :
if col_dtype == "category":
sell_prices[col] = sell_prices[col].cat.codes.astype("int16")
 sell_prices[col] -= sell_prices[col].min()
print("Memory usage(Mb)sell prices:", sell_prices.memory_usage().sum() / 1024**2)
 columns = ['item_id', 'store_id', 'sell_price']
for feature in columns:
if feature == 'sell_price':
sell_prices[feature].fillna(0, inplace=True)
 sales_train_validation = sales_train_validation.merge(sell_prices, on=["store_id","item_id","wm_yr_wk"], copy=False)
 del sell_prices; gc.collect()
print("Merged sales prices(in place)")
 print("Memory usage(Mb)after merging sales:", sales_train_validation.memory_usage().sum() / 1024**2)
 print("Colums after merge:", sales_train_validation.columns)
 return sales_train_validation<categorify>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
def make_lag_features(strain):

 print('in dataframe:', strain.shape)
 print("headers:", strain.columns)
 lags = [7, 28]
lag_cols = ['lag_{}'.format(lag)for lag in lags ]
for lag, lag_col in zip(lags, lag_cols):
strain[lag_col] = strain[['id', 'sales']].groupby('id')['sales'].shift(lag)
 print('lag sales done')
 windows= [7, 28]
for window in windows:
for lag, lag_col in zip(lags, lag_cols):
window_col = f'rmean_{lag_col}_{window}'
strain[window_col] = strain[['id', lag_col]].groupby('id')[lag_col].transform(
 lambda x: x.rolling(window ).mean() 
)
 print(f'Rolling mean sales for done for window {window}')
 def make_date_features(dt):
date_features = {
'week': 'weekofyear',
'quarter': 'quarter',
'mday': 'day',
"wday": "weekday"
}
for date_feat_name, date_feat_func in date_features.items() :
if not date_feat_name in dt.columns:
dt[date_feat_name] = getattr(dt['date'].dt, date_feat_func ).astype('int16')
 print('date features done')
 dt['day'] = dt['day'].apply(lambda x: int(x.replace('d_', '')))
 print('out dataframe:', dt.shape )<load_pretrained>
def Run_validation() :
torch.cuda.empty_cache()
valid_preds = []
labels=[]
tk0 = tqdm(cv_loader)
 for ii,(x_batch,label)in enumerate(tk0):
with torch.no_grad() :
pred = model(x_batch.to(device))
valid_preds.append(pred.detach().cpu().squeeze().numpy().ravel().reshape(-1, 1))
labels.append(label)
 predict_valid=valid_preds[0]
for i in range(1,len(cv_loader)) :
predict_valid=np.append(predict_valid,valid_preds[i])
 label_valid=labels[0].numpy()
for i in range(1,len(cv_loader)) :
label_valid=np.append(label_valid,labels[i].numpy())
 return predict_valid,label_valid
%%time
sales_train_validation = load_data()<data_type_conversions>
predict_valid,label_valid=Run_validation()
sales_train_validation["sale"] =(( sales_train_validation['sell_price'] * 100 % 10)< 6 ).astype('int8' )<feature_engineering>
optR = OptimizedRounder()
optR.fit(predict_valid,label_valid)
 coefficients=optR.coefficients()
print(coefficients)
 preds_valid=optR.predict(predict_valid,coefficients )
%%time
make_lag_features(sales_train_validation)
 make_date_features(sales_train_validation )<define_variables>
confusionMatrics(preds_valid,label_valid )
total = len(sales_train_validation['event_type_2'])
 zero = len(sales_train_validation.loc[sales_train_validation['event_type_2'] == 0])
 one = len(sales_train_validation.loc[sales_train_validation['event_type_2'] == 1])
 two = len(sales_train_validation.loc[sales_train_validation['event_type_2'] == 2])
 print('0: '+ str(zero))
print('1: ' + str(one))
print('2: ' + str(two))
print('-'*7 + '
Total: ' + str(total))
del total; del zero; del one; del two<count_missing_values>
print("Train Cohen Kappa score: %.3f" % cohen_kappa_score(preds_valid, label_valid, weights='quadratic'))
sales_train_validation.isnull().sum()<categorify>
predict_valid_1,label_valid_1=Run_validation()
preds_valid_1=optR.predict(predict_valid_1,coefficients)
 confusionMatrics(preds_valid_1,label_valid_1 )
before = len(sales_train_validation)
 sales_train_validation.dropna(inplace = True)
 after = len(sales_train_validation)
 print(f"Reduced {(before-after)/before}%")
<split>
print("Train Cohen Kappa score: %.3f" % cohen_kappa_score(preds_valid_1, label_valid_1, weights='quadratic'))
print("Splitting data into train, validation, evaluation set")
 train = sales_train_validation[(sales_train_validation['date'] >= BEGIN_DATE)&(sales_train_validation['date'] <= TRAIN_SPLIT)]
validation = sales_train_validation[(sales_train_validation['date'] > TRAIN_SPLIT)&(sales_train_validation['date'] <= EVAL_SPLIT)]
evaluation = sales_train_validation[(sales_train_validation['date'] > EVAL_SPLIT)]
del sales_train_validation; gc.collect()<define_variables>
coefficients_1=[0.5, 1.5, 2.5, 3.5]
preds_valid_2=optR.predict(predict_valid_1,coefficients_1)
 confusionMatrics(preds_valid_2,label_valid_1 )
categorical_features = [
'item_id', 'dept_id', 'store_id', 'state_id',
'event_name_1', 'event_type_1', 'event_name_2', 'event_type_2'
]<filter>
print("Train Cohen Kappa score: %.3f" % cohen_kappa_score(preds_valid_2, label_valid_1, weights='quadratic'))
drop = ["id", "date", "sales", "day", "wm_yr_wk", "weekday", "cat_id"]
train_columns = train.columns[~train.columns.isin(drop)]
print(train_columns )<drop_column>
for i in tqdm(range(10)) :
test_preds=[]
torch.cuda.empty_cache()
test_data_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=4)
 test_preds1 = np.zeros(( len(test_dataset), 1))
tk0 = tqdm(test_data_loader)
 for i, x_batch in enumerate(tk0):
with torch.no_grad() :
x_batch = x_batch["image"]
pred = model(x_batch.to(device))
test_preds1[i * batch_size:(i + 1)* batch_size] = pred.detach().cpu().squeeze().numpy().ravel().reshape(-1, 1)
 test_preds.append(test_preds1)
 predict = np.mean(test_preds, axis=0 )
train_sets = [train[train["cat_id"] == category].drop("cat_id", axis = 1)for category in range(3)]
del train; gc.collect()<create_dataframe>
test_preds=optR.predict(predict,coefficients)
 sample = pd.read_csv(".. /input/aptos2019-blindness-detection/sample_submission.csv")
 sample.diagnosis = test_preds.astype(int)
 sample.to_csv("submission.csv", index=False )
<create_dataframe><EOS> 
torch.save(model,"final_traindv25.pth" )