image_output_2.png image_output_1.png
from tensorflow.keras import layers

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import os

import pandas as pd

import random

import tensorflow as tf

import tensorflow_hub as hub

train_dir = '../input/yoga-poses-dataset/DATASET/TRAIN'
test_dir = '../input/yoga-poses-dataset/DATASET/TEST'

def plot_yoga_images(train_dir):
    for i, col in enumerate(os.listdir(train_dir)):
        image = random.choice(os.listdir(train_dir + '/' + col))
        image_path = train_dir + '/' + col + '/' + image
        img = mpimg.imread(image_path) / 255
        plt.axis(False)
        i = i + 1

from tensorflow.keras.preprocessing.image import ImageDataGenerator
IMAGE_SHAPE = (224, 224)
BATCH_SIZE = 32
train_datagen = ImageDataGenerator(rescale=1 / 255.0)
test_datagen = ImageDataGenerator(rescale=1 / 255.0)
train_data = train_datagen.flow_from_directory(train_dir, target_size=IMAGE_SHAPE, batch_size=BATCH_SIZE, class_mode='categorical')
test_data = test_datagen.flow_from_directory(test_dir, target_size=IMAGE_SHAPE, batch_size=BATCH_SIZE, class_mode='categorical')

import tensorflow_hub as hub
from tensorflow.keras import layers
efficientnet_url = 'https://tfhub.dev/tensorflow/efficientnet/b0/feature-vector/1'
resnet_url = 'https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5'
def create_model(model_url, num_classes=5):
    feature_extractor_layer = hub.KerasLayer(model_url, trainable=False, name='feature_extraction_layer', input_shape=IMAGE_SHAPE + (3,))
    model = tf.keras.Sequential([feature_extractor_layer, layers.Dense(num_classes, activation='softmax', name='output_layer')])
    return model

def plot_loss(history):
    model_df = pd.DataFrame(history.history)
    loss = model_df.loss
    val_loss = model_df.val_loss
    accuracy = model_df.accuracy
    val_accuracy = model_df.val_accuracy
    epochs = range(len(model_df.loss))

resnet_model = create_model(resnet_url, num_classes=train_data.num_classes)
resnet_model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])
resnet_history = resnet_model.fit(train_data, epochs=5, steps_per_epoch=len(train_data), validation_data=test_data, validation_steps=len(test_data))

efficientnet_model = create_model(efficientnet_url, num_classes=train_data.num_classes)
efficientnet_model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])
efficientnet_history = efficientnet_model.fit(train_data, epochs=5, steps_per_epoch=len(train_data), validation_data=test_data, validation_steps=len(test_data))

plot_loss(efficientnet_history)