Kaggle competition: Digit Recognizer
IMG_HEIGHT = 600
IMG_WIDTH = 800
IMG_SIZE = 600
IMG_TARGET_SIZE = 512
N_CHANNELS = 3
N_LABELS = 5
N_FOLDS = 5
BATCH_SIZE = 16
AUTO = tf.data.experimental.AUTOTUNE
IMAGENET_MEAN = tf.constant([0.485, 0.456, 0.406], dtype=tf.float32)
 IMAGENET_STD = tf.constant([0.229, 0.224, 0.225], dtype=tf.float32 )<choose_model_class>
df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df2 = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(df.shape)
 print(df2.shape )
def get_model(fold):
tf.keras.backend.clear_session()
net = efn.EfficientNetB4(
 include_top=False,
weights=None,
input_shape=(IMG_TARGET_SIZE, IMG_TARGET_SIZE, N_CHANNELS), 
)
 for layer in reversed(net.layers):
if isinstance(layer, tf.keras.layers.BatchNormalization):
layer.trainable = False
else:
layer.trainable = True
model = tf.keras.Sequential([
net,
tf.keras.layers.Dropout(0.45),
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dropout(0.45),
tf.keras.layers.Dense(N_LABELS, activation='softmax', dtype=tf.float32),
])
 model.load_weights(f'/kaggle/input/cassava-leaf-disease-prediction/model_fold_{fold}_weights.h5')
 return model<normalization>
scaler = MinMaxScaler()
x = scaler.fit_transform(x)
 x
@tf.function
def decode_tfrecord_test(file_path):
image = tf.io.read_file(file_path)
 image = tf.io.decode_jpeg(image)
 image = tf.reshape(image, [IMG_HEIGHT, IMG_WIDTH, N_CHANNELS])
 image = tf.cast(image, tf.float32)
 image_id = tf.strings.split(file_path, '/')[-1]
return image, image_id<define_variables>
x1 = np.array(df2)
 x1 = scaler.fit_transform(x1)
 x1 = x1.reshape(( 28000,28,28,1))
x1.shape
def get_test_dataset() :
ignore_order = tf.data.Options()
ignore_order.experimental_deterministic = False
test_dataset = tf.data.Dataset.list_files('/kaggle/input/cassava-leaf-disease-classification/test_images/*.jpg')
 test_dataset = test_dataset.with_options(ignore_order)
 test_dataset = test_dataset.map(decode_tfrecord_test, num_parallel_calls=AUTO)
 test_dataset = test_dataset.batch(BATCH_SIZE)
 test_dataset = test_dataset.prefetch(AUTO)
 return test_dataset<categorify>
y = np.array(y)
 enc = OneHotEncoder(sparse=False)
 y= y.reshape(( -1,1))
y = enc.fit_transform(y)
 y.shape
def show_first_test_batch() :
imgs, imgs_ids = next(iter(get_test_dataset()))
img = imgs[0].numpy().astype(np.float32)
 print(f'imgs.shape: {imgs.shape}, imgs.dtype: {imgs.dtype}, imgs_ids.shape: {imgs_ids.shape}, imgs_ids.dtype: {imgs_ids.dtype}')
 print('img mean: {:.3f}, img std {:.3f}, img min: {:.3f}, img max: {:.3f}'.format(img.mean() , img.std() , img.min() , img.max()))
print(f'imgs_id: {imgs_ids[0]}')
 img += abs(img.min())
 img /= img.max()
plt.imshow(img)
 plt.show()
show_first_test_batch()<predict_on_test>
x_train,x_test,y_train,y_test = tts(x,y,test_size = 0.2, random_state=42)
 print(x_train.shape)
 print(y_train.shape)
 print(x_test.shape)
 print(y_test.shape )
submission = pd.DataFrame(columns=['image_id', 'label'])
 preds_dict = dict()
for fold in range(N_FOLDS):
model = get_model(fold)
 for idx,(imgs, image_ids)in tqdm(enumerate(get_test_dataset())) :
for img, image_id in zip(imgs, image_ids.numpy().astype(str)) :
pred = predict_tta(model, img)
 if image_id in preds_dict:
preds_dict[image_id] += pred
else:
preds_dict[image_id] = pred
for idx,(image_id, preds)in enumerate(preds_dict.items()):
if idx is 0:
print(f'image {image_id} predictions:{preds}')
 label = np.argmax(preds)
 submission = submission.append({ 'image_id': image_id, 'label': label }, ignore_index=True)
 submission.to_csv('./submission.csv', index=False )<import_modules>
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
import numpy as np
import pandas as pd
import os 
<install_modules>
model = keras.Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.summary()
!pip install timm --no-index --find-links=file:///kaggle/input/timm-package/<install_modules>
callbacks = [
keras.callbacks.EarlyStopping(
 monitor='val_loss',
min_delta=1e-5,
patience=25,
verbose=1)
 ]
!pip install albumentations --no-index --find-links=file:///kaggle/input/albumentationspackage/<import_modules>
predictions=model.predict(x1)
 pre=predictions.argmax(axis=-1 )
import sys
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn import Parameter
import os
import cv2
import timm<import_modules>
submission = pd.Series(pre,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),submission],axis = 1)
 submission.to_csv("final_submission_v1.csv",index=False)
 submission.head()