#--------------------------------------------------------------------
#  Fine Tuning Model
#--------------------------------------------------------------------

import matplotlib
matplotlib.use("Agg")

# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import pickle
import cv2
import os

# dataset gambar
dataset = "dataset"

# model output
omodel = "./model/modelku.h5"

# label output
olabel = "./output/label.lb"

# set epoch
setepoch = 1

# plot output
oplot = "./output/plot.png"

# inisialisasi label yang ingin kita latih
LABELS = set(["blackspot", "canker", "greening", "healthy", "scab"])

# ambil gambar yang ingin kita latih
print("[INFO] loading images...")
imagePaths = list(paths.list_images(dataset))
data = []
labels = []

# loop ke semua image dalam path
for imagePath in imagePaths:
	# ambil labelnya dari path (folder)
	label = imagePath.split(os.path.sep)[-2]

	# kalau labelnya tidak ada dalam list tidak usah diambil
	if label not in LABELS:
		continue

	# ambil image, convert ke RGB (tadinya BGR), resize
	# fix 224x224 pixel, tidak melihat aspect ratio
	image = cv2.imread(imagePath)
	image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
	image = cv2.resize(image, (224, 224))

	# update data dan label di list, berurutan
	data.append(image)
	labels.append(label)

# konversi data dan label menjadi NumPy array
data = np.array(data)
labels = np.array(labels)

# lakukan one-hot encode ke label (jadikan binarizer)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)

# partisi date menjadi train dan test menjadi
# 75% training dan 25% testing
(trainX, testX, trainY, testY) = train_test_split(data, labels,
	test_size=0.25, stratify=labels, random_state=42)

# inisialisai data training menjadi data augmentation
trainAug = ImageDataGenerator(
	rotation_range=30,
	zoom_range=0.15,
	width_shift_range=0.2,
	height_shift_range=0.2,
	shear_range=0.15,
	horizontal_flip=True,
	fill_mode="nearest")

# inisialisasi data validasi augmentasi (yang akan kita gunakan)
valAug = ImageDataGenerator()

# define the ImageNet mean subtraction (in RGB order) and set the
# lakukan ImageNet mean substraction (urutan RGB) dan set nilai mean substraction
# untuk setiap data augmentation
mean = np.array([123.68, 116.779, 103.939], dtype="float32")
trainAug.mean = mean
valAug.mean = mean

# buka ResNet-50, pastikan FC layer dimatikan
baseModel = ResNet50(weights="imagenet", include_top=False,
	input_tensor=Input(shape=(224, 224, 3)))

# buat head model yang akan kita letakkan diatas base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation="softmax")(headModel)

# letakkan model kita (FC) ini akan menjadi model yang akan kita latih
model = Model(inputs=baseModel.input, outputs=headModel)

# lakukan pelatihan ke semua layer di base model dan freeze
# sehingga base model tidak di update pada saat pelatihan
for layer in baseModel.layers:
	layer.trainable = False

# compile our model
print("[INFO] compiling model...")
opt = SGD(lr=1e-4, momentum=0.9, decay=1e-4 / setepoch)
model.compile(loss="categorical_crossentropy", optimizer=opt,
	metrics=["accuracy"])

print("[INFO] training head...")
H = model.fit(
	x=trainAug.flow(trainX, trainY, batch_size=32),
	steps_per_epoch=len(trainX) // 32,
	validation_data=valAug.flow(testX, testY),
	validation_steps=len(testX) // 32,
	epochs=setepoch)

# evaluasi
print("[INFO] evaluating network...")
predictions = model.predict(x=testX.astype("float32"), batch_size=32)
print(classification_report(testY.argmax(axis=1),
	predictions.argmax(axis=1), target_names=lb.classes_))

# plot train loss grafik
N = setepoch
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy on Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig(oplot)

# save model
print("[INFO] serializing network...")
model.save(omodel, save_format="h5")

# save label
f = open(olabel, "wb")
f.write(pickle.dumps(lb))
f.close()

a9270d97733c097dd8af82d2663e0cf37ed52a17
<mask token>

<mask token>
matplotlib.use('Agg')
<mask token>
print('[INFO] loading images...')
<mask token>
for imagePath in imagePaths:
    label = imagePath.split(os.path.sep)[-2]
    if label not in LABELS:
        continue
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (224, 224))
    data.append(image)
    labels.append(label)
<mask token>
for layer in baseModel.layers:
    layer.trainable = False
print('[INFO] compiling model...')
<mask token>
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=[
    'accuracy'])
print('[INFO] training head...')
<mask token>
print('[INFO] evaluating network...')
<mask token>
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1
    ), target_names=lb.classes_))
<mask token>
plt.style.use('ggplot')
plt.figure()
plt.plot(np.arange(0, N), H.history['loss'], label='train_loss')
plt.plot(np.arange(0, N), H.history['val_loss'], label='val_loss')
plt.plot(np.arange(0, N), H.history['acc'], label='train_acc')
plt.plot(np.arange(0, N), H.history['val_acc'], label='val_acc')
plt.title('Training Loss and Accuracy on Dataset')
plt.xlabel('Epoch #')
plt.ylabel('Loss/Accuracy')
plt.legend(loc='lower left')
plt.savefig(oplot)
print('[INFO] serializing network...')
model.save(omodel, save_format='h5')
<mask token>
f.write(pickle.dumps(lb))
f.close()

<mask token>
matplotlib.use('Agg')
<mask token>
dataset = 'dataset'
omodel = './model/modelku.h5'
olabel = './output/label.lb'
setepoch = 1
oplot = './output/plot.png'
LABELS = set(['blackspot', 'canker', 'greening', 'healthy', 'scab'])
print('[INFO] loading images...')
imagePaths = list(paths.list_images(dataset))
data = []
labels = []
for imagePath in imagePaths:
    label = imagePath.split(os.path.sep)[-2]
    if label not in LABELS:
        continue
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (224, 224))
    data.append(image)
    labels.append(label)
data = np.array(data)
labels = np.array(labels)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
trainX, testX, trainY, testY = train_test_split(data, labels, test_size=
    0.25, stratify=labels, random_state=42)
trainAug = ImageDataGenerator(rotation_range=30, zoom_range=0.15,
    width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
    horizontal_flip=True, fill_mode='nearest')
valAug = ImageDataGenerator()
mean = np.array([123.68, 116.779, 103.939], dtype='float32')
trainAug.mean = mean
valAug.mean = mean
baseModel = ResNet50(weights='imagenet', include_top=False, input_tensor=
    Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name='flatten')(headModel)
headModel = Dense(512, activation='relu')(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation='softmax')(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
    layer.trainable = False
print('[INFO] compiling model...')
opt = SGD(lr=0.0001, momentum=0.9, decay=0.0001 / setepoch)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=[
    'accuracy'])
print('[INFO] training head...')
H = model.fit(x=trainAug.flow(trainX, trainY, batch_size=32),
    steps_per_epoch=len(trainX) // 32, validation_data=valAug.flow(testX,
    testY), validation_steps=len(testX) // 32, epochs=setepoch)
print('[INFO] evaluating network...')
predictions = model.predict(x=testX.astype('float32'), batch_size=32)
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1
    ), target_names=lb.classes_))
N = setepoch
plt.style.use('ggplot')
plt.figure()
plt.plot(np.arange(0, N), H.history['loss'], label='train_loss')
plt.plot(np.arange(0, N), H.history['val_loss'], label='val_loss')
plt.plot(np.arange(0, N), H.history['acc'], label='train_acc')
plt.plot(np.arange(0, N), H.history['val_acc'], label='val_acc')
plt.title('Training Loss and Accuracy on Dataset')
plt.xlabel('Epoch #')
plt.ylabel('Loss/Accuracy')
plt.legend(loc='lower left')
plt.savefig(oplot)
print('[INFO] serializing network...')
model.save(omodel, save_format='h5')
f = open(olabel, 'wb')
f.write(pickle.dumps(lb))
f.close()

import matplotlib
matplotlib.use('Agg')
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import pickle
import cv2
import os
dataset = 'dataset'
omodel = './model/modelku.h5'
olabel = './output/label.lb'
setepoch = 1
oplot = './output/plot.png'
LABELS = set(['blackspot', 'canker', 'greening', 'healthy', 'scab'])
print('[INFO] loading images...')
imagePaths = list(paths.list_images(dataset))
data = []
labels = []
for imagePath in imagePaths:
    label = imagePath.split(os.path.sep)[-2]
    if label not in LABELS:
        continue
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (224, 224))
    data.append(image)
    labels.append(label)
data = np.array(data)
labels = np.array(labels)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
trainX, testX, trainY, testY = train_test_split(data, labels, test_size=
    0.25, stratify=labels, random_state=42)
trainAug = ImageDataGenerator(rotation_range=30, zoom_range=0.15,
    width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
    horizontal_flip=True, fill_mode='nearest')
valAug = ImageDataGenerator()
mean = np.array([123.68, 116.779, 103.939], dtype='float32')
trainAug.mean = mean
valAug.mean = mean
baseModel = ResNet50(weights='imagenet', include_top=False, input_tensor=
    Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name='flatten')(headModel)
headModel = Dense(512, activation='relu')(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation='softmax')(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
    layer.trainable = False
print('[INFO] compiling model...')
opt = SGD(lr=0.0001, momentum=0.9, decay=0.0001 / setepoch)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=[
    'accuracy'])
print('[INFO] training head...')
H = model.fit(x=trainAug.flow(trainX, trainY, batch_size=32),
    steps_per_epoch=len(trainX) // 32, validation_data=valAug.flow(testX,
    testY), validation_steps=len(testX) // 32, epochs=setepoch)
print('[INFO] evaluating network...')
predictions = model.predict(x=testX.astype('float32'), batch_size=32)
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1
    ), target_names=lb.classes_))
N = setepoch
plt.style.use('ggplot')
plt.figure()
plt.plot(np.arange(0, N), H.history['loss'], label='train_loss')
plt.plot(np.arange(0, N), H.history['val_loss'], label='val_loss')
plt.plot(np.arange(0, N), H.history['acc'], label='train_acc')
plt.plot(np.arange(0, N), H.history['val_acc'], label='val_acc')
plt.title('Training Loss and Accuracy on Dataset')
plt.xlabel('Epoch #')
plt.ylabel('Loss/Accuracy')
plt.legend(loc='lower left')
plt.savefig(oplot)
print('[INFO] serializing network...')
model.save(omodel, save_format='h5')
f = open(olabel, 'wb')
f.write(pickle.dumps(lb))
f.close()

#--------------------------------------------------------------------
#  Fine Tuning Model
#--------------------------------------------------------------------

import matplotlib
matplotlib.use("Agg")

# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import pickle
import cv2
import os

# dataset gambar
dataset = "dataset"

# model output
omodel = "./model/modelku.h5"

# label output
olabel = "./output/label.lb"

# set epoch
setepoch = 1

# plot output
oplot = "./output/plot.png"

# inisialisasi label yang ingin kita latih
LABELS = set(["blackspot", "canker", "greening", "healthy", "scab"])

# ambil gambar yang ingin kita latih
print("[INFO] loading images...")
imagePaths = list(paths.list_images(dataset))
data = []
labels = []

# loop ke semua image dalam path
for imagePath in imagePaths:
	# ambil labelnya dari path (folder)
	label = imagePath.split(os.path.sep)[-2]

	# kalau labelnya tidak ada dalam list tidak usah diambil
	if label not in LABELS:
		continue

	# ambil image, convert ke RGB (tadinya BGR), resize
	# fix 224x224 pixel, tidak melihat aspect ratio
	image = cv2.imread(imagePath)
	image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
	image = cv2.resize(image, (224, 224))

	# update data dan label di list, berurutan
	data.append(image)
	labels.append(label)

# konversi data dan label menjadi NumPy array
data = np.array(data)
labels = np.array(labels)

# lakukan one-hot encode ke label (jadikan binarizer)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)

# partisi date menjadi train dan test menjadi
# 75% training dan 25% testing
(trainX, testX, trainY, testY) = train_test_split(data, labels,
	test_size=0.25, stratify=labels, random_state=42)

# inisialisai data training menjadi data augmentation
trainAug = ImageDataGenerator(
	rotation_range=30,
	zoom_range=0.15,
	width_shift_range=0.2,
	height_shift_range=0.2,
	shear_range=0.15,
	horizontal_flip=True,
	fill_mode="nearest")

# inisialisasi data validasi augmentasi (yang akan kita gunakan)
valAug = ImageDataGenerator()

# define the ImageNet mean subtraction (in RGB order) and set the
# lakukan ImageNet mean substraction (urutan RGB) dan set nilai mean substraction
# untuk setiap data augmentation
mean = np.array([123.68, 116.779, 103.939], dtype="float32")
trainAug.mean = mean
valAug.mean = mean

# buka ResNet-50, pastikan FC layer dimatikan
baseModel = ResNet50(weights="imagenet", include_top=False,
	input_tensor=Input(shape=(224, 224, 3)))

# buat head model yang akan kita letakkan diatas base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation="softmax")(headModel)

# letakkan model kita (FC) ini akan menjadi model yang akan kita latih
model = Model(inputs=baseModel.input, outputs=headModel)

# lakukan pelatihan ke semua layer di base model dan freeze
# sehingga base model tidak di update pada saat pelatihan
for layer in baseModel.layers:
	layer.trainable = False

# compile our model
print("[INFO] compiling model...")
opt = SGD(lr=1e-4, momentum=0.9, decay=1e-4 / setepoch)
model.compile(loss="categorical_crossentropy", optimizer=opt,
	metrics=["accuracy"])

print("[INFO] training head...")
H = model.fit(
	x=trainAug.flow(trainX, trainY, batch_size=32),
	steps_per_epoch=len(trainX) // 32,
	validation_data=valAug.flow(testX, testY),
	validation_steps=len(testX) // 32,
	epochs=setepoch)

# evaluasi
print("[INFO] evaluating network...")
predictions = model.predict(x=testX.astype("float32"), batch_size=32)
print(classification_report(testY.argmax(axis=1),
	predictions.argmax(axis=1), target_names=lb.classes_))

# plot train loss grafik
N = setepoch
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy on Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig(oplot)

# save model
print("[INFO] serializing network...")
model.save(omodel, save_format="h5")

# save label
f = open(olabel, "wb")
f.write(pickle.dumps(lb))
f.close()
