Kaggle competition: PetFinder.my Adoption Prediction
!pip install -q efficientnet_pytorch > /dev/null 
<set_options>
train_df = pd.read_csv('.. /input/petfinder-adoption-prediction/train/train.csv')
 train_df.info()
SEED = 42
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
torch.backends.cudnn.benchmark = True
seed_everything(SEED )<choose_model_class>
test_df = pd.read_csv('.. /input/petfinder-adoption-prediction/test/test.csv')
 test_df.info()
def get_net() :
net = EfficientNet.from_pretrained('efficientnet-b1')
 net._fc = nn.Linear(in_features=1280, out_features=4, bias=True)
 return net
net = get_net().cuda()<load_pretrained>
breed_label = pd.read_csv('.. /input/petfinder-adoption-prediction/breed_labels.csv')
 breed_label.head()
checkpoint = torch.load('.. /input/eb1-weights/best-checkpoint-045epoch_dell.bin')
 net.load_state_dict(checkpoint['model_state_dict']);
net.eval() ;<data_type_conversions>
train_df['Mixed_Breed'] = train_df.apply(lambda x: 0 if x.Breed2==0 and x.Breed1!=307 else 1, axis=1)
 test_df['Mixed_Breed'] = test_df.apply(lambda x: 0 if x.Breed2==0 and x.Breed1!=307 else 1, axis=1 )
class DatasetSubmissionRetriever(Dataset):
def __init__(self, image_names, transforms=None):
super().__init__()
self.image_names = image_names
self.transforms = transforms
def __getitem__(self, index: int):
image_name = self.image_names[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/Test/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image_name, image
def __len__(self)-> int:
return self.image_names.shape[0]<define_variables>
color_label = pd.read_csv('.. /input/petfinder-adoption-prediction/color_labels.csv')
 color_label
DATA_ROOT_PATH = '.. /input/alaska2-image-steganalysis'
results = []
for mode in range(0, 4):
dataset = DatasetSubmissionRetriever(
 image_names=np.array([path.split('/')[-1] for path in glob('.. /input/alaska2-image-steganalysis/Test/*.jpg')]),
transforms=get_test_transforms(mode), 
)
 data_loader = DataLoader(
 dataset,
batch_size=8,
shuffle=False,
num_workers=2,
drop_last=False, 
)
 result = {'Id': [], 'Label': []}
for step,(image_names, images)in enumerate(data_loader):
print(step, end='\r')
 y_pred = net(images.cuda())
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
result['Id'].extend(image_names)
 result['Label'].extend(y_pred)
 results.append(result )<save_to_csv>
train_df['Num_Color'] = train_df.apply(lambda x: 3-sum([y==0 for y in [x.Color1, x.Color2, x.Color3]]), axis=1)
 test_df['Num_Color'] = test_df.apply(lambda x: 3-sum([y==0 for y in [x.Color1, x.Color2, x.Color3]]), axis=1 )
y_pred = net(images.cuda())
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
result['Id'].extend(image_names)
 result['Label'].extend(y_pred)
 submissions = []
for mode in range(0,4):
submission = pd.DataFrame(results[mode])
 submissions.append(submission)
 for mode in range(0,4):
submissions[mode].to_csv(f'submission_{mode}.csv', index=False)
 submissions[0]['Label'] =(submissions[0]['Label']*3 + submissions[1]['Label']*1 + submissions[2]['Label']*1 + submissions[3]['Label']*1)/ 6
submissions[0].to_csv(f'submission_B1_c.csv', index=False )<install_modules>
state_label = pd.read_csv('.. /input/petfinder-adoption-prediction/state_labels.csv')
 state_label
!pip install -q efficientnet_pytorch > /dev/null<set_options>
train_df['Description_Length'] = train_df.Description.map(len)
 test_df['Description_Length'] = test_df.Description.map(len )
SEED = 42
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
torch.backends.cudnn.benchmark = True
seed_everything(SEED )<load_from_csv>
sentiment_list = os.listdir('.. /input/petfinder-adoption-prediction/train_sentiment' )
<normalization>
example_text = pd.read_json('.. /input/petfinder-adoption-prediction/train_sentiment/{}'.format(sentiment_list[1000]), orient='index', typ='series' )
def get_train_transforms() :
return A.Compose([
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<categorify>
def get_desc_stats(desc_json):
example = pd.read_json('.. /input/petfinder-adoption-prediction/train_sentiment/{}'.format(desc_json), orient='index', typ='series')
 result = {}
result['num_sentences'] = len(example.sentences)
 result['num_entities'] = len(example.entities)
 result['magnitude'] = example.documentSentiment['magnitude']
result['score'] = example.documentSentiment['score']
return result
DATA_ROOT_PATH = '.. /input/alaska2-image-steganalysis'
def onehot(size, target):
vec = torch.zeros(size, dtype=torch.float32)
 vec[target] = 1.
 return vec
class DatasetRetriever(Dataset):
def __init__(self, kinds, image_names, labels, transforms=None):
super().__init__()
self.kinds = kinds
self.image_names = image_names
self.labels = labels
self.transforms = transforms
def __getitem__(self, index: int):
kind, image_name, label = self.kinds[index], self.image_names[index], self.labels[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{kind}/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
target = onehot(4, label)
 return image, target
def __len__(self)-> int:
return self.image_names.shape[0]
def get_labels(self):
return list(self.labels )<define_variables>
sentiment = {}
for x in sentiment_list:
sentiment[x[:9]] = get_desc_stats(x )
fold_number = 0
train_dataset = DatasetRetriever(
 kinds=dataset[dataset['fold'] != fold_number].kind.values,
image_names=dataset[dataset['fold'] != fold_number].image_name.values,
labels=dataset[dataset['fold'] != fold_number].label.values,
transforms=get_train_transforms() , 
)
 validation_dataset = DatasetRetriever(
 kinds=dataset[dataset['fold'] == fold_number].kind.values,
image_names=dataset[dataset['fold'] == fold_number].image_name.values,
labels=dataset[dataset['fold'] == fold_number].label.values,
transforms=get_valid_transforms() ,
)<compute_train_metric>
sentiment_df = pd.DataFrame.from_dict(sentiment ).transpose()
class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def alaska_weighted_auc(y_true, y_valid):

 tpr_thresholds = [0.0, 0.4, 1.0]
weights = [2, 1]
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_valid, pos_label=1)
 areas = np.array(tpr_thresholds[1:])- np.array(tpr_thresholds[:-1])
 normalization = np.dot(areas, weights)
 competition_metric = 0
for idx, weight in enumerate(weights):
y_min = tpr_thresholds[idx]
y_max = tpr_thresholds[idx + 1]
mask =(y_min < tpr)&(tpr < y_max)
 x_padding = np.linspace(fpr[mask][-1], 1, 100)
 x = np.concatenate([fpr[mask], x_padding])
 y = np.concatenate([tpr[mask], [y_max] * len(x_padding)])
 y = y - y_min
score = metrics.auc(x, y)
 submetric = score * weight
best_subscore =(y_max - y_min)* weight
competition_metric += submetric
return competition_metric / normalization
class RocAucMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.y_true = np.array([0,1])
 self.y_pred = np.array([0.5,0.5])
 self.score = 0
def update(self, y_true, y_pred):
y_true = y_true.cpu().numpy().argmax(axis=1 ).clip(min=0, max=1 ).astype(int)
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
self.y_true = np.hstack(( self.y_true, y_true))
self.y_pred = np.hstack(( self.y_pred, y_pred))
self.score = alaska_weighted_auc(self.y_true, self.y_pred)
 @property
def avg(self):
return self.score<normalization>
train_df = train_df.join(sentiment_df, on='PetID' )
class LabelSmoothing(nn.Module):
def __init__(self, smoothing = 0.05):
super(LabelSmoothing, self ).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
if self.training:
x = x.float()
target = target.float()
logprobs = torch.nn.functional.log_softmax(x, dim = -1)
 nll_loss = -logprobs * target
nll_loss = nll_loss.sum(-1)
 smooth_loss = -logprobs.mean(dim=-1)
 loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()
else:
return torch.nn.functional.cross_entropy(x, target )<init_hyperparams>
train_df.magnitude.fillna(-1, inplace=True)
 train_df.score.fillna(-1, inplace=True)
 train_df.num_sentences.fillna(-1, inplace=True)
 train_df.num_entities.fillna(-1, inplace=True )
warnings.filterwarnings("ignore")
 class Fitter:
def __init__(self, model, device, config):
self.config = config
self.epoch = 0
self.base_dir = './'
self.log_path = f'{self.base_dir}/log.txt'
self.best_summary_loss = 10**5
self.model = model
self.device = device
param_optimizer = list(self.model.named_parameters())
 no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
self.optimizer = torch.optim.AdamW(self.model.parameters() , lr=config.lr)
 self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)
 self.criterion = LabelSmoothing().to(self.device)
 self.log(f'Fitter prepared.Device is {self.device}')
 def fit(self, train_loader, validation_loader):
for e in range(self.config.n_epochs):
if self.config.verbose:
lr = self.optimizer.param_groups[0]['lr']
timestamp = datetime.utcnow().isoformat()
self.log(f'
{timestamp}
LR: {lr}')
 t = time.time()
summary_loss, final_scores = self.train_one_epoch(train_loader)
 self.log(f'[RESULT]: Train.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, time: {(time.time() - t):.5f}')
 self.save(f'{self.base_dir}/last-checkpoint.bin')
 t = time.time()
summary_loss, final_scores = self.validation(validation_loader)
 self.log(f'[RESULT]: Val.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, time: {(time.time() - t):.5f}')
 if summary_loss.avg < self.best_summary_loss:
self.best_summary_loss = summary_loss.avg
self.model.eval()
self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch ).zfill(3)}epoch.bin')
 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin')) [:-3]:
os.remove(path)
 if self.config.validation_scheduler:
self.scheduler.step(metrics=summary_loss.avg)
 self.epoch += 1
def validation(self, val_loader):
self.model.eval()
summary_loss = AverageMeter()
final_scores = RocAucMeter()
t = time.time()
for step,(images, targets)in enumerate(val_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Val Step {step}/{len(val_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 with torch.no_grad() :
targets = targets.to(self.device ).float()
batch_size = images.shape[0]
images = images.to(self.device ).float()
outputs = self.model(images)
 loss = self.criterion(outputs, targets)
 final_scores.update(targets, outputs)
 summary_loss.update(loss.detach().item() , batch_size)
 return summary_loss, final_scores
def train_one_epoch(self, train_loader):
self.model.train()
summary_loss = AverageMeter()
final_scores = RocAucMeter()
t = time.time()
for step,(images, targets)in enumerate(train_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Train Step {step}/{len(train_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 targets = targets.to(self.device ).float()
images = images.to(self.device ).float()
batch_size = images.shape[0]
self.optimizer.zero_grad()
outputs = self.model(images)
 loss = self.criterion(outputs, targets)
 loss.backward()
final_scores.update(targets, outputs)
 summary_loss.update(loss.detach().item() , batch_size)
 self.optimizer.step()
if self.config.step_scheduler:
self.scheduler.step()
return summary_loss, final_scores
def save(self, path):
self.model.eval()
torch.save({
'model_state_dict': self.model.state_dict() ,
'optimizer_state_dict': self.optimizer.state_dict() ,
'scheduler_state_dict': self.scheduler.state_dict() ,
'best_summary_loss': self.best_summary_loss,
'epoch': self.epoch,
}, path)
 def load(self, path):
checkpoint = torch.load(path)
 self.model.load_state_dict(checkpoint['model_state_dict'])
 self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
 self.best_summary_loss = checkpoint['best_summary_loss']
self.epoch = checkpoint['epoch'] + 1
def log(self, message):
if self.config.verbose:
print(message)
 with open(self.log_path, 'a+')as logger:
logger.write(f'{message}
' )<choose_model_class>
sentiment_list_test = os.listdir('.. /input/petfinder-adoption-prediction/test_sentiment')
 def get_desc_stats_test(desc_json):
example = pd.read_json('.. /input/petfinder-adoption-prediction/test_sentiment/{}'.format(desc_json), orient='index', typ='series')
 result = {}
result['num_sentences'] = len(example.sentences)
 result['num_entities'] = len(example.entities)
 result['magnitude'] = example.documentSentiment['magnitude']
result['score'] = example.documentSentiment['score']
return result
sentiment_test = {}
for x in sentiment_list_test:
sentiment_test[x[:9]] = get_desc_stats_test(x)
 sentiment_df_test = pd.DataFrame.from_dict(sentiment_test ).transpose()
test_df = test_df.join(sentiment_df_test, on='PetID')
 test_df.magnitude.fillna(-1, inplace=True)
 test_df.score.fillna(-1, inplace=True)
 test_df.num_sentences.fillna(-1, inplace=True)
 test_df.num_entities.fillna(-1, inplace=True )
def get_net() :
net = EfficientNet.from_pretrained('efficientnet-b2')
 net._fc = nn.Linear(in_features=1408, out_features=4, bias=True)
 return net
net = get_net().cuda()<init_hyperparams>
from sklearn.feature_extraction.text import TfidfVectorizer
class TrainGlobalConfig:
num_workers = 4
batch_size = 16
n_epochs = 25
lr = 0.001
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08 
)
<load_pretrained>
pattern = re.compile('[\W_]+', re.UNICODE)
 texts = [pattern.sub(' ', x)for x in train_df.Description]
texts_test = [pattern.sub(' ', x)for x in test_df.Description]
def run_training() :
device = torch.device('cuda:0')
 train_loader = torch.utils.data.DataLoader(
 train_dataset,
sampler=BalanceClassSampler(labels=train_dataset.get_labels() , mode="downsampling"),
batch_size=TrainGlobalConfig.batch_size,
pin_memory=False,
drop_last=True,
num_workers=TrainGlobalConfig.num_workers, 
)
 val_loader = torch.utils.data.DataLoader(
 validation_dataset,
batch_size=TrainGlobalConfig.batch_size,
num_workers=TrainGlobalConfig.num_workers,
shuffle=False,
sampler=SequentialSampler(validation_dataset),
pin_memory=False, 
)
 fitter = Fitter(model=net, device=device, config=TrainGlobalConfig)
 fitter.fit(train_loader, val_loader )<load_from_csv>
Tfidf = TfidfVectorizer(stop_words='english',ngram_range=(1,3), max_features=20000,max_df=0.95,min_df=5 )
file = open('.. /input/alaska2-public-baseline/log.txt', 'r')
 for line in file.readlines() :
print(line[:-1])
 file.close()<load_pretrained>
train_x_tfidf_full = Tfidf.fit_transform(texts)
 test_x_tfidf = Tfidf.transform(texts_test )
checkpoint = torch.load('.. /input/alaska2-public-baseline/best-checkpoint-033epoch.bin')
 net.load_state_dict(checkpoint['model_state_dict']);
net.eval() ;<data_type_conversions>
from sklearn.decomposition import TruncatedSVD
class DatasetSubmissionRetriever(Dataset):
def __init__(self, image_names, transforms=None):
super().__init__()
self.image_names = image_names
self.transforms = transforms
def __getitem__(self, index: int):
image_name = self.image_names[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/Test/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image_name, image
def __len__(self)-> int:
return self.image_names.shape[0]<define_variables>
svd = TruncatedSVD(n_components=100, random_state=42)
 train_x_svd = svd.fit_transform(train_x_tfidf_full )
dataset = DatasetSubmissionRetriever(
 image_names=np.array([path.split('/')[-1] for path in glob('.. /input/alaska2-image-steganalysis/Test/*.jpg')]),
transforms=get_valid_transforms() , 
)
 data_loader = DataLoader(
 dataset,
batch_size=8,
shuffle=False,
num_workers=2,
drop_last=False,
)<save_to_csv>
train_df = train_df.join(pd.DataFrame(train_x_svd, columns=['svd_'+str(x)for x in np.arange(100)]))
submission = pd.DataFrame(result)
 submission.sort_values(by='Id', inplace=True)
 submission.reset_index(drop=True, inplace=True)
 submission.to_csv('submission_b2.csv', index=False)
 submission.head()<save_to_csv>
test_x_svd = svd.transform(test_x_tfidf)
 test_df = test_df.join(pd.DataFrame(test_x_svd, columns=['svd_'+str(x)for x in np.arange(100)]))
sub_stack = pd.read_csv('/kaggle/input/alaska-submission-files/submission_stack.csv')
 sub_stack.sort_values(by='Id', inplace=True)
 sub_stack.reset_index(drop=True, inplace=True)
 sub_stack.to_csv('submission_stack.csv', index=False)
 sub_stack.head()<save_to_csv>
from sklearn.feature_extraction.text import CountVectorizer
sub = sub_stack.copy()
sub['Label'] = sub['Label']*0.5+submission['Label']*0.5
sub.to_csv('submission_ensemble.csv', index=False)
 sub.head()<install_modules>
cv = CountVectorizer(stop_words='english',ngram_range=(1,3), max_features=20000,max_df=0.95,min_df=5 )
!pip install -q efficientnet_pytorch > /dev/null<set_options>
train_x_cv = cv.fit_transform(texts)
 test_x_cv = cv.transform(texts_test )
SEED = 42
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
torch.backends.cudnn.benchmark = True
seed_everything(SEED )<prepare_x_and_y>
def wm2df(wm, feat_names):
df = pd.DataFrame(data=wm.toarray() ,
columns=feat_names)
 return(df )
dataset = []
for label, kind in enumerate(['Cover', 'JMiPOD', 'JUNIWARD', 'UERD']):
for path in glob('.. /input/alaska2-image-steganalysis/Cover/*.jpg'):
dataset.append({
'kind': kind,
'image_name': path.split('/')[-1],
'label': label
})
 random.shuffle(dataset)
 dataset = pd.DataFrame(dataset)
 gkf = GroupKFold(n_splits=5)
 dataset['fold'] = 0
for fold_number,(train_index, val_index)in enumerate(gkf.split(X=dataset.index, y=dataset['label'], groups=dataset['image_name'])) :
dataset.loc[dataset.iloc[val_index].index, 'fold'] = fold_number<load_from_csv>
svd_cv = TruncatedSVD(n_components=100, random_state=42)
 train_x_svd_cv = svd_cv.fit_transform(train_x_cv)
 train_df = train_df.join(pd.DataFrame(train_x_svd_cv, columns=['svd_cv_'+str(x)for x in np.arange(100)]))
test_x_svd_cv = svd_cv.transform(test_x_cv)
 test_df = test_df.join(pd.DataFrame(test_x_svd_cv, columns=['svd_cv_'+str(x)for x in np.arange(100)]))
<normalization>
def isChinese(s):
if len(re.findall(u'[\u4e00-\u9fff]', s)) > 0:
return 1
else:
return 0
def get_train_transforms() :
return A.Compose([
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<categorify>
train_df['contains_chinese'] = train_df.Description.map(isChinese)
 test_df['contains_chinese'] = test_df.Description.map(isChinese )
DATA_ROOT_PATH = '.. /input/alaska2-image-steganalysis'
def onehot(size, target):
vec = torch.zeros(size, dtype=torch.float32)
 vec[target] = 1.
 return vec
class DatasetRetriever(Dataset):
def __init__(self, kinds, image_names, labels, transforms=None):
super().__init__()
self.kinds = kinds
self.image_names = image_names
self.labels = labels
self.transforms = transforms
def __getitem__(self, index: int):
kind, image_name, label = self.kinds[index], self.image_names[index], self.labels[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{kind}/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
target = onehot(4, label)
 return image, target
def __len__(self)-> int:
return self.image_names.shape[0]
def get_labels(self):
return list(self.labels )<define_variables>
train_df.contains_chinese.value_counts()
fold_number = 0
train_dataset = DatasetRetriever(
 kinds=dataset[dataset['fold'] != fold_number].kind.values,
image_names=dataset[dataset['fold'] != fold_number].image_name.values,
labels=dataset[dataset['fold'] != fold_number].label.values,
transforms=get_train_transforms() , 
)
 validation_dataset = DatasetRetriever(
 kinds=dataset[dataset['fold'] == fold_number].kind.values,
image_names=dataset[dataset['fold'] == fold_number].image_name.values,
labels=dataset[dataset['fold'] == fold_number].label.values,
transforms=get_valid_transforms() ,
)<compute_train_metric>
photo_list = os.listdir('.. /input/petfinder-adoption-prediction/train_metadata' )
class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def alaska_weighted_auc(y_true, y_valid):

 tpr_thresholds = [0.0, 0.4, 1.0]
weights = [2, 1]
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_valid, pos_label=1)
 areas = np.array(tpr_thresholds[1:])- np.array(tpr_thresholds[:-1])
 normalization = np.dot(areas, weights)
 competition_metric = 0
for idx, weight in enumerate(weights):
y_min = tpr_thresholds[idx]
y_max = tpr_thresholds[idx + 1]
mask =(y_min < tpr)&(tpr < y_max)
 x_padding = np.linspace(fpr[mask][-1], 1, 100)
 x = np.concatenate([fpr[mask], x_padding])
 y = np.concatenate([tpr[mask], [y_max] * len(x_padding)])
 y = y - y_min
score = metrics.auc(x, y)
 submetric = score * weight
best_subscore =(y_max - y_min)* weight
competition_metric += submetric
return competition_metric / normalization
class RocAucMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.y_true = np.array([0,1])
 self.y_pred = np.array([0.5,0.5])
 self.score = 0
def update(self, y_true, y_pred):
y_true = y_true.cpu().numpy().argmax(axis=1 ).clip(min=0, max=1 ).astype(int)
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
self.y_true = np.hstack(( self.y_true, y_true))
self.y_pred = np.hstack(( self.y_pred, y_pred))
self.score = alaska_weighted_auc(self.y_true, self.y_pred)
 @property
def avg(self):
return self.score<normalization>
profile_photo = [x for x in photo_list if "-1." in x]
class LabelSmoothing(nn.Module):
def __init__(self, smoothing = 0.05):
super(LabelSmoothing, self ).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
if self.training:
x = x.float()
target = target.float()
logprobs = torch.nn.functional.log_softmax(x, dim = -1)
 nll_loss = -logprobs * target
nll_loss = nll_loss.sum(-1)
 smooth_loss = -logprobs.mean(dim=-1)
 loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()
else:
return torch.nn.functional.cross_entropy(x, target )<init_hyperparams>
def get_dominant_color(photo_json):
example = pd.read_json('.. /input/petfinder-adoption-prediction/train_metadata/{}'.format(photo_json), orient='index', typ='series')
 max_index = np.argmax([x['pixelFraction'] for x in example.imagePropertiesAnnotation['dominantColors']['colors']])
 result = example.imagePropertiesAnnotation['dominantColors']['colors'][max_index]['color']
result['score'] = example.imagePropertiesAnnotation['dominantColors']['colors'][max_index]['score']
result['pixelFraction'] = example.imagePropertiesAnnotation['dominantColors']['colors'][max_index]['pixelFraction']
try:
result['image_description'] = example.labelAnnotations[0]['description']
result['image_description_score'] = example.labelAnnotations[0]['score']
except AttributeError:
result['image_description'] = -1
result['image_description_score'] = -1
result['image_confidence'] = example.cropHintsAnnotation['cropHints'][0]['confidence']
try:
result['image_importanceFraction'] = example.cropHintsAnnotation['cropHints'][0]['importanceFraction']
except KeyError:
result['image_importanceFraction'] = -1
return result
warnings.filterwarnings("ignore")
 class Fitter:
def __init__(self, model, device, config):
self.config = config
self.epoch = 0
self.base_dir = './'
self.log_path = f'{self.base_dir}/log.txt'
self.best_summary_loss = 10**5
self.model = model
self.device = device
param_optimizer = list(self.model.named_parameters())
 no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
self.optimizer = torch.optim.AdamW(self.model.parameters() , lr=config.lr)
 self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)
 self.criterion = LabelSmoothing().to(self.device)
 self.log(f'Fitter prepared.Device is {self.device}')
 def fit(self, train_loader, validation_loader):
for e in range(self.config.n_epochs):
if self.config.verbose:
lr = self.optimizer.param_groups[0]['lr']
timestamp = datetime.utcnow().isoformat()
self.log(f'
{timestamp}
LR: {lr}')
 t = time.time()
summary_loss, final_scores = self.train_one_epoch(train_loader)
 self.log(f'[RESULT]: Train.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, time: {(time.time() - t):.5f}')
 self.save(f'{self.base_dir}/last-checkpoint.bin')
 t = time.time()
summary_loss, final_scores = self.validation(validation_loader)
 self.log(f'[RESULT]: Val.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, time: {(time.time() - t):.5f}')
 if summary_loss.avg < self.best_summary_loss:
self.best_summary_loss = summary_loss.avg
self.model.eval()
self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch ).zfill(3)}epoch.bin')
 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin')) [:-3]:
os.remove(path)
 if self.config.validation_scheduler:
self.scheduler.step(metrics=summary_loss.avg)
 self.epoch += 1
def validation(self, val_loader):
self.model.eval()
summary_loss = AverageMeter()
final_scores = RocAucMeter()
t = time.time()
for step,(images, targets)in enumerate(val_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Val Step {step}/{len(val_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 with torch.no_grad() :
targets = targets.to(self.device ).float()
batch_size = images.shape[0]
images = images.to(self.device ).float()
outputs = self.model(images)
 loss = self.criterion(outputs, targets)
 final_scores.update(targets, outputs)
 summary_loss.update(loss.detach().item() , batch_size)
 return summary_loss, final_scores
def train_one_epoch(self, train_loader):
self.model.train()
summary_loss = AverageMeter()
final_scores = RocAucMeter()
t = time.time()
for step,(images, targets)in enumerate(train_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Train Step {step}/{len(train_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 targets = targets.to(self.device ).float()
images = images.to(self.device ).float()
batch_size = images.shape[0]
self.optimizer.zero_grad()
outputs = self.model(images)
 loss = self.criterion(outputs, targets)
 loss.backward()
final_scores.update(targets, outputs)
 summary_loss.update(loss.detach().item() , batch_size)
 self.optimizer.step()
if self.config.step_scheduler:
self.scheduler.step()
return summary_loss, final_scores
def save(self, path):
self.model.eval()
torch.save({
'model_state_dict': self.model.state_dict() ,
'optimizer_state_dict': self.optimizer.state_dict() ,
'scheduler_state_dict': self.scheduler.state_dict() ,
'best_summary_loss': self.best_summary_loss,
'epoch': self.epoch,
}, path)
 def load(self, path):
checkpoint = torch.load(path)
 self.model.load_state_dict(checkpoint['model_state_dict'])
 self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
 self.best_summary_loss = checkpoint['best_summary_loss']
self.epoch = checkpoint['epoch'] + 1
def log(self, message):
if self.config.verbose:
print(message)
 with open(self.log_path, 'a+')as logger:
logger.write(f'{message}
' )<choose_model_class>
dominant_color_train = {}
for x in profile_photo:
dominant_color_train[x[:9]] = get_dominant_color(x )
def get_net() :
net = EfficientNet.from_pretrained('efficientnet-b2')
 net._fc = nn.Linear(in_features=1408, out_features=4, bias=True)
 return net
net = get_net().cuda()<init_hyperparams>
dominant_color_df = pd.DataFrame(dominant_color_train ).transpose()
dominant_color_df.columns = ['photo_'+x for x in dominant_color_df.columns]
train_df = train_df.join(dominant_color_df, on='PetID', rsuffix='_color' )
class TrainGlobalConfig:
num_workers = 4
batch_size = 8
n_epochs = 25
lr = 0.002
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08 
)
<load_pretrained>
train_df[pd.DataFrame(dominant_color_df ).columns.tolist() ] = train_df[pd.DataFrame(dominant_color_df ).columns.tolist() ].fillna(-1 )
def run_training() :
device = torch.device('cuda:0')
 train_loader = torch.utils.data.DataLoader(
 train_dataset,
sampler=BalanceClassSampler(labels=train_dataset.get_labels() , mode="downsampling"),
batch_size=TrainGlobalConfig.batch_size,
pin_memory=False,
drop_last=True,
num_workers=TrainGlobalConfig.num_workers, 
)
 val_loader = torch.utils.data.DataLoader(
 validation_dataset,
batch_size=TrainGlobalConfig.batch_size,
num_workers=TrainGlobalConfig.num_workers,
shuffle=False,
sampler=SequentialSampler(validation_dataset),
pin_memory=False, 
)
 fitter = Fitter(model=net, device=device, config=TrainGlobalConfig)
 fitter.fit(train_loader, val_loader )<set_options>
photo_list_test = os.listdir('.. /input/petfinder-adoption-prediction/test_metadata')
 profile_photo_test = [x for x in photo_list_test if "-1." in x]
def get_dominant_color_test(photo_json):
example = pd.read_json('.. /input/petfinder-adoption-prediction/test_metadata/{}'.format(photo_json), orient='index', typ='series')
 max_index = np.argmax([x['pixelFraction'] for x in example.imagePropertiesAnnotation['dominantColors']['colors']])
 result = example.imagePropertiesAnnotation['dominantColors']['colors'][max_index]['color']
result['score'] = example.imagePropertiesAnnotation['dominantColors']['colors'][max_index]['score']
result['pixelFraction'] = example.imagePropertiesAnnotation['dominantColors']['colors'][max_index]['pixelFraction']
try:
result['image_description'] = example.labelAnnotations[0]['description']
result['image_description_score'] = example.labelAnnotations[0]['score']
except AttributeError:
result['image_description'] = -1
result['image_description_score'] = -1
result['image_confidence'] = example.cropHintsAnnotation['cropHints'][0]['confidence']
try:
result['image_importanceFraction'] = example.cropHintsAnnotation['cropHints'][0]['importanceFraction']
except KeyError:
result['image_importanceFraction'] = -1
return result
!nvidia-smi<load_from_csv>
dominant_color_test = {}
for x in profile_photo_test:
dominant_color_test[x[:9]] = get_dominant_color_test(x )
file = open('.. /input/alaska2-public-baseline/log.txt', 'r')
 for line in file.readlines() :
print(line[:-1])
 file.close()<load_pretrained>
dominant_color_df_test = pd.DataFrame(dominant_color_test ).transpose()
dominant_color_df_test.columns = ['photo_'+x for x in dominant_color_df_test.columns]
test_df = test_df.join(dominant_color_df_test, on='PetID', rsuffix='_color')
 test_df[pd.DataFrame(dominant_color_df_test ).columns.tolist() ] = test_df[pd.DataFrame(dominant_color_df_test ).columns.tolist() ].fillna(-1 )
checkpoint = torch.load('.. /input/alaska2-public-baseline/best-checkpoint-033epoch.bin')
 net.load_state_dict(checkpoint['model_state_dict']);
net.eval() ;<categorify>
photo_image_description = {}
for i,x in enumerate(pd.concat([train_df.photo_image_description, test_df.photo_image_description] ).value_counts().index):
photo_image_description[x] = i
train_df.photo_image_description = train_df.photo_image_description.map(lambda x: photo_image_description[x])
 test_df.photo_image_description = test_df.photo_image_description.map(lambda x: photo_image_description[x] )
def get_test_transforms(mode):
if mode == 0:
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 elif mode == 1:
return A.Compose([
A.HorizontalFlip(p=1),
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 elif mode == 2:
return A.Compose([
A.VerticalFlip(p=1),
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 else:
return A.Compose([
A.HorizontalFlip(p=1),
A.VerticalFlip(p=1),
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
dog_cat = [photo_image_description[x] for x in ['dog',
'dog breed',
'dog like mammal',
'dog breed group',
'pug',
'bull terrier',
'siberian husky',
'beagle',
'street dog',
'boston terrier',
'pomeranian',
'harrier',
'czechoslovakian wolfdog',
'basset hound',
'volpino italiano']]
class DatasetSubmissionRetriever(Dataset):
def __init__(self, image_names, transforms=None):
super().__init__()
self.image_names = image_names
self.transforms = transforms
def __getitem__(self, index: int):
image_name = self.image_names[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/Test/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image_name, image
def __len__(self)-> int:
return self.image_names.shape[0]<define_variables>
cat_cat = [photo_image_description[x] for x in ['cat',
'small to medium sized cats',
'cat like mammal',
'black cat']]
results = []
for mode in range(0, 4):
dataset = DatasetSubmissionRetriever(
 image_names=np.array([path.split('/')[-1] for path in glob('.. /input/alaska2-image-steganalysis/Test/*.jpg')]),
transforms=get_test_transforms(mode), 
)
 data_loader = DataLoader(
 dataset,
batch_size=8,
shuffle=False,
num_workers=2,
drop_last=False, 
)
 result = {'Id': [], 'Label': []}
for step,(image_names, images)in enumerate(data_loader):
print(step, end='\r')
 y_pred = net(images.cuda())
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
result['Id'].extend(image_names)
 result['Label'].extend(y_pred)
 results.append(result )<prepare_output>
def main_category(x):
if x in dog_cat:
return 1
elif x in cat_cat:
return 2
elif x == photo_image_description[-1]:
return 0
else:
return 3
submissions = []
for mode in range(0,4):
submission = pd.DataFrame(results[mode])
 submissions.append(submission )<save_to_csv>
train_df['photo_category'] = train_df.photo_image_description.map(main_category)
 test_df['photo_category'] = test_df.photo_image_description.map(main_category )
for mode in range(0,4):
submissions[mode].to_csv(f'submission_{mode}.csv', index=False )<save_to_csv>
image_stat_train = pd.read_csv('.. /input/image-statistics-for-petfinder/train_image.csv')
 image_stat_train.info()
submissions[0]['Label'] =(submissions[0]['Label']*12 + submissions[1]['Label']*3 + submissions[2]['Label']*3 + submissions[3]['Label'])/19
submissions[0].to_csv(f'submission.csv', index=False )<install_modules>
image_stat_test = pd.read_csv('.. /input/image-statistics-for-petfinder/test_image.csv')
 image_stat_test.info()
!pip install stegano
!pip install -q efficientnet_pytorch<set_options>
image_stat_train['PetID'] = image_stat_train.image.map(lambda x: x[:9])
 image_stat_test['PetID'] = image_stat_test.image.map(lambda x: x[:9] )
%matplotlib inline
RandomBrightness, RandomContrast, HueSaturationValue, Blur, GaussNoise)
 warnings.filterwarnings("ignore" )<train_model>
image_stat_train = image_stat_train.set_index('PetID')
 image_stat_test = image_stat_test.set_index('PetID' )
original_vector = np.array(original ).flatten()
altered_vector = np.array(altered ).flatten()
print('Original shape:', original_vector.shape, '
' +
'Altered shape:', altered_vector.shape)
 dist1 = np.sum(original_vector - original_vector)
 print('Dist1:', dist1)
 dist2 = np.sum(original_vector - altered_vector)
 print('Dist2:', dist2 )<define_variables>
train_df = train_df.join(image_stat_train, on='PetID', rsuffix='_image')
 test_df = test_df.join(image_stat_test, on='PetID', rsuffix='_image' )
base_path = '.. /input/alaska2-image-steganalysis'
def read_images_path(dir_name='Cover', test = False):

 series_name = pd.Series(os.listdir(base_path + '/' + dir_name))
if test:
series_name = pd.Series(os.listdir(base_path + '/' + 'Test'))
series_paths = pd.Series(base_path + '/' + dir_name + '/' + series_name)
 return series_paths<define_variables>
import lightgbm as lgb
cover_paths = read_images_path('Cover', False)
 jmipod_paths = read_images_path('JMiPOD', False)
 juniward_paths = read_images_path('JUNIWARD', False)
 uerd_paths = read_images_path('UERD', False)
 test_paths = read_images_path('Test', True )<train_model>
features = [x for x in train_df.columns if x not in ['Name', 'Type','RescuerID','AdoptionSpeed','Description','PetID','image','dullness_whiteness','temp_size','photo_image_description']]
image_sample = mpimg.imread(cover_paths[0])
 print('Image sample shape:', image_sample.shape)
 print('Image sample size:', image_sample.size)
 print('Image sample data type:', image_sample.dtype )<normalization>
df_train, df_val = train_test_split(train_df, test_size=0.3, random_state=420 )
def dct2(a):
return fftpack.dct(fftpack.dct(a, axis=0, norm='ortho'), axis=1, norm='ortho')
 imsize = image.shape
dct = np.zeros(imsize)
 for i in r_[:imsize[0]:8]:
for j in r_[:imsize[1]:8]:
dct[i:(i+8),j:(j+8)] = dct2(image[i:(i+8),j:(j+8)] )<set_options>
from sklearn.metrics import cohen_kappa_score
def set_seed(seed = 1234):

 np.random.seed(seed)
 random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
os.environ['PYTHONHASHSEED'] = str(seed)
 set_seed()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 print('Device available now:', device )<load_from_csv>
def kappa_scorer(pred, train_data):
length = len(train_data.get_label())
 pred_results = [[pred[x], pred[x+length*1], pred[x+length*2], pred[x+length*3], pred[x+length*4]] for x in np.arange(length)]
return 'kappa', cohen_kappa_score([np.argmax(x)for x in pred_results],train_data.get_label() , weights='quadratic'), True
sample_size = 256
num_classes = 4
train_df = pd.read_csv('.. /input/alaska2-trainvalid-4-class-csv/alaska2_train_data_4classes.csv',
header=0, names=['Path', 'Label'], dtype = {'Label':np.int32})
 valid_df = pd.read_csv('.. /input/alaska2-trainvalid-4-class-csv/alaska2_valid_data_4classes.csv',
header=0, names=['Path', 'Label'], dtype = {'Label':np.int32})
 def sample_data(dataframe, sample_size, num_classes, train=True):

 if train:
size = int(0.75 * sample_size)
 else:
size = int(0.25 * sample_size)
 no = int(np.floor(size/num_classes))
labels = [i for i in range(num_classes)]
new_data = pd.DataFrame()
for label in labels:
data = dataframe[dataframe['Label'] == label].sample(no, random_state=123)
 new_data = pd.concat([new_data, data], axis=0)
 return new_data<categorify>
d_train = lgb.Dataset(df_train[features], label=df_train['AdoptionSpeed'],feature_name=features,
categorical_feature=['Breed1', 'Breed2', 'Gender', 'Color1', 'Color2',
'Color3', 'MaturitySize', 'FurLength', 'Vaccinated', 'Dewormed',
'Sterilized', 'Health', 'State','Mixed_Breed','photo_category','contains_chinese'])
 d_val = lgb.Dataset(df_val[features], label=df_val['AdoptionSpeed'], reference=d_train,feature_name=features,
categorical_feature=['Breed1', 'Breed2', 'Gender', 'Color1', 'Color2',
'Color3', 'MaturitySize', 'FurLength', 'Vaccinated', 'Dewormed',
'Sterilized', 'Health', 'State', 'Mixed_Breed','photo_category','contains_chinese'])
 params = {"objective" : "multiclass",
"num_class": 5,
"metric" : "None",
"learning_rate" : 0.1,
"feature_fraction_seed" : 420,
"feature_fraction" : 0.4,
"early_stopping_rounds": 200
}
evals_result = {}
model = lgb.train(params, d_train, num_boost_round=2000, valid_sets=[d_train, d_val], feval=kappa_scorer, evals_result=evals_result, verbose_eval=50 )
class AlaskaDataset(Dataset):

 def __init__(self, dataframe, is_test, is_val, vertical_flip=0.5, horizontal_flip=0.5):
self.dataframe, self.is_test, self.is_val = dataframe, is_test, is_val
self.vertical_flip, self.horizontal_flip = vertical_flip, horizontal_flip
flag = is_test or is_val
if flag:
self.transform = Compose([Resize(512, 512),
Normalize() ,
ToFloat(max_value=255),
ToTensor() ])
 else:
self.transform = Compose([Resize(512, 512),
VerticalFlip(p = vertical_flip),
HorizontalFlip(p = horizontal_flip),
Normalize() ,
ToFloat(max_value=255),
ToTensor() ])
 def __len__(self):
return len(self.dataframe)
 def __getitem__(self, index):
if self.is_test:
path = self.dataframe.loc[index][0]
else:
path, label = self.dataframe.loc[index]
image = cv2.imread(path)[:, :, ::-1]
image = self.transform(image=image)
 image = image['image']
if self.is_test:
return image
else:
return image, label<load_pretrained>
d_train = lgb.Dataset(df_train[features], label=df_train['AdoptionSpeed'],feature_name=features,
categorical_feature=['Breed1', 'Breed2', 'Gender', 'Color1', 'Color2',
'Color3', 'MaturitySize', 'FurLength', 'Vaccinated', 'Dewormed',
'Sterilized', 'Health', 'State','Mixed_Breed','photo_category','contains_chinese'])
 cv_dict = lgb.cv(params, d_train, num_boost_round=1000, feval=kappa_scorer, verbose_eval=50 )
class EfficientNetwork(nn.Module):
def __init__(self, output_size, b1=False, b2=False):
super().__init__()
self.b1, self.b2 = b1, b2
if b1:
self.features = EfficientNet.from_pretrained('efficientnet-b1')
 elif b2:
self.features = EfficientNet.from_pretrained('efficientnet-b2')
 else:
self.features = EfficientNet.from_pretrained('efficientnet-b0')
 if b1:
self.classification = nn.Linear(1280, output_size)
 elif b2:
self.classification = nn.Linear(1408, output_size)
 else:
self.classification = nn.Linear(1280, output_size)
 def forward(self, image, prints=False):
if prints: print('Input Image shape:', image.shape)
 image = self.features.extract_features(image)
 if prints: print('Features Image shape:', image.shape)
 if self.b1:
image = F.avg_pool2d(image, image.size() [2:] ).reshape(-1, 1280)
 elif self.b2:
image = F.avg_pool2d(image, image.size() [2:] ).reshape(-1, 1408)
 else:
image = F.avg_pool2d(image, image.size() [2:] ).reshape(-1, 1280)
 if prints: print('Image Reshaped shape:', image.shape)
 out = self.classification(image)
 if prints: print('Out shape:', out.shape)
 return out<choose_model_class>
dummy_model = DummyClassifier(random_state=1)
 dummy_model.fit(df_train[features], df_train['AdoptionSpeed'])
 cohen_kappa_score(dummy_model.predict(df_val[features]), df_val['AdoptionSpeed'] )
model_example = EfficientNetwork(output_size=num_classes, b1=False, b2=True )<train_on_grid>
pred_test = model.predict(test_df[features], num_iteration=model.best_iteration )
example_data = AlaskaDataset(train_df, is_test=False, is_val=False)
 example_loader = torch.utils.data.DataLoader(example_data, batch_size = 1, shuffle=True)
 for image, labels in example_loader:
images_example = image
labels_example = torch.tensor(labels, dtype=torch.long)
 break
print('Images shape:', images_example.shape)
 print('Labels:', labels, '
')
 out = model_example(images_example, prints=True)
 criterion_example = nn.CrossEntropyLoss()
loss = criterion_example(out, labels_example)
 print('Loss:', loss.item() )<normalization>
submission = pd.concat([test_df.PetID,pd.DataFrame(pred_test, columns=['A','B','C','D','E'])],axis=1 )
class ResNet34Network(nn.Module):
def __init__(self, output_size):
super().__init__()
self.features = resnet34(pretrained=True)
 self.classification = nn.Linear(1000, output_size)
 def forward(self, image, prints=False):
if prints: print('Input Image shape:', image.shape)
 image = self.features(image)
 if prints: print('Features Image shape:', image.shape)
 out = self.classification(image)
 if prints: print('Out shape:', out.shape)
 return out<choose_model_class>
submission['AdoptionSpeed'] = submission.apply(lambda x: np.argmax([x.A,x.B,x.C,x.D,x.E]), axis=1 )
model_example = ResNet34Network(output_size=num_classes )<train_on_grid>
submission.to_csv('submission.csv',index=False )
example_data = AlaskaDataset(train_df, is_test=False, is_val=False)
 example_loader = torch.utils.data.DataLoader(example_data, batch_size = 1, shuffle=True)
 for image, labels in example_loader:
images_example = image
labels_example = torch.tensor(labels, dtype=torch.long)
 break
print('Images shape:', images_example.shape)
 print('Labels:', labels, '
')
 out = model_example(images_example, prints=True)
 criterion_example = nn.CrossEntropyLoss()
loss = criterion_example(out, labels_example)
 print('Loss:', loss.item() )<set_options>
submission.AdoptionSpeed.value_counts()