Kaggle competition: Digit Recognizer
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=10,
callbacks=[early], )<compute_test_metric>
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Input
import matplotlib.pyplot as plt
score = model.evaluate(valid_generator,batch_size=32)
 print("Accuracy: {:.2f}%".format(score[1] * 100))
print("Loss: ",score[0])
<import_modules>
data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 data_sub = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 X = data.iloc[:, 1:]
y = data.iloc[:, 0]
from sklearn.metrics import f1_score<predict_on_test>
X_train, X_test = X[:40000]/255.0, X[40000:]/255.0
y_train, y_test = y[:40000], y[40000:]
X_valid, y_valid = X_train[:10000], y_train[:10000]
Y_pred = model.predict(valid_generator)
 y_pred = np.argmax(Y_pred, axis=1 )<compute_test_metric>
model = tf.keras.models.Sequential([
Input(shape=X_train.shape[1:]),
Dense(256, activation='sigmoid'),
Dense(128, activation='sigmoid'),
Dense(10, activation='softmax'),
])
 model.compile(optimizer='sgd',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(X_train, y_train, epochs=20,
validation_data=(X_valid, y_valid))
f_score = f1_score(valid_generator.classes,y_pred,average='macro')
 print('F1 score:',f_score)
<predict_on_test>
model.evaluate(X_test, y_test )
pred=model.predict(test_generator )<load_from_csv>
model = tf.keras.Sequential([
Input(shape=X_train.shape[1:]),
Dense(256, activation='relu'),
Dense(128, activation='relu'),
Dense(10, activation='softmax'),
])
 model.compile(optimizer='RMSProp',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(X_train, y_train, epochs=20,
validation_data=(X_valid, y_valid))
df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')
 df_submission.head()<define_variables>
print('Accuracy {}'.format(np.round(model.evaluate(X_test, y_test)[1], 4)) )
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 = tf.keras.Sequential([
Input(shape=X_train.shape[1:]),
Dense(256, activation='relu'),
BatchNormalization() ,
Dropout(0.1),
Dense(128, activation='relu'),
BatchNormalization() ,
Dropout(0.45),
Dense(10, activation='softmax'),
])
 model.compile(optimizer='RMSProp',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(X_train, y_train, epochs=20,
validation_data=(X_valid, y_valid))
df_submission['id'] = id_list
df_submission.iloc[:,1:] = pred
df_submission.head()<define_variables>
print('Accuracy {}'.format(np.round(model.evaluate(X_test, y_test)[1], 4)) )
breeds=['id','beagle','chihuahua','doberman','french_bulldog', 'golden_retriever', 'malamute','pug','saint_bernard','scottish_deerhound','tibetan_mastiff']<filter>
X_train, X_valid, X_test = np.array(X_train ).reshape(-1, 28, 28, 1), np.array(X_valid ).reshape(-1, 28, 28, 1), np.array(X_test ).reshape(-1, 28, 28, 1 )
selected_breeds = df_submission.loc[:,breeds]<save_to_csv>
model = tf.keras.models.Sequential([
Input(shape=X_train.shape[1:]),
Conv2D(32, 7, activation='relu', padding='same'),
Conv2D(32, 5, activation='relu', padding='same'),
MaxPooling2D(pool_size=(2,2)) ,
BatchNormalization() ,
Dropout(0.3),
Conv2D(64, 5, activation='relu', padding='same'),
Conv2D(64, 5, activation='relu', padding='same'),
MaxPooling2D(pool_size=(2,2)) ,
BatchNormalization() ,
Dropout(0.3),
Flatten() ,
Dense(256, activation='relu'),
BatchNormalization() ,
Dropout(0.5),
Dense(10, activation='softmax')
 ])
 model.compile(optimizer='RMSProp',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(X_train, y_train,
epochs=20,
validation_data=(X_valid, y_valid))
final_sub = df_submission.set_index('id')
 final_sub.to_csv('Submission.csv' )<set_options>
print('Accuracy {}'.format(np.round(model.evaluate(X_test, y_test)[1], 4)) )
%%time
%matplotlib inline<define_variables>
train_datagen = ImageDataGenerator(
 rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split = 0.25 
)
 valid_datagen = ImageDataGenerator(
 rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split = 0.25 
)
 train_datagen.fit(X_train)
 valid_datagen.fit(X_valid)
 train_generator = train_datagen.flow(X_train, y_train, batch_size=50, subset='training')
 valid_generator = valid_datagen.flow(X_valid, y_valid, batch_size=50, subset='validation' )
%%time
train_dir = '.. /input/dog-breed-identification/train'
test_dir ='.. /input/dog-breed-identification/test'<load_from_csv>
history = model.fit_generator(generator=train_generator,
validation_data=valid_generator,
epochs = 20 )
%%time
def append_ext(fn):
return fn+".jpg"
traindf = pd.read_csv('.. /input/dog-breed-identification/labels.csv',dtype=str)
 testdf = pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv',dtype=str)
 traindf["id"] = traindf["id"].apply(append_ext)
 testdf["id"] = testdf["id"].apply(append_ext)
<define_variables>
print('Accuracy {}'.format(np.round(model.evaluate(X_test, y_test)[1], 4)) )
%%time
train_datagen=ImageDataGenerator(rescale=1./255.,
zoom_range = [0.7,1],
horizontal_flip = True,
validation_split=0.05
)<define_variables>
history = model.fit_generator(generator=train_generator,
validation_data=valid_generator,
epochs = 200,
callbacks=[checkpoint, early_stopping] )
BATCH_SIZE = 32<prepare_x_and_y>
model = tf.keras.models.load_model('model.h5')
 model.evaluate(X_test, y_test )
train_generator=train_datagen.flow_from_dataframe(
 dataframe=traindf,
directory=train_dir,
x_col="id",
y_col="breed",
subset="training",
batch_size=BATCH_SIZE,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(224,224),
color_mode="rgb"
)<prepare_x_and_y>
data_sub = np.array(data_sub ).reshape(-1, 28, 28 , 1 ).astype('float32')/ 255
x,y = next(train_generator )<create_dataframe>
preds = model.predict(data_sub )
valid_generator=train_datagen.flow_from_dataframe(
 dataframe=traindf,
directory=train_dir,
x_col="id",
y_col="breed",
subset="validation",
batch_size=BATCH_SIZE,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(224,224),
color_mode="rgb")
<prepare_x_and_y>
np.argmax(preds[0] )
test_datagen=ImageDataGenerator(rescale=1./255.)
test_generator=test_datagen.flow_from_dataframe(
 dataframe=testdf,
directory=test_dir,
x_col="id",
y_col=None,
batch_size=BATCH_SIZE,
seed=42,
shuffle=False,
class_mode=None,
target_size=(224,224),
color_mode="rgb" )<choose_model_class>
labels = [np.argmax(x)for x in preds]
ids = [x+1 for x in range(len(preds)) ]
sub = pd.DataFrame()
pretrained_model = tf.keras.applications.ResNet50V2(
 weights='imagenet',
include_top=False ,
input_shape=[224,224,3] 
)
 pretrained_model.trainable = False
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(120, activation='softmax')
 ] )<choose_model_class>
sub['ImageId'] = ids
sub['Label'] = labels
sub.to_csv('mnist_submission.csv', index=False)
 pd.read_csv('mnist_submission.csv' )