Kaggle competition: Dog Breed Identification
data['Age']=data['Age'].astype(int)
 data.drop(labels=["SibSp","Parch","Cabin","Fare","Age", "Ticket", "Name", "PassengerId"], axis=1, inplace = True)
 data.head()<categorify>
%matplotlib inline
sns.set_style('whitegrid')
data = pd.get_dummies(data,columns=["Pclass"])
 data = pd.get_dummies(data,columns=["Embarked"])
 data = pd.get_dummies(data,columns=["Family"])
 data = pd.get_dummies(data,columns=["Age_Limit"])
 data = pd.get_dummies(data,columns=["Fare_Limit"])
 data = pd.get_dummies(data,columns=["Title"])
 data.head()<import_modules>
DATA_DIR = '.. /input/dog-breed-identification/'
TRAIN_DIR = DATA_DIR + 'train/'
TEST_DIR = DATA_DIR + 'test/'
BATCH_SIZE = 128
INPUT_SIZE = 299
NUM_CLASSES = 120
SEED = 42
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC<train_model>
breeds_20 = train.breed.value_counts().head(NUM_CLASSES ).index
train = train[train.breed.isin(breeds_20)]
if len(data)==(len(train_data)+ len(test_data)) :
print("success" )<drop_column>
breed_labels = list(breeds_20.values )
test = data[len(train_data):]
test.drop(labels="Survived", axis=1, inplace=True )<split>
def create_model(lr=0.0001, dropout=None):
model = Sequential()
base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(INPUT_SIZE, INPUT_SIZE, 3))
for layer in base.layers:
layer.trainable = False
model.add(base)
 model.add(GlobalAveragePooling2D())
 model.add(Dense(NUM_CLASSES, activation='softmax'))
adam = Adam(lr=lr)
 sgd = SGD(lr=0.1, momentum=0.95, nesterov=False)
 model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['acc'],)
 return model
train = data[:len(train_data)]
X_train = train.drop(labels = "Survived", axis=1)
 y_train = train["Survived"]
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.3, random_state=42)
<train_model>
train_set, val_set = train_test_split(train, test_size=0.20, stratify=train['breed'], random_state=SEED )
log_reg = LogisticRegression(random_state=42)
 log_reg.fit(X_train, y_train)
 print("Accuracy: ", log_reg.score(X_test,y_test))<train_model>
gc.collect()
model = create_model()
train_generator.reset()
valid_generator.reset()
STEP_SIZE_TRAIN = train_set.shape[0] // BATCH_SIZE
STEP_SIZE_VALID = val_set.shape[0] // BATCH_SIZE
EPOCHS = 1
base_lr=0.0001
max_lr=1
step_size = EPOCHS * STEP_SIZE_TRAIN
clr = CyclicLR(base_lr=base_lr, max_lr=max_lr, step_size=step_size)
 model.fit_generator(train_generator,
epochs=EPOCHS,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
callbacks=[clr]
)
rf_reg = RandomForestClassifier(random_state=42)
 rf_reg.fit(X_train, y_train)
 print("Accuracy: ", rf_reg.score(X_test,y_test))<train_model>
gc.collect()
EPOCHS=40
STEP_SIZE_TRAIN = train_set.shape[0] // BATCH_SIZE
STEP_SIZE_VALID = val_set.shape[0] // BATCH_SIZE
train_generator.reset()
valid_generator.reset()
clr = CyclicLR(base_lr=0.04, max_lr=0.11, step_size=2*STEP_SIZE_TRAIN, mode='triangular')
 checkpoint = ModelCheckpoint('dog_breed_inceptionv3.hf5', monitor='val_acc', verbose=0, save_best_only=True, mode='max', save_weights_only=False)
 reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, verbose=1, min_lr = 0.00001)
 history = model.fit_generator(train_generator,
epochs=EPOCHS,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
callbacks=[checkpoint, clr]
)
svm_clsf = SVC()
svm_clsf.fit(X_train, y_train)
 print("Accuracy: ", svm_clsf.score(X_test,y_test))<train_model>
FileLink('dog_breed_inceptionv3.hf5' )
best_knn = []
for n in range(1,12):
knn = KNeighborsClassifier(n_neighbors=n)
 knn.fit(X_train, y_train)
 best_knn.insert(n, knn.score(X_test,y_test))
best_knn 
<train_model>
model.load_weights('dog_breed_inceptionv3.hf5')
 test_datagen = ImageDataGenerator(rescale=1./255)
 test_generator = test_datagen.flow_from_dataframe(dataframe=submission,
directory=TEST_DIR,
x_col='id',
class_mode=None,
has_ext=False,
batch_size=BATCH_SIZE,
shuffle=False,
seed=SEED,
target_size=(INPUT_SIZE, INPUT_SIZE)
)
 predictions = model.predict_generator(test_generator, verbose=1 )
knn_clsf = KNeighborsClassifier(n_neighbors=8)
 knn_clsf.fit(X_train, y_train)
 print("Accuracy: ", knn_clsf.score(X_test,y_test))<train_model>
submission.loc[:,1:] = predictions
submission.head()
<save_to_csv><EOS> 
submission.to_csv('submission.csv', index=False)
 FileLink('submission.csv' )