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]:
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]:
In [16]:
df.describe()
Out[16]:
In [17]:
df['tipe'].describe()
Out[17]:
In [18]:
df.groupby(by=['tipe']).count().plot(kind='bar')
Out[18]:
In [19]:
p = np.random.permutation(len(df))
df['tipe_id'] = df['tipe'].map(lambda tipe: TIPE_ID_MAP[tipe])
df.head()
Out[19]:
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)
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
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()
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)
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)
In [34]:
MM=model.evaluate_generator(all_gen, steps=11) # steps=len(test_idx)//128
MM
Out[34]:
In [35]:
dict(zip(model.metrics_names, MM))
Out[35]:
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))
In [38]:
print(len(tipe_true))
print(len(tipe_pred))
print(len(x_test[0]))
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]
Out[40]:
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))
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))
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)
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))
In [ ]:
In [ ]:
Tidak ada komentar:
Posting Komentar