Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<feature_engineering>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
train_df = pd.read_csv(input_directory + "/labels.csv")
 train_df.rename(columns={"breed": "label"}, inplace=True)
 train_df["id"] = train_df["id"].apply(lambda x: x+"."+"jpg")
 train_df.head()<prepare_output>
from fastai import *
from fastai.vision import *
test_files = os.listdir(testing_dir)
 test_df = pd.DataFrame({"id": test_files, "label": "boston_bull"})
 test_df.head()<feature_engineering>
print(torch.cuda.is_available() , torch.backends.cudnn.enabled )
train_df2 = train_df.copy()
train_df2["label"] = train_df["label"].apply(lambda x: re.sub("_", " ", x))
train_df2["label"] = train_df2["label"].apply(lambda x: x.title())
 classes2 = train_df2["label"].unique()
classes2.sort()
rows =(len(classes)- 1)/4<define_variables>
path_model='/kaggle/working/'
path_input="/kaggle/input/"
label_df = pd.read_csv(f"{path_input}labels.csv")
 label_df.head()
def get_data(batch_size=32, target_size=(299, 299), class_mode="categorical", training_dir=training_dir, testing_dir=testing_dir, x_col="id", y_col="label"):
print("Preprocessing and Generating Data Batches.......
 ")
 rescale = 1.0/255
train_batch_size = batch_size
validation_batch_size = batch_size*5
test_batch_size = batch_size*5
train_shuffle = True
val_shuffle = True
test_shuffle = False
train_datagen = ImageDataGenerator(
 horizontal_flip=True,
rotation_range=45,
shear_range=15,
rescale=rescale,
validation_split=0.25)
 train_generator = train_datagen.flow_from_dataframe(
 train_df,
training_dir,
x_col=x_col,
y_col=y_col,
target_size=target_size,
class_mode=class_mode,
batch_size=batch_size,
shuffle=True,
seed=42,
subset='training')
 validation_generator = train_datagen.flow_from_dataframe(
 train_df,
training_dir,
x_col=x_col,
y_col=y_col,
target_size=target_size,
class_mode=class_mode,
batch_size=validation_batch_size,
shuffle=True,
seed=42,
subset='validation')
 test_datagen = ImageDataGenerator(rescale=rescale)
 test_generator = test_datagen.flow_from_dataframe(
 test_df,
testing_dir,
x_col=x_col,
y_col=y_col,
target_size=target_size,
class_mode=class_mode,
batch_size=test_batch_size,
shuffle=False,
seed=42)
 class_weights = get_weight(train_generator.classes, binary=False)
 steps_per_epoch = len(train_generator)
 validation_steps = len(validation_generator)
 print("
Preprocessing and Data Batch Generation Completed.
 ")
 return train_generator, validation_generator, test_generator, class_weights, steps_per_epoch, validation_steps
def get_weight(y, binary=True, n_samples=-1):
if binary==False:
class_weights = cw.compute_class_weight('balanced', np.unique(y), y)
 else:
d = {x:y.count(x)for x in set(y)}
num_class_temp = 2
class_weights = {}
for cls in d:
count = d.get(cls)
 class_weight = n_samples /(num_class_temp * count)
 class_weights[cls] = class_weight
return class_weights 
<choose_model_class>
label_df.pivot_table(index='breed',aggfunc=len ).sort_values('id',ascending=False )
def get_model(model_name, input_shape=(96, 96, 3), num_class=2, weights='imagenet', dense_units=1024):
inputs = Input(input_shape)
 if model_name == "Xception":
base_model = Xception(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet50":
base_model = ResNet50(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet101":
base_model = keras.applications.resnet.ResNet101(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet152":
base_model = keras.applications.resnet.ResNet152(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet50V2":
base_model = resnet_v2.ResNet50V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet101V2":
base_model = resnet_v2.ResNet101V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet152V2":
base_model = resnet_v2.ResNet152V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNeXt50":
base_model = resnext.ResNeXt50(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNeXt101":
base_model = resnext.ResNeXt101(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "InceptionV3":
base_model = InceptionV3(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "InceptionResNetV2":
base_model = InceptionResNetV2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "DenseNet201":
base_model = DenseNet201(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "NASNetMobile":
base_model = NASNetMobile(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "NASNetLarge":
base_model = NASNetLarge(include_top=False, weights=weights, input_shape=input_shape)
 x = base_model(inputs)
 x = Dropout(0.8 )(x)
 x = GlobalAveragePooling2D()(x)
 x = BatchNormalization()(x)
 x = Dropout(0.8 )(x)
 if num_class>1:
outputs = Dense(num_class, activation="softmax" )(x)
 else:
outputs = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inputs, outputs=outputs)
 model.summary()
return model<choose_model_class>
data = ImageDataBunch.from_csv(
 path_input,
folder='train',
valid_pct=0.2,
ds_tfms=get_transforms(flip_vert=True,max_rotate=20., max_zoom=1.1),
size=224,
test='/kaggle/input/test/test',
suffix='.jpg',
bs=64,
num_workers=0 ).normalize(imagenet_stats )
def get_conv_model(num_class=2, input_shape=(150,150, 3)) :
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())
 if num_class>1:
model.add(Dense(num_class, activation='softmax'))
else:
model.add(Dense(num_class, activation='sigmoid'))
return model<choose_model_class>
data.show_batch(rows=3, figsize=(8,8))
print("Settting Callbacks")
 def step_decay(epoch, lr):
lrate = lr
if epoch==2:
lrate = 0.0001
return lrate
checkpoint = ModelCheckpoint(
 model_file,
monitor='val_acc',
save_best_only=True)
 early_stopping = EarlyStopping(
 monitor='val_loss',
patience=3,
verbose=1,
restore_best_weights=True)
 reduce_lr = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.5,
patience=1,
min_lr=0.0000001,
verbose=1)
 learning_rate_scheduler = LearningRateScheduler(step_decay, verbose=1)
 callbacks = [reduce_lr, early_stopping]
print("Set Callbacks at ", date_time(1))<define_variables>
[print(len(e)) for e in [data.train_ds, data.valid_ds, data.test_ds]]
dim = 299
input_shape =(dim, dim, 3)
 num_class = len(classes)
 weights = 'imagenet'
dense_units = 256<choose_model_class>
files = os.listdir(f'{path_input}train/')[:5]
img = plt.imread(f'{path_input}train/{files[0]}')
 plt.imshow(img )
print("Getting Base Model", date_time(1))
model = get_model(model_name="InceptionV3", input_shape=input_shape, num_class=num_class, weights=weights, dense_units=dense_units)
 print("Loaded Base Model", date_time(1))<define_variables>
learner.fit_one_cycle(3 )
batch_size = 64
class_mode = "categorical"
target_size =(dim, dim)
 y_col = "label"<define_variables>
np.set_printoptions(precision=6, suppress=True)
 test_result = learner.get_preds(ds_type=DatasetType.Test )
train_generator, validation_generator, test_generator, class_weights, steps_per_epoch, validation_steps = get_data(batch_size=batch_size,
target_size=target_size,
class_mode=class_mode,
y_col=y_col )<choose_model_class>
for i in range(0, 12):
print(np.array(test_result[0][1][i*10:i*10+10]))
print("Compliling Model...")
 learning_rate = 0.0001
optimizer = Adam(learning_rate)
 loss = 'categorical_crossentropy'
metrics = ['accuracy']
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
 print("Completed Model Compilation.
 " )<define_variables>
pd.options.display.float_format = '{:.6f}'.format
df = pd.DataFrame(np.array(test_result[0]))
df.columns = data.classes
df.head()
steps_per_epoch = len(train_generator)
 validation_steps = len(validation_generator)
 verbose = 1
epochs = 30<train_model>
df.insert(0, "id", [e.name[:-4] for e in data.test_ds.x.items] )
<compute_test_metric><EOS> 
df.to_csv(f"dog-breed-identification-submission.csv", index=False )