Kaggle competition: Digit Recognizer
with open('preds.pickle', 'wb')as handle:
pickle.dump(preds, handle )<feature_engineering>
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.model_selection import train_test_split
tta, _ = learn.tta(dl=test_dl )<save_to_csv>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
with open('tta.pickle', 'wb')as handle:
pickle.dump(tta, handle )<prepare_output>
y = train["label"]
y = tf.keras.utils.to_categorical(y, num_classes=10)
 image_id = list(test.index)
 image_id = [i+1 for i in image_id]
train = train.drop("label", axis=1)
 train = train.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1 )
cls_prds = torch.argmax(preds, dim=-1)
 len(cls_prds), cls_prds<load_from_csv>
import keras, os
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPool2D, BatchNormalization, MaxPool2D
from keras.callbacks import ModelCheckpoint, EarlyStopping
sample_submission = pd.read_csv('.. /input/hpa-single-cell-image-classification/sample_submission.csv')
 sample_submission.head()<feature_engineering>
xtrain, xtest, ytrain, ytest = train_test_split(train, y, test_size=0.2)
 xtrain, xval, ytrain, yval = train_test_split(xtrain, ytrain, test_size=0.25 )
df['cls'] = cls_prds
df['pred'] = df[['cls', 'enc']].apply(lambda r: str(r[0])+ ' 1 ' + r[1], axis=1)
 df.head()<groupby>
class LRFinder:

 def __init__(self, model):
self.model = model
self.losses = []
self.lrs = []
self.best_loss = 1e9
def on_batch_end(self, batch, logs):
lr = K.get_value(self.model.optimizer.lr)
 self.lrs.append(lr)
 loss = logs['loss']
self.losses.append(loss)
 if math.isnan(loss)or loss > self.best_loss * 4:
self.model.stop_training = True
return
if loss < self.best_loss:
self.best_loss = loss
lr *= self.lr_mult
K.set_value(self.model.optimizer.lr, lr)
 def find(self, x_train, y_train, start_lr, end_lr, batch_size=64, epochs=1):
num_batches = epochs * x_train.shape[0] / batch_size
self.lr_mult =(end_lr / start_lr)**(1 / num_batches)
 self.model.save_weights('tmp.h5')
 original_lr = K.get_value(self.model.optimizer.lr)
 K.set_value(self.model.optimizer.lr, start_lr)
 callback = LambdaCallback(on_batch_end=lambda batch, logs: self.on_batch_end(batch, logs))
self.model.fit(x_train, y_train,
batch_size=batch_size, epochs=epochs,
callbacks=[callback])
 self.model.load_weights('tmp.h5')
 K.set_value(self.model.optimizer.lr, original_lr)
 def plot_loss(self, n_skip_beginning=10, n_skip_end=5):

 plt.ylabel("loss")
 plt.xlabel("learning rate(log scale)")
 plt.plot(self.lrs[n_skip_beginning:-n_skip_end], self.losses[n_skip_beginning:-n_skip_end])
 plt.xscale('log')
 def plot_loss_change(self, sma=1, n_skip_beginning=10, n_skip_end=5, y_lim=(-0.01, 0.01)) :

 assert sma >= 1
derivatives = [0] * sma
for i in range(sma, len(self.lrs)) :
derivative =(self.losses[i] - self.losses[i - sma])/ sma
derivatives.append(derivative)
 plt.ylabel("rate of loss change")
 plt.xlabel("learning rate(log scale)")
 plt.plot(self.lrs[n_skip_beginning:-n_skip_end], derivatives[n_skip_beginning:-n_skip_end])
 plt.xscale('log')
 plt.ylim(y_lim )
subm = df.groupby(['image_id'])['pred'].apply(lambda x: ' '.join(x)).reset_index()
subm.head()<merge>
def model() :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
adam = tf.keras.optimizers.Adam()
model.compile(optimizer=adam, loss="categorical_crossentropy", metrics=["accuracy"])
 early = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=50)
 checkpoint_path = 'training_1/cp.ckpt'
checkpoint_dir = os.path.dirname(checkpoint_path)
 cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
save_weights_only=True,
verbose=1)
 return model
sub = pd.merge(
 sample_submission,
subm,
how="left",
left_on='ID',
right_on='image_id',
)<count_missing_values>
def model() :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
adam = tf.keras.optimizers.Adam(
 learning_rate=0.001,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-07,
amsgrad=False)
 model.compile(optimizer=adam, loss="categorical_crossentropy", metrics=["accuracy"])
 early = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=50)
 checkpoint_path = 'training_1/cp.ckpt'
checkpoint_dir = os.path.dirname(checkpoint_path)
 cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
save_weights_only=True,
verbose=1)
 print(model.summary())
 history = model.fit(xtrain, ytrain, epochs=100, callbacks=[cp_callback, early], validation_data=(xval, yval))
prediction = model.predict(test)
 prediction = np.argmax(prediction, axis=1)
 return history, prediction
def isNaN(num):
return num != num<feature_engineering>
history, prediction = model()
for i, row in sub.iterrows() :
if isNaN(row['pred']): continue
sub.PredictionString.loc[i] = row['pred']<save_to_csv>
data = {"ImageId": image_id, "Label":prediction}
results = pd.DataFrame(data)
 results.to_csv("result.csv",index=False )
sub.to_csv('submission_1.csv', index=False )<load_from_csv>
from keras.preprocessing.image import ImageDataGenerator
cell_df = pd.read_csv('cell_df.csv')
 cell_df.head()
cell_df['cls'] = ''<feature_engineering>
datagen_train = datagen_valid = ImageDataGenerator(
 featurewise_center = False,
samplewise_center = False,
featurewise_std_normalization = False,
samplewise_std_normalization = False,
zca_whitening = False,
horizontal_flip = False,
vertical_flip = False,
fill_mode = 'nearest',
rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1)
 datagen_train.fit(xtrain)
 train_gen = datagen_train.flow(xtrain, ytrain, batch_size=64)
 datagen_valid.fit(xval)
 valid_gen = datagen_valid.flow(xval, yval, batch_size=64 )
threshold = 0.0
for i in range(preds.shape[0]):
p = torch.nonzero(preds[i] > threshold ).squeeze().numpy().tolist()
if type(p)!= list: p = [p]
if len(p)== 0: cls = [(preds[i].argmax().item() , preds[i].max().item())]
else: cls = [(x, preds[i][x].item())for x in p]
cell_df['cls'].loc[i] = cls<categorify>
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same', input_shape =(28,28,1)) ,
Conv2D(32, kernel_size=(3, 3), activation='relu'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.2),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(64, kernel_size=(3, 3), activation='relu'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.2),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(128, kernel_size=(3, 3), activation='relu'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.2),
Flatten() ,
Dense(512, activation='relu'),
Dropout(0.5),
Dense(10, activation = "softmax")
 ])
 adam = tf.keras.optimizers.Adam(
 learning_rate=0.001,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-07,
amsgrad=False)
 model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
 early = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=50)
 checkpoint_path = 'training_1/cp.ckpt'
checkpoint_dir = os.path.dirname(checkpoint_path)
 cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
save_weights_only=True,
verbose=1)
 print(model.summary())
 history = model.fit(( train_gen), epochs=100, callbacks=[cp_callback, early], validation_data=(valid_gen))
prediction = model.predict(test)
 prediction = np.argmax(prediction, axis=1 )
<feature_engineering><EOS> 
data = {"ImageId": image_id, "Label":prediction}
results = pd.DataFrame(data)
 results.to_csv("result_data_generator.csv",index=False )