Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<load_from_csv>
root = '.. /input'
np.random.seed(2016)
 split_random_state = 7
split =.9
def load_numeric_training(standardize=True):

 data = pd.read_csv(os.path.join(root, 'train.csv'))
ID = data.pop('id')
 y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 X = StandardScaler().fit(data ).transform(data)if standardize else data.values
return ID, X, y
def load_numeric_test(standardize=True):

 test = pd.read_csv(os.path.join(root, 'test.csv'))
ID = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)if standardize else test.values
return ID, test
def resize_img(img, max_dim=96):

 max_ax = max(( 0, 1), key=lambda i: img.size[i])
 scale = max_dim / float(img.size[max_ax])
 return img.resize(( int(img.size[0] * scale), int(img.size[1] * scale)))
 def load_image_data(ids, max_dim=96, center=True):

 X = np.empty(( len(ids), max_dim, max_dim, 1))
for i, idee in enumerate(ids):
x = resize_img(load_img(os.path.join(root, 'images', str(idee)+ '.jpg'), grayscale=True), max_dim=max_dim)
 x = img_to_array(x)
 length = x.shape[0]
width = x.shape[1]
if center:
h1 = int(( max_dim - length)/ 2)
 h2 = h1 + length
w1 = int(( max_dim - width)/ 2)
 w2 = w1 + width
else:
h1, w1 = 0, 0
h2, w2 =(length, width)
 X[i, h1:h2, w1:w2, 0:1] = x
return np.around(X / 255.0)
 def load_train_data(split=split, random_state=None):

 ID, X_num_tr, y = load_numeric_training()
X_img_tr = load_image_data(ID)
 sss = StratifiedShuffleSplit(n_splits=1, train_size=split, random_state=random_state)
 train_ind, test_ind = next(sss.split(X_num_tr, y))
X_num_val, X_img_val, y_val = X_num_tr[test_ind], X_img_tr[test_ind], y[test_ind]
X_num_tr, X_img_tr, y_tr = X_num_tr[train_ind], X_img_tr[train_ind], y[train_ind]
return(X_num_tr, X_img_tr, y_tr),(X_num_val, X_img_val, y_val)
 def load_test_data() :

 ID, X_num_te = load_numeric_test()
X_img_te = load_image_data(ID)
 return ID, X_num_te, X_img_te
print('Loading the training data...')
(X_num_tr, X_img_tr, y_tr),(X_num_val, X_img_val, y_val)= load_train_data(random_state=split_random_state)
 y_tr_cat = to_categorical(y_tr)
 y_val_cat = to_categorical(y_val)
 print('Training data loaded!' )
df = pd.read_csv('.. /input/google-quest-challenge/test.csv')
 n = df['url'].apply(lambda x:(( 'ell.stackexchange.com' in x)or('english.stackexchange.com' in x)) ).tolist()
spelling = []
for x in n:
if x:
spelling.append(0.5)
 else:
spelling.append(0.)<save_to_csv>
class ImageDataGenerator2(ImageDataGenerator):
def flow(self, X, y=None, batch_size=32, shuffle=True, seed=None,
save_to_dir=None, save_prefix='', save_format='jpeg'):
return NumpyArrayIterator2(
 X, y, self,
batch_size=batch_size, shuffle=shuffle, seed=seed,
dim_ordering=self.dim_ordering,
save_to_dir=save_to_dir, save_prefix=save_prefix, save_format=save_format)
 class NumpyArrayIterator2(NumpyArrayIterator):
def next(self):
with self.lock:
self.index_array, current_index, current_batch_size = next(self.index_generator)
 batch_x = np.zeros(tuple([current_batch_size] + list(self.X.shape)[1:]))
for i, j in enumerate(self.index_array):
x = self.X[j]
x = self.image_data_generator.random_transform(x.astype('float32'))
x = self.image_data_generator.standardize(x)
 batch_x[i] = x
if self.save_to_dir:
for i in range(current_batch_size):
img = array_to_img(batch_x[i], self.dim_ordering, scale=True)
 fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
index=current_index + i,
hash=np.random.randint(1e4),
format=self.save_format)
 img.save(os.path.join(self.save_to_dir, fname))
if self.y is None:
return batch_x
batch_y = self.y[self.index_array]
return batch_x, batch_y
print('Creating Data Augmenter...')
 imgen = ImageDataGenerator2(
 rotation_range=20,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 imgen_train = imgen.flow(X_img_tr, y_tr_cat, seed=np.random.randint(1, 10000))
print('Finished making data augmenter...' )
sub[TARGET_COLS] = postprocess(np.mean(predictions, axis=0))
sub['question_type_spelling'] = spelling
sub.to_csv('submission.csv', index=False)
 sub.head()<define_variables>
def combined_model() :
image = Input(shape=(96, 96, 1), name='image')
 x = Convolution2D(8, 5, 5, input_shape=(96, 96, 1), border_mode='same' )(image)
 x =(Activation('relu'))(x)
 x =(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) )(x)
 x =(Convolution2D(32, 5, 5, border_mode='same'))(x)
 x =(Activation('relu'))(x)
 x =(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) )(x)
 x = Flatten()(x)
 numerical = Input(shape=(192,), name='numerical')
 concatenated = merge([x, numerical], mode='concat')
 x = Dense(100, activation='relu' )(concatenated)
 x = Dropout (.5 )(x)
 out = Dense(99, activation='softmax' )(x)
 model = Model(input=[image, numerical], output=out)
 model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
 return model
print('Creating the model...')
 model = combined_model()
print('Model created!' )
BS = 8
SEED = 42<install_modules>
def combined_generator(imgen, X):

 while True:
for i in range(X.shape[0]):
batch_img, batch_y = next(imgen)
 x = X[imgen.index_array]
yield [batch_img, x], batch_y
best_model_file = "leafnet.h5"
best_model = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=1, save_best_only=True)
 print('Training model...')
 history = model.fit_generator(combined_generator(imgen_train, X_num_tr),
samples_per_epoch=X_num_tr.shape[0],
nb_epoch=89,
validation_data=([X_img_val, X_num_val], y_val_cat),
nb_val_samples=X_num_val.shape[0],
verbose=0,
callbacks=[best_model])
 print('Loading the best model...')
 model = load_model(best_model_file)
 print('Best Model loaded!' )
<set_options><EOS> 
LABELS = sorted(pd.read_csv(os.path.join(root, 'train.csv')).species.unique())
 index, test, X_img_te = load_test_data()
yPred_proba = model.predict([X_img_te, test])
 yPred = pd.DataFrame(yPred_proba,index=index,columns=LABELS)
 print('Creating and writing submission...')
 fp = open('submit.csv', 'w')
 fp.write(yPred.to_csv())
 print('Finished writing submission')
 yPred.tail()