Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<normalization>
%matplotlib inline
plt.style.use('ggplot')
 warnings.filterwarnings("ignore" )
class Alaska2Dataset(Dataset):
def __init__(self, df, augmentations=None):
self.data = df
self.augment = augmentations
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
fn, label = self.data.loc[idx]
im = cv2.imread(fn)[:, :, ::-1]
if self.augment:
im = self.augment(image=im)
 return im, label
img_size = 512
AUGMENTATIONS_TRAIN = Compose([
VerticalFlip(p=0.5),
HorizontalFlip(p=0.5),
ToFloat(max_value=255),
ToTensor()
], p=1)
 AUGMENTATIONS_TEST = Compose([
ToFloat(max_value=255),
ToTensor()
], p=1)
<categorify>
breeds = pd.read_csv('.. /input/breed_labels.csv')
 colors = pd.read_csv('.. /input/color_labels.csv')
 states = pd.read_csv('.. /input/state_labels.csv')
 train = pd.read_csv('.. /input/train/train.csv')
 test = pd.read_csv('.. /input/test/test.csv')
 sub = pd.read_csv('.. /input/test/sample_submission.csv')
 train['DatasetType'] = 'train'
test['DatasetType'] = 'test'
all_data = pd.concat([train, test] )
class Net(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = EfficientNet.from_pretrained('efficientnet-b0')
 self.dense_output = nn.Linear(1280, num_classes)
 def forward(self, x):
feat = self.model.extract_features(x)
 feat = F.avg_pool2d(feat, feat.size() [2:] ).reshape(-1, 1280)
 return self.dense_output(feat )<define_variables>
import sklearn
from sklearn.preprocessing import scale
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn import preprocessing
models = os.listdir('.. /input/alaska2-efficientnet-trained-model-weights/')
 models<define_variables>
enc = OneHotEncoder(handle_unknown='ignore')
 l = LogisticRegression()
tpu_models = [fn for fn in models if 'model_effnet' in fn]
tpu_models<define_variables>
features = train.columns.difference(["Name", "Description", "PetID", "RescuerID", "AdoptionSpeed"])
 train_x = train[features]
train_y = train["AdoptionSpeed"]
test_x = test[features]
gpu_models = [fn for fn in models if 'efficientnetb0_lb' in fn]
gpu_models<train_model>
enc.fit(train_x)
 x_train_1h = enc.transform(train_x)
 train_xx,cv_x,train_yy,cv_y=train_test_split(x_train_1h,train_y,test_size=0.2)
 l.fit(train_xx,list(train_yy))
y_pred = l.predict(cv_x)
 accuracy = accuracy_score(list(cv_y),y_pred)
 accuracy
<load_pretrained>
enc.fit(train_x)
 X_train_one_hot = enc.transform(train_x)
 X_test_one_hot = enc.transform(test_x)
 l.fit(X_train_one_hot,train_y)
 y_pred = l.predict(X_test_one_hot)
 print(X_train_one_hot.shape )
loaded_gpu_models = []
for fn in gpu_models:
device = 'cuda'
model = Net(num_classes=len(class_labels)).to(device)
 model.load_state_dict(torch.load(( '.. /input/alaska2-efficientnet-trained-model-weights/' + fn)))
 loaded_gpu_models.append(model )<create_dataframe>
for i,val in enumerate(y_pred):
sub.at[i,'AdoptionSpeed'] = val
sub.AdoptionSpeed = sub.AdoptionSpeed.astype(int)
 sub.head()
class Alaska2TestDataset(Dataset):
def __init__(self, df, augmentations=None):
self.data = df
self.augment = augmentations
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
fn = self.data.loc[idx][0]
im = cv2.imread(fn)[:, :, ::-1]
if self.augment:
im = self.augment(image=im)
 return im
test_filenames = sorted(glob(f"{data_dir}/Test/*.jpg"))
test_df = pd.DataFrame({'ImageFileName': list(
 test_filenames)}, columns=['ImageFileName'])
 batch_size = 16
num_workers = 4
test_dataset = Alaska2TestDataset(test_df, augmentations=AUGMENTATIONS_TEST)
 test_loader = torch.utils.data.DataLoader(test_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=False,
drop_last=False )<find_best_params>
sub.to_csv('submission.csv', index=False )
<feature_engineering><EOS> 
sub.to_csv('submission.csv', index=False )