Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<feature_engineering>
!pip install pytorch_lightning
df1['Familysize'] = df1['SibSp'] + df1['Parch'] + 1
df1.loc[df1['Familysize'] == 1,'Familysize'] = 0
df1.loc[df1.Familysize >1,'Familysize'] = 1<drop_column>
%matplotlib inline
df1 = df1.drop(columns = ['Parch','SibSp'])
 df1.Fare = df1.Fare.fillna(0)
 df1.head()<predict_on_test>
torch.__version__
predictions = RFC.predict(np.asanyarray(df1))<save_to_csv>
base_dir = '.. /input/dogs-vs-cats-redux-kernels-edition'
train_dir = '.. /data/train'
test_dir = '.. /data/test'
df3 = pd.read_csv('.. /input/test.csv')
 submissions = pd.DataFrame({'PassengerID':df3['PassengerId'],'Survived':predictions})
 submissions.to_csv('submission.csv',index = False, header = True )<set_options>
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' )
config = tf.ConfigProto(device_count = {'GPU': 1 , 'CPU': 4})
 sess = tf.Session(config=config)
 keras.backend.set_session(sess)
 K.set_image_dim_ordering('tf' )<feature_engineering>
train_img_path = glob.glob(os.path.join(train_dir, '*.jpg'))
test_img_path = glob.glob(os.path.join(test_dir, '*.jpg'))
train_data = np.genfromtxt('.. /input/train.csv', delimiter=',')[1:]
train_X = train_data[:, 1:]
train_y_orig = train_data[:, :1]
train_y = np.zeros([train_y_orig.shape[0], 10])
 for ind in range(train_y_orig.shape[0]):
train_y[ind][int(train_y_orig[ind][0])] = 1<choose_model_class>
class ImageTransform() :
def __init__(self, img_size=224, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) :
self.data_transform = {
'train': transforms.Compose([
transforms.RandomResizedCrop(img_size, scale=(0.5, 1.0)) ,
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(img_size),
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ]),
'test': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(img_size),
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ])
 }
def __call__(self, img, phase):
return self.data_transform[phase](img )
model = Sequential()
input_shape =(28, 28, 1)
 model.add(Conv2D(32, kernel_size=(7, 7), padding='same', activation='relu', input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(7, 7), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size=(5, 5), strides=(2,2), padding='same', activation='relu'))
model.add(Conv2D(64, kernel_size=(5, 5), strides=(2,2), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size=(3, 3), strides=(3,3), padding='same', activation='relu'))
model.add(Conv2D(128, kernel_size=(3, 3), strides=(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.6))
model.add(Dense(10, activation='softmax'))
opt = RMSprop()
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'] )<train_model>
class DogvsCatDataset(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
batch_size=2**8
epochs = 100
n = train_X.shape[0]
train_X = train_X.reshape(( n, 28, 28, 1)).astype('float32')/ 255
history = model.fit(train_X, train_y,
epochs=epochs,
batch_size=batch_size )<compute_test_metric>
class DogCatModelSystem(pl.LightningModule):
def __init__(self, img_path, criterion, batch_size, img_size):
super(DogCatModelSystem, self ).__init__()
self.criterion = criterion
self.batch_size = batch_size
self.img_size = img_size
self.img_path = img_path
self.train_img_path, self.val_img_path = train_test_split(self.img_path, test_size=0.1)
 self.train_dataset = DogvsCatDataset(self.train_img_path,
ImageTransform(self.img_size),
phase='train')
 self.val_dataset = DogvsCatDataset(self.val_img_path,
ImageTransform(self.img_size),
phase='val')
 use_pretrained = True
self.net = models.vgg16(pretrained=use_pretrained)
 self.net.classifier[6] = nn.Linear(in_features=self.net.classifier[6].in_features, out_features=2)
 params_to_update = []
update_params_name = ['classifier.6.weight', 'classifier.6.bias']
for name, param in self.net.named_parameters() :
if name in update_params_name:
param.requires_grad = True
params_to_update.append(param)
 else:
param.requires_grad = False
self.optimizer = optim.SGD(params=params_to_update, lr=0.001, momentum=0.9)
 @pl.data_loader
def train_dataloader(self):

 return DataLoader(self.train_dataset, batch_size=self.batch_size,
shuffle=True, num_workers=4, pin_memory=True)
 @pl.data_loader
def val_dataloader(self):

 return DataLoader(self.val_dataset, batch_size=self.batch_size,
shuffle=False, num_workers=4, pin_memory=True)
 def forward(self, x):
return self.net(x)
 def configure_optimizers(self):
return [self.optimizer], []
def training_step(self, batch, batch_idx):

 imgs, labels = batch
preds = self.forward(imgs)
 loss = self.criterion(preds, labels)
 _, preds = torch.max(preds, 1)
 correct = torch.sum(preds == labels ).float() / preds.size(0)
 logs = {'train_loss': loss, 'train_correct': correct}
return {'loss': loss, 'log': logs, 'progress_bar': logs}
def validation_step(self, batch, batch_idx):

 imgs, labels = batch
preds = self.forward(imgs)
 loss = self.criterion(preds, labels)
 _, preds = torch.max(preds, 1)
 correct = torch.sum(preds == labels ).float() / preds.size(0)
 logs = {'val_loss': loss, 'val_correct': correct}
return {'val_loss': loss, 'val_correct': correct, 'log': logs, 'progress_bar': logs}
def validation_end(self, outputs):
avg_loss = torch.stack([x['val_loss'] for x in outputs] ).mean()
avg_correct = torch.stack([x['val_correct'] for x in outputs] ).mean()
logs = {'avg_val_loss': avg_loss, 'avg_val_correct': avg_correct}
torch.cuda.empty_cache()
return {'avg_val_loss': avg_loss, 'log': logs}
print(model.evaluate(train_X, train_y))<load_from_csv>
criterion = nn.CrossEntropyLoss()
batch_size = 32
img_size = 224
epoch = 2
model = DogCatModelSystem(train_img_path, criterion, batch_size, img_size)
 trainer = Trainer(
 max_nb_epochs=epoch, 
)
 trainer.fit(model )
test_data = np.genfromtxt('.. /input/test.csv', delimiter=',')[1:]<predict_on_test>
def prediction(test_img_path, model, img_size, device):
id_list = []
pred_list = []
with torch.no_grad() :
for path in tqdm(test_img_path):
img = Image.open(path)
 _id = int(path.split('/')[-1].split('.')[0])
 transform = ImageTransform(img_size)
 img = transform(img, phase='test')
 img = img.unsqueeze(0)
 img = img.to(device)
 model.eval()
outputs = model(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)
 return res
<save_to_csv><EOS> 
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 res = prediction(test_img_path, model, img_size, device )