Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
from tensorflow.keras.preprocessing.image \
input_word_ids = layers.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = layers.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = layers.Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = layers.Dense(1, activation='sigmoid' )(clf_output)
 model = models.Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(optimizers.Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 model.summary()<train_model>
df_train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 df_test=pd.read_csv('.. /input/digit-recognizer/test.csv')
 print([df_train.shape,df_test.shape])
 print(df_train.iloc[265,1:].values.reshape(28,28)[:,10] )
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
callbacks=[checkpoint],
batch_size=16
)<predict_on_test>
images=['%s%s'%('pixel',pixel_no)for pixel_no in range(0,784)]
x_train=df_train[images].values/255.
 x_train=x_train.reshape(-1,28,28,1)
 y_train=df_train['label'].values
x_test_out=df_test[images].values/255.
 x_test_out=x_test_out.reshape(-1,28,28,1 )
model.load_weights('model.h5')
 test_pred = model.predict(test_input )<save_to_csv>
num_classes=10; img_size,img_size2=28,96
N=df_train.shape[0]; n=int (.1*N)
 shuffle_ids=np.arange(N)
 np.random.RandomState(123 ).shuffle(shuffle_ids)
 x_train=x_train[shuffle_ids]; y_train=y_train[shuffle_ids]
x_test,x_valid,x_train=\
x_train[:n],x_train[n:2*n],x_train[2*n:]
y_test,y_valid,y_train=\
y_train[:n],y_train[n:2*n],y_train[2*n:]
df=pd.DataFrame(
 [[x_train.shape,x_valid.shape,x_test.shape],
[x_train.dtype,x_valid.dtype,x_test.dtype],
[y_train.shape,y_valid.shape,y_test.shape],
[y_train.dtype,y_valid.dtype,y_test.dtype]],
columns=['train','valid','test'],
index=['image shape','image type','label shape','label type'])
 df
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<import_modules>
def model() :
model=tf.keras.Sequential()
model.add(tkl.Input(shape=(28,28,1)))
 model.add(tkl.BatchNormalization())
 model.add(tkl.Conv2D(28,(5,5),padding='same'))
model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.MaxPooling2D(pool_size=(2,2)))
 model.add(tkl.Dropout (.2))
model.add(tkl.Conv2D(96,(5,5),padding='same'))
model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.MaxPooling2D(strides=(2,2)))
 model.add(tkl.Dropout (.2))
model.add(tkl.Conv2D(128,(5,5)))
 model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.MaxPooling2D(strides=(2,2)))
 model.add(tkl.Dropout (.2))
model.add(tkl.GlobalMaxPooling2D())
 model.add(tkl.Dense(1024))
model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.Dropout (.5))
model.add(tkl.Dense(num_classes,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='nadam',
metrics=['sparse_categorical_accuracy'])
 return model
import pandas as pd<import_modules>
cnn_model=model()
checkpointer=tkc.ModelCheckpoint(
 filepath='/tmp/checkpoint',verbose=2,save_weights_only=True,
monitor='val_sparse_categorical_accuracy',mode='max',save_best_only=True)
 lr_reduction=tkc.ReduceLROnPlateau(
 monitor='val_loss',patience=15,verbose=2,factor=.8)
 early_stopping=tkc.EarlyStopping(
 monitor='val_loss',patience=75,verbose=2)
 history=cnn_model.fit(
 x_train,y_train,epochs=120,batch_size=128,
verbose=2,validation_data=(x_valid,y_valid),
callbacks=[checkpointer,lr_reduction,early_stopping] )
import pandas as pd<load_from_csv>
cnn_model.load_weights('/tmp/checkpoint')
 scores=cnn_model.evaluate(x_test,y_test,verbose=0 )
train_df = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test_df = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 gt_df = pd.read_csv(".. /input/disasters-on-social-media/socialmedia-disaster-tweets-DFE.csv" )<data_type_conversions>
steps,epochs=int(len(x_train)/128),10
datagen=ImageDataGenerator(
 featurewise_center=True,
featurewise_std_normalization=True,
zoom_range=.2,shear_range=.2,rotation_range=30,
height_shift_range=.2,width_shift_range=.2)
 datagen.fit(x_train)
 history=cnn_model.\
fit(datagen.flow(x_train,y_train,batch_size=128),
steps_per_epoch=steps,epochs=epochs,verbose=2,
validation_data=datagen.flow(x_valid,y_valid,batch_size=16),
callbacks=[checkpointer,lr_reduction,early_stopping] )
gt_df = gt_df[['choose_one', 'text']]
gt_df['target'] =(gt_df['choose_one']=='Relevant' ).astype(int)
 gt_df['id'] = gt_df.index
gt_df<merge>
cnn_model.load_weights('/tmp/checkpoint')
 scores=cnn_model.evaluate(x_test,y_test,verbose=0 )
merged_df = pd.merge(test_df, gt_df, on='id')
 merged_df<save_to_csv>
predict_y_test_out=cnn_model.predict(x_test_out)
 predict_y_test_out=predict_y_test_out.argmax(axis=-1 )
subm_df.to_csv('submission.csv', index=False )<install_modules>
submission=pd.DataFrame(
 {'ImageId':range(1,len(predict_y_test_out)+1),
'Label':predict_y_test_out})
 print(submission[0:15].T)
 submission.to_csv('kaggle_digits_cnn.csv',index=False )
! pip install tf-models-official==2.4.0 -q
! pip install tensorflow-gpu==2.4.1 -q
! pip install tensorflow-text==2.4.1 -q
! python -m spacy download en_core_web_sm -q
! pip install dataprep | grep -v 'already satisfied'<set_options>
os.environ['TFHUB_MODEL_LOAD_FORMAT']='COMPRESSED'
model=th.load('https://tfhub.dev/captain-pool/esrgan-tf2/1')
 func=model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
func.inputs[0].set_shape([1,img_size2//4,img_size2//4,3])
 converter=tf.lite.TFLiteConverter.from_concrete_functions([func])
 converter.optimizations=[tf.lite.Optimize.DEFAULT]
tflite_model=converter.convert()
with tf.io.gfile.GFile('ESRGAN.tflite','wb')as f:
f.write(tflite_model)
 esrgan_model_path='./ESRGAN.tflite'
np.set_printoptions(precision=4)
 warnings.filterwarnings('ignore' )<import_modules>
N3=10000; n3=int (.1*N3)
 x_train3=x_train[:N3]; y_train3=y_train[:N3]
x_valid3=x_valid[:n3]; y_valid3=y_valid[:n3]
x_test3=x_test[:n3]; y_test3=y_test[:n3]
x_train3=tf.repeat(x_train3,3,axis=3 ).numpy()
x_valid3=tf.repeat(x_valid3,3,axis=3 ).numpy()
x_test3=tf.repeat(x_test3,3,axis=3 ).numpy()
x_test3.shape,x_test3.mean()
tf.__version__<define_variables>
def bicubic_resize(imgs,img_size=img_size2):
bicubic=tf.image.resize(
 imgs*255,[img_size,img_size],tf.image.ResizeMethod.BICUBIC)
 bicubic_contrast=tf.image.adjust_contrast(bicubic,.8)
 bicubic_contrast=tf.cast(bicubic_contrast,tf.uint8)
 return bicubic_contrast.numpy() /255
random.seed(319)
 np.random.seed(319)
 tf.random.set_seed(319 )<load_from_csv>
x_train3=bicubic_resize(x_train3)
 x_valid3=bicubic_resize(x_valid3)
 x_test3=bicubic_resize(x_test3)
 x_test3.shape,x_test3.mean()
train_full = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test_full = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 print('Training Set Shape = {}'.format(train_full.shape))
print('Training Set Memory Usage = {:.2f}MB'.format(train_full.memory_usage().sum() /2**20))
print('Test Set Shape = {}'.format(test_full.shape))
print('Test Set Memory Usage = {:.2f}MB'.format(test_full.memory_usage().sum() /2**20))<load_from_csv>
def esrgantf2_superresolution(
 img,super_size=img_size2,model_path=esrgan_model_path):
if img.mean() <1.: img=img*255.
 lr=tf.image.resize(img,[super_size//4,super_size//4])
 lr=tf.expand_dims(lr.numpy() [:,:,:3],axis=0)
 lr=tf.cast(lr,tf.float32)
 interpreter=tf.lite.Interpreter(model_path=model_path)
 interpreter.allocate_tensors()
input_details=interpreter.get_input_details()
output_details=interpreter.get_output_details()
interpreter.set_tensor(input_details[0]['index'],lr)
 interpreter.invoke()
output_data=interpreter.get_tensor(output_details[0]['index'])
 sr=tf.squeeze(output_data,axis=0)
 sr=tf.clip_by_value(sr,0,255)
 sr=tf.round(sr); sr=tf.cast(sr,tf.uint8)
 lr=tf.cast(tf.squeeze(lr,axis=0),tf.uint8)
 return lr,sr
df_train = pd.read_csv("/kaggle/input/disastertweet-prepared2/train_prepared.csv")
 df_test = pd.read_csv("/kaggle/input/disastertweet-prepared2/test_prepared.csv" )<drop_column>
for i in range(5):
lr,sr=esrgantf2_superresolution(
 tf.repeat(x_train[i],3,axis=2 ).numpy())
 low2superbicubic_imgs(
 tf.repeat(x_train[i],3,axis=2 ).numpy() ,lr,sr )
train_full = clean_text(train_full,'keyword')
 test_full = clean_text(test_full, 'keyword' )<feature_engineering>
def premodel(pix,den,mh,lbl,activ,loss):
model=tf.keras.Sequential([
tkl.Input(( pix,pix,3),name='input'),
th.KerasLayer(mh,trainable=True),
tkl.Flatten() ,
tkl.Dense(den,activation='relu'),
tkl.Dropout(rate=.5),
tkl.Dense(lbl,activation=activ)])
 model.compile(optimizer='nadam',loss=loss,
metrics=['sparse_categorical_accuracy'])
 return model
def cb(fw):
early_stopping=tkc.EarlyStopping(
 monitor='val_loss',patience=10,verbose=2)
 checkpointer=tkc.ModelCheckpoint(
 filepath=fw,verbose=2,save_weights_only=True,
monitor='val_sparse_categorical_accuracy',
mode='max',save_best_only=True)
 lr_reduction=tkc.ReduceLROnPlateau(
 monitor='val_loss',verbose=2,patience=5,factor=.8)
 return [checkpointer,early_stopping,lr_reduction]
df_train['keyword'] = train_full['keyword']
df_test['keyword'] = test_full['keyword']<load_pretrained>
fw='/tmp/checkpoint'
handle_base='mobilenet_v2_100_%d'%img_size2
mhandle='https://tfhub.dev/google/imagenet/{}/classification/4'\ 
.format(handle_base)
 hub_model=premodel(img_size2,1024,mhandle,num_classes,
'softmax','sparse_categorical_crossentropy')
 history=hub_model.fit(x=x_train3,y=y_train3,batch_size=128,
epochs=20,callbacks=cb(fw),verbose=0,
validation_data=(x_valid3,y_valid3))
nlp_spacy = spacy.load('en_core_web_sm')
 sentence_enc = hub.load('https://tfhub.dev/google/universal-sentence-encoder/4' )<categorify>
hub_model.load_weights('/tmp/checkpoint')
 hub_model.evaluate(x_test3,y_test3,verbose=0 )