Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<load_from_csv>
device = "cuda:0" if torch.cuda.is_available() else 'cpu'
def open_data(file):
data = pd.read_csv(".. /input/"+file)
 data = data.drop(["Name", "Ticket", "Cabin"], 1)
 le = preprocessing.LabelEncoder()
data["Sex"] = le.fit_transform(list(data["Sex"]))
data["Embarked"] = le.fit_transform(list(data["Embarked"]))
data["Age"] = data["Age"].fillna(value = data.Age.mean())
 data["Fare"] = data["Fare"].fillna(value = data.Fare.mean())
 return data<drop_column>
os.makedirs('.. /data', exist_ok=True)
 base_dir = '.. /input/dogs-vs-cats-redux-kernels-edition'
train_dir = '.. /data/train'
test_dir = '.. /data/test'
train_zip = zp.ZipFile(os.path.join(base_dir, 'train.zip'))
train_zip.extractall('.. /data')
 test_zip = zp.ZipFile(os.path.join(base_dir, 'test.zip'))
test_zip.extractall('.. /data' )
def param_label(data):
data = data.drop(["PassengerId"], 1)
 return data.drop(["Survived"], 1), data[["Survived"]]<split>
train_list = glob.glob(os.path.join(train_dir, '*.jpg'))
test_list = glob.glob(os.path.join(test_dir, '*.jpg'))
print(train_list[:5] )
def subset_data(X, Y, n):
return model_selection.train_test_split(X, Y, test_size = n )<compute_test_metric>
train_list, val_list = train_test_split(train_list, test_size=0.2 )
data = open_data("train.csv")
 y_true = data[["Survived"]]
y_test = np.array([1 for i in range(len(y_true)) ])
 print("Accuracy for survived = 1: ",metrics.accuracy_score(y_true, y_test))<compute_test_metric>
size = 224
mean =(0.485, 0.456, 0.406)
 std =(0.229, 0.224, 0.225 )
data = open_data("train.csv")
 y_true = data[["Survived"]]
y_test = np.array([random.choice(( 0, 1)) for i in range(len(y_true)) ])
 print("Accuracy for random survival: ",metrics.accuracy_score(y_true, y_test))<compute_test_metric>
class ImageTransform() :
def __init__(self, resize, mean, std):
self.data_transform = {
'train': transforms.Compose([
transforms.RandomResizedCrop(resize, scale=(0.5, 1.0)) ,
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(resize),
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ])
 }
def __call__(self, img, phase):
return self.data_transform[phase](img )
data = open_data("train.csv")
 y_true = data[["Survived"]]
y_test = np.array([0 for i in range(len(y_true)) ])
 print("Accuracy for survived = 1: ",metrics.accuracy_score(y_true, y_test))<save_to_csv>
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, file_list, transform=None, phase='train'):
self.file_list = file_list
self.transform = transform
self.phase = phase
def __len__(self):
self.filelength = len(self.file_list)
 return self.filelength
def __getitem__(self, idx):
img_path = self.file_list[idx]
img = Image.open(img_path)
 img_transformed = self.transform(img, self.phase)
 label = img_path.split('/')[-1].split('.')[0]
if label == 'dog':
label = 1
elif label == 'cat':
label = 0
return img_transformed, label
data_test = open_data("test.csv")
 solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], 0] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_naive.csv", index=False )<import_modules>
batch_size = 32
learning_rate = 0.001
epochs = 2
from sklearn.neighbors import KNeighborsClassifier<import_modules>
train_data = ImageDataset(train_list, transform=ImageTransform(size, mean, std), phase='train')
 val_data = ImageDataset(val_list, transform=ImageTransform(size, mean, std), phase='val')
 test_data = ImageDataset(test_list, transform=ImageTransform(size, mean, std), phase='val' )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
train_loader = torch.utils.data.DataLoader(dataset = train_data, batch_size=batch_size, shuffle=True)
 val_loader = torch.utils.data.DataLoader(dataset = val_data, batch_size=batch_size, shuffle=True)
 test_loader = torch.utils.data.DataLoader(dataset = test_data, batch_size=batch_size, shuffle=False )
from sklearn.neighbors import KNeighborsClassifier<split>
loss_function = F.cross_entropy
data = open_data("train.csv")
 X, Y = param_label(data)
 x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2 )<compute_train_metric>
def train(model: nn.Module, loss_function: F, params_to_update):
optimizer = torch.optim.SGD(params=params_to_update, lr=learning_rate, momentum=0.9)
 pbar=tqdm(total=len(train_loader))
model.train()
for epoch in range(epochs):
pbar.set_description('Epoch %d/%d' %(epoch + 1, epochs))
pbar.reset()
epoch_loss = 0
epoch_accuracy = 0
for data, labels in train_loader:
data = data.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
preds = model(data)
 error = loss_function(preds, labels)
 error.backward()
optimizer.step()
epoch_accuracy +=(( preds.argmax(dim=1)== labels ).float().mean())
 epoch_loss += error.item()
pbar.update()
epoch_accuracy /= len(train_loader)
 epoch_loss /= len(train_loader)
 print('Epoch %d finished with acc: %g, loss: %g' %(epoch+1, epoch_accuracy, epoch_loss))
pbar.close()
model = KNeighborsClassifier()
model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))<find_best_model_class>
use_pretrained = True
net = models.vgg16(pretrained=use_pretrained )
neighboors = [i for i in range(1, 101)]
averages = []
mins = []
maxs = []
for n in neighboors:
average_acc = 0
min_acc = 1
max_acc = 0
for i in range(100):
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = KNeighborsClassifier(n_neighbors = n)
 model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 average_acc = average_acc + acc
if acc > max_acc: max_acc = acc
if acc < min_acc: min_acc = acc
averages = averages + [average_acc/100]
mins = mins + [min_acc]
maxs = maxs + [max_acc]
plt.figure(figsize=(24,8))
plt.plot(averages, color = 'r', linewidth=2)
 plt.plot(mins, color = 'r', linestyle='--')
 plt.plot(maxs, color = 'r', linestyle='--')
 plt.xticks(neighboors )<save_to_csv>
net.classifier[6] = nn.Linear(in_features=4096, out_features=2 )
model = KNeighborsClassifier(n_neighbors = 12)
 model.fit(X, Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test.drop(["PassengerId"], 1))
solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_KNN_allfeatures.csv", index=False )<find_best_model_class>
params_to_update = []
update_params_name = ['classifier.6.weight', 'classifier.6.bias']
for name, param in net.named_parameters() :
if name in update_params_name:
param.requires_grad = True
params_to_update.append(param)
 else:
param.requires_grad = False
data = open_data("train.csv")
 X, Y = param_label(data)
 X = X[["Pclass", "Sex"]]
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = KNeighborsClassifier()
model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))<compute_train_metric>
net = net.to(device)
 train(net, loss_function, params_to_update )
model = KNeighborsClassifier(n_neighbors = 8)
 model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))<save_to_csv>
def evaluate_model(model: nn.Module, dataset: torch.utils.data.DataLoader, loss_function: F):
loss = 0
acc = 0
model.eval()
for data, labels in tqdm(dataset):
data = data.to(device)
 labels = labels.to(device)
 with torch.no_grad() :
preds = model(data)
 loss += loss_function(preds, labels ).item()
acc +=(( preds.argmax(dim=1)== labels ).float().mean())
 return acc / len(dataset), loss / len(dataset)
model = KNeighborsClassifier(n_neighbors = 8)
 model.fit(X[["Pclass", "Sex"]], Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test[["Pclass", "Sex"]])
 solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_KNN_Class_Sex.csv", index=False )<find_best_model_class>
acc_train, loss_train = evaluate_model(net, train_loader, loss_function)
 print('Training set: ')
 print('Loss: %g, Accuracy: %g' %(loss_train, acc_train))
print('')
 acc_val, loss_val = evaluate_model(net, val_loader, loss_function)
 print('Validation set: ')
 print('Loss: %g, Accuracy: %g' %(loss_val, acc_val))
<save_to_csv><EOS> 
res.to_csv('submission.csv', index=False )