Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<load_from_csv>
%matplotlib inline
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
torch.__version__
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<categorify>
base_dir = '.. /input/dogs-vs-cats-redux-kernels-edition'
train_dir = '.. /data/train'
test_dir = '.. /data/test'
train_drop = train.copy()
train_drop = train_drop.dropna(subset = ['Age'])
 def encodeSex(sex):
if sex == "male": return 0
return 1
def encodeAge(age):
return int(age/5)
 train_drop.Sex = train_drop.Sex.apply(encodeSex)
 train_drop.Age = train_drop.Age.apply(encodeAge )<define_variables>
with zipfile.ZipFile(os.path.join(base_dir, 'train.zip')) as train_zip:
train_zip.extractall('.. /data')
 with zipfile.ZipFile(os.path.join(base_dir, 'test.zip')) as test_zip:
test_zip.extractall('.. /data' )
features = ['Pclass','Sex', 'Age', 'Fare']<train_model>
train_list = glob.glob(os.path.join(train_dir, '*.jpg'))
test_list = glob.glob(os.path.join(test_dir, '*.jpg'))
X = train_drop[features]
y = train_drop.Survived
train_X, test_X, train_y, test_y = train_test_split(X, y, random_state=1)
 forest = RandomForestClassifier(max_leaf_nodes = 55)
 forest.fit(train_X, train_y)
 print("Feature importance: ", forest.feature_importances_)
 print("Accuracy: ", forest.score(test_X, test_y))<train_model>
len(train_list )
test_features = ["Title", "SibSp", "Parch", "Pclass", "Fare"]
X = train_with_title[test_features]
y = train_with_title.Age
train_X, test_X, train_y, test_y = train_test_split(X, y, random_state=1)
 line.fit(train_X, train_y)
 print("Accuracy: ", line.score(test_X, test_y))<predict_on_test>
train_list, val_list = train_test_split(train_list, test_size=0.1 )
predicted_ages = train[train['Age'].isnull() ]
predicted_ages['Title'] = predicted_ages.Name.apply(extractTitle)
 predicted_ages['Title'] = predicted_ages.Title.apply(encodeTitle)
 predictions = pd.Series(line.predict(predicted_ages[test_features]))
train_with_ages = train
inc = 0
for i in range(0, len(train['Age'])) :
if math.isnan(train['Age'][i]):
train_with_ages['Age'][i] = predictions[inc]
inc += 1<train_model>
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 )
train_with_ages.Sex = train_with_ages.Sex.apply(encodeSex)
 train_with_ages.Age = train_with_ages.Age.apply(encodeAge)
 features = ['Pclass','Sex', 'Age', 'Fare']
X = train_with_ages[features]
y = train_with_ages.Survived
train_X, test_X, train_y, test_y = train_test_split(X, y, random_state=1)
 forest = RandomForestClassifier(max_leaf_nodes = 55)
 forest.fit(train_X, train_y)
 print("Feature importance: ", forest.feature_importances_)
 print("Accuracy: ", forest.score(test_X, test_y))<feature_engineering>
class DogvsCatDataset(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):
return len(self.file_list)
 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
predicted_ages = test[test['Age'].isnull() ]
predicted_ages['Title'] = predicted_ages.Name.apply(extractTitle)
 predicted_ages['Title'] = predicted_ages.Title.apply(encodeTitle)
 predictions = pd.Series(line.predict(predicted_ages[test_features]))
inc = 0
for i in range(0, len(test['Age'])) :
if math.isnan(test['Age'][i]):
test['Age'][i] = predictions[inc]
inc += 1
test.Sex = test.Sex.apply(encodeSex)
 test.Age = test.Age.apply(encodeAge)
 test['Fare'] = test['Fare'].fillna(value = 13)
 features = ['Pclass','Sex', 'Age', 'Fare']
X = test[features]
final_predict = forest.predict(X)
 prediction = pd.DataFrame(test.PassengerId)
 prediction['Survived'] = final_predict.astype('int')
 prediction.to_csv('predict.csv',index = False )<set_options>
size = 224
mean =(0.485, 0.456, 0.406)
 std =(0.229, 0.224, 0.225)
 batch_size = 32
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
%matplotlib inline
warnings.filterwarnings("ignore" )<load_from_csv>
train_dataset = DogvsCatDataset(train_list, transform=ImageTransform(size, mean, std), phase='train')
 val_dataset = DogvsCatDataset(val_list, transform=ImageTransform(size, mean, std), phase='val')
 print('Operation Check')
 index = 0
print(train_dataset.__getitem__(index)[0].size())
 print(train_dataset.__getitem__(index)[1] )
df_titanic_train = pd.read_csv('.. /input/train.csv')
 df_titanic_test = pd.read_csv('.. /input/test.csv')
 PassengerId = df_titanic_test["PassengerId"]<drop_column>
train_dataloader = data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
 val_dataloader = data.DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
 dataloader_dict = {'train': train_dataloader, 'val': val_dataloader}
print('Operation Check')
 batch_iterator = iter(train_dataloader)
 inputs, label = next(batch_iterator)
 print(inputs.size())
 print(label )
df_titanic_train = df_titanic_train.drop(outliers_to_drop, axis = 0 ).reset_index(drop=True )<concatenate>
use_pretrained = True
net = models.vgg16(pretrained=use_pretrained)
 print(net )
train_size = len(df_titanic_train)
 df_titanic = pd.concat(objs=[df_titanic_train, df_titanic_test], axis=0 ).reset_index(drop=True)
 df_titanic.head()<count_missing_values>
net.classifier[6] = nn.Linear(in_features=4096, out_features=2)
 print('Done' )
df_titanic = df_titanic.fillna(np.nan)
 df_titanic.isnull().sum()<count_values>
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)
 print(name)
 else:
param.requires_grad = False
def absolute_relative_freq(variable):
absolute_frequency = variable.value_counts()
relative_frequency = round(variable.value_counts(normalize = True)*100, 2)
 df = pd.DataFrame({'Absolute Frequency':absolute_frequency, 'Relative Frequency(%)':relative_frequency})
 print('Absolute and Relative Frequency of [',variable.name,']')
 display(df )<feature_engineering>
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(params=params_to_update, lr=0.001, momentum=0.9 )
df_titanic['Fare'] = df_titanic['Fare'].fillna(df_titanic['Fare'].median() )<feature_engineering>
def train_model(net, dataloader_dict, criterion, optimizer, num_epoch):
since = time.time()
best_model_wts = copy.deepcopy(net.state_dict())
 best_acc = 0.0
net = net.to(device)
 for epoch in range(num_epoch):
print('Epoch {}/{}'.format(epoch + 1, num_epoch))
print('-'*20)
 for phase in ['train', 'val']:
if phase == 'train':
net.train()
else:
net.eval()
epoch_loss = 0.0
epoch_corrects = 0
for inputs, labels in tqdm(dataloader_dict[phase]):
inputs = inputs.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = net(inputs)
 _, preds = torch.max(outputs, 1)
 loss = criterion(outputs, labels)
 if phase == 'train':
loss.backward()
optimizer.step()
epoch_loss += loss.item() * inputs.size(0)
 epoch_corrects += torch.sum(preds == labels.data)
 epoch_loss = epoch_loss / len(dataloader_dict[phase].dataset)
 epoch_acc = epoch_corrects.double() / len(dataloader_dict[phase].dataset)
 print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(net.state_dict())
 time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
 time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
net.load_state_dict(best_model_wts)
 return net
df_titanic["Fare"] = df_titanic["Fare"].map(lambda i: np.log(i)if i > 0 else 0 )<count_values>
num_epoch = 2
net = train_model(net, dataloader_dict, criterion, optimizer, num_epoch )
<categorify><EOS> 
id_list = []
pred_list = []
with torch.no_grad() :
for test_path in tqdm(test_list):
img = Image.open(test_path)
 _id = int(test_path.split('/')[-1].split('.')[0])
 transform = ImageTransform(size, mean, std)
 img = transform(img, phase='val')
 img = img.unsqueeze(0)
 img = img.to(device)
 net.eval()
outputs = net(img)
 preds = F.softmax(outputs, dim=1)[:, 1].tolist()
id_list.append(_id)
 pred_list.append(preds[0])
 res = pd.DataFrame({
'id': id_list,
'label': pred_list
})
 res.sort_values(by='id', inplace=True)
 res.reset_index(drop=True, inplace=True)
 res.to_csv('submission.csv', index=False )