Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<define_variables>
import numpy as np
import pandas as pd
import os
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Flatten, Dropout, BatchNormalization
from tensorflow.python.keras import optimizers, regularizers
from tensorflow.python.keras.applications import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.optimizers import sgd
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
label_map =(train_generator.class_indices)
 label_map_inv = {v: k for k, v in label_map.items() }<predict_on_test>
df=pd.read_csv('.. /input/labels.csv')
 test_df=pd.read_csv('.. /input/sample_submission.csv')
 image_size = 299
ypreds = model.predict_generator(generator=test_generator, steps = len(test_generator), verbose=1 )<prepare_output>
model = Sequential()
model.add(InceptionResNetV2(include_top=False, pooling='avg', weights="imagenet"))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.8))
model.add(Dense(120, activation='softmax'))
model.layers[0].trainable = False
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'] )
ypred = ypreds.argmax(axis=-1)
<define_variables>
datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rescale=1./255., horizontal_flip=True,width_shift_range = 0.2,height_shift_range = 0.2,validation_split=0.15)
 train_generator=datagen.flow_from_dataframe(dataframe=df,directory=".. /input/train/",x_col="id",y_col="breed",has_ext=False,subset="training",batch_size=16,seed=1997,shuffle=True,class_mode="categorical",target_size=(image_size, image_size))
valid_generator=datagen.flow_from_dataframe(dataframe=df,directory=".. /input/train/",x_col="id",y_col="breed",has_ext=False,subset="validation",batch_size=1,seed=1997,shuffle=True,class_mode="categorical",target_size=(image_size, image_size))
test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)
test_generator=test_datagen.flow_from_dataframe(dataframe=test_df,directory=".. /input/test/",x_col="id",y_col=None,has_ext=False,batch_size=1,seed=42,shuffle=False,class_mode=None,target_size=(image_size, image_size))
def get_rand_test_img(test_generator=None, labels_test=None, count=5):
filepaths = test_generator.filepaths
file_names = test_generator.filenames
selected_filepaths = []
selected_file_names = []
selected_labels = []
mem = set()
i = count
num_test_sample = test_generator.n
for file in test_generator.labels:
if i<=0:
break
rnd = random.randint(0, test_generator.n)
 while rnd in mem:
rnd = random.randint(0, test_generator.n)
 selected_filepaths.append(filepaths[rnd])
 selected_file_names.append(file_names[rnd])
 lbl = label_map_inv[labels_test[rnd]]
lbl = re.sub("_", " ", lbl)
 lbl = lbl.title()
selected_labels.append(lbl)
 i-=1
return selected_file_names, selected_file_names, selected_labels<split>
model.fit_generator(generator=train_generator,steps_per_epoch=train_generator.n,validation_data=valid_generator,validation_steps=train_generator.n,epochs=1 )
test_img_count = 10
labels_test = ypred
selected_file_names, selected_file_names, selected_labels = get_rand_test_img(test_generator=test_generator, labels_test=labels_test, count=test_img_count )<set_options>
pred=model.predict_generator(test_generator,verbose=1 )
<load_from_csv><EOS> 
labels =(train_generator.class_indices)
 labels = list(labels.keys())
 df = pd.DataFrame(data=pred,
columns=labels)
 columns = list(df)
 columns.sort()
df = df.reindex(columns=columns)
 filenames = test_df["id"]
df["id"] = filenames
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols]
df.head(5)
 df.to_csv("submission_InceptionResnet50.csv",index=False )