<jupyter_start><jupyter_text>leaf-classification https://www.kaggle.com/c/leaf-classification DescriptionThere are estimated to be nearly half a million species of plant in the world. Classification of species has been historically problematic and often results in duplicate identifications. Automating plant recognition might have many applications, including:- Species population tracking and preservation- Plant-based medicinal research- Crop and food supply management- Leaf ClassificationThe objective of this playground competition is to use binary leaf images and extracted features, including shape, margin & texture, to accurately identify 99 species of plants. Leaves, due to their volume, prevalence, and unique characteristics, are an effective means of differentiating plant species. They also provide a fun introduction to applying techniques that involve image-based features.As a first step, try building a classifier that uses the provided pre-extracted features. Next, try creating a set of your own features. Finally, examine the errors you're making and see what you can do to improve. AcknowledgmentsKaggle is hosting this competition for the data science community to use for fun and education. This dataset originates from leaf images collected by  <NAME>, <NAME>, <NAME>, & <NAME> of the Royal Botanic Gardens, Kew, UK.<NAME>, <NAME>, <NAME>. Plant Leaf Classification Using Probabilistic Integration of Shape, Texture and Margin Features. Signal Processing, Pattern Recognition and Applications, in press. 2013.We thank the UCI machine learning repository for hosting the dataset.<jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf 
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
train.head()
train.isna().sum()
train['species'].value_counts()
def get_map(data, name):
    mapp = {}
    num = 0
    for i in range(len(data)):
        if data[name][i] not in mapp:
            mapp[data[name][i]] = num
            num += 1
        else:
            pass
    return mapp
mapp = get_map(train,'species')

train_ = [train]
for data in train_:
    data['species'] = data['species'].map(mapp)<jupyter_output><empty_output><jupyter_text>Modelingi'm gonna using simple linear regression with batch normalization to classification<jupyter_code>drop = ['id', 'species']
trainX = train.drop(drop,axis=1)
trainY = train.species
trainX.shape, trainY.shape
trainX = np.array(trainX)
trainY = np.array(trainY).reshape(-1,1)
trainX.shape, trainY.shape<jupyter_output><empty_output><jupyter_text>Implement custom Batch Generator if you wanna see more about my batch generator https://github.com/che9992/BatchGenerator<jupyter_code>class BatchGenerator():
    where = 0

    def __init__(self, x, y, batch_size, one_hot = False, nb_classes = 0):
        self.nb_classes = nb_classes
        self.one_hot = one_hot
        self.x_ = x
        self.y_ = y
        self.batch_size = batch_size
        
        self.total_batch = int(len(x) / batch_size)
        self.x = self.x_[:batch_size,:]
        self.y = self.y_[:batch_size,:]
        self.where = batch_size
        
        if self.one_hot :
            self.set_one_hot()

    def next_batch(self):
        if self.where + self.batch_size > len(self.x_) :
            self.where = 0
            
        self.x = self.x_[self.where:self.where+self.batch_size,:]
        self.y = self.y_[self.where:self.where+self.batch_size,:]
        self.where += self.batch_size
        
        if self.one_hot:
            self.set_one_hot()
        
    def set_one_hot(self):
        self.y = np.int32(self.y)
        one_hot = np.array(self.y).reshape(-1)
        self.y = np.eye(self.nb_classes)[one_hot].astype(np.int)<jupyter_output><empty_output><jupyter_text>Using Batch nomalization before ReLu if you wanna see more about BN check it out https://github.com/che9992/Batch_Normalization<jupyter_code>def TO_FC_BN(X, size, phase, scope):
    with tf.variable_scope(scope):
        fc1 = tf.contrib.layers.fully_connected(X, size, activation_fn=None, scope='fully_connected', reuse=tf.AUTO_REUSE)
        fc2 = tf.contrib.layers.batch_norm(fc1, center = True, scale = True, is_training= phase, scope='bn', reuse=tf.AUTO_REUSE)
        return tf.nn.relu(fc2, name='relu')
    
def FC_2_NC(X, nb_classes, scope):
    return tf.contrib.layers.fully_connected(X, nb_classes, activation_fn=None, scope=scope, reuse=tf.AUTO_REUSE)    
tf.reset_default_graph() 
nb_classes = 99

phase = tf.placeholder(dtype=tf.bool, name = 'phase')
X = tf.placeholder(shape=[None, 192], dtype=tf.float32, name='X')
Y = tf.placeholder(shape=[None, nb_classes], dtype=tf.float32, name='Y')

FC1 = TO_FC_BN(X, 128, phase=phase, scope='layer1')
FC2 = TO_FC_BN(FC1, 256, phase=phase, scope='layer2')
FC3 = TO_FC_BN(FC2, 512, phase=phase, scope='layer3')
FC4 = TO_FC_BN(FC3, 1024, phase=phase, scope='layer4')
FC5 = TO_FC_BN(FC4, 1024, phase=phase, scope='layer5')
FC6 = TO_FC_BN(FC5, 512, phase=phase, scope='layer6')
FC7 = TO_FC_BN(FC6, 512, phase=phase, scope='layer7')
FC8 = TO_FC_BN(FC7, 256, phase=phase, scope='layer8')
logits = FC_2_NC(FC8, nb_classes, scope='logits')

with tf.name_scope('accuracy'):
    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.name_scope('cost'):
     cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits = logits, labels = Y))
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
    optimizer = tf.train.AdamOptimizer(1e-4).minimize(cost)


sess = tf.Session()
sess.run(tf.global_variables_initializer())

def train(epochs = 20, batch_size = 200):
    batch = BatchGenerator(trainX,trainY,batch_size=batch_size,nb_classes=nb_classes, one_hot=True)
    check = BatchGenerator(trainX,trainY,batch_size=len(trainX),nb_classes=nb_classes, one_hot=True)
    
    for epoch in range(epochs):
        avg_cost = 0
        
        for i in range(batch.total_batch):
            c, _ = sess.run([cost, optimizer], feed_dict= {'X:0': batch.x, 'Y:0': batch.y, 'phase:0': True})
            avg_cost += c / batch.total_batch
            batch.next_batch()
            
        if epoch % 100 == 0:
            print("Epoch:", '%04d,' % (epoch), 'cost = ', '{:.9f}'.format(avg_cost))
            train_acc = sess.run(accuracy,feed_dict={'X:0': check.x, 'Y:0': check.y, 'phase:0': True})
            print('Train Accuracy: {:.2f}% '.format(train_acc * 100))

train(500,10)
test.head()
test_ = test.drop('id',axis=1)
def testing():
    t = sess.run(tf.argmax(logits, 1), feed_dict={'X:0': test_, 'phase:0': False})
    return t
result = testing()
result = pd.DataFrame(result)
mapp_2 = {v: k for k, v in mapp.items()}
result_ = [result]
for data in result_:
    data[0] = data[0].map(mapp_2)
result.head()
r = np.array(result).reshape(-1)
r = pd.DataFrame(result, columns=r)
r.to_csv('result.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12405208184289598, 'fraction_numerical': 0.01917298612104736, 'mean_word_length': 4.003579098067287, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '625584', 'license': 'MIT', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2293, 'n_words': 597, 'path': 'main.ipynb', 'repo_name': 'che9992/kaggle-leaf-classification', 'size': '7034'}