Kaggle competition: Digit Recognizer
comp_df.breed.value_counts()<categorify>
%matplotlib inline
np.random.seed(2 )
comp_df['label'] = LabelEncoder().fit_transform(comp_df.breed)
 dict_df = comp_df[['label','breed']].copy()
dict_df.drop_duplicates(inplace=True)
 dict_df.set_index('label',drop=True,inplace=True)
 index_to_breed = dict_df.to_dict() ['breed']<feature_engineering>
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
train_dir = '.. /input/dog-breed-identification/train'
comp_df.id = comp_df.id.apply(lambda x: x+'.jpg')
 comp_df.id = comp_df.id.apply(lambda x:train_dir+'/'+x)
 comp_df.pop('breed' )<prepare_x_and_y>
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
class img_dataset(Dataset):
def __init__(self,dataframe,transform=None,test=False):
self.dataframe = dataframe
self.transform = transform
self.test = test
def __getitem__(self,index):
x = Image.open(self.dataframe.iloc[index,0])
 if self.transform:
x = self.transform(x)
 if self.test:
return x
else:
y = self.dataframe.iloc[index,1]
return x,y
def __len__(self):
return self.dataframe.shape[0]<define_search_model>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
train_transformer = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomRotation(15),
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 val_transformer = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])] )<train_model>
X_train /= 255.0
test /= 255.0
def print_epoch_result(train_loss,train_acc,val_loss,val_acc):
print('loss: {:.3f}, acc: {:.3f}, val_loss: {:.3f}, val_acc: {:.3f}'.format(train_loss,
train_acc,
val_loss,
val_acc))
def train_model(model, cost_function, optimizer,num_epochs=5):
train_losses = []
val_losses = []
train_acc = []
val_acc = []
train_acc_object = metrics.Accuracy(compute_on_step=False)
 val_acc_object = metrics.Accuracy(compute_on_step=False)
 for epoch in range(num_epochs):

 print('-'*15)
 print('Start training {}/{}'.format(epoch+1, num_epochs))
print('-'*15)
 train_sub_losses = []
model.train()
for x,y in train_loader:
optimizer.zero_grad()
x,y = x.to(device),y.to(device)
 y_hat = model(x)
 loss = cost_function(y_hat,y)
 loss.backward()
optimizer.step()
train_sub_losses.append(loss.item())
 train_acc_object(y_hat.cpu() ,y.cpu())
 val_sub_losses = []
model.eval()
for x,y in val_loader:
x,y = x.to(device),y.to(device)
 y_hat = model(x)
 loss = cost_function(y_hat,y)
 val_sub_losses.append(loss.item())
 val_acc_object(y_hat.cpu() ,y.cpu())

train_losses.append(np.mean(train_sub_losses))
val_losses.append(np.mean(val_sub_losses))
train_epoch_acc = train_acc_object.compute()
val_epoch_acc = val_acc_object.compute()
train_acc.append(train_epoch_acc)
 val_acc.append(val_epoch_acc)
 train_acc_object.reset()
val_acc_object.reset()
print_epoch_result(np.mean(train_sub_losses),train_epoch_acc,np.mean(val_sub_losses),val_epoch_acc)
 print('Finish Training.')
 return train_losses, train_acc, val_losses, val_acc<set_options>
Y_train = to_categorical(Y_train, num_classes = 10 )
device = torch.device('cuda:0' if torch.cuda.is_available else 'cpu' )<split>
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
training_samples = comp_df.shape[0]
test_size=0.05
batch_size = 64
sample_df = comp_df.sample(training_samples)
 x_train,x_val,_,_ = train_test_split(sample_df,sample_df,test_size=test_size)
 train_set = img_dataset(x_train, transform=train_transformer)
 val_set = img_dataset(x_val, transform=val_transformer)
 train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
 val_loader = DataLoader(val_set , batch_size=batch_size, shuffle=True)
 print('Training set: {}, Validation set: {}'.format(x_train.shape[0],x_val.shape[0]))<choose_model_class>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Valid', activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(512, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-3), metrics=["accuracy"])
 annealer = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.5, min_lr=0.00001)
 epochs = 50
batch_size = 64
class net(torch.nn.Module):
def __init__(self, base_model, base_out_features, num_classes):
super(net,self ).__init__()
self.base_model=base_model
self.linear1 = torch.nn.Linear(base_out_features, 512)
 self.output = torch.nn.Linear(512,num_classes)
 def forward(self,x):
x = F.relu(self.base_model(x))
x = F.relu(self.linear1(x))
x = self.output(x)
 return x
res = torchvision.models.resnet50(pretrained=True)
 for param in res.parameters() :
param.requires_grad=False
model_final = net(base_model=res, base_out_features=res.fc.out_features, num_classes=120)
 model_final = model_final.to(device )<choose_model_class>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.5, min_lr=0.00001 )
cost_function = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam([param for param in model_final.parameters() if param.requires_grad], lr=0.0003)
 EPOCHS = 30<train_model>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )
train_losses, train_acc, val_losses, val_acc = train_model(model=model_final,
cost_function=cost_function,
optimizer=optimizer,
num_epochs=EPOCHS )<load_from_csv>
datagen.fit(X_train )
test_dir = '.. /input/dog-breed-identification/test'
test_df = test_df[['id']]
test_df.id = test_df.id.apply(lambda x: x+'.jpg')
 test_df.id = test_df.id.apply(lambda x : test_dir+'/'+x)
 test_set = img_dataset(test_df,transform=val_transformer, test=True)
 test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False )<predict_on_test>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
model_final.eval()
predictions = torch.tensor([])
 print('Start predicting.... ')
 for x in test_loader:
x = x.to(device)
 y_hat = model_final(x)
 predictions = torch.cat([predictions, y_hat.cpu() ])
 print('Finish prediction.' )<predict_on_test>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<save_to_csv><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("sub2.csv",index=False)