Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
%matplotlib inline
sys.path.append(".. /input/segmentation-models-pytorch-install")
 !pip install -q --no-deps.. /input/deepflash2-lfs
warnings.filterwarnings("ignore" )<categorify>
training_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 submission_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
def rle_encode_less_memory(img):
pixels = img.T.flatten()
pixels[0] = 0
pixels[-1] = 0
runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def load_model_weights(model, file, strict=True):
state = torch.load(file, map_location='cpu')
 stats = state['stats']
model_state = state['model']
model.load_state_dict(model_state, strict=strict)
 return model, stats
def _get_gaussian(patch_size, sigma_scale=1./ 8)-> np.ndarray:
tmp = np.zeros(patch_size)
 center_coords = [i // 2 for i in patch_size]
sigmas = [i * sigma_scale for i in patch_size]
tmp[tuple(center_coords)] = 1
gaussian_importance_map = gaussian_filter(tmp, sigmas, 0, mode='constant', cval=0)
 gaussian_importance_map = gaussian_importance_map / np.max(gaussian_importance_map)* 1
gaussian_importance_map = gaussian_importance_map.astype(np.float32)
 gaussian_importance_map[gaussian_importance_map == 0] = np.min(
 gaussian_importance_map[gaussian_importance_map != 0])
 return gaussian_importance_map<train_model>
print("Training data(rows, columns):" + str(training_data.shape))
print("Submission data(rows, columns):" + str(submission_df.shape))
class HubmapDataset(Dataset):
'HubmapDataset class that does not load the full tiff files.'
def __init__(self, file, stats, scale=3, shift=.8, output_shape=(512,512), s_th = 40):
self.mean, self.std = stats
self.scale = scale
self.shift = shift
self.output_shape = output_shape
self.input_shape = tuple(int(t*scale)for t in self.output_shape)
 self.s_th = s_th
self.p_th = 1000*(self.output_shape[0]//256)**2
identity = rasterio.Affine(1, 0, 0, 0, 1, 0)
 self.data = rasterio.open(file, transform = identity, num_threads='all_cpus')
 if self.data.count != 3:
subdatasets = self.data.subdatasets
self.layers = []
if len(subdatasets)> 0:
for i, subdataset in enumerate(subdatasets, 0):
self.layers.append(rasterio.open(subdataset))
self.slices = []
self.out_slices = []
self.out_data_shape = tuple(int(x//self.scale)for x in self.data.shape)
 start_points = [o//2 for o in self.output_shape]
end_points = [(s - st)for s, st in zip(self.out_data_shape, start_points)]
n_points = [int(s//(o*self.shift)) +1 for s, o in zip(self.out_data_shape, self.output_shape)]
center_points = [np.linspace(st, e, num=n, endpoint=True, dtype=np.int64)for st, e, n in zip(start_points, end_points, n_points)]
for cx in center_points[1]:
for cy in center_points[0]:
slices = tuple(slice(int(( c*self.scale - o/2 ).clip(0, s)) , int(( c*self.scale + o/2 ).clip(max=s)))
 for(c, o, s)in zip(( cy, cx), self.input_shape, self.data.shape))
self.slices.append(slices)
 out_slices = tuple(slice(int(( c - o/2 ).clip(0, s)) , int(( c + o/2 ).clip(max=s)))
 for(c, o, s)in zip(( cy, cx), self.output_shape, self.out_data_shape))
self.out_slices.append(out_slices)
 def __len__(self):
return len(self.slices)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
slices = self.slices[idx]
if self.data.count == 3:
img = self.data.read([1, 2, 3],
window=Window.from_slices(*slices)
)
 img = np.moveaxis(img, 0, -1)
 else:
img = np.zeros(( *self.input_shape, 3), dtype=np.uint8)
 for fl in range(3):
img[:, :, fl] = self.layers[fl].read(
 window=Window.from_slices(*slices)
)
 if self.scale!=1:
img = cv2.resize(img, self.output_shape, interpolation = cv2.INTER_AREA)
 hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
 h,s,v = cv2.split(hsv)
 if(s>self.s_th ).sum() <= self.p_th or img.sum() <= self.p_th:
idx = -1
img =(img/255.0 - self.mean)/self.std
img = img.transpose(2, 0, 1 ).astype('float32')
 return torch.from_numpy(img), idx
class Model_pred:
'Class for prediction with multiple models'
def __init__(self, models, use_tta=True, batch_size=32):
self.models = models
self.bs = batch_size
self.tfms = [tta.HorizontalFlip() ] if use_tta else []
def predict(self, ds):
dl = DataLoader(ds, self.bs, num_workers=0, shuffle=False, pin_memory=True)
 pred = np.zeros(ds.out_data_shape, dtype='float32')
 merge_map = np.zeros(ds.out_data_shape, dtype='float32')
 gw_numpy = _get_gaussian(ds.output_shape)
 gw = torch.from_numpy(gw_numpy ).to(device)
 with torch.no_grad() :
for images, idxs in tqdm(iter(dl), total=len(dl)) :
if(( idxs>=0 ).sum() > 0):
images = images[idxs>=0].to(device)
 idxs = idxs[idxs>=0]
merger = tta.Merger()
for t in tta.Compose(self.tfms):
aug_images = t.augment_image(images)
 model_merger = tta.Merger()
for model in self.models:
out = model(aug_images)
 out = F.softmax(out, dim=1)
 model_merger.append(out)
 out = t.deaugment_mask(model_merger.result())
 merger.append(out)
 batch_smx = merger.result() *gw.view(1,1,*gw.shape)
 batch_smx = [x for x in batch_smx.permute(0,2,3,1 ).cpu().numpy() ]
for smx, idx in zip(batch_smx, idxs):
slcs = ds.out_slices[idx]
pred[slcs] += smx[...,1]
merge_map[slcs] += gw_numpy
pred /= merge_map
return pred<define_variables>
training_df = training_data.sample(frac=0.7, random_state=1)
 testing_df = training_data.drop(training_df.index)
 print("Training data(rows, columns):" + str(training_df.shape))
print("Testing data(rows, columns):" + str(testing_df.shape))
print(str(training_data.shape[0] -(training_df.shape[0] + testing_df.shape[0])) + " rows not accounted for")
class CONFIG() :
data_path = Path('.. /input/hubmap-kidney-segmentation')
 model_path = Path('.. /input/hubmap-efficient-sampling-deepflash2-train')
 scale = 3
shift = 0.8
tile_shape =(512, 512)
 encoder_name = "efficientnet-b4"
encoder_weights = None
in_channels = 3
classes = 2
batch_size = 32
tta = True
threshold = 0.5
cfg = CONFIG()<load_from_csv>
training_labels = training_df["label"].values
training_df = training_df.drop(columns=['label'] )
df_sample = pd.read_csv(cfg.data_path/'sample_submission.csv', index_col='id')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 MODELS = [f for f in cfg.model_path.iterdir() if f.suffix=='.pth']
print(f'Found {len(MODELS)} models', *MODELS)
 models = []
for i, m_path in enumerate(MODELS):
model = smp.Unet(encoder_name=cfg.encoder_name,
encoder_weights=cfg.encoder_weights,
in_channels=cfg.in_channels,
classes=cfg.classes)
 model, stats = load_model_weights(model, m_path)
 model.float()
model.eval()
model.to(device)
 models.append(model)
 models.append(model)
 mp = Model_pred(models, use_tta=cfg.tta, batch_size=cfg.batch_size )<save_to_csv>
print("Training data(rows, columns):" + str(training_df.shape))
print("Label Count: " + str(training_labels.size))
df = pd.DataFrame({'id':names,'predicted':preds})
 df.to_csv('submission.csv',index=False)
 df.head()<save_to_csv>
testing_labels = testing_df['label'].values
testing_df = testing_df.drop(columns=['label'] )
huang = pd.read_csv('.. /input/again-newmachine/submission(29 ).csv')
 huang.reset_index(drop = True)
 dfsample = pd.read_csv('.. /input/hubmap-kidney-segmentation/sample_submission.csv')
 mydict = dict(zip(huang['id'], huang['predicted']))
dfsample['predicted'] = dfsample['id'].map(mydict ).fillna(dfsample['predicted'])
 dfsample = dfsample.replace(np.nan, '', regex=True)
 dfsample.to_csv('submission.csv',index=False)
 dfsample.head()<import_modules>
testing_df /= 255
training_df /= 255
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator<load_from_csv>
training_values = training_df.values.reshape(training_df.shape[0], IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)
 testing_values = testing_df.values.reshape(testing_df.shape[0], IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS )
train_data = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test_data = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 print(train_data.shape,test_data.shape )<prepare_x_and_y>
NUM_CLASSES = 10
training_labels_categorical = tf.keras.utils.to_categorical(training_labels)
 testing_labels_categorical = tf.keras.utils.to_categorical(testing_labels)
 print(training_labels_categorical.shape)
 print(testing_labels_categorical.shape )
X = train_data.drop(["label"],axis = 1 ).values
Y = train_data["label"].values<prepare_x_and_y>
print("Tensorflow Version: " + tf.version.VERSION)
 print("Keras Version: " + tf.keras.__version__)
 EPOCHS=35
BATCH_SIZE=64
X = X.reshape([60000,28,28,1])
 Y = Y.reshape([60000,1] )<categorify>
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, 3, 3, activation='relu', input_shape=(IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS)))
 model.add(layers.Conv2D(32, 3, 3, activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
 model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dropout(0.50))
model.add(layers.Dense(10, activation="softmax"))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model.summary()
history = model.fit(training_values, training_labels_categorical, validation_split=0.1, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, verbose=1)
Y = to_categorical(Y, num_classes = 10 )<split>
score = model.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Model1 Score: " + str(score))
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.1, random_state = 14 )<feature_engineering>
model2 = tf.keras.Sequential()
model2.add(layers.Conv2D(32, kernel_size=5, padding='same', activation='relu', input_shape=(IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS)))
 model2.add(layers.MaxPool2D())
 model2.add(layers.Dropout(0.40))
model2.add(layers.Conv2D(64, kernel_size=5, activation='relu'))
model2.add(layers.MaxPool2D())
 model2.add(layers.Dropout(0.40))
model2.add(layers.Flatten())
 model2.add(layers.Dense(128, activation="relu"))
model2.add(layers.Dropout(0.40))
model2.add(layers.Dense(10, activation="softmax"))
model2.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model2.summary()
history = model2.fit(training_values, training_labels_categorical, validation_split=0.1, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, verbose=1 )
x_train = x_train/255
x_test = x_test/255<choose_model_class>
score = model2.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Model2 Score: " + str(score))
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), padding = 'same', activation='relu', input_shape=(28, 28, 1)) ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64,(3,3), padding = 'same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), padding = 'same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), padding = 'same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
 ] )<choose_model_class>
model3 = tf.keras.Sequential()
model3.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model3.add(layers.BatchNormalization())
 model3.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.MaxPool2D(pool_size=(2,2)))
 model3.add(layers.Dropout(0.25))
model3.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model3.add(layers.Dropout(0.25))
model3.add(layers.Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.Dropout(0.25))
model3.add(layers.Flatten())
 model3.add(layers.Dense(256, activation = "relu"))
model3.add(layers.BatchNormalization())
 model3.add(layers.Dropout(0.25))
model3.add(layers.Dense(10, activation = "softmax"))
model3.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model3.summary()
history = model3.fit(training_values, training_labels_categorical, validation_split=0.1, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, verbose=1 )
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
 model.compile(optimizer = optimizer,
loss = 'categorical_crossentropy',
metrics = ['accuracy'] )<choose_model_class>
score = model3.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Model3 Score: " + str(score))
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.6,
min_lr=0.00001 )<define_variables>
modelCheckPoint = ModelCheckpoint(filepath="weights.{epoch:02d}-{val_loss:.2f}.hdf5",
monitor="val_acc",
verbose=1,
save_best_only=True,
mode='max')
 datagen = ImageDataGenerator(
 rotation_range= 8,
zoom_range = 0.13,
width_shift_range=0.13,
height_shift_range=0.13)
 initial_learning_rate = 0.001
optimizer = Adam(lr=initial_learning_rate, decay= initial_learning_rate /(EPOCHS*1.3))
model4 = tf.keras.Sequential()
model4.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model4.add(layers.BatchNormalization())
 model4.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.MaxPool2D(pool_size=(2,2)))
 model4.add(layers.Dropout(0.25))
model4.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model4.add(layers.Dropout(0.25))
model4.add(layers.Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.Dropout(0.25))
model4.add(layers.Flatten())
 model4.add(layers.Dense(256, activation = "relu"))
model4.add(layers.BatchNormalization())
 model4.add(layers.Dropout(0.25))
model4.add(layers.Dense(10, activation = "softmax"))
model4.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model4.summary()
history = model4.fit_generator(datagen.flow(training_values, training_labels_categorical, batch_size=BATCH_SIZE), validation_data=(testing_values, testing_labels_categorical), steps_per_epoch=training_values.shape[0] // BATCH_SIZE, epochs=EPOCHS, verbose=1, callbacks=[modelCheckPoint] )
batch_size = 128
epochs = 50<train_model>
score = model4.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Score: " + str(score))
<train_model><EOS> 
submission_df /= 255
submission_values = submission_df.values.reshape(submission_df.shape[0], IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)
 predictions = model4.predict_classes(submission_values, verbose=0)
 submissions = pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("DR.csv", index=False, header=True )