Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_test_metric>
import torch
import torchvision
from torch import nn
from torchvision.transforms import transforms
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.model_selection import train_test_split
from torch import nn
from torch import optim
import torch.nn.functional as F
import numpy as np
from torch.utils.data import DataLoader, Dataset
from torchvision import datasets, transforms, models
from torchvision.datasets import ImageFolder
from torch.autograd import Variable
report=classification_report(y_test,pred)
 print("Decision Tree report
",report )<train_model>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 print(device )
rfc=ensemble.RandomForestClassifier(max_depth=6,random_state=0,n_estimators=64)
 rfc.fit(X_train, y_train)
 pred_train = rfc.predict(X_train)
 pred=rfc.predict(X_test)
 pred_train_df=pd.DataFrame({"Actual":y_train,"Pred":pred_train})
 pred_df=pd.DataFrame({"Actual":y_test,"Pred":pred})
 cm=confusion_matrix(y_test,pred)
 print(cm)
 report=classification_report(y_test,pred)
 print("Random Forest report
",report )<save_to_csv>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
y_test_rfc = rfc.predict(test ).astype(int)
 test_out = pd.concat([test_copy['PassengerId'],pd.Series(y_test_rfc,name="Survived")],axis=1)
 test_out['Survived'] = test_out['Survived'].astype('int')
 test_out.to_csv('submission.csv',index=False )<install_modules>
x = train_df.iloc[:,1:].values
y = train_df.iloc[:,0].values
z = test_df.iloc[::].values
print("X shape : {}".format(x.shape))
print("Y shape : {}".format(y.shape))
print("Z shape : {}".format(z.shape))
%env SM_FRAMEWORK=tf.keras
!pip install.. /input/segmentation-models-keras/Keras_Applications-1.0.8-py3-none-any.whl --quiet
!pip install.. /input/segmentation-models-keras/image_classifiers-1.0.0-py3-none-any.whl --quiet
!pip install.. /input/segmentation-models-keras/efficientnet-1.0.0-py3-none-any.whl --quiet
!pip install.. /input/segmentation-models-keras/segmentation_models-1.0.1-py3-none-any.whl --quiet
print("Segmentation Models installed." )<define_variables>
class Dataset(Dataset):
def __init__(self, data):
self.data = data
self.n_samples = data.shape[0]
self.x_data = torch.tensor(data.iloc[::].values, dtype=torch.long)
 def __getitem__(self, index):
return self.x_data[index].reshape(28,28)
 def __len__(self):
return self.n_samples
def get_long_type(self, target):
target = target.type(torch.LongTensor)
 return target
DEBUG = False<import_modules>
input_size = 784
hidden_size = 100
num_classes = 10
num_epochs = 2
batch_size = 100
learning_rate = 0.001
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.Compose(
 [transforms.ToTensor() , transforms.Normalize(( 0.1307,),(0.3081,)) ]), download=True)
 test_dataset = Dataset(test_df)
 train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
 test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
print(tf.__version__ )<define_variables>
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self ).__init__()
self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=0)
 self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2)
 self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
 self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2)
 self.fc1 = nn.Linear(32 * 4 * 4, 10)
 def forward(self, x):
out = self.cnn1(x)
 out = self.relu1(out)
 out = self.maxpool1(out)
 out = self.cnn2(out)
 out = self.relu2(out)
 out = self.maxpool2(out)
 out = out.view(out.size(0), -1)
 out = self.fc1(out)
 return out
n_total_steps = len(train_loader)
 batch_size = 100
num_epochs = 10
model = CNNModel()
if torch.cuda.is_available() :
model.cuda()
criterion = nn.CrossEntropyLoss()
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters() , lr=learning_rate )
data_dir = '.. /input/ranzcr-clip-catheter-line-classification'
model_dir = '.. /input/ranzcr-1st-place-solution-by-tf-models'
seg_image_size = 1024
cls_image_size = 512
batch_size = 16<load_from_csv>
for epoch in range(num_epochs):
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 train = Variable(images.view(100,1,28,28)).to(device)
 labels = labels.to(device)
 outputs = model(train)
 loss = criterion(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i + 1)% 100 == 0:
print(f'Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{n_total_steps}], Loss: {loss.item() :.4f}' )
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv')) 
<define_variables>
def evalueate_model(model, test_loader):
predictions = []
model.eval()
for images in test_loader:
images = images.to(device)
 train = Variable(images.view(100,1,28,28 ).type(torch.float32)).to(device)
 with torch.no_grad() :
predicts = model(train)
 predicts = predicts.argmax(axis=1)
 predicts = predicts.cpu().numpy()
for pred in predicts:
predictions.append(pred)
 return(predictions )
seg_model_names = [
'seg_model_V10_0.hdf5'
]
cls_model_names = [
'cls_model_V14_0.hdf5',
'cls_model_V15_1.hdf5',
'cls_model_V15_2.hdf5',
'cls_model_V16_3.hdf5',
'cls_model_V16_4.hdf5'
]<sort_values>
pred = evalueate_model(model, test_loader )
tfrec_path = data_dir + '/test_tfrecords/*.tfrec'
tfrec_file_names = sorted(tf.io.gfile.glob(tfrec_path))
tfrec_file_names = \
[ tfrec_file_names[0] ] if DEBUG else tfrec_file_names
tfrec_file_names<split>
submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submission['Label'] = pred
submission.head(25 )
AUTOTUNE = tf.data.experimental.AUTOTUNE
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 return image
def read_tfrecord(example):
TFREC_FORMAT = {
'image': tf.io.FixedLenFeature([], tf.string),
'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, TFREC_FORMAT)
 image = decode_image(example['image'])
 study_inst_id = example['StudyInstanceUID']
return image, study_inst_id
def load_dataset(filenames):
ds = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTOTUNE)
 ds = ds.map(read_tfrecord, num_parallel_calls=AUTOTUNE)
 return ds<create_dataframe>
submission.to_csv('submission.csv', index=False )
raw_test_ds = load_dataset(tfrec_file_names)
 raw_test_ds<categorify>
submission.to_csv('submission.csv', index=False )