Kaggle competition: Digit Recognizer
print('Original tweet:')
 print(tweets[0])
 print('Encoded tweet:')
 print(temp['input_ids'][0])
 print('Decoded tweet:')
 print(roberta_tokenizer.decode(temp['input_ids'][0]))<string_transform>
train= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test= pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
all_tweets = list(pd.concat([df,df_test],axis=0)['text'])
 all_tweets = process_tweets(all_tweets)
 max_len = max([len(t)for t in roberta_tokenizer(all_tweets)['input_ids']])
 print(max_len )<split>
Ytrain= train['label'].astype('float32')
X_train, X_test, y_train, y_test = train_test_split(tweets,y,test_size=0.30)
 X_train = roberta_tokenizer(X_train,padding='max_length',max_length=max_len,return_tensors='tf')
 X_test = roberta_tokenizer(X_test,padding='max_length',max_length=max_len,return_tensors='tf' )<define_variables>
train= train.drop('label',axis=1 )
batch_size = 8
train_dataset = tf.data.Dataset.from_tensor_slices(( dict(X_train),y_train))
train_dataset = train_dataset.batch(batch_size)
 test_dataset = tf.data.Dataset.from_tensor_slices(( dict(X_test),y_test))
test_dataset = test_dataset.batch(batch_size )<concatenate>
train= train.values.reshape(-1,28,28,1 ).astype('float32')
 test= test.values.reshape(-1,28,28,1 ).astype('float32')
 train =train / 255.0
test = test / 255.0
temp_x, temp_y = next(iter(test_dataset))
temp = roberta_seq(temp_x,temp_y)
 temp<choose_model_class>
Ytrain=to_categorical(Ytrain,num_classes=10)
 x_train,x_test,y_train,y_test=train_test_split(train,Ytrain,test_size=0.25 )
optimizer = tf.keras.optimizers.Adam(learning_rate=5e-6)
 loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
 roberta_seq.compile(optimizer=optimizer,loss=loss,metrics=['accuracy'] )<train_model>
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D,BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
history = roberta_seq.fit(train_dataset,epochs=3,
validation_data=test_dataset,
callbacks=[callback_chkpt] )<load_pretrained>
model = Sequential()
roberta_seq.load_weights(chkpt )<predict_on_test>
model.add(Conv2D(32,(3,3),padding='same',activation= 'relu',input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,(3,3),padding='same',activation= 'relu',input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
outputs = roberta_seq.predict(test_dataset)
 y_pred = outputs[0].argmax(axis=1 )<compute_test_metric>
model.add(Conv2D(64,(3,3),padding='same',activation= 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3,3),padding='same',activation= 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.4))
print('Confusion matrix:')
 print(confusion_matrix(y_test,y_pred,labels=[0,1]))
print()
print('Classification report:')
 print(classification_report(y_test,y_pred,labels=[0,1],target_names=['not a disaster','disaster']))<define_variables>
model.add(Dense(256,activation= 'relu'))
model.add(Dropout(0.25))
model.add(Dense(128,activation= 'relu'))
model.add(Dropout(0.50))
model.add(Dense(10,activation= 'softmax'))
tweets_test = list(df_test['text'])
 tweets_test = process_tweets(tweets_test)
 X_real_test = roberta_tokenizer(tweets_test,padding='max_length',max_length=max_len,return_tensors='tf')
 real_test_dataset = tf.data.Dataset.from_tensor_slices(dict(X_real_test))
real_test_dataset = real_test_dataset.batch(batch_size)
 real_test_dataset<predict_on_test>
optimizer =Adam(lr=0.004 )
outputs_test = roberta_seq.predict(real_test_dataset)
 y_pred_test = outputs_test[0].argmax(axis=1 )<save_to_csv>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
results = pd.Series(y_pred_test,index=df_test.index,name='target')
 results.to_csv('./submission.csv' )<import_modules>
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image("model.png")
import pandas as pd
import numpy as np
import cv2
from glob import glob
import sklearn
from sklearn.model_selection import GroupKFold, StratifiedKFold
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
from sklearn.metrics import log_loss
from skimage import io
import os
from datetime import datetime
import time
import random
import torchvision
from torchvision import transforms
from tqdm import tqdm
import matplotlib.pyplot as plt
import torch
from torch import nn
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import warnings
import joblib
from scipy.ndimage.interpolation import zoom<init_hyperparams>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)
CFG = {
'fold_num': 12,
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 384,
'epochs': 120,
'train_bs': 28,
'valid_bs': 32,
'lr': 1e-2,
'num_workers': 5,
'accum_iter': 1,
'verbose_step': 2,
'device': 'cuda:0',
'tta': 10,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}<set_options>
datagen = ImageDataGenerator(zoom_range = 0.2, 
)
 datagen.fit(x_train )
def all_seed(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True<normalization>
hist = model.fit_generator(datagen.flow(x_train, y_train, batch_size=256),
steps_per_epoch=600,
epochs=15,
verbose=1,
validation_data=(x_test, y_test)
 )
def get_img(path):
im_bgr = cv2.imread(path)
 im_rgb = im_bgr[:, :, ::-1]
return im_rgb<load_pretrained>
y_pred = model.predict(test, verbose = 1)
img = get_img('.. /input/cassava-leaf-disease-classification/train_images/1000015157.jpg')
 plt.imshow(img)
 plt.show()<load_from_csv>
predictions=[]
for i in range(len(test)) :
a=np.where(y_pred[i] == max(y_pred[i]))
predictions.append(a[0][0] )
<count_values><EOS> 
counter = range(1, len(predictions)+ 1)
 solution = pd.DataFrame({"ImageId": counter, "label": list(predictions)})
 solution.to_csv("digit_recognizer8.csv", index = False )