application_vnd.jupyter.stderr_output_3.png text_plain_output_2.png text_plain_output_1.png image_output_3.png image_output_2.png image_output_1.png
from glob import glob

from keras.applications.vgg16 import VGG16

from keras.layers import Input, Lambda, Dense, Flatten

from keras.models import Model

from keras.preprocessing import image

from keras.preprocessing.image import ImageDataGenerator

from sklearn.metrics import confusion_matrix

from utils import plot_confusion_matrix

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

IMAGE_SIZE = [100, 100]
epochs = 5
batch_size = 32
train_path = '../input/fruits/fruits-360/Training'
valid_path = '../input/fruits/fruits-360/Test'
image_files = glob(train_path + '/*/*.jp*g')
valid_image_files = glob(valid_path + '/*/*.jp*g')
folders = glob(train_path + '/*')
plt.imshow(image.load_img(np.random.choice(image_files)))
plt.show()
vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
for layer in vgg.layers:
    layer.trainable = False
x = Flatten()(vgg.output)
prediction = Dense(len(folders), activation='softmax')(x)
model = Model(inputs=vgg.input, outputs=prediction)
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
gen = ImageDataGenerator(rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, vertical_flip=True)
test_gen = gen.flow_from_directory(valid_path, target_size=IMAGE_SIZE)
print(test_gen.class_indices)
labels = [None] * len(test_gen.class_indices)
for k, v in test_gen.class_indices.items():
    labels[v] = k
for x, y in test_gen:
    print('min:', x[0].min(), 'max:', x[0].max())
    plt.title(labels[np.argmax(y[0])])
    plt.imshow(x[0])
    plt.show()
    break
train_generator = gen.flow_from_directory(train_path, target_size=IMAGE_SIZE, shuffle=True, batch_size=batch_size)
valid_generator = gen.flow_from_directory(valid_path, target_size=IMAGE_SIZE, shuffle=True, batch_size=batch_size)
r = model.fit_generator(train_generator, validation_data=valid_generator, epochs=epochs, steps_per_epoch=len(image_files) // batch_size, validation_steps=len(valid_image_files) // batch_size)
def get_confusion_matrix(data_path, N):
    print('Generating Confusion Matrix', N)
    predictions = []
    targets = []
    i = 0
    for x, y in gen.flow_from_directory(data_path, target_size=IMAGE_SIZE, shuffle=False, batch_size=batch_size * 2):
        i += 1
        if i % 50 == 0:
            print(i)
        p = model.predict(x)
        p = np.argmax(p, axis=1)
        y = np.argmax(y, axis=1)
        predictions = np.concatenate((predictions, p))
        targets = np.concatenate((targets, y))
        if len(targets) >= N:
            break
        cm = confusion_matrix(targets, predictions)
        return cm
cm = get_confusion_matrix(train_path, len(image_files))
print(cm)
valid_cm = get_confusion_matrix(valid_path, len(valid_image_files))
print(valid_cm)
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.plot(r.history['acc'], label='train acc')
plt.plot(r.history['val_acc'], label='val acc')
plt.legend()
plt.show()
from utils import plot_confusion_matrix
plot_confusion_matrix(cm, labels, title='Train Confusion Matrix')
plot_confusion_matrix(valid_cm, labels, title='Validation Confusion Matrix')