Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<import_modules>
print(check_output(["ls", ".. /input"] ).decode("utf8"))
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold<define_variables>
df_train = pd.read_csv('.. /input/dog-breed-identification/labels.csv')
 df_test = pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv' )
folds = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
 predicted = np.zeros(( test_data.shape[0],9))
measured= np.zeros(( train_data.shape[0]))
score = 0<compute_train_metric>
%matplotlib inline
for times,(trn_idx,val_idx)in enumerate(folds.split(train_data.values,target['surface'].values)) :
rf = RandomForestClassifier(n_estimators=500,n_jobs = -1)
 rf.fit(train_data.iloc[trn_idx],target['surface'][trn_idx])
 measured[val_idx] = rf.predict(train_data.iloc[val_idx])
 predicted += rf.predict_proba(test_data)/folds.n_splits
score += rf.score(train_data.iloc[val_idx],target['surface'][val_idx])
 print("Fold: {} score: {}".format(times,rf.score(train_data.iloc[val_idx],target['surface'][val_idx])))
 gc.collect()<compute_test_metric>
train_files = glob('.. /input/dog-breed-identification/train/*.jpg')
 test_files = glob('.. /input/dog-breed-identification/test/*.jpg' )
print('Avg.accuracy',score /folds.n_splits )<save_to_csv>
plt.imshow(plt.imread(train_files[100]))
submission['surface'] = le.inverse_transform(predicted.argmax(axis=1))
submission.to_csv('submission_92.csv',index=False)
 submission.head()<import_modules>
targets_series = pd.Series(df_train['breed'])
 one_hot = pd.get_dummies(targets_series, sparse = True)
 one_hot_labels = np.asarray(one_hot )
fastai.__version__<set_options>
im_size = 224
pd.options.display.max_rows = 20
pd.options.display.max_columns = None<define_variables>
y_train = []
y_val = []
x_train_raw = bcolz.zeros(( 0,im_size,im_size,3),np.float32)
 x_val_raw = bcolz.zeros(( 0,im_size,im_size,3),np.float32 )
path = Path('/kaggle/input/titanic-extended' )<set_options>
i = 0
for f, breed in tqdm(df_train.values):
image = load_img('.. /input/dog-breed-identification/train/{}.jpg'.format(f), target_size=(im_size, im_size))
image = img_to_array(image)
 label = one_hot_labels[i]
if random.randint(1,101)< 80:
x_train_raw.append(image)
 y_train.append(label)
 else:
x_val_raw.append(image)
 y_val.append(label)
 i += 1
path.ls()<load_from_csv>
y_train_raw = np.array(y_train, np.uint8)
 y_val_raw = np.array(y_val, np.uint8)
 del(y_train,y_val)
 gc.collect()
df = pd.read_csv(path/'train.csv', low_memory=False)
 df_test = pd.read_csv(path/'test.csv', low_memory=False )<define_variables>
num_class = y_train_raw.shape[1]
procs = [Categorify, FillMissing]<choose_model_class>
base_model = VGG19(weights = 'imagenet', include_top=False, input_shape=(im_size, im_size, 3))
base_model.summary()
splits = RandomSplitter(valid_pct=0.2 )(range_of(df))<define_variables>
fine_tuning_layers = 0
layers_to_freeze = len(base_model.layers)- fine_tuning_layers
print(layers_to_freeze)
 for layer in base_model.layers[0:layers_to_freeze]:
layer.trainable = False
dep_var='Survived'<split>
x = base_model.layers[layers_to_freeze-1+fine_tuning_layers].output
x = GlobalAveragePooling2D()(x)
 model = Model(inputs=base_model.input, outputs=x)
 model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model.summary()
cont,cat = cont_cat_split(df, 1, dep_var=dep_var )<create_dataframe>
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, data, labels, im_size = 224, batch_size=32, shuffle=True, data_augment = False, test = False):
'Initialization'
self.batch_size = batch_size
self.list_IDs = np.arange(0,data.shape[0])
 self.shuffle = shuffle
if self.shuffle == True:
np.random.shuffle(self.list_IDs)
 self.data = data
self.data_augment = data_augment
self.test = test
if self.test == False:
self.labels = labels
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs)/ self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.list_IDs[index*self.batch_size:(index+1)*self.batch_size]
if self.test == False:
X, y = self.__data_generation(indexes)
 return preprocess_input(X), y
else:
X = self.__data_generation(indexes)
 return preprocess_input(X)
 def on_epoch_end(self):
'Updates indexes after each epoch'
if self.shuffle == True:
np.random.shuffle(self.list_IDs)
 def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples'
X = np.zeros(( list_IDs_temp.shape[0],im_size,im_size,3), dtype=np.float32)
 if self.test == False:
y = np.zeros(( list_IDs_temp.shape[0],self.labels.shape[1]), dtype=np.uint8)
 for i, ID in enumerate(list_IDs_temp):
if self.data_augment == True:
if random.randint(1,101)< 50:
flip_horizontal = True
else:
flip_horizontal = False
if random.randint(1,101)< 50:
flip_vertical = True
else:
flip_vertical = False
tx = im_size*random.randint(1,2)/100.0
ty = im_size*random.randint(1,2)/100.0
shear = random.randint(1,10)/100.0
zx = random.randint(80,120)/100.0
zy = random.randint(80,120)/100.0
brightness = random.randint(1,2)/100.0
channel_shift_intensity = random.randint(1,10)/100.0
X[i,] = self.data[ID,]
else:
X[i,] = self.data[ID,]
if self.test == False:
y[i,] = self.labels[ID,]
if self.test == False:
return X, y
else:
return X
to = TabularPandas(df, procs, cat, cont, y_names=dep_var, splits=splits, y_block=CategoryBlock() )<prepare_x_and_y>
batch_size = 32
params_trn = {
'im_size': im_size,
'batch_size': batch_size,
'shuffle': True,
'data_augment' : True,
'test' : False
}
params_val = {
'im_size': im_size,
'batch_size': batch_size,
'shuffle': True,
'data_augment' : False,
'test' : False
}
training_generator = DataGenerator(x_train_raw, y_train_raw, **params_trn)
 validation_generator = DataGenerator(x_val_raw, y_val_raw, **params_val )
X_train, y_train = to.train.xs,to.train.y
X_valid, y_valid = to.valid.xs,to.valid.y<choose_model_class>
preds_trn = model.predict_generator(training_generator, steps = x_train_raw.shape[0], verbose=1)
 preds_val = model.predict_generator(validation_generator, steps = x_train_raw.shape[0], verbose=1 )
<train_model><EOS> 
sub = pd.DataFrame(preds_trn)
 sub.to_csv("preds_trn.csv",index =False)
 sub = pd.DataFrame(y_train_raw)
 sub.to_csv("labels_trn.csv",index =False)
 sub = pd.DataFrame(preds_val)
 sub.to_csv("preds_val.csv",index =False)
 sub = pd.DataFrame(y_val_raw)
 sub.to_csv("labels_val.csv",index =False)