Kaggle competition: Dog Breed Identification
train_df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv' )<count_missing_values>
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Flatten, Dropout, BatchNormalization
import h5py
train_df.isnull().sum()<count_missing_values>
traindf=pd.read_csv('.. /input/dog-breed-identification/labels.csv')
 testdf=pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv' )
test_df.isnull().sum()<concatenate>
num_classes = 120
resnet_weights_path = '.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(512))
my_new_model.add(Activation('relu'))
my_new_model.add(Dropout(0.5))
my_new_model.add(Dense(num_classes, activation='softmax'))
my_new_model.layers[0].trainable = False
comp_df = pd.concat([train_df, test_df])
 comp_df.reset_index(drop=True, inplace=True )<count_missing_values>
my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'] )
comp_df.isnull().sum()<drop_column>
image_size = 224
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.25 )
comp_df.drop('PassengerId',axis=1, inplace=True)
 comp_df.drop('Cabin',axis=1,inplace=True)
 comp_df.drop("Ticket",axis=1, inplace=True )<categorify>
train_generator=datagen.flow_from_dataframe(
 dataframe=traindf,
directory=".. /input/dog-breed-identification/train/",
x_col="id",
y_col="breed",
has_ext=False,
subset="training",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(image_size, image_size))
p1 = comp_df[comp_df.Pclass==1]['Age'].median()
p2 = comp_df[comp_df.Pclass==2]['Age'].median()
p3 = comp_df[comp_df.Pclass==3]['Age'].median()
def fill_age(row):
if np.isnan(row.Age):
if row.Pclass == 1:
return p1
elif row.Pclass == 2:
return p2
elif row.Pclass == 3:
return p3
else:
return row.Age
comp_df.Age = comp_df.apply(fill_age, axis=1 )<filter>
valid_generator=datagen.flow_from_dataframe(
 dataframe=traindf,
directory=".. /input/dog-breed-identification/train/",
x_col="id",
y_col="breed",
has_ext=False,
subset="validation",
batch_size=1,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(image_size, image_size))
comp_df = comp_df[comp_df.Age<80]<count_missing_values>
test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)
test_generator=test_datagen.flow_from_dataframe(
 dataframe=testdf,
directory=".. /input/dog-breed-identification/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))
comp_df.Fare.isnull().sum()<correct_missing_values>
STEP_SIZE_TRAIN=train_generator.n
STEP_SIZE_VALID=valid_generator.n
print(STEP_SIZE_TRAIN)
 print(STEP_SIZE_VALID)
 my_new_model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=5
)
comp_df.Fare.fillna(comp_df.Fare.median() , inplace=True )<define_variables>
my_new_model.evaluate_generator(generator=valid_generator )
upper_limit = comp_df.Fare.quantile(0.75)+(1.5 * iqr(comp_df.Fare))
lower_limit = comp_df.Fare.quantile(0.25)-(1.5 * iqr(comp_df.Fare))<filter>
test_generator.reset()
pred=my_new_model.predict_generator(test_generator,verbose=1 )
<filter><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 = testdf["id"]
df["id"] = filenames
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols]
df.head(5)
 df.to_csv("results_tl_01.csv",index=False )