Cainvas

FISH BREED CLASSIFICATION

IMPORT LIBRARY AND DATA

In [3]:
!pip install wget
import numpy as np 
import pandas as pd
import os
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
from sklearn.model_selection import StratifiedShuffleSplit,train_test_split
import glob
import tensorflow as tf
from sklearn import metrics
from tensorflow import keras
!wget -N 'https://cainvas-static.s3.amazonaws.com/media/user_data/devanshchowd/fish.tar.gz'
import tarfile
file = tarfile.open('fish.tar.gz')
file.extractall('./')
file.close()
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: wget in ./.local/lib/python3.7/site-packages (3.2)
WARNING: You are using pip version 20.3.1; however, version 21.1.3 is available.
You should consider upgrading via the '/opt/tljh/user/bin/python -m pip install --upgrade pip' command.
--2021-07-06 18:53:46--  https://cainvas-static.s3.amazonaws.com/media/user_data/devanshchowd/fish.tar.gz
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.62.64
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.62.64|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 70613423 (67M) [application/gzip]
Saving to: ‘fish.tar.gz’

fish.tar.gz         100%[===================>]  67.34M   100MB/s    in 0.7s    

2021-07-06 18:53:47 (100 MB/s) - ‘fish.tar.gz’ saved [70613423/70613423]

BASIC ANALYSIS

In [23]:
data = pd.read_csv('fish/data.csv')
display(data)
sns.set_style('darkgrid')
sns.countplot(data = data,y = 'label')
path label
0 fish/Black Sea Sprat/00033.png Black Sea Sprat
1 fish/Black Sea Sprat/00004.png Black Sea Sprat
2 fish/Black Sea Sprat/00023.png Black Sea Sprat
3 fish/Black Sea Sprat/00041.png Black Sea Sprat
4 fish/Black Sea Sprat/00024.png Black Sea Sprat
... ... ...
425 fish/Gilt Head Bream/14.JPG Gilt Head Bream
426 fish/Gilt Head Bream/18.JPG Gilt Head Bream
427 fish/Gilt Head Bream/49.JPG Gilt Head Bream
428 fish/Gilt Head Bream/2.JPG Gilt Head Bream
429 fish/Gilt Head Bream/25.JPG Gilt Head Bream

430 rows × 2 columns

Out[23]:
<AxesSubplot:xlabel='count', ylabel='label'>
In [109]:
def get_img(index):
    img = plt.imread(data.iloc[index]['path'])
    return img
demo = pd.DataFrame(data.groupby('label').first()).reset_index()
fig,ax = plt.subplots(3,3,figsize=(15,15))
for i in range(3):
    for j in range(3):
        ax[i][j].imshow(plt.imread(demo.iloc[i*3+j].path))
        ax[i][j].tick_params(left = False, bottom = False)
        ax[i][j].set_xticks([])
        ax[i][j].set_yticks([])
        ax[i][j].set_title(demo.iloc[i*3+j].label)
display(demo)
fig.savefig('Sample.png')
label path
0 Black Sea Sprat fish/Black Sea Sprat/00033.png
1 Gilt Head Bream fish/Gilt Head Bream/45.JPG
2 Horse Mackerel fish/Horse Mackerel/00033.png
3 Red Mullet fish/Red Mullet/00033.png
4 Red Sea Bream fish/Red Sea Bream/SAM_5577.JPG
5 Sea Bass fish/Sea Bass/45.JPG
6 Shrimp fish/Shrimp/00033.png
7 Striped Red Mullet fish/Striped Red Mullet/00033.png
8 Trout fish/Trout/00004.png

MODEL

In [100]:
def get_model(dim = (288,384,3)):
    keras.backend.clear_session()
    model = keras.models.Sequential()
    model.add(keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=dim))
    model.add(keras.layers.MaxPooling2D((2,2)))
    model.add(keras.layers.Dropout(.4))
    model.add(keras.layers.Conv2D(32,(3,3),activation='relu',padding="valid"))
    model.add(keras.layers.MaxPooling2D((2,2)))
    model.add(keras.layers.Dropout(.4))
    model.add(keras.layers.Conv2D(64,(3,3),activation='relu',padding="valid"))
    model.add(keras.layers.MaxPooling2D((2,2)))
    model.add(keras.layers.Dropout(.4))
    model.add(keras.layers.Conv2D(64,(5,5),activation='relu',padding="valid"))
    model.add(keras.layers.MaxPooling2D((2,2)))
    model.add(keras.layers.Dropout(.4))
    model.add(keras.layers.Conv2D(64,(5,5),activation='relu',padding="valid"))
    model.add(keras.layers.MaxPooling2D((2,2)))
    model.add(keras.layers.Dropout(.4))
    model.add(keras.layers.Flatten()) 
    model.add(keras.layers.Dropout(.4))
    model.add(keras.layers.Dense(9,activation='softmax'))
    model.summary()
    return model
train_val,test = train_test_split(data,stratify=data['label'],test_size=45,shuffle=True)
train,val  = train_test_split(train_val,stratify=train_val['label'],test_size=.25,shuffle=True)
train.reset_index(drop=True,inplace=True)
val.reset_index(drop=True,inplace=True)
test.reset_index(drop=True,inplace=True)
print(len(train),len(val),len(test))
get_model()
288 97 45
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 286, 382, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 143, 191, 32)      0         
_________________________________________________________________
dropout (Dropout)            (None, 143, 191, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 141, 189, 32)      9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 70, 94, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 70, 94, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 68, 92, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 34, 46, 64)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 34, 46, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 30, 42, 64)        102464    
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 15, 21, 64)        0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 15, 21, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 11, 17, 64)        102464    
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 8, 64)          0         
_________________________________________________________________
dropout_4 (Dropout)          (None, 5, 8, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 2560)              0         
_________________________________________________________________
dropout_5 (Dropout)          (None, 2560)              0         
_________________________________________________________________
dense (Dense)                (None, 9)                 23049     
=================================================================
Total params: 256,617
Trainable params: 256,617
Non-trainable params: 0
_________________________________________________________________
Out[100]:
<tensorflow.python.keras.engine.sequential.Sequential at 0x7fa51c1f45c0>
In [102]:
datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1/255.,
                                  horizontal_flip = True,
                                  vertical_flip=True,)
datagen_scale = keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
train_generator= datagen.flow_from_dataframe(train,x_col ='path',y_col='label',target_size=(288,384),batch_size=32,shuffle=True)
valid_generator= datagen.flow_from_dataframe(val,x_col ='path',y_col='label',target_size=(288,384),shuffle=False)
test_generator = datagen.flow_from_dataframe(test,x_col ='path',y_col='label',target_size=(288,384),shuffle=False)
Found 288 validated image filenames belonging to 9 classes.
Found 97 validated image filenames belonging to 9 classes.
Found 45 validated image filenames belonging to 9 classes.
In [103]:
model = get_model()
model.compile(loss='categorical_crossentropy',metrics='accuracy',optimizer=keras.optimizers.Adam(learning_rate=.0008))
early_stop = keras.callbacks.EarlyStopping(monitor= 'val_accuracy',patience=12,restore_best_weights=True)
red_lr = keras.callbacks.ReduceLROnPlateau(patience=4,monitor= 'val_accuracy',factor=0.75)
model_check = keras.callbacks.ModelCheckpoint(f"fishes.h5",save_best_only=True)
history = model.fit(train_generator,validation_data=valid_generator,epochs=200,callbacks = [early_stop,model_check,red_lr])
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 286, 382, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 143, 191, 32)      0         
_________________________________________________________________
dropout (Dropout)            (None, 143, 191, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 141, 189, 32)      9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 70, 94, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 70, 94, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 68, 92, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 34, 46, 64)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 34, 46, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 30, 42, 64)        102464    
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 15, 21, 64)        0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 15, 21, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 11, 17, 64)        102464    
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 8, 64)          0         
_________________________________________________________________
dropout_4 (Dropout)          (None, 5, 8, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 2560)              0         
_________________________________________________________________
dropout_5 (Dropout)          (None, 2560)              0         
_________________________________________________________________
dense (Dense)                (None, 9)                 23049     
=================================================================
Total params: 256,617
Trainable params: 256,617
Non-trainable params: 0
_________________________________________________________________
Epoch 1/200
9/9 [==============================] - 2s 262ms/step - loss: 2.3758 - accuracy: 0.0764 - val_loss: 2.1969 - val_accuracy: 0.1443
Epoch 2/200
9/9 [==============================] - 2s 225ms/step - loss: 2.1959 - accuracy: 0.1181 - val_loss: 2.1967 - val_accuracy: 0.1134
Epoch 3/200
9/9 [==============================] - 2s 224ms/step - loss: 2.1866 - accuracy: 0.1424 - val_loss: 2.1960 - val_accuracy: 0.1134
Epoch 4/200
9/9 [==============================] - 2s 225ms/step - loss: 2.1846 - accuracy: 0.1458 - val_loss: 2.1949 - val_accuracy: 0.1134
Epoch 5/200
9/9 [==============================] - 2s 222ms/step - loss: 2.1873 - accuracy: 0.1319 - val_loss: 2.1950 - val_accuracy: 0.1134
Epoch 6/200
9/9 [==============================] - 2s 218ms/step - loss: 2.1898 - accuracy: 0.0868 - val_loss: 2.1953 - val_accuracy: 0.1237
Epoch 7/200
9/9 [==============================] - 2s 222ms/step - loss: 2.1848 - accuracy: 0.1389 - val_loss: 2.1948 - val_accuracy: 0.1546
Epoch 8/200
9/9 [==============================] - 2s 228ms/step - loss: 2.1827 - accuracy: 0.1250 - val_loss: 2.1939 - val_accuracy: 0.1856
Epoch 9/200
9/9 [==============================] - 2s 210ms/step - loss: 2.1969 - accuracy: 0.1076 - val_loss: 2.1959 - val_accuracy: 0.1546
Epoch 10/200
9/9 [==============================] - 2s 218ms/step - loss: 2.1811 - accuracy: 0.1493 - val_loss: 2.1941 - val_accuracy: 0.1959
Epoch 11/200
9/9 [==============================] - 2s 224ms/step - loss: 2.1731 - accuracy: 0.1667 - val_loss: 2.1895 - val_accuracy: 0.2268
Epoch 12/200
9/9 [==============================] - 2s 224ms/step - loss: 2.1796 - accuracy: 0.1458 - val_loss: 2.1941 - val_accuracy: 0.1546
Epoch 13/200
9/9 [==============================] - 2s 222ms/step - loss: 2.1484 - accuracy: 0.1528 - val_loss: 2.1821 - val_accuracy: 0.2577
Epoch 14/200
9/9 [==============================] - 2s 220ms/step - loss: 2.1353 - accuracy: 0.2014 - val_loss: 2.1740 - val_accuracy: 0.2990
Epoch 15/200
9/9 [==============================] - 2s 226ms/step - loss: 2.1187 - accuracy: 0.2049 - val_loss: 2.1543 - val_accuracy: 0.2577
Epoch 16/200
9/9 [==============================] - 2s 225ms/step - loss: 2.0652 - accuracy: 0.2361 - val_loss: 2.1203 - val_accuracy: 0.2784
Epoch 17/200
9/9 [==============================] - 2s 223ms/step - loss: 2.0007 - accuracy: 0.2326 - val_loss: 2.0829 - val_accuracy: 0.3093
Epoch 18/200
9/9 [==============================] - 2s 225ms/step - loss: 1.9553 - accuracy: 0.2882 - val_loss: 2.0498 - val_accuracy: 0.3093
Epoch 19/200
9/9 [==============================] - 2s 226ms/step - loss: 1.9318 - accuracy: 0.2778 - val_loss: 2.0412 - val_accuracy: 0.2990
Epoch 20/200
9/9 [==============================] - 2s 233ms/step - loss: 1.9278 - accuracy: 0.3264 - val_loss: 1.9931 - val_accuracy: 0.2887
Epoch 21/200
9/9 [==============================] - 2s 222ms/step - loss: 1.9265 - accuracy: 0.2743 - val_loss: 2.0297 - val_accuracy: 0.3505
Epoch 22/200
9/9 [==============================] - 2s 225ms/step - loss: 1.8798 - accuracy: 0.2882 - val_loss: 2.0210 - val_accuracy: 0.2680
Epoch 23/200
9/9 [==============================] - 2s 228ms/step - loss: 1.7887 - accuracy: 0.3438 - val_loss: 1.8431 - val_accuracy: 0.3814
Epoch 24/200
9/9 [==============================] - 2s 225ms/step - loss: 1.7407 - accuracy: 0.3403 - val_loss: 1.9165 - val_accuracy: 0.3918
Epoch 25/200
9/9 [==============================] - 2s 223ms/step - loss: 1.6685 - accuracy: 0.3819 - val_loss: 1.8490 - val_accuracy: 0.3814
Epoch 26/200
9/9 [==============================] - 2s 232ms/step - loss: 1.7422 - accuracy: 0.3542 - val_loss: 1.8398 - val_accuracy: 0.3918
Epoch 27/200
9/9 [==============================] - 2s 226ms/step - loss: 1.6778 - accuracy: 0.4062 - val_loss: 1.8628 - val_accuracy: 0.3814
Epoch 28/200
9/9 [==============================] - 2s 229ms/step - loss: 1.6795 - accuracy: 0.3889 - val_loss: 1.7760 - val_accuracy: 0.4124
Epoch 29/200
9/9 [==============================] - 2s 227ms/step - loss: 1.6512 - accuracy: 0.3958 - val_loss: 1.7714 - val_accuracy: 0.3402
Epoch 30/200
9/9 [==============================] - 2s 213ms/step - loss: 1.6280 - accuracy: 0.3785 - val_loss: 1.8002 - val_accuracy: 0.4639
Epoch 31/200
9/9 [==============================] - 2s 231ms/step - loss: 1.5455 - accuracy: 0.4132 - val_loss: 1.7231 - val_accuracy: 0.4330
Epoch 32/200
9/9 [==============================] - 2s 221ms/step - loss: 1.5261 - accuracy: 0.4201 - val_loss: 1.6798 - val_accuracy: 0.4845
Epoch 33/200
9/9 [==============================] - 2s 222ms/step - loss: 1.4942 - accuracy: 0.4618 - val_loss: 1.7040 - val_accuracy: 0.5258
Epoch 34/200
9/9 [==============================] - 2s 226ms/step - loss: 1.4887 - accuracy: 0.4514 - val_loss: 1.6444 - val_accuracy: 0.4639
Epoch 35/200
9/9 [==============================] - 2s 227ms/step - loss: 1.5221 - accuracy: 0.4201 - val_loss: 1.6696 - val_accuracy: 0.4742
Epoch 36/200
9/9 [==============================] - 2s 230ms/step - loss: 1.4352 - accuracy: 0.4896 - val_loss: 1.5753 - val_accuracy: 0.5052
Epoch 37/200
9/9 [==============================] - 2s 225ms/step - loss: 1.4746 - accuracy: 0.4479 - val_loss: 1.6803 - val_accuracy: 0.5258
Epoch 38/200
9/9 [==============================] - 2s 228ms/step - loss: 1.3434 - accuracy: 0.4896 - val_loss: 1.5214 - val_accuracy: 0.5773
Epoch 39/200
9/9 [==============================] - 2s 230ms/step - loss: 1.3513 - accuracy: 0.5208 - val_loss: 1.4961 - val_accuracy: 0.4948
Epoch 40/200
9/9 [==============================] - 2s 225ms/step - loss: 1.3899 - accuracy: 0.5312 - val_loss: 1.5417 - val_accuracy: 0.4948
Epoch 41/200
9/9 [==============================] - 2s 227ms/step - loss: 1.3136 - accuracy: 0.5000 - val_loss: 1.5473 - val_accuracy: 0.6289
Epoch 42/200
9/9 [==============================] - 2s 225ms/step - loss: 1.3020 - accuracy: 0.5417 - val_loss: 1.4781 - val_accuracy: 0.5464
Epoch 43/200
9/9 [==============================] - 2s 226ms/step - loss: 1.2976 - accuracy: 0.5104 - val_loss: 1.4897 - val_accuracy: 0.6804
Epoch 44/200
9/9 [==============================] - 2s 227ms/step - loss: 1.2256 - accuracy: 0.5729 - val_loss: 1.3516 - val_accuracy: 0.6392
Epoch 45/200
9/9 [==============================] - 2s 227ms/step - loss: 1.2829 - accuracy: 0.5035 - val_loss: 1.3651 - val_accuracy: 0.5773
Epoch 46/200
9/9 [==============================] - 2s 221ms/step - loss: 1.2568 - accuracy: 0.5347 - val_loss: 1.3760 - val_accuracy: 0.5876
Epoch 47/200
9/9 [==============================] - 2s 226ms/step - loss: 1.1812 - accuracy: 0.5451 - val_loss: 1.3024 - val_accuracy: 0.6598
Epoch 48/200
9/9 [==============================] - 2s 232ms/step - loss: 1.1108 - accuracy: 0.6111 - val_loss: 1.2861 - val_accuracy: 0.6289
Epoch 49/200
9/9 [==============================] - 2s 220ms/step - loss: 1.1647 - accuracy: 0.5764 - val_loss: 1.2883 - val_accuracy: 0.6289
Epoch 50/200
9/9 [==============================] - 2s 227ms/step - loss: 1.1256 - accuracy: 0.5868 - val_loss: 1.2524 - val_accuracy: 0.7216
Epoch 51/200
9/9 [==============================] - 2s 218ms/step - loss: 1.0580 - accuracy: 0.6319 - val_loss: 1.2930 - val_accuracy: 0.6804
Epoch 52/200
9/9 [==============================] - 2s 216ms/step - loss: 0.9959 - accuracy: 0.6528 - val_loss: 1.2006 - val_accuracy: 0.7113
Epoch 53/200
9/9 [==============================] - 2s 226ms/step - loss: 0.9852 - accuracy: 0.6458 - val_loss: 1.1262 - val_accuracy: 0.6186
Epoch 54/200
9/9 [==============================] - 2s 223ms/step - loss: 1.0689 - accuracy: 0.5938 - val_loss: 1.1466 - val_accuracy: 0.7010
Epoch 55/200
9/9 [==============================] - 2s 228ms/step - loss: 0.9553 - accuracy: 0.6806 - val_loss: 1.1290 - val_accuracy: 0.7216
Epoch 56/200
9/9 [==============================] - 2s 231ms/step - loss: 0.9574 - accuracy: 0.6562 - val_loss: 1.1180 - val_accuracy: 0.6495
Epoch 57/200
9/9 [==============================] - 2s 227ms/step - loss: 0.9193 - accuracy: 0.6771 - val_loss: 1.1058 - val_accuracy: 0.7113
Epoch 58/200
9/9 [==============================] - 2s 229ms/step - loss: 0.9779 - accuracy: 0.6389 - val_loss: 1.0868 - val_accuracy: 0.6289
Epoch 59/200
9/9 [==============================] - 2s 224ms/step - loss: 0.8500 - accuracy: 0.6979 - val_loss: 1.0857 - val_accuracy: 0.7113
Epoch 60/200
9/9 [==============================] - 2s 221ms/step - loss: 0.9140 - accuracy: 0.6667 - val_loss: 1.0472 - val_accuracy: 0.7320
Epoch 61/200
9/9 [==============================] - 2s 224ms/step - loss: 0.9015 - accuracy: 0.6736 - val_loss: 1.0804 - val_accuracy: 0.6495
Epoch 62/200
9/9 [==============================] - 2s 225ms/step - loss: 0.9221 - accuracy: 0.6701 - val_loss: 1.0506 - val_accuracy: 0.7423
Epoch 63/200
9/9 [==============================] - 2s 220ms/step - loss: 0.8136 - accuracy: 0.7188 - val_loss: 1.0596 - val_accuracy: 0.7216
Epoch 64/200
9/9 [==============================] - 2s 228ms/step - loss: 0.8890 - accuracy: 0.6701 - val_loss: 1.0293 - val_accuracy: 0.7423
Epoch 65/200
9/9 [==============================] - 2s 226ms/step - loss: 0.8351 - accuracy: 0.7083 - val_loss: 1.0257 - val_accuracy: 0.7526
Epoch 66/200
9/9 [==============================] - 2s 225ms/step - loss: 0.8295 - accuracy: 0.7083 - val_loss: 1.0002 - val_accuracy: 0.7526
Epoch 67/200
9/9 [==============================] - 2s 218ms/step - loss: 0.7985 - accuracy: 0.7153 - val_loss: 0.9969 - val_accuracy: 0.7423
Epoch 68/200
9/9 [==============================] - 2s 235ms/step - loss: 0.8286 - accuracy: 0.7014 - val_loss: 0.9886 - val_accuracy: 0.7320
Epoch 69/200
9/9 [==============================] - 2s 228ms/step - loss: 0.7589 - accuracy: 0.7326 - val_loss: 0.9802 - val_accuracy: 0.7526
Epoch 70/200
9/9 [==============================] - 2s 231ms/step - loss: 0.7945 - accuracy: 0.7326 - val_loss: 0.9494 - val_accuracy: 0.7216
Epoch 71/200
9/9 [==============================] - 2s 226ms/step - loss: 0.8392 - accuracy: 0.7049 - val_loss: 0.9604 - val_accuracy: 0.7423
Epoch 72/200
9/9 [==============================] - 2s 224ms/step - loss: 0.8426 - accuracy: 0.7188 - val_loss: 0.9719 - val_accuracy: 0.7216
Epoch 73/200
9/9 [==============================] - 2s 227ms/step - loss: 0.7479 - accuracy: 0.7326 - val_loss: 0.9568 - val_accuracy: 0.7320
Epoch 74/200
9/9 [==============================] - 2s 215ms/step - loss: 0.8307 - accuracy: 0.7118 - val_loss: 0.9585 - val_accuracy: 0.7629
Epoch 75/200
9/9 [==============================] - 2s 230ms/step - loss: 0.7582 - accuracy: 0.7292 - val_loss: 0.9367 - val_accuracy: 0.7423
Epoch 76/200
9/9 [==============================] - 2s 219ms/step - loss: 0.7585 - accuracy: 0.6910 - val_loss: 0.9283 - val_accuracy: 0.7629
Epoch 77/200
9/9 [==============================] - 2s 219ms/step - loss: 0.7018 - accuracy: 0.7361 - val_loss: 0.8912 - val_accuracy: 0.7526
Epoch 78/200
9/9 [==============================] - 2s 228ms/step - loss: 0.6755 - accuracy: 0.7708 - val_loss: 0.9171 - val_accuracy: 0.7526
Epoch 79/200
9/9 [==============================] - 2s 223ms/step - loss: 0.7695 - accuracy: 0.7188 - val_loss: 0.8935 - val_accuracy: 0.7113
Epoch 80/200
9/9 [==============================] - 2s 222ms/step - loss: 0.7431 - accuracy: 0.7465 - val_loss: 0.9076 - val_accuracy: 0.7629
Epoch 81/200
9/9 [==============================] - 2s 220ms/step - loss: 0.7193 - accuracy: 0.7535 - val_loss: 0.9100 - val_accuracy: 0.7629
Epoch 82/200
9/9 [==============================] - 2s 218ms/step - loss: 0.6974 - accuracy: 0.7500 - val_loss: 0.8981 - val_accuracy: 0.7629
Epoch 83/200
9/9 [==============================] - 2s 220ms/step - loss: 0.7147 - accuracy: 0.7708 - val_loss: 0.8965 - val_accuracy: 0.7629
Epoch 84/200
9/9 [==============================] - 2s 226ms/step - loss: 0.7768 - accuracy: 0.7188 - val_loss: 0.8742 - val_accuracy: 0.7835
Epoch 85/200
9/9 [==============================] - 2s 212ms/step - loss: 0.7246 - accuracy: 0.7465 - val_loss: 0.9030 - val_accuracy: 0.7732
Epoch 86/200
9/9 [==============================] - 2s 220ms/step - loss: 0.6684 - accuracy: 0.7708 - val_loss: 0.8850 - val_accuracy: 0.7526
Epoch 87/200
9/9 [==============================] - 2s 223ms/step - loss: 0.7510 - accuracy: 0.7465 - val_loss: 0.8863 - val_accuracy: 0.7113
Epoch 88/200
9/9 [==============================] - 2s 229ms/step - loss: 0.7223 - accuracy: 0.7361 - val_loss: 0.8985 - val_accuracy: 0.7423
Epoch 89/200
9/9 [==============================] - 2s 215ms/step - loss: 0.7761 - accuracy: 0.6979 - val_loss: 0.8962 - val_accuracy: 0.7835
Epoch 90/200
9/9 [==============================] - 2s 225ms/step - loss: 0.6891 - accuracy: 0.7674 - val_loss: 0.8899 - val_accuracy: 0.7835
Epoch 91/200
9/9 [==============================] - 2s 217ms/step - loss: 0.7954 - accuracy: 0.7083 - val_loss: 0.9098 - val_accuracy: 0.7423
Epoch 92/200
9/9 [==============================] - 2s 222ms/step - loss: 0.7519 - accuracy: 0.7569 - val_loss: 0.9025 - val_accuracy: 0.7629
Epoch 93/200
9/9 [==============================] - 2s 226ms/step - loss: 0.7344 - accuracy: 0.7222 - val_loss: 0.8930 - val_accuracy: 0.7423
Epoch 94/200
9/9 [==============================] - 2s 222ms/step - loss: 0.7101 - accuracy: 0.7535 - val_loss: 0.9013 - val_accuracy: 0.7320
Epoch 95/200
9/9 [==============================] - 2s 225ms/step - loss: 0.7082 - accuracy: 0.7431 - val_loss: 0.8651 - val_accuracy: 0.7526
Epoch 96/200
9/9 [==============================] - 2s 221ms/step - loss: 0.7603 - accuracy: 0.7257 - val_loss: 0.9092 - val_accuracy: 0.7732
In [105]:
fig = plt.figure()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
fig.savefig('Accuracy vs Epoch.png')
In [106]:
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
fig.savefig('Loss vs Epoch.png')

EVALUATE

In [108]:
datagen_scale = keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
train_generator= datagen_scale.flow_from_dataframe(train,x_col ='path',y_col='label',target_size=(288,384),batch_size=48,shuffle=True)
valid_generator= datagen_scale.flow_from_dataframe(val,x_col ='path',y_col='label',target_size=(288,384),shuffle=False)
test_generator = datagen_scale.flow_from_dataframe(test,x_col ='path',y_col='label',target_size=(288,384),shuffle=False)
model.evaluate(train_generator)
model.evaluate(valid_generator)
model.evaluate(test_generator)
Found 288 validated image filenames belonging to 9 classes.
Found 97 validated image filenames belonging to 9 classes.
Found 45 validated image filenames belonging to 9 classes.
6/6 [==============================] - 1s 225ms/step - loss: 0.7935 - accuracy: 0.8160
4/4 [==============================] - 0s 88ms/step - loss: 0.8989 - accuracy: 0.7629
2/2 [==============================] - 0s 21ms/step - loss: 0.8094 - accuracy: 0.8222
Out[108]:
[0.8094291090965271, 0.8222222328186035]

PREDICT

In [118]:
demo = pd.DataFrame(test.groupby('label').first()).reset_index()
datagen_scale = keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
test_generator = datagen_scale.flow_from_dataframe(demo,x_col ='path',y_col='label',target_size=(288,384),shuffle=False)
demo = pd.concat((demo,pd.Series(np.argmax(model.predict(test_generator),axis=1),name='Prediction').map({a:b for b,a in test_generator.class_indices.items()})),axis=1)
fig,ax = plt.subplots(3,3,figsize=(15,10))
plt.tick_params(left = False, bottom = False)
for i in range(3):
    for j in range(3):
        ax[i][j].imshow(plt.imread(demo.iloc[i*3+j].path))
        ax[i][j].tick_params(left = False, bottom = False)
        ax[i][j].set_xticks([])
        ax[i][j].set_yticks([])
        ax[i][j].set_title("Truth Label "+demo.iloc[i*3+j].label+"   \n PREDICTED   "+demo.iloc[i*3+j].Prediction)
fig.savefig('Predict.png')
Found 9 validated image filenames belonging to 9 classes.
In [119]:
model.save("Fish_breed_classification.h5")