Kaggle competition: Digit Recognizer
opt=tf.keras.optimizers.SGD(lr=1e-4, momentum=0.9)
 model.compile(optimizer = opt ,
loss="categorical_crossentropy",
metrics=["accuracy"])
 model.summary()<choose_model_class>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
early = tf.keras.callbacks.EarlyStopping(patience=2,
min_delta=0.001,
restore_best_weights=True )<train_model>
X = train.drop("label",axis=1 ).values.reshape(-1,28,28,1)
 y = train["label"].values
X_test = test.values.reshape(-1,28,28,1 )
STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size
STEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size
STEP_SIZE_TEST = test_generator.n//test_generator.batch_size
history = model.fit(train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=25,
callbacks=[early], )<compute_test_metric>
X = X.astype('float32')
 X_test = X_test.astype('float32')
 X = X/255.0
X_test = X_test/255.0
y = tensorflow.keras.utils.to_categorical(y,10 )
score = model.evaluate(valid_generator,batch_size=32)
 print("Accuracy: {:.2f}%".format(score[1] * 100))
print("Loss: ",score[0])
<import_modules>
X_train,X_val,y_train,y_val = train_test_split(X,y,test_size=0.2,random_state=1 )
from sklearn.metrics import f1_score, confusion_matrix<define_variables>
target_names = []
for key in train_generator.class_indices:
target_names.append(key )<predict_on_test>
nn = 10
model = [0]*nn
for j in range(nn):
model[j] = Sequential()
model[j].add(Conv2D(24,kernel_size=(3,3),padding='same',activation='relu',input_shape=(28,28,1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(24,kernel_size=(3,3),padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(filters=64,kernel_size=(3,3),activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(filters=64,kernel_size=(3,3),activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128,kernel_size=4,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10,activation='softmax'))
model[j].compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
Y_pred = model.predict(valid_generator)
 y_pred = np.argmax(Y_pred, axis=1 )<compute_test_metric>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1 
)
 datagen.fit(X )
cm = confusion_matrix(valid_generator.classes,y_pred)
<compute_test_metric>
epochs = 50
history = [0]*nn
for j in range(nn):
X_train,X_val,y_train,y_val = train_test_split(X,y,test_size=0.1,random_state=7)
 history[j] = model[j].fit_generator(datagen.flow(X_train,y_train,batch_size=64),
epochs=epochs,validation_data=(X_val,y_val),
verbose=1)
f_score = f1_score(valid_generator.classes,y_pred,average='macro')
 print('F1 score:',f_score)
<predict_on_test>
results = np.zeros(( X_test.shape[0],10))
for j in range(nn):
results = results+model[j].predict(X_test)
 results = np.argmax(results,axis=1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name='ImageId'),results],axis=1)
 submission.to_csv('ENSEMBLE.csv',index=False )
pred=model.predict(test_generator )<load_from_csv>
model.fit(datagen.flow(X_train,y_train,batch_size=128),epochs=10,validation_data=(X_val,y_val))
df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')
 df_submission.head()<define_variables>
model.fit(datagen.flow(X,y,batch_size=128),epochs=20,verbose=1 )
file_list = test_generator.filenames
id_list = []
for name in file_list:
m = re.sub('test/', '', name)
 m = re.sub('.jpg', '', m)
 id_list.append(m )<prepare_output>
model_log = model.fit(X_train,y_train,batch_size=128,epochs=10,verbose=1,validation_data=(X_val,y_val))
df_submission['id'] = id_list
df_submission.iloc[:,1:] = pred
df_submission.head()<define_variables>
model_log = model.fit(X,y,batch_size=128,epochs=20,verbose=1 )
breeds=['id','beagle','chihuahua','doberman','french_bulldog', 'golden_retriever', 'malamute','pug','saint_bernard','scottish_deerhound','tibetan_mastiff']<filter>
X_train = X_train.reshape(X_train.shape[0],28,28,1)
 X_test = X_test.reshape(X_test.shape[0],28,28,1)
 X_train.astype('float32')
 X_test.astype('float32')
 X_train=X_train/255
X_val=X_val/255
selected_breeds = df_submission.loc[:,breeds]<save_to_csv>
import numpy as np
import pandas as pd
final_sub = df_submission.set_index('id')
 final_sub.to_csv('Submission.csv' )<import_modules>
results = model.predict(X_test )
from sklearn.preprocessing impbort LabelEncoder 
<load_from_csv>
np.argmax(results,axis=1 )
comp_df = pd.read_csv('.. /input/dog-breed-identification/labels.csv')
 test_df = pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv')
 print('Training set: {}, Test set: {}'.format(comp_df.shape[0],test_df.shape[0]))<count_values>
results = np.argmax(results,axis=1)
 results = pd.Series(results,name='Label')
 submission = pd.concat([pd.Series(range(1,28001),name='ImageId'),results],axis=1)
 submission.to_csv('submission8.csv',index=False )