Kaggle competition: Digit Recognizer
class_names = train_ds.class_names
print(len(class_names))
print(class_names )<create_dataframe>
df_train=pd.read_csv(".. /input/digit-recognizer/train.csv")
 df_train.head()
val_ds = image_dataset_from_directory(
 directory = train_dir,
labels = 'inferred',
label_mode='int',
batch_size=BATCH_SIZE,
image_size=(IMG_HEIGHT, IMG_WIDTH),
shuffle = True,
seed=1234,
validation_split=0.1,
subset="validation",
)<create_dataframe>
df_train = shuffle(df_train )
test_ds = image_dataset_from_directory(
 directory = test_dir,
label_mode= None,
batch_size=BATCH_SIZE,
image_size=(IMG_HEIGHT, IMG_WIDTH),
shuffle = False,
seed=1234
)<drop_column>
X=df_train.drop(["label"],axis=1)
 y=df_train["label"]
del class_names<data_type_conversions>
y.value_counts(normalize=True )
plt.figure(figsize=(20, 20))
for images, labels in train_ds.take(1):
for i in range(16):
ax = plt.subplot(4, 4, i + 1)
 plt.imshow(images[i].numpy().astype("uint8"))
plt.title(dog_breeds[labels[i]])
 plt.axis("off" )<data_type_conversions>
from tensorflow.keras.layers import Conv2D,Dense,Reshape,MaxPool2D,Dropout
from tensorflow.keras.models import Sequential
plt.figure(figsize=(20, 20))
for images, labels in val_ds.take(1):
for i in range(16):
ax = plt.subplot(4, 4, i + 1)
 plt.imshow(images[i].numpy().astype("uint8"))
plt.title(dog_breeds[labels[i]])
 plt.axis("off" )<data_type_conversions>
from tensorflow.keras.layers import Flatten
plt.figure(figsize=(20, 20))
for images in test_ds.take(1):
for i in range(16):
ax = plt.subplot(4, 4, i + 1)
 plt.imshow(images[i].numpy().astype("uint8"))
plt.axis("off" )<load_pretrained>
import tensorflow as tf
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)
 val_ds = val_ds.prefetch(buffer_size=AUTOTUNE)
 test_ds = test_ds.prefetch(buffer_size=AUTOTUNE)
<choose_model_class>
model=Sequential()
model.add(Reshape(( 28,28,1),input_shape=(784,)))
 model.add(Conv2D(64,(3,3),activation="relu",kernel_initializer="he_uniform"))
model.add(Dropout(0.5))
model.add(MaxPool2D(pool_size=(2,2),strides=2))
model.add(Conv2D(64,(5,5),activation="relu",kernel_initializer="he_uniform"))
model.add(Dropout(0.5))
model.add(MaxPool2D(pool_size=(2,2),strides=2))
model.add(Flatten())
 model.add(Dense(64,activation="relu",kernel_initializer="he_uniform"))
model.add(Dropout(0.3))
model.add(Dense(10,activation="softmax"))
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["accuracy"])
 model.summary()
data_augmentation = Sequential(
 [
preprocessing.RandomFlip("horizontal"),
preprocessing.RandomRotation(0.1),
preprocessing.RandomZoom(0.1),
]
)<choose_model_class>
model.fit(tf.cast(X,tf.float32)/255.0,tf.cast(y,tf.float32),validation_split=0.3,batch_size=100,verbose=2,epochs=100)
base_model_1 = xception.Xception(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))
base_model_2 = inception_v3.InceptionV3(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))
base_model_3 = inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))
base_model_5 = nasnet.NASNetLarge(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))
base_model_1.trainable = False
base_model_2.trainable = False
base_model_3.trainable = False
base_model_5.trainable = False
inputs = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))
aug_inputs = data_augmentation(inputs)
 x1 = xception.preprocess_input(aug_inputs)
 x1 = base_model_1(x1, training=False)
 x1 = GlobalAveragePooling2D()(x1)
 x2 = inception_v3.preprocess_input(aug_inputs)
 x2 = base_model_2(x2, training=False)
 x2 = GlobalAveragePooling2D()(x2)
 x3 = inception_resnet_v2.preprocess_input(aug_inputs)
 x3 = base_model_3(x3, training=False)
 x3 = GlobalAveragePooling2D()(x3)
 x5 = nasnet.preprocess_input(aug_inputs)
 x5 = base_model_5(x5, training=False)
 x5 = GlobalAveragePooling2D()(x5)
 x = Concatenate()([x1, x2, x3, x5])
 x = Dropout (.7 )(x)
 outputs = Dense(120, activation='softmax' )(x)
 model = Model(inputs, outputs)
 display(model.summary() )<choose_model_class>
df_test=pd.read_csv(".. /input/digit-recognizer/test.csv")
 df_test.head()
optimizer = Adam(learning_rate=0.001)
 model.compile(loss="sparse_categorical_crossentropy", metrics=['accuracy'], optimizer=optimizer )<choose_model_class>
submit=pd.DataFrame(columns=["ImageId","Label"] )
EarlyStop_callback = EarlyStopping(min_delta=0.001, patience=10, restore_best_weights=True)
<train_model>
submit["ImageId"]=df_test.index.values
submit["ImageId"]=submit["ImageId"]+1
submit
history = model.fit(
 train_ds,
epochs=N_EPOCHS,
validation_data=val_ds,
callbacks=[EarlyStop_callback]
)<predict_on_test>
import numpy as np
wrong_pred_images = np.array([])
 actual_labels = np.array([])
 predicted_labels = np.array([])
 batch = 1
for images, labels in val_ds:
batch_predictions_probs = model.predict_on_batch(images)
 batch_predictions = np.argmax(batch_predictions_probs, axis=1)
 mask =(batch_predictions != labels.numpy())
 print("No of wrong predictions on batch {}: {}".format(batch, mask.sum()))
wrong_pred_indices = np.arange(len(batch_predictions)) [mask]
print(wrong_pred_indices)
 if len(wrong_pred_images)== 0:
wrong_pred_images = images.numpy() [wrong_pred_indices]
actual_labels = labels.numpy() [wrong_pred_indices]
predicted_labels = batch_predictions[wrong_pred_indices]
else:
wrong_pred_images = np.append(wrong_pred_images, images.numpy() [wrong_pred_indices], axis = 0)
 actual_labels = np.append(actual_labels, labels.numpy() [wrong_pred_indices], axis = 0)
 predicted_labels = np.append(predicted_labels, batch_predictions[wrong_pred_indices], axis = 0)
 batch = batch + 1
print(wrong_pred_images.shape)
 print(actual_labels.shape)
 print(predicted_labels.shape )<predict_on_test>
output=model.predict(tf.cast(df_test,tf.float32)/255.0)
 output
predictions = model.predict(
 test_ds,
batch_size = BATCH_SIZE,
verbose=1
)<prepare_output>
prediction=np.argmax(output,axis=1)
 len(prediction )
submission.loc[:, dog_breeds] = predictions
submission.head()<save_to_csv>
submit["Label"]=prediction
submit
submission.to_csv('submission.csv', index=False)
<import_modules>
submit.to_csv("digit_submission",index=False )