Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
from fastai import *
from fastai.vision import *
import pandas as pd
epochs = 5
for epoch in range(epochs):
model.train()
epoch_train_loss = 0
for images, labels in tqdm(loader_train):
images = images.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
outputs = model(images)
 loss = criterion(outputs, labels)
 epoch_train_loss += loss.item()
loss.backward()
optimizer.step()
print(f'에폭 [{epoch+1}/{epochs}] - 훈련 데이터 손실값 : {epoch_train_loss/len(loader_train):.4f}')
 model.eval()
epoch_valid_loss = 0
preds_list = []
true_onehot_list = []
with torch.no_grad() :
for images, labels in loader_valid:
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = criterion(outputs, labels)
 epoch_valid_loss += loss.item()
preds = torch.softmax(outputs.cpu() , dim=1 ).numpy()
true_onehot = torch.eye(4)[labels].cpu().numpy()
preds_list.extend(preds)
 true_onehot_list.extend(true_onehot)
 print(f'에폭 [{epoch+1}/{epochs}] - 검증 데이터 손실값 : {epoch_valid_loss/len(loader_valid):.4f} / 검증 데이터 ROC AUC : {roc_auc_score(true_onehot_list, preds_list):.4f}' )<load_pretrained>
df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 df2 = pd.read_csv('.. /input/mnist-in-csv/mnist_train.csv')
 df2.columns = df.columns
df_temp = pd.concat([df,df2], join='outer',axis=0 ,ignore_index=False ,sort=False ).reset_index(drop=True)
 df_temp.to_csv('train.csv',index=False )
dataset_test = ImageDataset(test, img_dir=img_dir,
transform=transform_test, is_test=True)
 loader_test = DataLoader(dataset_test, batch_size=batch_size,
shuffle=False, worker_init_fn=seed_worker,
generator=g, num_workers=2 )<find_best_params>
df_temp.to_csv('train.csv',index=False )
model.eval()
preds = np.zeros(( len(test), 4))
with torch.no_grad() :
for i, images in enumerate(loader_test):
images = images.to(device)
 outputs = model(images)
 preds_part = torch.softmax(outputs.cpu() , dim=1 ).squeeze().numpy()
preds[i*batch_size:(i+1)*batch_size] += preds_part<save_to_csv>
class CustomImageItemList(ImageList):
def open(self, fn):
img = fn.reshape(28, 28)
 img = np.stack(( img,)*3, axis=-1)
 return Image(pil2tensor(img, dtype=np.float32))
@classmethod
def from_csv_custom(cls, path:PathOrStr, csv_name:str, imgIdx:int=1, header:str='infer', **kwargs)-> 'ItemList':
df = pd.read_csv(Path(path)/csv_name, header=header)
 res = super().from_df(df, path=path, cols=0, **kwargs)
 res.items = df.iloc[:,imgIdx:].apply(lambda x: x.values / 783.0, axis=1 ).values
return res
submission[['healthy', 'multiple_diseases', 'rust', 'scab']] = preds
submission.to_csv('submission.csv', index=False )<install_modules>
test = CustomImageItemList.from_csv_custom(path=path, csv_name='test.csv', imgIdx=0)
 data =(CustomImageItemList.from_csv_custom(path='.', csv_name='train.csv')
.split_by_rand_pct (.2)
.label_from_df(cols='label')
.add_test(test, label=0)
.transform(get_transforms(do_flip = False, max_rotate = 0.) , size=49)
.databunch(bs=1024, num_workers=16)
.normalize(mnist_stats))
data
!pip install -q efficientnet<import_modules>
data.show_batch(rows=3, figsize=(12,9))
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.layers as L
from sklearn import metrics
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
from tensorflow.keras.applications.densenet import DenseNet121, DenseNet169, DenseNet201
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2
from tensorflow.keras.applications.nasnet import NASNetLarge
from efficientnet.tfkeras import EfficientNetB7, EfficientNetL2
from tensorflow.keras.layers import Flatten,Dense,Dropout,BatchNormalization
from tensorflow.keras.models import Model,Sequential
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint<train_on_grid>
arch = models.resnet50
arch
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 50
BATCH_SIZE = 8 * strategy.num_replicas_in_sync<define_variables>
learn = cnn_learner(data, arch,pretrained = False, metrics=[error_rate, accuracy], model_dir='.. /kaggle/working')
def format_path(st):
return GCS_DS_PATH + '/images/' + st + '.jpg'<load_from_csv>
lr = 1e-02
train = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
 test = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 sub = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 train_paths = train.image_id.apply(format_path ).values
test_paths = test.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values
train_paths, valid_paths, train_labels, valid_labels = train_test_split(
 train_paths, train_labels, test_size=0.06, random_state=2020)
 train_paths = train.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values<normalization>
learn.fit_one_cycle(15, lr)
 learn.save('stage-1-50' )
img_size = 768
def decode_image(filename, label=None, image_size=(img_size, img_size)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None, seed=2020):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 if label is None:
return image
else:
return image, label<categorify>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( valid_paths, valid_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )<init_hyperparams>
tmp_df = pd.read_csv(path+'sample_submission.csv')
 tmp_df.head()
LR_START = 0.00001
LR_MAX = 0.0001 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))<choose_model_class>
for i in range(28000):
img = learn.data.test_ds[i][0]
tmp_array[i,1] = int(learn.predict(img)[1] )
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(train_labels.shape[1], activation="softmax" )(x)
 return Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model = get_model(EfficientNetB7)
 model.compile(optimizer='nadam', loss='categorical_crossentropy',metrics=['categorical_accuracy'])
 model.summary()<train_model>
tmp_df = pd.DataFrame(tmp_array,columns = ['ImageId','Label'])
 tmp_df
history = model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback, ModelCheckpoint(filepath='pretrained_EfficientNetB7.h5', monitor='val_loss', save_best_only=True)],
validation_data=valid_dataset, epochs=EPOCHS )<load_pretrained>
tmp_df.to_csv('submission.csv',index=False )
<save_to_csv><EOS> 
mnist_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-in-csv/mnist_train.csv" )