text_plain_output_1.png
from PIL import ImageFile

from keras.applications import vgg16

from keras.layers import Activation, Dropout, Flatten, Dense

from keras.models import Model

from keras.optimizers import Adam

from keras.preprocessing.image import ImageDataGenerator

from sklearn.metrics import confusion_matrix

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

train_path = '../input/yoga-poses-dataset/DATASET/TRAIN'
test_path = '../input/yoga-poses-dataset/DATASET/TEST'

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1 / 255.0, rotation_range=10, zoom_range=0.3, shear_range=0.3, fill_mode='nearest', validation_split=0.2)
test_datagen = ImageDataGenerator(rescale=1 / 255.0)

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
batch_size = 8
train_generator = train_datagen.flow_from_directory(directory=train_path, color_mode='rgb', target_size=(224, 224), batch_size=batch_size, class_mode='sparse', subset='training', shuffle=True, seed=42)
validation_generator = train_datagen.flow_from_directory(directory=train_path, color_mode='rgb', target_size=(224, 224), batch_size=batch_size, class_mode='sparse', subset='validation', shuffle=True, seed=42)
test_generator = test_datagen.flow_from_directory(directory=test_path, target_size=(224, 224), color_mode='rgb', batch_size=batch_size, class_mode='sparse', shuffle=False, seed=42)

from keras.applications import vgg16
base_model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling='max')

for layer in base_model.layers[:-5]:
    layer.trainable = False

from keras.models import Model
from keras.layers import Activation, Dropout, Flatten, Dense
last_output = base_model.output
x = Dense(train_generator.num_classes, activation='softmax', name='softmax')(last_output)
model = Model(inputs=base_model.input, outputs=x)
model.summary()

from keras.optimizers import Adam
epochs = 10
learning_rate = 0.0001
opt = Adam(learning_rate=learning_rate, decay=learning_rate / (epochs * 0.5))
model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

history = model.fit(train_generator, validation_data=validation_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, validation_steps=validation_generator.n // validation_generator.batch_size, epochs=10)

plt.gca().set_ylim(0, 1)

score = model.evaluate(test_generator)

predict = np.argmax(model.predict(test_generator), axis=1)

cnf_matrix = confusion_matrix(test_generator.labels, predict)

ax = plt.subplot()
sns.heatmap(cnf_matrix, annot=True, fmt='g', ax=ax)
ax.set_xlabel('Predicted labels')
ax.set_ylabel('True labels')
ax.set_title('Confusion Matrix')
ax.xaxis.set_ticklabels(['downdog', 'goddess', 'plank', 'tree', 'warrior2'])
ax.yaxis.set_ticklabels(['downdog', 'goddess', 'plank', 'tree', 'warrior2'])