Rabu, 19 Februari 2020

CNN 1 OUTPUT

app_finish2
In [11]:
import numpy as np 
import pandas as pd
import os
import glob
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [12]:
DATA_DIR = "img_new" 
TRAIN_TEST_SPLIT = 0.7
IM_WIDTH = IM_HEIGHT = 198
ID_TIPE_MAP = {0: 'Phospor', 1: 'Kalium', 2: 'Nitrogen', 3: 'Normal'}
TIPE_ID_MAP = dict((r, i) for i, r in ID_TIPE_MAP.items())
ID_TIPE_MAP, TIPE_ID_MAP
Out[12]:
({0: 'Phospor', 1: 'Kalium', 2: 'Nitrogen', 3: 'Normal'},
 {'Phospor': 0, 'Kalium': 1, 'Nitrogen': 2, 'Normal': 3})
In [13]:
def parse_filepath(filepath):
    try:
        path, filename = os.path.split(filepath)
        filename, ext = os.path.splitext(filename)
        tipe, _ = filename.split("_") 
        
        return ID_TIPE_MAP[int(tipe)]
    except Exception as e:
        print(filepath)
        return None
In [14]:
files = glob.glob(os.path.join(DATA_DIR, "*.jpg"))
attributes = list(map(parse_filepath, files))
In [15]:
df = pd.DataFrame(attributes)
df['file'] = files
df.columns = ['tipe', 'file']
df = df.dropna()
df.head()
Out[15]:
tipe file
0 Phospor img_new\0_pho.001.jpg
1 Phospor img_new\0_pho.002.jpg
2 Phospor img_new\0_pho.003.jpg
3 Phospor img_new\0_pho.004.jpg
4 Phospor img_new\0_pho.005.jpg
In [16]:
df.describe()
Out[16]:
tipe file
count 100 100
unique 4 100
top Normal img_new\0_pho.021.jpg
freq 25 1
In [17]:
df['tipe'].describe()
Out[17]:
count        100
unique         4
top       Normal
freq          25
Name: tipe, dtype: object
In [18]:
df.groupby(by=['tipe']).count().plot(kind='bar')
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x21d6a2c9e10>
In [19]:
p = np.random.permutation(len(df))
df['tipe_id'] = df['tipe'].map(lambda tipe: TIPE_ID_MAP[tipe])
df.head()
Out[19]:
tipe file tipe_id
0 Phospor img_new\0_pho.001.jpg 0
1 Phospor img_new\0_pho.002.jpg 0
2 Phospor img_new\0_pho.003.jpg 0
3 Phospor img_new\0_pho.004.jpg 0
4 Phospor img_new\0_pho.005.jpg 0
In [20]:
p = np.random.permutation(len(df))
df['tipe_id'] = df['tipe'].map(lambda tipe: TIPE_ID_MAP[tipe])
all_idx=p
print(all_idx)
[64 84 79 58 12 25 82 26 21 32 54 29  1 96 67 63 47 24 66 43 81 93  0 51
 68 33 88 42 94 62 16 57 95 48 30 59 74 75 69 40  2 14 85 19 35 46 23 76
 65 52 11 15 18 92 72 86 39  8 91  5 55 31 87 56 77 61 73 60 20 98  6 36
 28 27 71 70 44 49 83 80 41 99 10 38 78 37 13 22 89  4  3 97 17  9 45  7
 50 34 90 53]
In [21]:
from keras.utils import to_categorical
from PIL import Image

def get_data_generator(df, indices, for_training, batch_size=16):
    images, ltipe = [], []
    while True:
        for i in indices:
            r = df.iloc[i]
            file, t= r['file'],r['tipe_id']
            im = Image.open(file)
            im = im.resize((IM_WIDTH, IM_HEIGHT))
            im = np.array(im) / 255.0
            images.append(im)
            ltipe.append(to_categorical(t, len(TIPE_ID_MAP)))
            if len(images) >= batch_size:
                yield np.array(images), [np.array(ltipe)]
                images,ltipe = [],[]
        if not for_training:
            print("No")
            break
Using TensorFlow backend.

Model training

In [22]:
from keras.layers import Input, Dense, BatchNormalization, Conv2D, MaxPool2D, GlobalMaxPool2D, Dropout
from keras.optimizers import SGD
from keras.models import Model

def conv_block(inp, filters=32, bn=True, pool=True):
    _ = Conv2D(filters=filters, kernel_size=3, activation='relu')(inp)
    if bn:
        _ = BatchNormalization()(_)
    if pool:
        _ = MaxPool2D()(_)
    return _

input_layer = Input(shape=(IM_HEIGHT, IM_WIDTH, 3))
_ = conv_block(input_layer, filters=32, bn=False, pool=False)
_ = conv_block(_, filters=32*2)
_ = conv_block(_, filters=32*3)
_ = conv_block(_, filters=32*4)
_ = conv_block(_, filters=32*5)
_ = conv_block(_, filters=32*6)
bottleneck = GlobalMaxPool2D()(_)

# for tipe prediction
_ = Dense(units=128, activation='relu')(bottleneck)
tipe_output = Dense(units=len(TIPE_ID_MAP), activation='softmax', name='tipe_output')(_)


model = Model(inputs=input_layer, outputs=[tipe_output])
model.compile(optimizer='rmsprop', 
              loss={'tipe_output': 'categorical_crossentropy'},
              loss_weights={'tipe_output': 1.5},
              metrics={'tipe_output': 'accuracy'})
#model.summary()
WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:66: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:190: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:197: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:2041: The name tf.nn.fused_batch_norm is deprecated. Please use tf.compat.v1.nn.fused_batch_norm instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:4267: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From C:\Users\USER\Anaconda3\lib\site-packages\keras\optimizers.py:793: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

In [29]:
from keras.callbacks import ModelCheckpoint

data_size =25 #len(all_idx)
all_gen = get_data_generator(df, all_idx, for_training=True, batch_size=data_size)
valid_gen = get_data_generator(df, all_idx, for_training=True, batch_size=data_size)
callbacks = [
    ModelCheckpoint("./model_checkpoint", monitor='val_loss')
]

print(valid_gen)
print(all_gen)
<generator object get_data_generator at 0x0000021D716E5B10>
<generator object get_data_generator at 0x0000021D716E5A98>
In [32]:
history = model.fit_generator(all_gen,steps_per_epoch=len(all_idx)/data_size,epochs=5,callbacks=callbacks,validation_data=valid_gen,validation_steps= len(all_idx)/data_size)
#history = model.fit_generator(all_gen,steps_per_epoch=1,epochs=1,callbacks=callbacks,validation_data=valid_gen,validation_steps=1)

print(model.metrics_names)

print(len(all_idx))
print(len(all_idx)//data_size)
print(all_gen)
Epoch 1/5
4/4 [==============================] - 94s 24s/step - loss: 6.7925 - acc: 0.3100 - val_loss: 7.6092 - val_acc: 0.2800
Epoch 2/5
4/4 [==============================] - 87s 22s/step - loss: 1.6555 - acc: 0.6600 - val_loss: 1.1757 - val_acc: 0.6900
Epoch 3/5
4/4 [==============================] - 92s 23s/step - loss: 0.6840 - acc: 0.7500 - val_loss: 0.3845 - val_acc: 0.9000
Epoch 4/5
4/4 [==============================] - 89s 22s/step - loss: 0.3728 - acc: 0.8800 - val_loss: 0.5089 - val_acc: 0.8700
Epoch 5/5
4/4 [==============================] - 91s 23s/step - loss: 0.2068 - acc: 0.9600 - val_loss: 0.2983 - val_acc: 0.9500
['loss', 'acc']
100
4
<generator object get_data_generator at 0x0000021D716E5A98>
In [34]:
MM=model.evaluate_generator(all_gen, steps=11) # steps=len(test_idx)//128
MM
Out[34]:
[0.3100681711326946, 0.9454545324498956]
In [35]:
dict(zip(model.metrics_names, MM)) 
Out[35]:
{'loss': 0.3100681711326946, 'acc': 0.9454545324498956}
In [36]:
def uk(img):
    #print(img)
    uk1=len(img)
    uk2=len(img[0])
    uk3=''
    try:
        m=len(img[0][0])
        uk3="x"+str(m)
    except:
        uk3=0
        uk3=""
    
    print(type(img))
    print(str(uk1)+"x"+str(uk2)+uk3)
    return "OK"
In [37]:
x_test, tipe_true= next(all_gen)  
tipe_pred= model.predict_on_batch(x_test)
print(type(tipe_pred))
print(type(tipe_true))
<class 'numpy.ndarray'>
<class 'list'>
In [38]:
print(len(tipe_true))
print(len(tipe_pred))
print(len(x_test[0]))
1
25
198
In [39]:
LT1=tipe_true[0].tolist()
AT1=np.asarray(tipe_true[0], dtype=np.float32)

LT2=tipe_pred.tolist()
AT2=tipe_pred
In [40]:
uk(LT1)
uk(AT1)
uk(LT2)
uk(AT2)
LT1[0]
AT2[3]
<class 'list'>
25x4
<class 'numpy.ndarray'>
25x4
<class 'list'>
25x4
<class 'numpy.ndarray'>
25x4
Out[40]:
array([4.3394024e-04, 2.4629688e-02, 6.4191776e-03, 9.6851718e-01],
      dtype=float32)
In [41]:
tipe_true =AT1.argmax(axis=-1)
tipe_pred =AT2.argmax(axis=-1)
In [42]:
print(tipe_true)
print(tipe_pred)
print(len(tipe_pred))
[1 3 1 3 2 0 2 3 1 1 2 2 3 2 1 0 0 3 0 1 1 0 3 2 2]
[1 3 1 3 2 0 2 1 1 1 2 2 3 2 1 0 0 3 0 1 1 0 3 2 2]
25
In [43]:
from sklearn.metrics import classification_report
print("Classification report for tipe")
#print(classification_report(tipe_true2, tipe_pred))
print(classification_report(tipe_true,tipe_pred))
Classification report for tipe
              precision    recall  f1-score   support

           0       1.00      1.00      1.00         5
           1       0.88      1.00      0.93         7
           2       1.00      1.00      1.00         7
           3       1.00      0.83      0.91         6

    accuracy                           0.96        25
   macro avg       0.97      0.96      0.96        25
weighted avg       0.96      0.96      0.96        25

In [44]:
import math
n = data_size #30
random_indices = np.random.permutation(n)
n_cols = 5
n_rows = math.ceil(n / n_cols)
print(random_indices)
[20  8  5 22  1  4 15 12  3 21 24  2 11  9 23 16  6 14  7  0 19 10 13 18
 17]
In [46]:
benar=0
for i, img_idx in enumerate(random_indices):
    T=tipe_true[img_idx]
    P=tipe_pred[img_idx]
    V='No'
    if T==P:
        V='Yes'
        benar=benar+1
    print(str(T),' ', str(P),' ', str(V))
    
   
print('Benar:',str(benar))
acc=benar/n
print('Acc:',str(acc))
1   1   Yes
1   1   Yes
0   0   Yes
3   3   Yes
3   3   Yes
2   2   Yes
0   0   Yes
3   3   Yes
3   3   Yes
0   0   Yes
2   2   Yes
1   1   Yes
2   2   Yes
1   1   Yes
2   2   Yes
0   0   Yes
2   2   Yes
1   1   Yes
3   1   No
1   1   Yes
1   1   Yes
2   2   Yes
2   2   Yes
0   0   Yes
3   3   Yes
Benar: 24
Acc: 0.96
In [ ]:
 
In [ ]:
 

Tidak ada komentar:

Posting Komentar