text_plain_output_1.png
import matplotlib.pyplot as plt

import os

import pandas as pd

import pathlib

import tensorflow as tf

data_path = pathlib.Path('../input/a-large-scale-fish-dataset/Fish_Dataset/Fish_Dataset')

all_images = list(data_path.glob('*/*/*.jpg')) + list(data_path.glob('*/*/*.png'))
images = []
labels = []
for item in all_images:
    path = os.path.normpath(item)
    splits = path.split(os.sep)
    if 'GT' not in splits[-2]:
        images.append(item)
        label = splits[-2]
        labels.append(label)

image_pathes = pd.Series(images).astype(str)
labels = pd.Series(labels)
dataframe = pd.concat([image_pathes, labels], axis=1)
dataframe.columns = ['images', 'labels']

fig, axes = plt.subplots(nrows=3, ncols=5, figsize=(15,10), subplot_kw={'xticks':[], 'yticks':[]})
for i, ax in enumerate(axes.flat):
    ax.imshow(plt.imread(dataframe.images[i]))
    ax.set_title(dataframe.labels[i])
    
plt.show()

training_data_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.0, rotation_range=40, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, horizontal_flip=True, vertical_flip=True)
training_generator = training_data_gen.flow_from_dataframe(dataframe=train, x_col='images', y_col='labels', target_size=(224, 224), color_mode='rgb', class_mode='categorical', batch_size=64)
val_data_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.0)
validation_generator = val_data_gen.flow_from_dataframe(dataframe=val, x_col='images', y_col='labels', target_size=(224, 224), color_mode='rgb', class_mode='categorical', batch_size=64)
test_data_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.0)
test_generator = test_data_gen.flow_from_dataframe(dataframe=test, x_col='images', y_col='labels', target_size=(224, 224), color_mode='rgb', class_mode='categorical', batch_size=64)

mlp_model = tf.keras.models.Sequential()
mlp_model.add(tf.keras.layers.Flatten(input_shape=(224, 224, 3)))
mlp_model.add(tf.keras.layers.Dense(256, activation='relu'))
mlp_model.add(tf.keras.layers.Dropout(0.4))
mlp_model.add(tf.keras.layers.Dense(256, activation='relu'))
mlp_model.add(tf.keras.layers.Dense(128, activation='relu'))
mlp_model.add(tf.keras.layers.Dense(9, activation='softmax'))

cnn_model = tf.keras.models.Sequential()
cnn_model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), strides=1, activation='relu', input_shape=(224, 224, 3)))
cnn_model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
cnn_model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
cnn_model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
cnn_model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
cnn_model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
cnn_model.add(tf.keras.layers.Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu'))
cnn_model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
cnn_model.add(tf.keras.layers.Conv2D(256, kernel_size=(3, 3), strides=1, activation='relu'))
cnn_model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
cnn_model.add(tf.keras.layers.Flatten())
cnn_model.add(tf.keras.layers.Dense(256, activation='relu'))
cnn_model.add(tf.keras.layers.Dropout(0.4))
cnn_model.add(tf.keras.layers.Dense(9, activation='softmax'))

cnn_model.summary()

cnn_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc'])

history = cnn_model.fit(training_generator, steps_per_epoch=99, validation_data=validation_generator, validation_steps=20, epochs=25)

cnn_train_loss = history.history['loss']
cnn_val_loss = history.history['val_loss']

train_acc = history.history['acc']
val_acc = history.history['val_acc']
plt.plot(history.epoch, train_acc, label='Training Accuracy')
plt.plot(history.epoch, val_acc, label='Validation Accuracy')
plt.grid(True)
plt.legend()